-
Notifications
You must be signed in to change notification settings - Fork 23
/
testlab1-2.c
306 lines (288 loc) · 8.91 KB
/
testlab1-2.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
#include "testLab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int testN = 0;
static const int seed[] = {1234u, 383u};
static int LabTimeout;
static const struct {const char *const in; int n; int out[64];} testInOut[] = {
{"example\nthis is simple example", 11, { 0, 0, 0, 0, 0, 0, 1, 14, 1, 16, 7}},
{"x\n", 1, {0}},
{"x\noasdkxmnosaxkmxokasmdx", 9, {0,6, 1, 12, 1, 15, 1, 22, 1}},
{"0123456789abcdef\n0123456789abcdef", 18, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16}},
{"0123\n01230123", 8, {0, 0, 0, 0, 1, 4 ,5 ,4}},
{"0123\n0123x0123", 8, {0, 0, 0, 0, 1, 4 ,6 ,4}},
{"0123\n012300123", 10, {0, 0, 0, 0, 1, 4 ,5, 1 ,6 ,4}},
{"012013\n0123101201343", 10, {0, 0, 0, 1, 2, 0, 1, 3, 6, 6}},
{"abcabc\nabcabcabcabcabc", 14, {0, 0, 0, 1, 2, 3, 1, 6, 4, 6, 7, 6, 10, 6}},
{"0123456789abcdef\n01234", 16, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{"0101230101234\n0101010122301341010101230101234", 25, {0, 0, 1, 2, 0, 0, 1, 2, 3, 4, 5, 6, 0, 1, 4, 3, 4, 5, 5, 12, 2, 17, 4, 19, 13}},
{"\nasdbaf", 0, {0}},
{"\xE0\xE1\xE2\xE3\n\xE0\xE1\xE2\xE3\xE0\xE1\xE2\xE3",8,{0, 0, 0, 0, 1, 4, 5, 4}}, // абвг\nабвгабвг
{"\xE0\xE1 \xE2\xE3\n\xE0\xE1 \xE2\xE3\xE0\xE1\xE2\xE3", 7, {0, 0, 0, 0, 0, 1, 5}}, // аб вг\nаб вгабвг
{"\xE0\xE1 \xE2\xE3\n\xE0\xE1\xE2\xE3\n\xE0\xE1 \xE2\xE3", 9, {0, 0, 0, 0, 0, 1, 2, 6, 5}}, // аб вг\nабвг\nаб вг
{"000001\n000000000010", 18,{0, 1, 2, 3, 4, 0, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 6}}
};
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;
}
fprintf(in, "%s", testInOut[testN].in);
fclose(in);
return 0;
}
static int CheckFromArray(void) {
FILE *const out = fopen("out.txt", "r");
int passed = 1;
if (!out) {
printf("can't open out.txt\n");
++testN;
return -1;
}
for (int i = 0; i < testInOut[testN].n; ++i) {
int n;
if (ScanInt(out, &n) != Pass || testInOut[testN].out[i] != n) {
passed = 0;
printf("wrong output -- ");
break;
}
}
if (passed) {
passed = !HaveGarbageAtTheEnd(out);
}
fclose(out);
if (passed) {
printf("PASSED\n");
++testN;
return 0;
} else {
printf("FAILED\n");
++testN;
return 1;
}
}
static int FeederBigRand1(void) {
FILE *const in = fopen("in.txt", "w");
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
unsigned int bigTestN = testN - sizeof(testInOut) / sizeof(testInOut[0]);
if (bigTestN >= sizeof(seed) / sizeof(seed[0])) {
printf("\nInternal error: incorrect test number\n");
++testN;
fclose(in);
return -1;
}
printf("Creating large text with seed %u... ", seed[bigTestN]);
fflush(stdout);
const char pattern[] = "0123456789abcdef";
srand(seed[bigTestN]);
DWORD t = GetTickCount();
if (fputs(pattern, in) == EOF || fputc('\n',in) == EOF) {
printf("can't write in in.txt. No space on disk?\n");
fclose(in);
return -1;
}
for (unsigned int i = 1; i < 1024 * 1024 * 8; ++i) {
unsigned int randomPrefixLength = (unsigned int)rand() % 17;
if (randomPrefixLength == 0) {
if (putc(' ', in) == EOF) {
printf("can't write in in.txt. No space on disk?\n");
fclose(in);
return -1;
}
} else {
if (fwrite(pattern, 1, randomPrefixLength, in) != randomPrefixLength) {
printf("can't write in in.txt. No space on disk?\n");
fclose(in);
return -1;
}
}
}
if (fputs(pattern, in) == EOF) {
printf("can't write in in.txt. No space on disk?\n");
fclose(in);
return -1;
}
fclose(in);
t = RoundUptoThousand(GetTickCount() - t);
printf("done in T=%u seconds. Starting exe with timeout 6*T... ", (unsigned)(t / 1000));
LabTimeout = (int)t * 6;
fflush(stdout);
return 0;
}
static int CheckerBigRand1(void) {
FILE *const out = fopen("out.txt", "r");
int passed = 1;
unsigned int bigTestN = testN - sizeof(testInOut) / sizeof(testInOut[0]);
if (bigTestN >= sizeof(seed) / sizeof(seed[0])) {
printf("\nInternal error: incorrect test number\n");
++testN;
return -1;
}
if (!out) {
printf("can't open out.txt\n");
++testN;
return -1;
}
for (unsigned int i = 0; i < 16; ++i) {
int n;
if(ScanInt(out, &n) != Pass || n != 0) {
passed = 0;
printf("wrong output -- ");
break;
}
}
if (passed) {
srand(seed[bigTestN]);
unsigned int number = 1;
unsigned int a, b;
for (unsigned int i = 1; i < 1024 * 1024 * 8; ++i) {
unsigned int randomPrefixLength = ((unsigned int) rand()) % 17;
if (randomPrefixLength == 0) {
++number;
} else {
if (ScanUintUint(out, &a, &b) != Pass || a != number || b != randomPrefixLength) {
passed = 0;
printf("wrong output -- ");
break;
}
number += randomPrefixLength;
}
}
if (ScanUintUint(out, &a, &b) != Pass) {
passed = 0;
} else if (a != number || b != 16) {
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 FeederBig(void) {
FILE *const in = fopen("in.txt", "w");
if (!in) {
printf("can't create in.txt. No space on disk?\n");
return -1;
}
printf("Creating large text... ");
fflush(stdout);
const char str[] = "abcdabcd";
DWORD t = GetTickCount();
if (fputs(str, in) == EOF || fputc('\n', in) == EOF) {
printf("can't write in in.txt. No space on disk?\n");
return -1;
}
for (int i = 0; i < 1024 * 1024 * 8; ++i) {
if (fprintf(in, "%s", str) != strlen(str)) {
printf("can't write in in.txt. No space on disk?\n");
return -1;
}
}
fclose(in);
t = RoundUptoThousand(GetTickCount() - t);
printf("done in T=%u seconds. Starting exe with timeout 6*T... ", (unsigned)(t / 1000));
LabTimeout = (int)t * 6;
fflush(stdout);
return 0;
}
static int CheckBig(void) {
FILE *const out = fopen("out.txt", "r");
int passed = 1;
if (!out) {
printf("can't open out.txt\n");
++testN;
return -1;
}
for (int i = 0; i < 4; ++i) {
int n;
if (ScanInt(out, &n) != Pass || n != 0) {
passed = 0;
printf("wrong output -- ");
break;
}
}
for (int i = 1; i < 5; ++i) {
int n;
if (ScanInt(out, &n) != Pass || n != i) {
passed = 0;
printf("wrong output -- ");
break;
}
}
if (passed) {
for (unsigned int i = 0; i < 1024 * 1024 * 8 * 2 - 1; ++i) {
unsigned int a, b;
if (ScanUintUint(out, &a, &b) != Pass || a != 1 + i * 4 || b != 8) {
passed = 0;
printf("wrong output -- ");
break;
}
}
}
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},
{FeederBigRand1, CheckerBigRand1},
{FeederBigRand1, CheckerBigRand1},
{FeederBig, CheckBig}
};
TLabTest GetLabTest(int testIdx) {
return LabTests[testIdx];
}
int GetTestCount(void) {
return sizeof(LabTests)/sizeof(LabTests[0]);
}
const char* GetTesterName(void) {
return "Lab 1-2 Knuth–Morris–Pratt";
}
static int LabTimeout = 3000;
int GetTestTimeout(void) {
return LabTimeout;
}
size_t GetTestMemoryLimit(void) {
return MIN_PROCESS_RSS_BYTES;
}