-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtestlab6-3.c
474 lines (450 loc) · 15.2 KB
/
testlab6-3.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "testLab.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define BUFF_SIZE 256
#define ALPHABET_SIZE 26
#define MAX_WORD_LENGTH 10000
static int testN = 0;
static int testTimeOut = CLOCKS_PER_SEC * 3;
static size_t LabMemoryLimit = MIN_PROCESS_RSS_BYTES;
static const struct {const char *const in; const char *const out1; int out2;} testInOut[] = {
{"3\napple\nmilk\nbread\nap\n", "apple", 4},
{"3\napple\nmilk\nbread\nre\n", "None", 4},
{"8\nappearance\napplicable\nappreciate\napprentice\nappendices\napocalypse\napologetic\napolitical\napp\n", "appearance appendices applicable appreciate apprentice", 14},
{"0\n", "None", 0},
{"2\nlike\ndislike\nd\n", "dislike", 3},
{"3\nlike\ndislike\nlikely\nlik\n", "like likely", 4},
{"3\nвечер\nвечерело\nвчера\nв\n", "вечер вечерело вчера", 4},
{"5\nинтернет\nинтервью\nинтерактивный\nинтроверт\nинтерпретация\nинтер\n", "интернет интервью интерактивный интерпретация", 7},
{"2\naa\naa\na\n", "aa", 1},
{"2\naaaaaaa\na\na\n", "a aaaaaaa", 2},
{"5\nred\nrose\ngreen\nyellow\npink\nr\n", "red rose", 7},
{"3\naba\nabacaba\nabacabaaba\naba\n", "aba abacaba abacabaaba", 3},
{"6\nбег\nбегун\nбеговой\nбегунья\nпробег\nпробежка\nбег\n", "бег бегун бегунья беговой", 8},
{"7\nаббревиатура\nабсурд\nлаконичный\nлакомый\nадрес\nпожелание\nпроза\nа\n", "аббревиатура абсурд адрес", 12},
{"3\naba\nabacaba\nabacabaaba\nabac\n", "abacaba abacabaaba", 3},
{"3\naba\nabacaba\nabacabaaba\nabacab\n", "abacaba abacabaaba", 3},
{"8\nappearance\napplicable\nappreciate\napprentice\nappendices\napocalypse\napologetic\napolitical\nappr\n", "appreciate apprentice", 14},
{"3\nphone\ntelephone\niphone\nphone\n", "phone", 4},
{"10\nprecaution\npredicament\npresume\nprefix\nprejudice\nprearrange\npreconceive\npredestination\npreeminent\npreoccupied\npred\n", "predicament predestination", 13},
{"10\nprecaution\npredicament\npresume\nprefix\nprejudice\nprearrange\npreconceive\npredestination\npreeminent\npreoccupied\nprez\n", "None", 13},
{"10\nprecaution\npredicament\npresume\nprefix\nprejudice\nprearrange\npreconceive\npredestination\npreeminent\npreoccupied\npre\n", "precaution preconceive predicament predestination presume prefix prejudice prearrange preeminent preoccupied", 13},
{"5\nbeautiful\ncolorful\nplayful\nthoughtful\nrespectful\nful\n", "None", 6},
{"3\nintriguing\nnostalgic\noptimistic\nintriguingg\n", "None", 4},
{"4\ndislike\ndisagree\ndisappear\ndisrupt\ndisa\n", "disagree disappear", 6},
{"4\ndislike\ndisagree\ndisappear\ndisrupt\ndislike\n", "dislike", 6},
{"4\ndislike\ndisagree\ndisappear\ndisrupt\ndi\n", "dislike disagree disappear disrupt", 6},
};
static bool Equal(char *const string, const char *const pattern) {
size_t lenString = strlen(string);
size_t lenPattern = strlen(pattern);
if (lenString == 0) {
return false;
}
if (string[lenString - 1] == ' ') {
string[lenString - 1] = '\0';
lenString--;
}
if (lenString != lenPattern) {
return false;
}
if (strchr(pattern, ' ') != NULL) {
const char *begin = pattern;
char *spacePointer = strchr(begin, ' ');
while (spacePointer != NULL) {
char word[BUFF_SIZE] = "";
strncpy(word, begin, spacePointer - begin);
if (strstr(string, word) == NULL) {
return false;
}
begin = spacePointer + 1;
spacePointer = strchr(begin, ' ');
}
if (strstr(string, begin) == NULL) {
return false;
}
} else {
for (size_t i = 0; i < lenString; i++) {
if (string[i] != pattern[i]) {
return false;
}
}
}
return true;
}
static bool Check(const char *const firstAns, const int secondAns, FILE *stream) {
bool passed = true;
char buff[BUFF_SIZE] = "";
int n;
if (ScanChars(stream, BUFF_SIZE, buff) != Pass) {
passed = false;
} else {
if (!Equal(buff, firstAns)) {
passed = false;
printf("wrong output 1 -- ");
} else {
if (ScanInt(stream, &n) != Pass) {
passed = false;
} else {
if (secondAns != n) {
passed = false;
printf("wrong output 2 got %d, expected %d -- ", n, secondAns);
}
}
}
}
return passed;
}
static bool GenerateSequence(FILE *out, int startInd, int len, char *output) {
bool res = true;
if (len > 0) {
for (int i = 0; i < ALPHABET_SIZE; i++) {
output[startInd] = 'a' + i;
res = GenerateSequence(out, startInd + 1, len - 1, output);
}
}
if (len == 0) {
if (fprintf(out, "%s\n", output) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(out);
res = false;
}
}
return res;
}
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) {
printf("can't open out.txt\n");
testN++;
return -1;
}
bool passed = Check(testInOut[testN].out1, testInOut[testN].out2, 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+");
printf("Creating Large test... ");
clock_t start = clock();
int n = 17576;
int finalAmountVerts = 18278;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "%d\n", n);
char output[500 + 1] = "";
memset(output, 'a', sizeof(char) * 500);
GenerateSequence(in, 0, 3, output);
fprintf(in, "zzzz\n");
clock_t end = clock();
fclose(in);
testTimeOut = end - start;
printf("done in T=%ld seconds. Starting exe with timeout T+3 seconds... ", RoundUptoThousand(testTimeOut) / CLOCKS_PER_SEC);
testTimeOut = testTimeOut + CLOCKS_PER_SEC * 3;
size_t memoryForTreeNodes = finalAmountVerts * (GetLabPointerSize() + sizeof(int) * 2 + 2 * GetLabPointerSize());
size_t memoryForChars = (n * 500 * sizeof(char));
LabMemoryLimit = memoryForChars + memoryForTreeNodes + MIN_PROCESS_RSS_BYTES;
return 0;
}
static int CheckerBig1(void) {
FILE *const out = fopen("out.txt", "r");
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
bool passed = Check("None", 18279, out);
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
static int FeederBig2(void) {
FILE *const in = fopen("in.txt", "w+");
printf("Creating Large test... ");
clock_t start = clock();
int n = 17576;
int finalAmountVerts = 18279;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "%d\n", n);
char output[500 + 1] = "";
memset(output, 'a', sizeof(char) * 500);
GenerateSequence(in, 497, 3, output);
fprintf(in, "tree\n");
clock_t end = clock();
fclose(in);
testTimeOut = end - start;
printf("done in T=%ld seconds. Starting exe with timeout T+3 seconds... ", RoundUptoThousand(testTimeOut) / CLOCKS_PER_SEC);
testTimeOut = testTimeOut + CLOCKS_PER_SEC * 3;
size_t memoryForTreeNodes = finalAmountVerts * (GetLabPointerSize() + sizeof(int) * 2 + 2 * GetLabPointerSize());
size_t memoryForChars = (n * 500 * sizeof(char));
LabMemoryLimit = memoryForChars + memoryForTreeNodes + MIN_PROCESS_RSS_BYTES;
return 0;
}
static int CheckerBig2(void) {
FILE *const out = fopen("out.txt", "r");
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
bool passed = Check("None", 18279, out);
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
static int FeederBig3(void) {
FILE *const in = fopen("in.txt", "w+");
printf("Creating Large test... ");
clock_t start = clock();
int n = MAX_WORD_LENGTH / 5 * ALPHABET_SIZE;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "%d\n", n + 1);
for (int i = 0; i < ALPHABET_SIZE; i++) {
char output[MAX_WORD_LENGTH / 5 + 1] = "";
for (int j = 0; j < MAX_WORD_LENGTH / 5; j++) {
output[j] = 'a' + i;
if (fprintf(in, "%s\n", output) < 0) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
}
}
fprintf(in, "abacaba\naba");
clock_t end = clock();
fclose(in);
testTimeOut = end - start;
printf("done in T=%ld seconds. Starting exe with timeout T+3 seconds... ", RoundUptoThousand(testTimeOut) / CLOCKS_PER_SEC);
testTimeOut = testTimeOut + CLOCKS_PER_SEC * 3;
size_t memoryForRecursion = n * (sizeof(void *) * 3);
size_t memoryForTreeNodes = (n + 1) * (GetLabPointerSize() + sizeof(int) * 2 + 2 * GetLabPointerSize());
size_t memoryForChars = n * sizeof(char);
LabMemoryLimit = memoryForChars + memoryForTreeNodes + memoryForRecursion + MIN_PROCESS_RSS_BYTES;
return 0;
}
static int CheckerBig3(void) {
FILE *const out = fopen("out.txt", "r");
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
bool passed = Check("abacaba", 52000 + 2, out);
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
static int FeederBig4(void) {
FILE *const in = fopen("in.txt", "w+");
printf("Creating Large test... ");
clock_t start = clock();
int n = MAX_WORD_LENGTH;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
fprintf(in, "%d\n", n);
char output[MAX_WORD_LENGTH + 1] = "";
memset(output, 'b', sizeof(char) * n);
fprintf(in, "%s\n", output);
for (int i = n - 1; i > 0; i--) {
output[i] = '\0';
if (fprintf(in, "%s\n", output) < 0) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
}
fprintf(in, "d\n");
clock_t end = clock();
fclose(in);
testTimeOut = end - start;
printf("done in T=%ld seconds. Starting exe with timeout T+3 seconds... ", RoundUptoThousand(testTimeOut) / CLOCKS_PER_SEC);
testTimeOut = testTimeOut + CLOCKS_PER_SEC * 3;
size_t memoryForTreeNodes = n * ALPHABET_SIZE * (GetLabPointerSize() + sizeof(int) * 2 + 2 * GetLabPointerSize());
size_t memoryForChars = n * sizeof(char);
LabMemoryLimit = memoryForChars + memoryForTreeNodes + MIN_PROCESS_RSS_BYTES;
return 0;
}
static int CheckerBig4(void) {
FILE *const out = fopen("out.txt", "r");
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
bool passed = Check("None", MAX_WORD_LENGTH, out);
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
testN++;
return 0;
} else {
printf("FAILED\n");
testN++;
return 1;
}
}
static int FeederBig5(void) {
FILE *const in = fopen("in.txt", "w+");
printf("Creating Large test... ");
clock_t start = clock();
int n = MAX_WORD_LENGTH;
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
char output[MAX_WORD_LENGTH + 1] = "";
fprintf(in, "%d\n", n);
for (int i = 0; i < n; i++) {
output[i] = 'a';
if (fprintf(in, "%s\n", output) < 0) {
printf("can't create in.txt. No space on disk?\n");
fclose(in);
return -1;
}
}
fprintf(in, "b\n");
clock_t end = clock();
fclose(in);
testTimeOut = end - start;
printf("done in T=%ld seconds. Starting exe with timeout T+3 seconds... ", RoundUptoThousand(testTimeOut) / CLOCKS_PER_SEC);
testTimeOut = testTimeOut + CLOCKS_PER_SEC * 3;
size_t memoryForTreeNodes = n * (sizeof(char *) + sizeof(int) * 2 + 2 * GetLabPointerSize());
size_t memoryForChars = n * sizeof(char);
LabMemoryLimit = memoryForChars + memoryForTreeNodes + MIN_PROCESS_RSS_BYTES;
return 0;
}
static int CheckerBig5(void) {
FILE *const out = fopen("out.txt", "r");
if (!out) {
printf("can't open out.txt\n");
testN++;
return -1;
}
bool passed = Check("None", MAX_WORD_LENGTH, out);
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},
{FeederBig1, CheckerBig1},
{FeederBig2, CheckerBig2},
{FeederBig3, CheckerBig3},
{FeederBig4, CheckerBig4},
{FeederBig5, CheckerBig5},
};
TLabTest GetLabTest(int testIdx) {
return LabTests[testIdx];
}
int GetTestCount(void) {
return sizeof(LabTests)/sizeof(LabTests[0]);
}
const char *GetTesterName(void) {
return "Lab 6-3 radix tree";
}
int GetTestTimeout(void) {
return testTimeOut;
}
size_t GetTestMemoryLimit(void) {
return LabMemoryLimit;
}