-
Notifications
You must be signed in to change notification settings - Fork 23
/
testlab5.c
282 lines (252 loc) · 8.51 KB
/
testlab5.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
#include "testLab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int TestN = 0;
typedef struct {
const char *const Input;
size_t InputLength;
size_t CompressedLength;
} TTestCase;
#define CalcCompressedLength(abSize, bitsPerChar, charCount) (4+(10*(abSize)+(charCount)*(bitsPerChar)+7)/8+100)
static const TTestCase TestCases[] = {
{"0123456789ABCD\xD\xA" "0123456789ABCD\xD\xA" "0123456789ABCD\xD\xA" "0123456789ABCD\xD\xA"
"0123456789ABCD\xD\xA" "0123456789ABCD\xD\xA" "0123456789ABCD\xD\xA" "0123456789ABCD\xD\xA"
"0123456789ABCD\xD\xA", 16*9, CalcCompressedLength(16, 4, 16*9)}, // 1 2
{"", 0, CalcCompressedLength(0, 0, 0)}, // 3 4
{"z", 1, CalcCompressedLength(1, 1, 1)}, // 5 6
{"zz", 2, CalcCompressedLength(1, 1, 2)}, // 7 8
{"zzzz", 4, CalcCompressedLength(1, 1, 4)}, // 9 10
{"\xFF\xFF\xFF\xFF", 4, CalcCompressedLength(1, 1, 4)}, // 11 12
{"zzzz\xFF\xFF\xFF\xFF", 8, CalcCompressedLength(2, 1, 8)}, // 13 14
{"\xA\xD\xA\xD", 4, CalcCompressedLength(2, 1, 4)}, // 15 16
{"\xD\xA\xD\xA", 4, CalcCompressedLength(2, 1, 4)}, // 17 18
{"\xA\xA\xA\xA", 4, CalcCompressedLength(1, 1, 4)}, // 19 20
{"\xD\xD\xD\xD", 4, CalcCompressedLength(1, 1, 4)}, // 21 22
{"\0\0\0\0", 4, CalcCompressedLength(1, 1, 4)}, // 23 24
{"\0\1\0\1", 4, CalcCompressedLength(2, 1, 4)}
};
#undef CalcCompressedLength
static unsigned char Input[32*1024*1024];
static size_t InputLength = 0;
static unsigned char LabOutput[sizeof(Input) + 1];
static size_t LabOutputLength = 0;
static int IsInputTooLarge(size_t inputLength) {
if (inputLength > sizeof(Input)) {
printf("Tester error: generated input is too large");
return 1;
}
return 0;
}
static int FeedData(char mode, const unsigned char* data, size_t count) {
FILE *const input = fopen("in.txt", "wb");
int error = input == NULL || fprintf(input, "%c", mode) != 1 || fwrite(data, 1, count, input) != count;
fclose(input);
if (error) {
printf("can't create in.txt. No space on disk?\n");
}
return error;
}
static int FeedCompress(void) {
if (TestN%2 != 0) {
printf("Tester error: compression test must preceed decompression test\n");
return 1;
}
return FeedData('c', Input, InputLength);
}
static int CheckAtMost(int gotBytes, int expectedBytes) {
if (gotBytes > expectedBytes) {
printf("output is too long, got %d bytes, expect at most %d bytes -- ", gotBytes, expectedBytes);
return 0;
}
return 1;
}
static int CheckCompress(size_t expectedLength) {
FILE *const out = fopen("out.txt", "rb");
if (!out) {
printf("can't open out.txt\n");
TestN++;
return -1;
}
if (expectedLength+1 > sizeof(LabOutput)) {
printf("Tester error: expected output is too large");
return -1;
}
LabOutputLength = fread(LabOutput, 1, expectedLength+1, out);
fclose(out);
if (CheckAtMost(LabOutputLength, expectedLength)) {
printf("PASSED\n");
TestN++;
return 0;
} else {
printf("FAILED\n");
TestN++;
return 1;
}
}
static int FeedCompressFromArray(void) {
InputLength = TestCases[TestN/2].InputLength;
memcpy(Input, TestCases[TestN/2].Input, InputLength);
return FeedCompress();
}
static int CheckCompressFromArray(void) {
return CheckCompress(TestCases[TestN/2].CompressedLength);
}
static int FeedDecompress(void) {
if (TestN%2 != 1) {
printf("Tester error: compression test must preceed decompression test\n");
return 1;
}
return FeedData('d', LabOutput, LabOutputLength);
}
static int CheckSame(int gotBytes, int expectedBytes) {
if (gotBytes != expectedBytes) {
printf("output is wrong, got %d bytes, expect exactly %d bytes -- ", gotBytes, expectedBytes);
return 0;
}
return 1;
}
static int CheckDecompress(void) {
FILE *const out = fopen("out.txt", "rb");
if (!out) {
printf("can't open out.txt\n");
TestN++;
return -1;
}
const size_t outputLength = fread(LabOutput, 1, InputLength+1, out);
fclose(out);
int passed = CheckSame(outputLength, InputLength);
if (passed && memcmp(LabOutput, Input, InputLength) != 0) {
passed = 0;
printf("output is wrong -- ");
}
if (passed) {
printf("PASSED\n");
TestN++;
return 0;
} else {
printf("FAILED\n");
TestN++;
return 1;
}
}
static unsigned AlphabetSize = 16;
static unsigned CalcFib(unsigned n) {
static unsigned f[128] = {1, 1};
if (n >= sizeof(f)/sizeof(f[0])) {
printf("Tester error: out of range in CalcFib\n");
exit(1);
}
if (f[n] == 0) {
f[n] = CalcFib(n-1)+CalcFib(n-2);
}
return f[n];
}
static int FeedCompressBig1(void) {
InputLength = 0;
for (unsigned char c = 0; c < AlphabetSize; ++c) {
if (IsInputTooLarge(InputLength + CalcFib(c))) {
return -1;
}
memset(Input + InputLength, c, CalcFib(c));
InputLength += CalcFib(c);
}
return FeedCompress();
}
static int CheckCompressBig1(void) {
unsigned bits = CalcFib(0)*(AlphabetSize-1);
for (unsigned char c = 1; c < AlphabetSize; ++c) {
bits += CalcFib(c)*(AlphabetSize-c);
}
int expectedLength = 4+(10*AlphabetSize+bits+7)/8;
AlphabetSize = AlphabetSize == 16 ? 21
: AlphabetSize == 21 ? 26
: AlphabetSize == 26 ? 31
: AlphabetSize == 31 ? 35
: AlphabetSize == 35 ? 41
: 41;
return CheckCompress(expectedLength);
}
static int FeedCompressBig2(void) {
InputLength = 256*256;
if (IsInputTooLarge(InputLength)) {
return -1;
}
for (size_t i = 0; i < InputLength; ++i) {
Input[i] = i & 0xff;
}
return FeedCompress();
}
static int CheckCompressBig2(void) {
return CheckCompress(4+(10*256+256*256*8+7)/8);
}
static int FeedCompressBig3(void) {
InputLength = 256*256*256;
if (IsInputTooLarge(InputLength)) {
return -1;
}
for (size_t i = 0; i < InputLength; ++i) {
Input[i] = 'x';
}
return FeedCompress();
}
static int CheckCompressBig3(void) {
return CheckCompress(4+(9+256*256*256+7)/8);
}
const TLabTest LabTests[] = {
{FeedCompressFromArray, CheckCompressFromArray}, // 1
{FeedDecompress, CheckDecompress}, // 2
{FeedCompressFromArray, CheckCompressFromArray}, // 3
{FeedDecompress, CheckDecompress}, // 4
{FeedCompressFromArray, CheckCompressFromArray}, // 5
{FeedDecompress, CheckDecompress}, // 6
{FeedCompressFromArray, CheckCompressFromArray}, // 7
{FeedDecompress, CheckDecompress}, // 8
{FeedCompressFromArray, CheckCompressFromArray}, // 9
{FeedDecompress, CheckDecompress}, // 10
{FeedCompressFromArray, CheckCompressFromArray}, // 11
{FeedDecompress, CheckDecompress}, // 12
{FeedCompressFromArray, CheckCompressFromArray}, // 13
{FeedDecompress, CheckDecompress}, // 14
{FeedCompressFromArray, CheckCompressFromArray}, // 15
{FeedDecompress, CheckDecompress}, // 16
{FeedCompressFromArray, CheckCompressFromArray}, // 17
{FeedDecompress, CheckDecompress}, // 18
{FeedCompressFromArray, CheckCompressFromArray}, // 19
{FeedDecompress, CheckDecompress}, // 20
{FeedCompressFromArray, CheckCompressFromArray}, // 21
{FeedDecompress, CheckDecompress}, // 22
{FeedCompressFromArray, CheckCompressFromArray}, // 23
{FeedDecompress, CheckDecompress}, // 24
{FeedCompressFromArray, CheckCompressFromArray}, // 25
{FeedDecompress, CheckDecompress}, // 26
{FeedCompressBig1, CheckCompressBig1}, // 27 n=16
{FeedDecompress, CheckDecompress}, // 28
{FeedCompressBig2, CheckCompressBig2}, // 29
{FeedDecompress, CheckDecompress}, // 30
{FeedCompressBig3, CheckCompressBig3}, // 31
{FeedDecompress, CheckDecompress}, // 32
{FeedCompressBig1, CheckCompressBig1}, // 33 n=21
{FeedDecompress, CheckDecompress}, // 34
{FeedCompressBig1, CheckCompressBig1}, // 35 n=26
{FeedDecompress, CheckDecompress}, // 36
{FeedCompressBig1, CheckCompressBig1}, // 37 n=31
{FeedDecompress, CheckDecompress}, // 38
{FeedCompressBig1, CheckCompressBig1}, // 39 n=35
{FeedDecompress, CheckDecompress}, // 40
};
TLabTest GetLabTest(int testIdx) {
return LabTests[testIdx];
}
int GetTestCount(void) {
return sizeof(LabTests)/sizeof(LabTests[0]);
}
const char* GetTesterName(void) {
return "Lab 5 Huffman";
}
int GetTestTimeout(void) {
return 6000;
}
size_t GetTestMemoryLimit(void) {
return MIN_PROCESS_RSS_BYTES;
}