-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
547 lines (496 loc) · 17.5 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "common.h"
#include "sugar.h"
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#define FIBSEQ_MINSZ 10U
#define TEST_COUNT 13U
#define DECIMAL_BASE 10
/* cheese */
string const cheese[] = {"Red Leicester", "42", "Tilsit", "EVEN", "EVEN", "Caerphilly", "Bel Paese", "ODD", "94", "41",
"3", "Red Windsor", "Stilton", "Gruyere", "Ementhal", "Norweigan Jarlsburg", "ODD", "EVEN", "ODD", "0", "19",
"Lipta", "Lancashire", "White Stilton"};
size_t const cheeselen = sizeof(cheese) / sizeof(*cheese);
static bool test_take(void)
{
/* Build a fibonacci array, for verification later */
uint32_t fibarr[FIBSEQ_MINSZ * 2] = {0, 1};
for (size_t i = 2; i < FIBSEQ_MINSZ * 2; i++) {
fibarr[i] = fibarr[i - 1] + fibarr[i - 2];
}
Iterable(uint32_t) it = get_fibitr(); /* Create an infinite fibonacci sequence iterable */
/* Iterable of the first FIBSEQ_MINSZ number of items in the sequence */
Iterable(uint32_t) itslice = take(it, FIBSEQ_MINSZ);
/* Verify the values */
size_t i = 0;
foreach (uint32_t, n, itslice) {
if (n != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i], n, i);
return false;
}
i++;
}
/* Consumed FIBSEQ_MINSZ number of items from `it` - take the next FIBSEQ_MINSZ number of items */
Iterable(uint32_t) itslice_ex = take(it, FIBSEQ_MINSZ);
/* Verify the values */
foreach (uint32_t, n, itslice_ex) {
if (n != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i], n, i);
return false;
}
i++;
}
return true;
}
static bool test_drop(void)
{
/* Build a fibonacci array, for verification later */
uint32_t fibarr[FIBSEQ_MINSZ * 2] = {0, 1};
for (size_t i = 2; i < FIBSEQ_MINSZ * 2; i++) {
fibarr[i] = fibarr[i - 1] + fibarr[i - 2];
}
/* Drop the first FIBSEQ_MINSZ number of items and take the next FIBSEQ_MINSZ number of items */
Iterable(uint32_t) itslice = take(drop(get_fibitr(), FIBSEQ_MINSZ), FIBSEQ_MINSZ);
/* Verify the values */
size_t i = FIBSEQ_MINSZ;
foreach (uint32_t, n, itslice) {
if (n != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i], n, i);
return false;
}
i++;
}
return true;
}
static NumType u32_to_numtype(uint32_t x) { return x % 2 == 0 ? EVEN : ODD; }
static bool test_map(void)
{
/* Build a NumType array based on the fibonacci sequence, for verification later */
uint32_t prev = 0;
uint32_t curr = 1;
NumType numtypearr[FIBSEQ_MINSZ] = {0};
for (size_t i = 0; i < FIBSEQ_MINSZ; i++) {
numtypearr[i] = u32_to_numtype(prev);
uint32_t new_curr = prev + curr;
prev = curr;
curr = new_curr;
}
/* Map u32_to_numtype over it and take the first FIBSEQ_MINSZ number of items */
Iterable(NumType) itslice = take(map(get_fibitr(), u32_to_numtype), FIBSEQ_MINSZ);
/* Verify the values */
size_t i = 0;
foreach (NumType, n, itslice) {
if (n != numtypearr[i]) {
fprintf(stderr, "%s: Expected: %d Actual: %d at index: %zu\n", __func__, numtypearr[i], n, i);
return false;
}
i++;
}
return true;
}
static bool is_even(uint32_t x) { return x % 2 == 0; }
static bool is_odd(uint32_t x) { return x % 2 != 0; }
static bool test_filter(void)
{
/* Build an array consisting of even fibonacci numbers, for verification later */
uint32_t prev = 0;
uint32_t curr = 1;
uint32_t filteredarr[FIBSEQ_MINSZ] = {0};
for (size_t i = 0; i < FIBSEQ_MINSZ;) {
if (is_even(prev)) {
filteredarr[i] = prev;
i++;
}
uint32_t new_curr = prev + curr;
prev = curr;
curr = new_curr;
}
/* Filter out only the even fibonacci numbers, and take the first FIBSEQ_MINSZ number of them */
Iterable(uint32_t) itslice = take(filter(get_fibitr(), is_even), FIBSEQ_MINSZ);
/* Verify the values */
size_t i = 0;
foreach (uint32_t, n, itslice) {
if (n != filteredarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, filteredarr[i],
n, i);
return false;
}
i++;
}
return true;
}
/* Returns true if length of string is less than 7 */
#define SMALLSTR_MAXLEN 7
static bool is_smallstr(string str) { return strlen(str) < SMALLSTR_MAXLEN; }
/* Try-parse a string to a number */
static Maybe(uint32_t) parse_posu32(string str)
{
char* end;
unsigned long l = strtoul(str, &end, DECIMAL_BASE);
errno = 0;
if (errno == ERANGE || *end != '\0') {
return Nothing(uint32_t);
}
return Just(l, uint32_t);
}
/* Try-parse a string to a NumType */
static Maybe(NumType) parse_numtype(string str)
{
if (strcmp(str, "EVEN") == 0) {
return Just(EVEN, NumType);
}
if (strcmp(str, "ODD") == 0) {
return Just(ODD, NumType);
}
return Nothing(NumType);
}
static bool test_filtermap(void)
{
/* Build arrays of expected data, for verification later */
uint32_t expectednums[FIBSEQ_MINSZ];
NumType expectednumtypes[FIBSEQ_MINSZ];
for (size_t i = 0, j = 0, k = 0; i < sizeof(cheese) / sizeof(*cheese); i++) {
string s = cheese[i];
if (is_smallstr(s)) {
Maybe(uint32_t) parsednum = parse_posu32(s);
if (is_just(parsednum)) {
expectednums[j++] = from_just_(parsednum);
}
Maybe(NumType) parsednumtype = parse_numtype(s);
if (is_just(parsednumtype)) {
expectednumtypes[k++] = from_just_(parsednumtype);
}
}
}
/*
Obtain only the first 10 "small" strings, map a parsing function over it, and obtain only the
successfully parsed numbers
*/
Iterable(uint32_t) parsedit =
filter_map(take(filter(strarr_to_iter(cheese, cheeselen), is_smallstr), 10), parse_posu32);
size_t i = 0;
foreach (uint32_t, n, parsedit) {
if (n != expectednums[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, expectednums[i],
n, i);
return false;
}
i++;
}
/*
Obtain only the first 10 "small" strings, map another parsing function over it, and obtain only the
successfully parsed numtypes
*/
Iterable(NumType) numtypeit =
filter_map(take(filter(strarr_to_iter(cheese, cheeselen), is_smallstr), 10), parse_numtype);
i = 0;
foreach (NumType, x, numtypeit) {
if (x != expectednumtypes[i]) {
fprintf(stderr, "%s: Expected: %d Actual: %d at index: %zu\n", __func__, expectednumtypes[i], x, i);
return false;
}
i++;
}
return true;
}
static bool test_chain(void)
{
/* Build a fibonacci array, for verification later */
uint32_t fibarr[FIBSEQ_MINSZ * 3] = {0, 1};
for (size_t i = 2; i < FIBSEQ_MINSZ * 3; i++) {
fibarr[i] = fibarr[i - 1] + fibarr[i - 2];
}
/* Chain 2 sequential parts of the fibonacci sequence- 0-10 -> 10-20 */
Iterable(uint32_t) chained =
chain(take(get_fibitr(), FIBSEQ_MINSZ), take(drop(get_fibitr(), FIBSEQ_MINSZ), FIBSEQ_MINSZ));
size_t i = 0;
foreach (uint32_t, n, chained) {
if (n != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i], n, i);
return false;
}
i++;
}
/* Chain various parts of the fibonacci sequence- 0-10 -> (10-20 -> 20-30) */
Iterable(uint32_t) dual_chained_fr =
chain(take(get_fibitr(), FIBSEQ_MINSZ), chain(take(drop(get_fibitr(), FIBSEQ_MINSZ), FIBSEQ_MINSZ),
take(drop(get_fibitr(), FIBSEQ_MINSZ * 2), FIBSEQ_MINSZ)));
i = 0;
foreach (uint32_t, n, dual_chained_fr) {
if (n != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i], n, i);
return false;
}
i++;
}
/* Chain various parts of the fibonacci sequence- (0-10 -> 10-20) -> 20-30 */
Iterable(uint32_t) dual_chained_bk =
chain(chain(take(get_fibitr(), FIBSEQ_MINSZ), take(drop(get_fibitr(), FIBSEQ_MINSZ), FIBSEQ_MINSZ)),
take(drop(get_fibitr(), FIBSEQ_MINSZ * 2), FIBSEQ_MINSZ));
i = 0;
foreach (uint32_t, n, dual_chained_bk) {
if (n != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i], n, i);
return false;
}
i++;
}
return true;
}
static uint32_t add_u32(uint32_t x, uint32_t y) { return x + y; }
static bool test_reduce(void)
{
/* Build an array consisting of even fibonacci numbers, for verification later */
uint32_t prev = 0;
uint32_t curr = 1;
uint32_t result = prev;
for (size_t i = 1; i < FIBSEQ_MINSZ; i++) {
uint32_t new_curr = prev + curr;
prev = curr;
curr = new_curr;
result = add_u32(result, prev);
}
/*
Reduce an iterable of the first FIBSEQ_MINSZ number of items of the fibonacci
sequence with add_u32
*/
Maybe(uint32_t) reduced = reduce(take(get_fibitr(), FIBSEQ_MINSZ), add_u32);
if (from_just(reduced, uint32_t) != result) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 "\n", __func__, result, from_just_(reduced));
return false;
}
/* Make sure reducing an empty iterable yields `Nothing` */
Maybe(uint32_t) nothing = reduce(take(get_fibitr(), 0), add_u32);
if (is_just(nothing)) {
fprintf(stderr, "%s: Expected: Nothing Actual: %" PRIu32 "\n", __func__, from_just_(nothing));
return false;
}
return true;
}
static bool test_takewhile(void)
{
/* Build an array of the first odd fibonacci numbers, for verification later */
uint32_t prev = 0;
uint32_t curr = 1;
uint32_t first_oddarr[FIBSEQ_MINSZ] = {0};
for (size_t i = 0; i < FIBSEQ_MINSZ; i++) {
if (!is_odd(prev)) {
break;
}
first_oddarr[i] = prev;
uint32_t new_curr = prev + curr;
prev = curr;
curr = new_curr;
}
/* Take the longest prefix of odd numbers from the infinite fibonacci sequence */
Iterable(uint32_t) first_odds = takewhile(get_fibitr(), is_odd);
size_t i = 0;
foreach (uint32_t, n, first_odds) {
if (n != first_oddarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, first_oddarr[i],
n, i);
return false;
}
i++;
}
return true;
}
static bool test_dropwhile(void)
{
/* Build an array of the first odd fibonacci numbers, for verification later */
uint32_t prev = 0;
uint32_t curr = 1;
uint32_t odds_after_evensarr[FIBSEQ_MINSZ] = {0};
for (; is_even(prev);) {
uint32_t new_curr = prev + curr;
prev = curr;
curr = new_curr;
}
for (size_t i = 0; i < FIBSEQ_MINSZ; i++) {
if (!is_odd(prev)) {
break;
}
odds_after_evensarr[i] = prev;
uint32_t new_curr = prev + curr;
prev = curr;
curr = new_curr;
}
/* Take the longest prefix of odd numbers from the infinite fibonacci sequence */
Iterable(uint32_t) odds_after_evens = takewhile(dropwhile(get_fibitr(), is_even), is_odd);
size_t i = 0;
foreach (uint32_t, n, odds_after_evens) {
if (n != odds_after_evensarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__,
odds_after_evensarr[i], n, i);
return false;
}
i++;
}
return true;
}
static bool test_collect(void)
{
/* Build a fibonacci array, for verification later */
uint32_t fibarr[FIBSEQ_MINSZ] = {0, 1};
for (size_t i = 2; i < FIBSEQ_MINSZ; i++) {
fibarr[i] = fibarr[i - 1] + fibarr[i - 2];
}
/* Collect the first `FIBSEQ_MINSZ` number of items from the fibonacci sequence into an array */
size_t fibarrsz = 0;
uint32_t* const clct_fibarr = collect(take(get_fibitr(), FIBSEQ_MINSZ), &fibarrsz);
if (clct_fibarr == NULL) {
fprintf(stderr, "Collect returned NULL\n");
return false;
}
for (size_t i = 0; i < fibarrsz; i++) {
if (clct_fibarr[i] != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 " at index: %zu\n", __func__, fibarr[i],
clct_fibarr[i], i);
return false;
}
}
free(clct_fibarr);
return true;
}
/* Accumulating function to parse strings to numbers and add them */
static uint32_t addparse_u32(uint32_t acc, string s)
{
Maybe(uint32_t) parsed = parse_posu32(s);
return is_just(parsed) ? acc + from_just_(parsed) : acc;
}
/* Function that takes 2 strings and returns the second one */
static string unconst_str(string a, string b)
{
(void)a;
return b;
}
static bool test_fold(void)
{
uint32_t expectedsum = 0;
for (size_t i = 0; i < sizeof(cheese) / sizeof(*cheese); i++) {
string s = cheese[i];
Maybe(uint32_t) parsednum = parse_posu32(s);
if (is_just(parsednum)) {
expectedsum += from_just_(parsednum);
}
}
/* Fold the string iterable with addparse_u32, starting with a value of 0 */
uint32_t sum = fold(strarr_to_iter(cheese, cheeselen), 0, addparse_u32);
if (sum != expectedsum) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 "\n", __func__, expectedsum, sum);
return false;
}
/* Fold the string iterable with unconst_str, starting with a value of "" */
string lastcheese = fold(strarr_to_iter(cheese, cheeselen), "", unconst_str);
if (lastcheese != cheese[cheeselen - 1]) {
fprintf(stderr, "%s: Expected: %s Actual: %s\n", __func__, cheese[cheeselen - 1], lastcheese);
return false;
}
return true;
}
static bool test_enumerate(void)
{
/* Build a fibonacci array, for verification later */
uint32_t fibarr[FIBSEQ_MINSZ] = {0, 1};
for (size_t i = 2; i < FIBSEQ_MINSZ; i++) {
fibarr[i] = fibarr[i - 1] + fibarr[i - 2];
}
/* Get the enumerated iterator of the first FIBSEQ_MINSZ number of items from the fibonacci sequence */
Iterable(Pair(size_t, uint32_t)) enumrted = enumerate(take(get_fibitr(), FIBSEQ_MINSZ));
size_t i = 0;
foreach (Pair(size_t, uint32_t), x, enumrted) {
if (fst(x) != i) {
fprintf(stderr, "%s: Expected: %zu Actual: %zu\n", __func__, i, fst(x));
return false;
}
if (snd(x) != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 "\n", __func__, fibarr[i], snd(x));
return false;
}
i++;
}
if (i != FIBSEQ_MINSZ) {
fprintf(stderr, "%s: Expected: %zu Actual: %zu\n", __func__, (size_t)FIBSEQ_MINSZ, i);
return false;
}
return true;
}
static bool test_zip(void)
{
/* Build a fibonacci array, for verification later */
uint32_t fibarr[FIBSEQ_MINSZ + 1] = {0, 1};
for (size_t i = 2; i < FIBSEQ_MINSZ + 1; i++) {
fibarr[i] = fibarr[i - 1] + fibarr[i - 2];
}
/* Zip together 2 fibonacci iterators, the first of which is shifted to the right by one element */
Iterable(Pair(uint32_t, uint32_t)) enumrted = zip(drop(get_fibitr(), 1), take(get_fibitr(), FIBSEQ_MINSZ));
size_t i = 0;
foreach (Pair(uint32_t, uint32_t), x, enumrted) {
if (fst(x) != fibarr[i + 1]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 "\n", __func__, fibarr[i + 1], fst(x));
return false;
}
if (snd(x) != fibarr[i]) {
fprintf(stderr, "%s: Expected: %" PRIu32 " Actual: %" PRIu32 "\n", __func__, fibarr[i], snd(x));
return false;
}
i++;
}
if (i != FIBSEQ_MINSZ) {
fprintf(stderr, "%s: Expected: %zu Actual: %zu\n", __func__, (size_t)FIBSEQ_MINSZ, i);
return false;
}
return true;
}
int main(void)
{
size_t passed = 0;
if (test_take()) {
passed++;
}
if (test_map()) {
passed++;
}
if (test_filter()) {
passed++;
}
if (test_drop()) {
passed++;
}
if (test_filtermap()) {
passed++;
}
if (test_chain()) {
passed++;
}
if (test_reduce()) {
passed++;
}
if (test_takewhile()) {
passed++;
}
if (test_dropwhile()) {
passed++;
}
if (test_collect()) {
passed++;
}
if (test_fold()) {
passed++;
}
if (test_enumerate()) {
passed++;
}
if (test_zip()) {
passed++;
}
if (passed == TEST_COUNT) {
puts("All tests passing....");
} else {
puts("Some tests failed....");
}
return 0;
}