-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.c
943 lines (768 loc) · 27.3 KB
/
test.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
#include "ok_lib.h"
#include <assert.h>
#include <stdio.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wfloat-equal"
#pragma GCC diagnostic ignored "-Wunused-value"
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
#endif
#ifdef WIN32
#define strcat(dest, src) strcat_s(dest, sizeof(dest), src)
#define strdup(str) _strdup(str)
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Test framework
static int ok_test_total_count = 0;
static int ok_test_failure_count = 0;
#ifdef WIN32
#define OK_FILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define OK_FILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
#define ok_assert(condition, message) do {\
ok_test_total_count++; \
if (!(condition)) { \
ok_test_failure_count++; \
printf("FAIL: %s:%i - function: %s - test: %s\n", OK_FILE, __LINE__, __func__, message);\
} \
} while (0)
static void ok_tests_start(void) {
ok_test_total_count = 0;
ok_test_failure_count = 0;
}
static void ok_tests_finish(void) {
printf("%s: %i of %i tests passed\n", OK_FILE, (ok_test_total_count - ok_test_failure_count),
ok_test_total_count);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Common
static int counter = 0;
typedef struct {
float x;
float y;
} point_t;
static ok_hash_t point_hash(point_t p) {
ok_hash_t hash_x = ok_float_hash(p.x);
ok_hash_t hash_y = ok_float_hash(p.y);
return ok_hash_combine(hash_x, hash_y);
}
static bool point_equals(const void *v1, const void *v2) {
const point_t *p1 = (const point_t *)v1;
const point_t *p2 = (const point_t *)v2;
return (p1->x == p2->x && p1->y == p2->y);
}
static ok_hash_t bad_hash(int v) {
(void)v;
// Don't do this! For testing only
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Test vec
static void vec_int_ptr_func(int *value) {
*value = counter++;
}
static void vec_int_func(int value) {
counter += value;
}
static int vec_int_cmp(const void *v1, const void *v2) {
const int a = *(const int *)v1;
const int b = *(const int *)v2;
return (a < b) ? -1 : (a > b);
}
static int vec_str_cmp(const void *a, const void *b) {
const char *str1 = *(const char * const *)a;
const char *str2 = *(const char * const *)b;
return strcmp(str1, str2);
}
static void test_vec(void) {
typedef struct ok_vec_of(int) vec_int_t;
vec_int_t vec;
ok_vec_init(&vec);
ok_vec_push(&vec, 10);
ok_vec_push(&vec, 10);
ok_vec_push(&vec, 10);
ok_assert(ok_vec_count(&vec) == 3, "ok_vec_push");
ok_assert(ok_vec_get(&vec, 0) == 10 && ok_vec_get(&vec, 1) == 10 && ok_vec_get(&vec, 2) == 10,
"ok_vec_get");
*ok_vec_get_ptr(&vec, 1) = 20;
*ok_vec_get_ptr(&vec, 2) = 30;
ok_assert(ok_vec_get(&vec, 0) == 10 && ok_vec_get(&vec, 1) == 20 && ok_vec_get(&vec, 2) == 30,
"ok_vec_get_ptr");
ok_vec_remove_at(&vec, 1);
ok_vec_insert_at(&vec, 1, 20);
ok_assert(ok_vec_get(&vec, 0) == 10 && ok_vec_get(&vec, 1) == 20 && ok_vec_get(&vec, 2) == 30,
"ok_vec_remove_at / ok_vec_insert_at");
ok_vec_remove(&vec, 10);
ok_vec_insert_at(&vec, 0, 10);
ok_assert(ok_vec_get(&vec, 0) == 10 && ok_vec_get(&vec, 1) == 20 && ok_vec_get(&vec, 2) == 30,
"ok_vec_remove / ok_vec_insert_at");
ok_vec_remove_at(&vec, ok_vec_count(&vec) - 1);
ok_vec_insert_at(&vec, ok_vec_count(&vec), 30);
ok_assert(ok_vec_get(&vec, 0) == 10 && ok_vec_get(&vec, 1) == 20 && ok_vec_get(&vec, 2) == 30,
"ok_vec_remove_at / ok_vec_insert_at end");
////////////////////////////////////////////////////////////////////////////////////////////////
// ok_vec_foreach
int sum = 0;
ok_vec_foreach(&vec, int value) {
sum += value;
}
ok_assert(sum == 60, "ok_vec_foreach");
// ok_vec_foreach: No curly braces
sum = 0;
ok_vec_foreach(&vec, int value)
sum += value;
ok_assert(sum == 60, "ok_vec_foreach without curly braces");
// ok_vec_foreach: Break
int i = 0;
sum = 0;
ok_vec_foreach(&vec, int value) {
sum += value;
if (i == 1) {
break;
}
i++;
}
ok_assert(sum == 30, "ok_vec_foreach with break");
// ok_vec_foreach: Inner loop
sum = 0;
ok_vec_foreach(&vec, int value) {
sum += value;
ok_vec_foreach(&vec, int value2) {
sum += value2;
}
sum -= 60;
}
ok_assert(sum == 60, "ok_vec_foreach with inner loop");
////////////////////////////////////////////////////////////////////////////////////////////////
// ok_vec_foreach_rev
sum = 0;
ok_vec_foreach_rev(&vec, int value) {
sum += value;
}
ok_assert(sum == 60, "ok_vec_foreach_rev");
// ok_vec_foreach_rev: No curly braces
sum = 0;
ok_vec_foreach_rev(&vec, int value)
sum += value;
ok_assert(sum == 60, "ok_vec_foreach_rev without curly braces");
// ok_vec_foreach_rev: Break
i = 0;
sum = 0;
ok_vec_foreach_rev(&vec, int value) {
sum += value;
if (i == 1) {
break;
}
i++;
}
ok_assert(sum == 50, "ok_vec_foreach_rev with break");
// ok_vec_foreach_rev: Inner loop
sum = 0;
ok_vec_foreach_rev(&vec, int value) {
sum += value;
ok_vec_foreach_rev(&vec, int value2) {
sum += value2;
}
sum -= 60;
}
ok_assert(sum == 60, "ok_vec_foreach_rev with inner loop");
////////////////////////////////////////////////////////////////////////////////////////////////
// ok_vec_foreach_ptr
sum = 0;
ok_vec_foreach_ptr(&vec, int *value) {
sum += *value;
}
ok_assert(sum == 60, "ok_vec_foreach_ptr");
// ok_vec_foreach_ptr: No curly braces
sum = 0;
ok_vec_foreach_ptr(&vec, int *value)
sum += *value;
ok_assert(sum == 60, "ok_vec_foreach_ptr without curly braces");
// ok_vec_foreach_ptr: Break
i = 0;
sum = 0;
ok_vec_foreach_ptr(&vec, int *value) {
sum += *value;
if (i == 1) {
break;
}
i++;
}
ok_assert(sum == 30, "ok_vec_foreach_ptr with break");
// ok_vec_foreach_ptr: Inner loop
sum = 0;
ok_vec_foreach_ptr(&vec, int *value) {
sum += *value;
ok_vec_foreach_ptr(&vec, int *value2) {
sum += *value2;
}
sum -= 60;
}
ok_assert(sum == 60, "ok_vec_foreach_ptr with inner loop");
// ok_vec_foreach_ptr: set values
i = 0;
ok_vec_foreach_ptr(&vec, int *value) {
*value = i;
i += 10;
}
ok_assert(ok_vec_get(&vec, 0) == 0 && ok_vec_get(&vec, 1) == 10 && ok_vec_get(&vec, 2) == 20,
"ok_vec_foreach_ptr: set values");
////////////////////////////////////////////////////////////////////////////////////////////////
*ok_vec_get_ptr(&vec, 0) = 10;
*ok_vec_get_ptr(&vec, 1) = 20;
*ok_vec_get_ptr(&vec, 2) = 30;
// ok_vec_foreach_ptr_rev
sum = 0;
ok_vec_foreach_ptr_rev(&vec, int *value) {
sum += *value;
}
ok_assert(sum == 60, "ok_vec_foreach_ptr_rev");
// ok_vec_foreach_ptr_rev: No curly braces
sum = 0;
ok_vec_foreach_ptr_rev(&vec, int *value)
sum += *value;
ok_assert(sum == 60, "ok_vec_foreach_ptr_rev without curly braces");
// ok_vec_foreach_ptr_rev: Break
i = 0;
sum = 0;
ok_vec_foreach_ptr_rev(&vec, int *value) {
sum += *value;
if (i == 1) {
break;
}
i++;
}
ok_assert(sum == 50, "ok_vec_foreach_ptr_rev with break");
// ok_vec_foreach_ptr_rev: Inner loop
sum = 0;
ok_vec_foreach_ptr_rev(&vec, int *value) {
sum += *value;
ok_vec_foreach_ptr_rev(&vec, int *value2) {
sum += *value2;
}
sum -= 60;
}
ok_assert(sum == 60, "ok_vec_foreach_ptr_rev with inner loop");
// ok_vec_foreach_ptr_rev: set values
i = 0;
ok_vec_foreach_ptr_rev(&vec, int *value) {
*value = i;
i += 10;
}
ok_assert(ok_vec_get(&vec, 0) == 20 && ok_vec_get(&vec, 1) == 10 && ok_vec_get(&vec, 2) == 0,
"ok_vec_foreach_ptr_rev: set values");
////////////////////////////////////////////////////////////////////////////////////////////////
// Apply ptr
counter = 0;
ok_vec_apply_ptr(&vec, vec_int_ptr_func);
ok_assert(ok_vec_get(&vec, 0) == 0 && ok_vec_get(&vec, 1) == 1 && ok_vec_get(&vec, 2) == 2,
"ok_vec_apply_ptr");
// Apply
counter = 0;
ok_vec_apply(&vec, vec_int_func);
ok_assert(counter == 3, "ok_vec_apply");
// Sort
*ok_vec_get_ptr(&vec, 0) = 2;
*ok_vec_get_ptr(&vec, 1) = 0;
*ok_vec_get_ptr(&vec, 2) = 1;
ok_vec_sort(&vec, vec_int_cmp);
ok_assert(ok_vec_get(&vec, 0) == 0 && ok_vec_get(&vec, 1) == 1 && ok_vec_get(&vec, 2) == 2,
"ok_vec_sort");
// ok_vec_clear, ok_vec_push_new, and capacity (reallocs)
ok_vec_clear(&vec);
ok_assert(ok_vec_count(&vec) == 0, "ok_vec_clear");
const int count = 1000;
for (i = 0; i < count; i++) {
int *v = ok_vec_push_new(&vec);
if (v) {
*v = i;
}
}
ok_assert(ok_vec_count(&vec) == (size_t)count, "ok_vec_push_new");
// ok_vec_begin, ok_vec_end
i = 0;
sum = 0;
int sum2 = 0;
for (int *v = ok_vec_begin(&vec); v != ok_vec_end(&vec); v++) {
sum2 += *v;
sum += i;
i++;
}
ok_assert(sum == sum2, "ok_vec_push_new / ok_vec_begin / ok_vec_end");
// ok_vec_last
ok_assert(count - 1 == *ok_vec_last(&vec), "ok_vec_last");
////////////////////////////////////////////////////////////////////////////////////////////////
// ok_vec_push_all
ok_vec_clear(&vec);
vec_int_t vec2;
ok_vec_init(&vec2);
ok_vec_push(&vec2, 10);
ok_vec_push(&vec2, 20);
ok_vec_push(&vec2, 30);
ok_vec_push_all(&vec, &vec2);
ok_vec_deinit(&vec2);
ok_assert(ok_vec_get(&vec, 0) == 10 && ok_vec_get(&vec, 1) == 20 && ok_vec_get(&vec, 2) == 30,
"ok_vec_push_all");
// Done
ok_vec_deinit(&vec);
////////////////////////////////////////////////////////////////////////////////////////////////
struct vec_of_strs ok_vec_of(const char *);
struct vec_of_strs str_vec = OK_VEC_INIT;
ok_vec_push(&str_vec, "banana");
ok_vec_push(&str_vec, "carrot");
ok_vec_push(&str_vec, "apple");
ok_vec_sort(&str_vec, vec_str_cmp);
ok_assert(strcmp(ok_vec_get(&str_vec, 0), "apple") == 0 &&
strcmp(ok_vec_get(&str_vec, 1), "banana") == 0 &&
strcmp(ok_vec_get(&str_vec, 2), "carrot") == 0,
"ok_vec_sort for strings");
ok_vec_deinit(&str_vec);
////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct ok_vec_of(point_t) vec_point_t;
vec_point_t point_vec = OK_VEC_INIT;
point_t p0 = {0, 1};
ok_vec_push(&point_vec, p0);
point_t p1 = {2, 3};
ok_vec_push(&point_vec, p1);
point_t p2 = {4, 5};
ok_vec_push(&point_vec, p2);
ok_assert(ok_vec_get(&point_vec, 0).x == p0.x && ok_vec_get(&point_vec, 0).y == p0.y &&
ok_vec_get(&point_vec, 1).x == p1.x && ok_vec_get(&point_vec, 1).y == p1.y &&
ok_vec_get(&point_vec, 2).x == p2.x && ok_vec_get(&point_vec, 2).y == p2.y,
"ok_vec_push for custom type");
ok_vec_deinit(&point_vec);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Test map
static void test_map(void) {
// str-to-str map
struct str_map_s ok_map_of(const char *, const char *);
struct str_map_s str_map;
bool success = ok_map_init(&str_map);
ok_assert(success, "ok_map_init");
// Put / get
ok_map_put(&str_map, "dave", "bananas");
ok_map_put(&str_map, "mary", "carrots");
ok_map_put(&str_map, "lucy", "apples");
ok_assert(ok_map_count(&str_map) == 3, "ok_map_put / ok_map_count");
ok_assert(strcmp(ok_map_get(&str_map, "dave"), "bananas") == 0 &&
strcmp(ok_map_get(&str_map, "mary"), "carrots") == 0 &&
strcmp(ok_map_get(&str_map, "lucy"), "apples") == 0,
"ok_map_put / ok_map_get");
// Put ptr / get ptr
*ok_map_put_and_get_ptr(&str_map, "sue") = "beans";
*ok_map_put_and_get_ptr(&str_map, "john") = "fries";
ok_assert(ok_map_count(&str_map) == 5, "ok_map_put_and_get_ptr / ok_map_count");
ok_assert(strcmp(*ok_map_get_ptr(&str_map, "sue"), "beans") == 0 &&
strcmp(*ok_map_get_ptr(&str_map, "john"), "fries") == 0,
"ok_map_put_and_get_ptr / ok_map_get");
// Overwrite
ok_map_put(&str_map, "john", "salad");
ok_assert(ok_map_count(&str_map) == 5, "ok_map overwrite value / ok_map_count");
ok_assert(strcmp(ok_map_get(&str_map, "john"), "salad") == 0,
"ok_map overwrite value / ok_map_get");
// Contains
ok_map_put(&str_map, "cyrus", "(who knows)");
ok_assert(ok_map_contains(&str_map, "cyrus"), "ok_map_contains");
// Remove
ok_map_remove(&str_map, "cyrus");
ok_assert(ok_map_count(&str_map) == 5, "ok_map_remove");
// Contains (false)
ok_assert(ok_map_contains(&str_map, "cyrus") == false, "Contains (false)");
////////////////////////////////////////////////////////////////////////////////////////////////
// ok_map_foreach
char keys[1024] = {0};
char values[1024] = {0};
ok_map_foreach(&str_map, const char *key, const char *value) {
strcat(keys, key);
strcat(values, value);
}
ok_assert(strstr(keys, "dave") != NULL &&
strstr(keys, "mary") != NULL &&
strstr(keys, "lucy") != NULL &&
strstr(keys, "sue") != NULL &&
strstr(keys, "john") != NULL,
"ok_map_foreach keys");
ok_assert(strstr(values, "bananas") != NULL &&
strstr(values, "carrots") != NULL &&
strstr(values, "apples") != NULL &&
strstr(values, "beans") != NULL &&
strstr(values, "salad") != NULL,
"ok_map_foreach values");
// ok_map_foreach: No curly braces
keys[0] = 0;
const char *key;
const char *value;
ok_map_foreach(&str_map, key, value)
strcat(keys, key);
ok_assert(strstr(keys, "dave") != NULL &&
strstr(keys, "mary") != NULL &&
strstr(keys, "lucy") != NULL &&
strstr(keys, "sue") != NULL &&
strstr(keys, "john") != NULL,
"ok_map_foreach keys / no curly braces");
// ok_map_foreach: Break
ok_map_foreach(&str_map, key, value) {
if (strcmp(key, "dave")) {
break;
}
}
ok_assert(strcmp(ok_map_get(&str_map, "dave"), "bananas") == 0,
"ok_map_foreach / break");
// ok_map_foreach: Inner loop
size_t count = 0;
ok_map_foreach(&str_map, key, value) {
ok_map_foreach(&str_map, const char *key2, const char *value2) {
(void)value2;
if (strcmp(key, key2) == 0) {
count++;
}
}
}
ok_assert(count == ok_map_count(&str_map), "ok_map_foreach inner loop");
////////////////////////////////////////////////////////////////////////////////////////////////
// Copy
struct str_map_s str_map2;
ok_map_init(&str_map2);
ok_map_put_all(&str_map2, &str_map);
count = 0;
ok_map_foreach(&str_map2, const char *key, const char *value) {
if (strcmp(ok_map_get(&str_map, key), value) == 0) {
count++;
}
}
ok_assert(count == ok_map_count(&str_map) && count == ok_map_count(&str_map2), "ok_map_put_all");
ok_map_deinit(&str_map2);
ok_map_deinit(&str_map);
////////////////////////////////////////////////////////////////////////////////////////////////
// str-to-int map
struct str_int_map_s ok_map_of(const char *, int);
struct str_int_map_s str_int_map;
ok_map_init(&str_int_map);
ok_map_put(&str_int_map, "The answer", 42);
ok_assert(ok_map_get(&str_int_map, "The answer") == 42, "int values");
ok_map_deinit(&str_int_map);
////////////////////////////////////////////////////////////////////////////////////////////////
// int-to-str map
struct int_map_s ok_map_of(int, char *);
struct int_map_s int_map;
// On C11, you can use `ok_map_init(&int_map)` instead
ok_map_init_custom(&int_map, ok_int32_hash, ok_32bit_equals);
for (int i = 0; i <= 9999; i++) {
char *value = (char *)malloc(5);
snprintf(value, 5, "%d", i);
ok_map_put(&int_map, i, value);
}
count = 0;
ok_map_foreach(&int_map, int key, char *value) {
if (strtol(value, NULL, 10) == key) {
count++;
}
}
ok_assert(count == ok_map_count(&int_map), "int keys / realloc");
// Free
ok_map_foreach(&int_map, int key, char *value) {
(void)key;
free(value);
}
ok_map_deinit(&int_map);
////////////////////////////////////////////////////////////////////////////////////////////////
// Structs as values
struct str_point_map_s ok_map_of(const char *, point_t);
struct str_point_map_s str_point_map;
ok_map_init(&str_point_map);
point_t p;
p.x = 100.0f;
p.y = 200.0f;
ok_map_put(&str_point_map, "player1", p);
point_t p2 = ok_map_get(&str_point_map, "player1");
ok_assert(point_equals(&p, &p2), "struct values");
ok_map_deinit(&str_point_map);
// Structs as keys
struct point_map_s ok_map_of(point_t, int);
struct point_map_s point_map;
ok_map_init_custom(&point_map, point_hash, point_equals);
for (int x = 0; x <= 9; x++) {
for (int y = 0; y <= 9; y++) {
point_t p;
p.x = (float)x;
p.y = (float)y;
ok_map_put(&point_map, p, x + 10 * y);
}
}
count = 0;
ok_map_foreach(&point_map, point_t key, int value) {
if (key.x + 10 * key.y == value) {
count++;
}
}
ok_assert(count == ok_map_count(&point_map), "struct keys / realloc");
ok_map_deinit(&point_map);
////////////////////////////////////////////////////////////////////////////////////////////////
// Pointers as keys
const void *key1 = "hello";
const void *key2 = NULL;
struct void_map_s ok_map_of(const void *, int);
struct void_map_s void_map;
ok_map_init_custom(&void_map, ok_const_ptr_hash, ok_ptr_equals);
ok_map_put(&void_map, key1, 1);
ok_map_put(&void_map, key2, 2);
ok_assert(ok_map_get(&void_map, key1) == 1, "pointer keys");
ok_assert(ok_map_get(&void_map, key2) == 2, "pointer keys");
ok_map_deinit(&void_map);
////////////////////////////////////////////////////////////////////////////////////////////////
// Bad hash function
struct int_map_s bad_map;
ok_map_init_custom(&bad_map, bad_hash, ok_32bit_equals);
for (int i = 0; i <= 9999; i++) {
char *value = (char *)malloc(5);
snprintf(value, 5, "%d", i);
ok_map_put(&bad_map, i, value);
}
count = 0;
ok_map_foreach(&bad_map, int key, char *value) {
if (strtol(value, NULL, 10) == key) {
count++;
}
}
ok_assert(count == ok_map_count(&bad_map), "bad hash function");
// Free
ok_map_foreach(&bad_map, int key, char *value) {
(void)key;
free(value);
}
ok_map_deinit(&bad_map);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Test queue
#if !defined(__EMSCRIPTEN__)
#if !defined(_WIN32)
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#define THREAD_RETURN_VALUE void *
static int64_t ok_time_us(void) {
struct timeval t;
gettimeofday(&t, NULL);
return (int64_t)t.tv_sec * 1000000 + (int64_t)t.tv_usec;
}
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef HANDLE pthread_t;
#define THREAD_RETURN_VALUE DWORD WINAPI
static int64_t ok_time_us(void) {
FILETIME filetime;
ULARGE_INTEGER large_int;
GetSystemTimeAsFileTime(&filetime);
large_int.LowPart = filetime.dwLowDateTime;
large_int.HighPart = filetime.dwHighDateTime;
return large_int.QuadPart / 10; // Convert from 100-nanosecond intervals to 1 us intervals
}
static int pthread_create(HANDLE *thread, const void *attr,
LPTHREAD_START_ROUTINE start_routine, void *arg) {
(void)attr;
*thread = CreateThread(NULL, 0, start_routine, arg, 0, NULL);
return (*thread == NULL);
}
static int pthread_join(HANDLE thread, void **value_ptr) {
(void)value_ptr;
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
return 0;
}
#endif // _WIN32
#define PRODUCER_THREAD_COUNT 8
#define CONSUMER_THREAD_COUNT 8
#define VALUE_COUNT 100000
typedef long datatype;
typedef struct ok_queue_of(datatype) queue_t;
static int datatype_cmp(const void *v1, const void *v2) {
const datatype a = *(const datatype *)v1;
const datatype b = *(const datatype *)v2;
return (a < b) ? -1 : (a > b);
}
typedef struct {
pthread_t thread;
queue_t *queue1;
queue_t *queue2;
datatype value;
} thread_context;
static _Atomic(bool) production_complete = false;
static THREAD_RETURN_VALUE producer_thread_entry(void *context) {
thread_context *c = context;
datatype value = c->value;
for (int i = 0; i < VALUE_COUNT; i++) {
ok_queue_push(c->queue1, value);
value++;
}
return 0;
}
static THREAD_RETURN_VALUE consumer_thread_entry(void *context) {
thread_context *c = context;
while (1) {
datatype value;
if (ok_queue_pop(c->queue1, &value)) {
ok_queue_push(c->queue2, value);
} else if (atomic_load(&production_complete)) {
break;
}
}
return 0;
}
static void test_queue_multithreaded(void) {
thread_context producer_thread_contexts[PRODUCER_THREAD_COUNT];
thread_context consumer_thread_contexts[CONSUMER_THREAD_COUNT];
size_t count = PRODUCER_THREAD_COUNT * VALUE_COUNT;
datatype *out_values = calloc(count, sizeof(datatype));
if (!out_values) {
printf("Error: Not enough memory");
return;
}
int64_t start_time = ok_time_us();
queue_t queue1 = OK_QUEUE_INIT;
queue_t queue2;
ok_queue_init(&queue2);
// Start producer threads
for (int i = 0; i < PRODUCER_THREAD_COUNT; i++) {
producer_thread_contexts[i].queue1 = &queue1;
producer_thread_contexts[i].value = 1 + i * VALUE_COUNT;
pthread_create(&producer_thread_contexts[i].thread, NULL, producer_thread_entry, &producer_thread_contexts[i]);
}
// Start consumer threads
for (int i = 0; i < CONSUMER_THREAD_COUNT; i++) {
consumer_thread_contexts[i].queue1 = &queue1;
consumer_thread_contexts[i].queue2 = &queue2;
consumer_thread_contexts[i].value = i;
pthread_create(&consumer_thread_contexts[i].thread, NULL, consumer_thread_entry, &consumer_thread_contexts[i]);
}
// Wait for producers to finish
for (int i = 0; i < PRODUCER_THREAD_COUNT; i++) {
pthread_join(producer_thread_contexts[i].thread, NULL);
}
atomic_store(&production_complete, true);
// Wait for consumers to finish
for (int i = 0; i < CONSUMER_THREAD_COUNT; i++) {
pthread_join(consumer_thread_contexts[i].thread, NULL);
}
// Check if queue1 is empty
datatype value;
bool is_empty = !ok_queue_pop(&queue1, &value);
ok_assert(is_empty, "queue1 not empty");
// Dump contents of queue2 to out_values
bool prematurely_empty = false;
for (size_t i = 0; i < count; i++) {
datatype value;
if (!ok_queue_pop(&queue2, &value)) {
prematurely_empty = true;
break;
}
out_values[i] = value;
}
ok_assert(!prematurely_empty, "queue2 not full");
is_empty = !ok_queue_pop(&queue2, &value);
ok_assert(is_empty, "queue2 not empty");
ok_queue_deinit(&queue1);
ok_queue_deinit(&queue2);
int64_t end_time = ok_time_us();
printf("Multithreaded queue test duration: %ims\n", (int)((end_time - start_time) / 1000));
// Make sure out_values has the values 1..(PRODUCER_THREAD_COUNT * VALUE_COUNT)
bool values_valid = true;
qsort((void *)out_values, count, sizeof(*out_values), datatype_cmp);
for (size_t i = 0; i < count; i++) {
if (out_values[i] != (datatype)(i + 1)) {
values_valid = false;
break;
}
}
ok_assert(values_valid, "queue2 has incorrect values");
free(out_values);
}
#else
static void test_queue_multithreaded(void) {
// Emscripten: Do nothing
}
#endif // __EMSCRIPTEN__
static void str_deallocator(void *value_ptr) {
char *str = *(char **)value_ptr;
//printf("Deallocating: %s\n", str);
free(str);
}
static void test_queue(void) {
typedef struct ok_queue_of(int) queue_int_t;
int count = 100;
bool success = false;
queue_int_t queue;
ok_queue_init(&queue);
int int_value = 0;
for (int i = 0; i < count; i++) {
ok_queue_push(&queue, i);
}
for (int i = 0; i < count; i++) {
success = ok_queue_pop(&queue, &int_value);
if (!success || int_value != i) {
success = false;
break;
}
}
ok_assert(success, "int queue error");
ok_assert(ok_queue_pop(&queue, &int_value) == false, "int queue not empty");
success = false;
for (int i = 0; i < count; i++) {
ok_queue_push(&queue, i);
success = ok_queue_pop(&queue, &int_value);
if (!success || int_value != i) {
success = false;
break;
}
}
ok_assert(success, "int queue error (size 1)");
ok_assert(ok_queue_pop(&queue, &int_value) == false, "int queue (size 1) not empty");
ok_queue_deinit(&queue);
struct ok_queue_of(char *) str_queue = OK_QUEUE_INIT;
char *name = strdup("dave");
ok_queue_push(&str_queue, name);
name = strdup("mary");
ok_queue_push(&str_queue, name);
name = strdup("lucy");
ok_queue_push(&str_queue, name);
ok_queue_deinit_with_deallocator(&str_queue, str_deallocator);
ok_assert(true, "queue deallocate test");
typedef struct ok_queue_of(point_t) queue_point_t;
queue_point_t point_queue;
point_t point_array[3] = {{1, 2}, {3, 4}, {5, 6}};
size_t num_points = sizeof(point_array) / sizeof(*point_array);
success = false;
ok_queue_init_with_capacity(&point_queue, 4);
for (size_t i = 0; i < num_points; i++) {
ok_queue_push(&point_queue, point_array[i]);
}
for (size_t i = 0; i < num_points; i++) {
point_t point = {0, 0};
success = ok_queue_pop(&point_queue, &point);
if (!success || !point_equals(&point, &point_array[i])) {
success = false;
break;
}
}
ok_assert(success, "point queue error");
ok_queue_deinit(&point_queue);
test_queue_multithreaded();
}
int main(void) {
//ok_static_assert(2 + 2 == 5, "2+2 is not 5");
ok_static_assert(true, "Identifier `true` must be true");
ok_tests_start();
test_vec();
test_map();
test_queue();
ok_tests_finish();
return ok_test_failure_count;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif