-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart1_driver.c
303 lines (261 loc) · 10.2 KB
/
Part1_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* CSC A48 - Intro to Computer Science II, Summer 2020
*
* This file provides a test driver you can use while developing your
* solution to test different components.
*
* This is provided *only* with the intent of helping you test, and
* of getting an idea of what *some* of the tests we will subject
* your code to look like.
*
* Passing all the tests in this driver *does not mean your code is
* bug free*, you have to do much more extensive and thorough testing
* on your own (a separate interactive driver is provided, and should
* be helpful with that).
*
* Remember: You are expected to test for *correctness*, that means
* it's not enough for your functions to produce the right result,
* they have to do so by manipulating all the relevant data correctly.
* You should check for structure and correctness at every step.
*
* Developed by Mustafa Quraish for CSCA48
* (c) Mustafa Quraish 2020
*/
#define SIZEX 512
#define SIZEY 512
#include "turtle.c"
int checkNode(CmdNode *a, char cmd[10], int val) {
return (strcmp(a->cmd, cmd) == 0 && a->val == val);
}
int main() {
Image *im;
CmdNode *head = NULL;
CmdNode *tmp = NULL;
CmdNode *tmp2 = NULL;
int val, i, j, ex, ey;
printf("Running tests...\n");
/*-------------------------------------------------------------------------*/
// Test 0, Creating a node with valid and invalid commands
tmp = newCmdNode("forward", 100);
printf("Expecting \"Invalid command.\"\n> ");
tmp2 = newCmdNode("forwrd", 100); // Typo
if (tmp == NULL || tmp2 != NULL) {
printf("Test 0 failed, newCmdNode() did not initialize node properly.\n");
return 1;
} else if (!checkNode(tmp, "forward", 100) || tmp->next != NULL) {
printf("Test 0 failed, Incorrect data in new node.\n");
return 1;
}
printf("Test 0 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 1, Inserting into an empty list at the tail
head = insertCommand(head, tmp);
if (head == NULL) {
printf("Test 1 failed, head is still NULL.\n");
return 1;
} else if (!checkNode(head, "forward", 100) || tmp->next != NULL) {
printf("Test 1 failed, Incorrect data in the head of list.\n");
return 1;
}
printf("Test 1 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 2, Inserting into a list with one element at the tail
tmp = newCmdNode("colour", 150);
head = insertCommand(head, tmp);
if (head == NULL) {
printf("Test 2 failed, head is NULL.\n");
return 1;
} else if (head->next == NULL) {
printf("Test 2 failed, list only has one node.\n");
return 1;
} else if (!checkNode(head->next, "colour", 150) ||
head->next->next != NULL) {
printf("Test 2 failed, node not added in the right location\n");
return 1;
}
printf("Test 2 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 3, Inserting into a list with two elements in between
tmp = newCmdNode("right", 0);
head = insertCommandBefore(head, tmp, 1);
if (head == NULL) {
printf("Test 3 failed, head is NULL.\n");
return 1;
} else if (!checkNode(head, "forward", 100) ||
!checkNode(head->next, "right", 0) ||
!checkNode(head->next->next, "colour", 150) ||
head->next->next->next != NULL) {
printf("Test 3 failed, node not inserted correctly in the middle.\n");
return 1;
}
printf("Test 3 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 4, Inserting at the head of the list
tmp = newCmdNode("penup", 0);
head = insertCommandBefore(head, tmp, 0);
if (head == NULL) {
printf("Test 4 failed, head is NULL.\n");
return 1;
} else if (!checkNode(head, "penup", 0) ||
!checkNode(head->next, "forward", 100) ||
!checkNode(head->next->next, "right", 0) ||
!checkNode(head->next->next->next, "colour", 150) ||
head->next->next->next->next != NULL) {
printf("Test 4 failed, node not inserted correctly at the head.\n");
return 1;
}
printf("Test 4 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 5, Updating a the node in the middle of the list, and also
// checking if it ignores invalid commands
updateCommand(head, 1, "backward", 40);
printf("Expecting \"Invalid command.\"\n> ");
updateCommand(head, 2, "notvalid", 101);
if (!checkNode(head, "penup", 0) ||
!checkNode(head->next, "backward", 40) ||
!checkNode(head->next->next, "right", 0) ||
!checkNode(head->next->next->next, "colour", 150) ||
head->next->next->next->next != NULL) {
printf("Test 5 failed, node not correctly updated.\n");
return 1;
}
printf("Test 5 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 6, Deleting a node in the middle of the list
head = deleteCommand(head, 2);
if (!checkNode(head, "penup", 0) ||
!checkNode(head->next, "backward", 40) ||
!checkNode(head->next->next, "colour", 150)) {
printf("Test 6 failed, node not deleted properly.\n");
return 1;
}
printf("Test 6 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 7, Deleting the head of the list
head = deleteCommand(head, 0);
if (head == NULL) {
printf("Test 7 failed, head is NULL.\n");
return 1;
} else if (!checkNode(head, "backward", 40) ||
!checkNode(head->next, "colour", 150)) {
printf("Test 7 failed, node not deleted properly.\n");
return 1;
}
printf("Test 7 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 8, Counting the number of commands
val = countCommands(head);
if (val != 2) {
printf("Test 8 failed, countCommands() did not return correct value.\n");
return 1;
}
printf("Test 8 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 9, Deleting the list
head = deleteCommandList(head);
if (head != NULL) {
printf("Test 9 failed, head is not NULL after deleting list.\n");
return 1;
} else if ((val = countCommands(head))) {
printf("Test 9 failed, countCommands() says %d commands in list.\n", val);
return 1;
}
printf("Test 9 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 10, Turtle should always remain within bounds in run()
head = insertCommand(head, newCmdNode("right", 0));
head = insertCommand(head, newCmdNode("right", 0));
head = insertCommand(head, newCmdNode("forward", 100));
head = insertCommand(head, newCmdNode("right", 0));
head = insertCommand(head, newCmdNode("forward", 100));
im = newImage(SIZEX, SIZEY);
run(im, head, &ex, &ey);
deleteImage(im);
if (ex != 0 && ey != 0) {
printf("Test 10 failed, should turtle should not go out of bounds.\n");
return 1;
}
head = deleteCommandList(head);
printf("Test 10 passed.\n");
/*-------------------------------------------------------------------------*/
// Setting up a list of commands that draws a square in the middle of the
// image
head = insertCommand(head, newCmdNode("penup", 0));
head = insertCommand(head, newCmdNode("forward", 100));
head = insertCommand(head, newCmdNode("right", 0));
head = insertCommand(head, newCmdNode("forward", 100));
head = insertCommand(head, newCmdNode("pendown", 0));
head = insertCommand(head, newCmdNode("forward", 300));
head = insertCommand(head, newCmdNode("left", 0));
head = insertCommand(head, newCmdNode("forward", 300));
head = insertCommand(head, newCmdNode("left", 0));
head = insertCommand(head, newCmdNode("forward", 300));
head = insertCommand(head, newCmdNode("left", 0));
head = insertCommand(head, newCmdNode("forward", 300));
im = newImage(SIZEX, SIZEY);
/*-------------------------------------------------------------------------*/
// Test 11, testing the run() function. This should draw a square on the
// image with the corners (100,100), (100,400), (400,400) and (400, 100)
// The turtle should end back up on pixel (100, 100)
run(im, head, &ex, &ey);
// The image should be exactly as expected, this checks every pixel to
// make sure it matches.
for (i = 0; i < SIZEX; i++) {
for (j = 0; j < SIZEY; j++) {
if (((i == 100 || i == 400) && j >= 100 && j <= 400) ||
((j == 100 || j == 400) && i >= 100 && i <= 400)) {
if (im->data[i + j * SIZEX] != 0) {
printf("Test 11 failed, Pixel at (%d, %d) is %d, should be 0\n", i, j,
im->data[i + j * SIZEX]);
return 1;
}
} else if (im->data[i + j * SIZEX] != 255) {
printf("Test 11 failed, Pixel at (%d, %d) is %d, should be 255\n", i, j,
im->data[i + j * SIZEX]);
return 1;
}
}
}
if (ex != 100 || ey != 100) {
printf("Test 11 failed, end location of the turtle should be (100, 100)\n");
return 1;
}
deleteImage(im);
printf("Test 11 passed.\n");
/*-------------------------------------------------------------------------*/
// Test 12, printing out the command list
printf("Test 12, check this yourself!\n");
/* This needs to be inspected manually. Here is the expected output:
0: penup
1: forward 100
2: right
3: forward 100
4: pendown
5: forward 300
6: left
7: forward 300
8: left
9: forward 300
10: left
11: forward 300
*/
printCommandList(head);
/*-------------------------------------------------------------------------*/
// Test 13, querying the list for "forward"
printf("Test 13, check this yourself!\n");
/* This needs to be inspected manually. Here is the expected output:
1: forward 100
3: forward 100
5: forward 300
7: forward 300
9: forward 300
11: forward 300
*/
queryByCommand(head, "forward");
/*-------------------------------------------------------------------------*/
// Congratulations! your solution passed all the test cases here. This does
// NOT mean it is fully correct, however. Remember to run your own tests as
// well to make sure your code works under all conditions
deleteCommandList(head);
return 0; // All good
}