-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
376 lines (290 loc) · 8.31 KB
/
main.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
#include "c_utils.h"
#include "rsw_cstr.h"
#define PRIORITY_QUEUE_IMPLEMENTATION
#include "priority_queue.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <CUnit/Automated.h>
void file_rw_test()
{
c_array file_contents, file_contents2;
int size = file_open_read("../alt-2600-hack-faq.txt", "r", &file_contents);
CU_ASSERT_EQUAL(size, 247708);
CU_ASSERT_EQUAL(file_contents.elem_size, 1);
CU_ASSERT_EQUAL(file_contents.len, size);
file_open_write("../alt-2600.out", "w", &file_contents);
FILE* file = fopen("../alt-2600.out", "r");
size = file_read(file, &file_contents2);
CU_ASSERT_EQUAL(size, 247708);
CU_ASSERT_EQUAL(file_contents.elem_size, file_contents2.elem_size);
CU_ASSERT_EQUAL(file_contents.len, file_contents2.len);
CU_ASSERT_STRING_EQUAL(file_contents.data, file_contents2.data);
free(file_contents.data);
free(file_contents2.data);
}
char *int2bin(int a, char *buffer, int buf_size) {
char* buf = buffer + buf_size - 1;
do {
*buf-- = (a & 1) + '0';
a >>= 1;
} while (a);
return memmove(buffer, ++buf, buffer+buf_size-buf);
}
void misc_test()
{
char* zero = int_to_str(0, 10);
CU_ASSERT_STRING_EQUAL(zero, "0");
char* neg = int_to_str(-1532, 10);
CU_ASSERT_STRING_EQUAL(neg, "-1532");
char* hexdollar = int_to_str(256, 16);
CU_ASSERT_STRING_EQUAL(hexdollar, "100");
srand(time(NULL));
int r = rand();
char* randnum = int_to_str(r, 10);
char buf[100] = { 0 };
sprintf(buf, "%d", r);
CU_ASSERT_STRING_EQUAL(randnum, buf);
char* randbin = int_to_str(r, 2);
int2bin(r, buf, 99);
printf("%s\n%s\n", randbin, buf);
CU_ASSERT_STRING_EQUAL(randbin, buf);
char* randoct = int_to_str(r, 8);
sprintf(buf, "%o", r);
CU_ASSERT_STRING_EQUAL(randoct, buf);
char* randhex = int_to_str(r, 16);
sprintf(buf, "%X", r);
CU_ASSERT_STRING_EQUAL(randhex, buf);
free(zero);
free(neg);
free(hexdollar);
free(randnum);
free(randbin);
free(randoct);
free(randhex);
}
void mystrtok_test()
{
char test1[] = "hello|blah,goodbye|,whatever|,-who-are-|you";
char test2[] = "abc,,cdef,ghi ,jkl,|,mnp";
char* results1[] =
{
"hello",
"blah,goodbye",
",whatever",
",-who-are-",
"you"
};
char* results2[] =
{
"abc",
"",
"cdef",
"ghi ",
"jkl",
"|",
"mnp"
};
char buf[256];
strcpy(buf, test1);
char* tok = mystrtok(buf, '|');
int i = 0;
while (tok) {
CU_ASSERT_STRING_EQUAL(tok, results1[i]);
tok = mystrtok(NULL, '|');
i++;
}
strcpy(buf, test2);
i = 0;
tok = mystrtok(buf, ',');
while (tok) {
CU_ASSERT_STRING_EQUAL(tok, results2[i]);
tok = mystrtok(NULL, ',');
i++;
}
strcpy(buf, test1);
i = 0;
tok = mystrtok_alloc(buf, '|');
while (tok) {
CU_ASSERT_STRING_EQUAL(tok, results1[i]);
free(tok);
tok = mystrtok_alloc(NULL, '|');
i++;
}
strcpy(buf, test2);
i = 0;
tok = mystrtok_alloc(buf, ',');
while (tok) {
CU_ASSERT_STRING_EQUAL(tok, results2[i]);
free(tok);
tok = mystrtok_alloc(NULL, ',');
i++;
}
}
void file_rw_cstr_test()
{
rsw_cstr file_str;
int size = file_open_read_new_cstr("../alt-2600-hack-faq.txt", &file_str);
CU_ASSERT_EQUAL(size, 247708);
CU_ASSERT_EQUAL(file_str.size, size);
CU_ASSERT_EQUAL(file_str.capacity, size+1); //still haven't decided if I'll change this capacity
file_open_write_cstr("../alt_2600.out", &file_str);
FILE* file = fopen("../alt_2600.out", "r");
rsw_cstr file_str2;
init_cstr(&file_str2);
size = file_read_cstr(file, &file_str2);
CU_ASSERT_EQUAL(size, 247708);
CU_ASSERT_EQUAL(file_str2.size, size);
CU_ASSERT_STRING_EQUAL(file_str.a, file_str2.a);
free_cstr(&file_str);
free_cstr(&file_str2);
}
void bottles_of_beer_test()
{
const char verse[] = "#a bottles of beer on the wall, #a bottles of beer, take one down, pass it around, #b bottles of beer on the wall.";
const char verse0[] = "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store, buy some more, 99 bottles of beer on the wall.";
const char verse1[] = "1 bottle of beer on the wall, 1 bottle of beer.\nTake one down, pass it around, no more bottles of beer on the wall.";
const char of_beer[] = "of beer";
const char wall[] = "on the wall";
const char take_one[] = "Take one down, pass it around,";
int len = strlen(verse);
char int_buf[5], int_buf2[5];
rsw_cstr template_str;
// init_cstr_str(&template_str, verse, len);
int tmp;
#define NUM_BOTTLES 5
FILE* file1 = fopen("bottles1.txt", "w");
for (int i=NUM_BOTTLES; i>=0; --i) {
switch (i) {
case 0:
fprintf(file1, "No more bottles %s %s, no more bottles %s.\nGo to the store, buy some more, 99 bottles %s %s.\n", of_beer, wall, of_beer, of_beer, wall);
break;
case 1:
fprintf(file1, "1 bottle %s %s, 1 bottle %s.\n%s no more bottles %s %s.\n\n", of_beer, wall, of_beer, take_one, of_beer, wall);
break;
default:
fprintf(file1, "%d bottles %s %s, %d bottles %s.\n", i, of_beer, wall, i, of_beer);
fprintf(file1, "%s %d %s %s %s.\n\n", take_one, i-1, (i!=2) ? "bottles" : "bottle", of_beer, wall);
}
}
fclose(file1);
rsw_cstr bottle_str;
int size = file_open_read_new_cstr("bottles1.txt", &bottle_str);
rsw_cstr song;
init_cstr(&song);
for (int i=NUM_BOTTLES; i>=0; --i) {
switch (i) {
case 0:
cstr_concatenate(&song, verse0, strlen(verse0));
cstr_push(&song, '\n');
break;
case 1:
cstr_concatenate(&song, verse1, strlen(verse1));
cstr_push(&song, '\n');
cstr_push(&song, '\n');
break;
default:
snprintf(int_buf, 5, "%d", i);
snprintf(int_buf2, 5, "%d", i-1);
cstr_concatenate(&song, int_buf, strlen(int_buf));
cstr_concatenate(&song, " bottles ", 9);
cstr_concatenate(&song, of_beer, strlen(of_beer));
cstr_push(&song, ' ');
cstr_concatenate(&song, wall, strlen(wall));
cstr_concatenate(&song, ", ", 2);
cstr_concatenate(&song, int_buf, strlen(int_buf));
cstr_concatenate(&song, " bottles ", 9);
cstr_concatenate(&song, of_beer, strlen(of_beer));
cstr_concatenate(&song, ".\n", 2);
cstr_concatenate(&song, take_one, strlen(take_one));
cstr_push(&song, ' ');
cstr_concatenate(&song, int_buf2, strlen(int_buf2));
if (i != 2)
cstr_concatenate(&song, " bottles ", 9);
else
cstr_concatenate(&song, " bottle ", 8);
cstr_concatenate(&song, of_beer, strlen(of_beer));
cstr_push(&song, ' ');
cstr_concatenate(&song, wall, strlen(wall));
cstr_concatenate(&song, ".\n\n", 3);
}
}
CU_ASSERT_STRING_EQUAL(song.a, bottle_str.a);
cstr_replace(&song, "beer", "soda", 0);
printf("\"%s\"\n\n", song.a);
cstr_replace(&song, "wall", "shelf", 0);
printf("\"%s\"\n\n", song.a);
free_cstr(&song);
free_cstr(&bottle_str);
}
void priority_queue_test()
{
int heap[100];
size_t sz = 0, cap = 100;
int a, b = INT_MAX;
for (int i=0; i<50; ++i)
maxheap_push(heap, &sz, cap, rand() % 400);
for (int i=0; i<25; ++i) {
a = maxheap_pop(heap, &sz);
CU_ASSERT(a <= b);
//printf("%d\n", a);
b = a;
}
putchar('\n');
for (int i=0; i<20; ++i)
maxheap_push(heap, &sz, cap, rand() % 400);
b = INT_MAX;
while (sz > 0) {
a = maxheap_pop(heap, &sz);
CU_ASSERT(a <= b);
//printf("%d\n", a);
b = a;
}
}
CU_TestInfo c_utils_tests[] = {
{ "file_rw", file_rw_test },
{ "misc_test", misc_test },
{ "mystrtok_test", mystrtok_test },
CU_TEST_INFO_NULL
};
CU_TestInfo rsw_cstr_tests[] = {
{ "file_rw_cstr", file_rw_cstr_test },
{ "bottles_of_beer", bottles_of_beer_test },
CU_TEST_INFO_NULL
};
CU_TestInfo priority_queue_tests[] = {
{ "pqtest", priority_queue_test },
CU_TEST_INFO_NULL
};
CU_SuiteInfo suites[] = {
#ifndef OLD_CUNIT
{ "c_utils", NULL, NULL, NULL, NULL, c_utils_tests },
{ "rsw_cstr", NULL, NULL, NULL, NULL, rsw_cstr_tests },
{ "priority_queue_tests", NULL, NULL, NULL, NULL, priority_queue_tests },
#else
{ "c_utils", NULL, NULL, c_utils_tests },
{ "rsw_cstr", NULL, NULL, rsw_cstr_tests },
{ "priority_queue_tests", NULL, NULL, priority_queue_tests },
#endif
CU_SUITE_INFO_NULL
};
int main()
{
CU_ErrorCode error;
error = CU_initialize_registry();
if (error != CUE_SUCCESS) {
fprintf(stderr, "Failed to initialize registry\n");
return CU_get_error();
}
error = CU_register_suites(suites);
if (error != CUE_SUCCESS) {
fprintf(stderr, "Failed to register test suite\n");
return CU_get_error();
}
CU_automated_run_tests();
fprintf(stdout, "CU_get_error() returned %d\n", CU_get_error());
CU_cleanup_registry();
return CU_get_error();
}