-
Notifications
You must be signed in to change notification settings - Fork 23
/
testlab6-0.c
348 lines (326 loc) · 8.29 KB
/
testlab6-0.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "testLab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int testN = 0;
static const struct {const char *const in, *const out;} testInOut[] = {
// Possible errors
{"", "Empty input"},
{" ", "Empty input"},
{"0\n\n", "0"},
{"1\n0", "1"},
{"1\n1", "1"},
{"-1\n3", "Bad input"},
{"2000002\n2 5", "Bad input"},
{"2000000\n2 5", "Bad input"},
// Small rotates variations
{"5\n3 5 8 9 10", "SL SL 3"},
{"5\n15 9 8 5 2", "SR SR 3"},
{"5\n3 5 8 1 0", "SL SR 3"},
{"5\n15 9 2 32 168", "SR SL 3"},
// Pairs of small + big rotates variations
{"5\n1 2 3 5 4", "SL BL 3"},
{"5\n1 2 5 3 4", "SL BR 3"},
{"5\n3 2 1 5 4", "SR BL 3"},
{"5\n5 2 1 3 4", "SR BR 3"},
// 2 small rotates and 2 big rotates in different order (start from small)
{"10\n12 32 168 9 2 200 170 1 3 4", "SL SR BL BR 4"},
{"8\n12 32 168 9 2 22 30 23", "SL SR BR BL 4"},
{"9\n12 32 168 64 200 48 11 10 22", "SL BL SR BR 4"},
{"10\n12 32 168 64 200 48 11 13 22 10", "SL BL BR SR 4"},
{"9\n12 32 168 22 10 15 9 8 11", "SL BR SR BR 4"},
{"10\n13 32 168 22 10 15 9 11 12 8", "SL BR BR SR 4"},
{"10\n12 9 2 32 168 22 10 3 1 4", "SR SL BL BR 4"},
{"11\n12 9 2 32 168 1 22 10 23 15 17", "SR SL BR BL 4"},
// 2 small rotates and 2 big rotates in different order (start from big)
{"8\n25 52 39 13 9 35 64 259", "BL SR BR SL 4"},
{"9\n25 52 39 -5 38 30 67 -39 -18", "BL BR SL BR 4"},
{"8\n18 39 20 -25 10 -30 105 110", "BL BR SR SL 4"},
{"9\n52 25 39 87 99 14 0 112 105", "BR SL SR BL 4"},
{"7\n52 25 39 195 207 75 -8", "BR SL BL SR 3"},
{"9\n52 25 39 5 0 159 201 64 55", "BR SR SL BL 4"},
{"9\n17 10 13 4 -8 28 20 45 52", "BR SR BL SL 4"},
{"9\n5 20 10 30 40 0 3 -2 -5", "BL SL BR SR 4"},
};
static int FeedFromArray(void)
{
FILE *const in = fopen("in.txt", "w+");
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
if (fprintf(in, "%s", testInOut[testN].in) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
return -1;
}
fclose(in);
return 0;
}
static int CheckFromArray(void)
{
FILE* const out = fopen("out.txt", "r");
if (out == NULL) {
printf("can't open out.txt\n");
testN++;
return -1;
}
char buf[128] = {0};
const char* status = ScanChars(out, sizeof(buf), buf);
fclose(out);
if (status == Pass && _strnicmp(testInOut[testN].out, buf, strlen(testInOut[testN].out)) != 0) {
status = Fail;
}
if (status == Pass && HaveGarbageAtTheEnd(out)) {
status = Fail;
}
printf("%s\n", status);
++testN;
return status == Fail;
}
static size_t LabMemoryLimit;
static int feederBig(void)
{
FILE *const in = fopen("in.txt", "w+");
int i;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "2000000\n");
for (i = 0; i < 2000000; i++) {
if (fprintf(in, "%d ", i) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
return -1;
}
}
fprintf(in, "\n");
fclose(in);
LabMemoryLimit = 2000000*(sizeof(int)+sizeof(int)+2*GetLabPointerSize())+MIN_PROCESS_RSS_BYTES;
return 0;
}
static int checkerBig(void)
{
FILE *const out = fopen("out.txt", "r");
int passed = 1;
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
{
int n;
if (ScanInt(out, &n) != Pass) {
passed = 0;
} else if (21 != n) {
passed = 0;
printf("wrong output -- ");
}
}
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
static int feederBig1(void)
{
FILE *const in = fopen("in.txt", "w+");
unsigned i;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "2000000\n");
for (i = 0; i < 2000000; i++) {
if (fprintf(in, "%d ", i^0xcafecafe) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
return -1;
}
}
fprintf(in, "\n");
fclose(in);
LabMemoryLimit = 2000000*(sizeof(int)+sizeof(int)+2*GetLabPointerSize())+MIN_PROCESS_RSS_BYTES;
return 0;
}
static int checkerBig1(void)
{
FILE *const out = fopen("out.txt", "r");
int passed = 1;
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
{
int n;
if (ScanInt(out, &n) != Pass) {
passed = 0;
} else if (23 != n) {
passed = 0;
printf("wrong output -- ");
}
}
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
static void genTree(FILE *in, int height, int min, int max)
{
int mid = (min*21+max*34)/55;
if (height == 0) {
return;
}
if (height == 1) {
if (fprintf(in, "%d ", min) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
exit(1);
}
return;
}
if (height == 2) {
if (min == max) {
printf("Internal error: generation failed, h = %d, min = %d, max = %d\n", height, min, max);
fclose(in);
exit(1);
}
if (fprintf(in, "%d %d ", max, min) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
exit(1);
}
return;
}
if (mid <= min || max <= mid) {
printf("Internal error: generation failed, h = %d, min = %d, max = %d\n", height, min, max);
fclose(in);
exit(1);
}
genTree(in, height-1, min, mid-1);
genTree(in, height-2, mid+1, max);
if (fprintf(in, "%d ", mid) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
exit(1);
}
}
static int feederBig2(void)
{
FILE *const in = fopen("in.txt", "w+");
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "1346269\n");
genTree(in, 30, 0, 4000000);
fprintf(in, "\n");
fclose(in);
LabMemoryLimit = 1346269*(sizeof(int)+sizeof(int)+2*GetLabPointerSize())+MIN_PROCESS_RSS_BYTES;
return 0;
}
static int checkerBig2(void)
{
FILE *const out = fopen("out.txt", "r");
int passed = 1;
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
{
int n;
if (ScanInt(out, &n) != Pass) {
passed = 0;
} else if (29 != n) {
passed = 0;
printf("wrong output -- ");
}
}
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
const TLabTest LabTests[] = {
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{FeedFromArray, CheckFromArray},
{feederBig, checkerBig},
{feederBig1, checkerBig1},
{feederBig2, checkerBig2},
};
TLabTest GetLabTest(int testIdx) {
return LabTests[testIdx];
}
int GetTestCount(void) {
return sizeof(LabTests)/sizeof(LabTests[0]);
}
const char* GetTesterName(void) {
return "Lab 6-0 AVL trees";
}
int GetTestTimeout(void) {
return 6000;
}
static size_t LabMemoryLimit = MIN_PROCESS_RSS_BYTES;
size_t GetTestMemoryLimit(void) {
return LabMemoryLimit;
}