-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.c
3653 lines (3006 loc) · 122 KB
/
tests.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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <limits.h>
#include "tests_rjd_wrapped.h"
#if RJD_PLATFORM_WINDOWS
#if RJD_COMPILER_MSVC
#pragma warning(push)
#pragma warning(disable:5105) // windows.h triggers warning C5105: macro expansion producing 'defined' has undefined behavior
#endif
#include <windows.h> // GetModuleHandle
#if RJD_COMPILER_MSVC
#pragma warning(pop)
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// expect utils
#define expect_true(condition) if (!(condition)) { RJD_ASSERTFAIL("Expected \"%s\" to be true, but got false\n", #condition); }
#define expect_false(condition) if (condition) { RJD_ASSERTFAIL("Expected \"%s\" to be false, but got true\n", #condition); }
// #define expect_str(expected, actual)
// if (expected != actual && (expected == NULL || actual == NULL || strcmp(expected, actual))) {
// RJD_ASSERTFAIL("Expected:\n%s\nbut got:\n%s\n", expected ? expected : "", actual ? actual : ""); }
void expect_str(const char* expected, const char* actual)
{
if (expected != actual && (expected == NULL || actual == NULL || strcmp(expected, actual))) {
RJD_ASSERTFAIL("Expected:\n%s\nbut got:\n%s\n", expected ? expected : "", actual ? actual : "");
}
}
void expect_float(double expected, double actual)
{
if (fabs(expected - actual) > 0.00001) {
RJD_ASSERTFAIL("Expected: %f, but got: %f", expected, actual);
}
}
void expect_bool(bool expected, bool actual)
{
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d", expected, actual);
}
}
void expect_int64(int64_t expected, int64_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %lld, but got: %lld\n", expected, actual);
}
}
void expect_int32(int32_t expected, int32_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d\n", expected, actual);
}
}
void expect_int16(int16_t expected, int16_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d\n", expected, actual);
}
}
void expect_int8(int8_t expected, int8_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d\n", expected, actual);
}
}
void expect_uint64(uint64_t expected, uint64_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %lld, but got: %lld\n", expected, actual);
}
}
void expect_uint32(uint32_t expected, uint32_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d\n", expected, actual);
}
}
void expect_uint16(uint16_t expected, uint16_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d\n", expected, actual);
}
}
void expect_uint8(uint8_t expected, uint8_t actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %d, but got: %d\n", expected, actual);
}
}
void expect_pointer(const void* expected, const void* actual) {
if (expected != actual) {
RJD_ASSERTFAIL("Expected: %p, but got: %p\n", expected, actual);
}
}
void expect_path(const char* expected, struct rjd_path path)
{
expect_str(expected, rjd_path_get(&path));
}
#define expect_result_ok(actual) RJD_ASSERTMSG(actual.error == NULL, "Expected OK result, but got error '%s'", actual.error)
#define expect_result_notok(actual) RJD_ASSERTMSG(actual.error != NULL, "Expected bad result, but got OK")
// void expect_result_ok(struct rjd_result actual)
// {
// RJD_ASSERTMSG(actual.error == NULL, "Expected OK result, but got error '%s'", actual.error);
// }
// void expect_result_notok(struct rjd_result actual)
// {
// RJD_ASSERTMSG(actual.error != NULL, "Expected bad result, but got OK");
// }
void expect_no_leaks(const struct rjd_mem_allocator* allocator)
{
struct rjd_mem_allocator_stats stats = rjd_mem_allocator_getstats(allocator);
RJD_ASSERTMSG(rjd_atomic_uint64_get(&stats.current.used) == 0, "Found some leaks");
}
////////////////////////////////////////////////////////////////////////////////
static int RJD_COMPILER_MSVC_ONLY(__cdecl) compare_int32(const void* a, const void* b)
{
int32_t aa = *(int32_t*)a;
int32_t bb = *(int32_t*)b;
if (aa == bb) {
return 0;
} else if (aa < bb) {
return -1;
} else {
return 1;
}
}
static int RJD_COMPILER_MSVC_ONLY(__cdecl) compare_int32_c(void* context, const void* a, const void* b)
{
RJD_ASSERT(context);
return compare_int32(a, b);
}
////////////////////////////////////////////////////////////////////////////////
RJD_STATIC_ASSERT(true);
RJD_STATIC_ASSERT(1 == 1);
RJD_STATIC_ASSERT(sizeof(uint32_t) == sizeof(char) * 4);
////////////////////////////////////////////////////////////////////////////////
char g_logbuffer[1024 * 128];
size_t g_logbuffer_pos = 0;
void test_log_hook(const char* msg, size_t length)
{
strncpy(g_logbuffer + g_logbuffer_pos, msg, sizeof(g_logbuffer) - g_logbuffer_pos);
g_logbuffer_pos += length;
}
void test_logging_redirect_to_logbuffer(void)
{
static struct rjd_logchannel local = {
.name = "Test Channel",
.enabled = true,
.hook = test_log_hook,
.verbosity = RJD_LOG_VERBOSITY_MED,
};
g_rjd_global_logchannel = &local;
}
void test_logging_reset(void)
{
g_logbuffer_pos = 0;
g_logbuffer[0] = 0;
rjd_log_resetglobal();
}
////////////////////////////////////////////////////////////////////////////////
// rjd_debug
void test_logging()
{
struct rjd_logchannel local = {
.name = "Test Channel",
.enabled = true,
.hook = test_log_hook,
.verbosity = RJD_LOG_VERBOSITY_MED,
};
g_rjd_global_logchannel = &local;
// normal tests
const int line_test_begin = __LINE__;
RJD_LOG("test");
RJD_LOG("");
RJD_LOG("%s%d%d%s", "forma", 1, 1, "ed!");
// disabled
local.enabled = false;
RJD_LOG("shouldn't get printed");
local.enabled = true;
// verbosity
local.verbosity = RJD_LOG_VERBOSITY_LOW;
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_LOW, "ok1");
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_MED, "nope");
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_HIGH, "nope");
local.verbosity = RJD_LOG_VERBOSITY_MED;
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_LOW, "ok2");
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_MED, "ok2");
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_HIGH, "nope");
local.verbosity = RJD_LOG_VERBOSITY_HIGH;
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_LOW, "ok3");
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_MED, "ok3");
RJD_LOG_CHANNEL(&local, RJD_LOG_VERBOSITY_HIGH, "ok3");
local.verbosity = RJD_LOG_VERBOSITY_MED;
// other channel
struct rjd_logchannel local2 = {
.name = "Test2",
.enabled = true,
.hook = test_log_hook,
.verbosity = RJD_LOG_VERBOSITY_MED,
};
RJD_LOG_CHANNEL(&local2, RJD_LOG_VERBOSITY_MED, "other channel");
// on windows this is the filename, but on osx this is the fully-qualified path
const char* filename = __FILE__;
// expect equals
char expected[1024];
snprintf(expected, sizeof(expected),
"%s(%d): test\n"
"%s(%d): \n"
"%s(%d): forma11ed!\n"
"%s(%d): ok1\n"
"%s(%d): ok2\n"
"%s(%d): ok2\n",
filename, line_test_begin + 1,
filename, line_test_begin + 2,
filename, line_test_begin + 3,
filename, line_test_begin + 12,
filename, line_test_begin + 17,
filename, line_test_begin + 18);
snprintf(expected + strlen(expected), sizeof(expected) - strlen(expected),
"%s(%d): ok3\n"
"%s(%d): ok3\n"
"%s(%d): ok3\n"
"%s(%d): other channel\n",
filename, line_test_begin + 22,
filename, line_test_begin + 23,
filename, line_test_begin + 24,
filename, line_test_begin + 36);
expect_str(expected, g_logbuffer);
test_logging_reset();
}
////////////////////////////////////////////////////////////////////////////////
// rjd_result
struct rjd_result check_result(bool condition) {
RJD_RESULT_CHECK(condition, "not ok");
return RJD_RESULT_OK();
}
struct rjd_result promote_result(struct rjd_result result) {
RJD_RESULT_PROMOTE(result);
return RJD_RESULT_OK();
}
void test_result()
{
struct rjd_result r1 = RJD_RESULT("not ok");
struct rjd_result r2 = RJD_RESULT_OK();
expect_false(rjd_result_isok(r1));
expect_true(rjd_result_isok(r2));
expect_true(rjd_result_isok(check_result(true)));
expect_false(rjd_result_isok(check_result(false)));
expect_true(promote_result(r1).error == r1.error);
expect_true(rjd_result_isok(promote_result(r2)));
}
////////////////////////////////////////////////////////////////////////////////
// rjd_enum
#define TEST_ENUM1_LIST(macro) \
macro(e1_a) \
macro(e1_b) \
macro(e1_c) \
macro(e1_d)
#define TEST_ENUM2_LIST(macro) \
macro(e2_ok, "OK") \
macro(e2_notok, "NOTOK")
RJD_ENUM_DECLARE(e1, TEST_ENUM1_LIST);
RJD_ENUM_DEFINE(e1, TEST_ENUM1_LIST);
RJD_ENUM_DECLARE_WITH_STRINGS(e2, TEST_ENUM2_LIST);
RJD_ENUM_DEFINE_WITH_STRINGS(e2, TEST_ENUM2_LIST);
void test_enum()
{
// enum 1
expect_str("e1_a", s_e1_strings[0]);
expect_str("e1_b", s_e1_strings[1]);
expect_str("e1_c", s_e1_strings[2]);
expect_str("e1_d", s_e1_strings[3]);
expect_uint32(4, k_e1_count);
expect_str("e1_a", e1_tostring(e1_a));
expect_str("e1_b", e1_tostring(e1_b));
expect_str("e1_c", e1_tostring(e1_c));
expect_str("e1_d", e1_tostring(e1_d));
enum e1 e1_value;
expect_true(e1_parse("e1_a", &e1_value) && e1_value == e1_a);
expect_true(e1_parse("e1_b", &e1_value) && e1_value == e1_b);
expect_true(e1_parse("e1_c", &e1_value) && e1_value == e1_c);
expect_true(e1_parse("e1_d", &e1_value) && e1_value == e1_d);
expect_false(e1_parse("e1_e", &e1_value));
// enum 2
expect_str("OK", s_e2_strings[0]);
expect_str("NOTOK", s_e2_strings[1]);
char test[k_e2_count];
expect_uint32(2, rjd_countof(test));
expect_str("OK", e2_tostring(e2_ok));
expect_str("NOTOK", e2_tostring(e2_notok));
enum e2 e2_value;
expect_true(e2_parse("OK", &e2_value) && e2_value == e2_ok);
expect_true(e2_parse("NOTOK", &e2_value) && e2_value == e2_notok);
}
////////////////////////////////////////////////////////////////////////////////
// rjd_hash
void test_hash()
{
const char* str1 = "test1";
const char* str2 = "a longer string that has a bunch of characters in it!!$%^&*(";
const uint8_t* data1 = (const uint8_t*)str1;
const uint8_t* data2 = (const uint8_t*)str2;
const uint8_t* data3 = NULL;
expect_uint32(rjd_hash32_data(data1, (uint32_t)strlen((const char*)data1)).value, rjd_hash32_data(data1, -1).value);
expect_uint32(rjd_hash32_data(data2, (uint32_t)strlen((const char*)data2)).value, rjd_hash32_data(data2, -1).value);
expect_uint32(rjd_hash32_data(data3, 0).value, rjd_hash32_data(data3, -1).value);
expect_uint32(rjd_hash32_str(str1).value, rjd_hash32_data(data1, -1).value);
expect_uint32(rjd_hash32_str(str2).value, rjd_hash32_data(data2, -1).value);
expect_uint64(rjd_hash64_data(data1, (uint32_t)strlen((const char*)data1)).value, rjd_hash64_data(data1, -1).value);
expect_uint64(rjd_hash64_data(data2, (uint32_t)strlen((const char*)data2)).value, rjd_hash64_data(data2, -1).value);
expect_uint64(rjd_hash64_data(data3, 0).value, rjd_hash64_data(data3, -1).value);
expect_uint64(rjd_hash64_str(str1).value, rjd_hash64_data(data1, -1).value);
expect_uint64(rjd_hash64_str(str2).value, rjd_hash64_data(data2, -1).value);
}
////////////////////////////////////////////////////////////////////////////////
// rjd_mem
void test_mem()
{
//macros
{
#if RJD_COMPILER_MSVC
#pragma warning(push)
#pragma warning(disable:4127) // conditional expression is constant (yes we know, that's the point of this test)
#endif
expect_true(RJD_MEM_ISALIGNED(0, 4));
expect_true(RJD_MEM_ISALIGNED(4, 4));
expect_true(RJD_MEM_ISALIGNED(8, 4));
expect_true(RJD_MEM_ISALIGNED(8, 8));
expect_true(RJD_MEM_ISALIGNED(16, 8));
expect_true(RJD_MEM_ISALIGNED(16, 16));
expect_true(RJD_MEM_ISALIGNED(32, 16));
expect_true(RJD_MEM_ISALIGNED(32, 32));
expect_false(RJD_MEM_ISALIGNED(1, 4));
expect_false(RJD_MEM_ISALIGNED(2, 4));
expect_false(RJD_MEM_ISALIGNED(3, 4));
expect_int32(0, RJD_MEM_ALIGN(0, 4));
expect_int32(4, RJD_MEM_ALIGN(1, 4));
expect_int32(4, RJD_MEM_ALIGN(2, 4));
expect_int32(4, RJD_MEM_ALIGN(3, 4));
expect_int32(4, RJD_MEM_ALIGN(4, 4));
expect_int32(8, RJD_MEM_ALIGN(5, 4));
expect_int32(8, RJD_MEM_ALIGN(6, 4));
expect_int32(8, RJD_MEM_ALIGN(7, 4));
expect_int32(8, RJD_MEM_ALIGN(8, 4));
expect_int32(12, RJD_MEM_ALIGN(9, 4));
expect_int32(12, RJD_MEM_ALIGN(10, 4));
expect_int32(12, RJD_MEM_ALIGN(11, 4));
expect_int32(12, RJD_MEM_ALIGN(12, 4));
expect_int32(0, RJD_MEM_ALIGN(0, 8));
expect_int32(8, RJD_MEM_ALIGN(2, 8));
expect_int32(8, RJD_MEM_ALIGN(4, 8));
expect_int32(8, RJD_MEM_ALIGN(6, 8));
expect_int32(8, RJD_MEM_ALIGN(8, 8));
expect_int32(16, RJD_MEM_ALIGN(10, 8));
expect_int32(16, RJD_MEM_ALIGN(12, 8));
expect_int32(16, RJD_MEM_ALIGN(14, 8));
expect_int32(16, RJD_MEM_ALIGN(16, 8));
expect_int32(24, RJD_MEM_ALIGN(18, 8));
#if RJD_COMPILER_MSVC
#pragma warning(pop)
#endif
}
// default allocator
{
struct rjd_mem_allocator allocator = rjd_mem_allocator_init_default();
int32_t* p0 = rjd_mem_alloc(int32_t, &allocator);
expect_true(p0 != NULL);
*p0 = 1337;
char* p1 = rjd_mem_alloc_array(char, 128, &allocator);
expect_true(p1 != NULL);
strncpy(p1, "thequickbrownfoxjumpedoverthesuperdeduperlazydog!", 128);
p1[127] = 0;
char* p2 = rjd_mem_alloc_array(char, 64, &allocator);
expect_true(p2 != NULL);
strncpy(p2, "this fox wasn't as quick as the last one", 64);
p2[63] = 0;
char* p3 = rjd_mem_alloc_aligned(char, &allocator, 64);
expect_true(p3 != NULL);
expect_uint64(RJD_MEM_ALIGN((uint64_t)p3, 64), (uint64_t)p3);
struct aligned_struct {
double allocator;
double b;
double c;
double e;
};
char* p4 = rjd_mem_alloc_array_aligned(char, 8, &allocator, 32);
expect_true(p4 != NULL);
expect_uint64(RJD_MEM_ALIGN((uint64_t)p4, 32), (uint64_t)p4);
rjd_mem_free(p0);
rjd_mem_free(p1);
rjd_mem_free(p2);
rjd_mem_free(p3);
rjd_mem_free(p4);
expect_no_leaks(&allocator);
expect_false(rjd_mem_allocator_reset(&allocator));
}
// linear allocator
{
char stackmem[1024];
struct rjd_mem_allocator allocator = rjd_mem_allocator_init_linear(stackmem, sizeof(stackmem));
expect_true(rjd_mem_allocator_type(&allocator) != NULL);
expect_true(rjd_mem_alloc_array(char, 256, &allocator) != NULL);
expect_true(rjd_mem_alloc_array(char, 256, &allocator) != NULL);
expect_true(rjd_mem_alloc_array(char, 256, &allocator) != NULL);
expect_true(rjd_atomic_uint64_get(&allocator.stats.total_size) <= sizeof(stackmem));
{
const uint64_t total = rjd_atomic_uint64_get(&allocator.stats.total_size);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.used) - rjd_atomic_uint64_get(&allocator.stats.current.overhead), 256 * 3);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.peak), rjd_atomic_uint64_get(&allocator.stats.current.used));
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.unused), total - ((256 * 3) + rjd_atomic_uint64_get(&allocator.stats.current.overhead)));
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.allocs), 3);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.frees), 0);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.peak), rjd_atomic_uint64_get(&allocator.stats.current.peak));
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.allocs), 3);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.frees), 0);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.resets), 0);
}
const uint64_t old_peak = rjd_atomic_uint64_get(&allocator.stats.lifetime.peak);
expect_true(rjd_mem_allocator_reset(&allocator));
expect_true(rjd_mem_alloc_array(char, 512, &allocator) != NULL);
{
const uint64_t total = rjd_atomic_uint64_get(&allocator.stats.total_size);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.used) - rjd_atomic_uint64_get(&allocator.stats.current.overhead), 512);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.peak), rjd_atomic_uint64_get(&allocator.stats.current.used));
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.unused), total - (512 + rjd_atomic_uint64_get(&allocator.stats.current.overhead)));
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.allocs), 1);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.current.frees), 0);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.peak), old_peak);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.allocs), 4);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.frees), 0);
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.resets), 1);
}
expect_true(rjd_mem_allocator_reset(&allocator));
expect_uint64(rjd_atomic_uint64_get(&allocator.stats.lifetime.resets), 2);
}
// stack allocations
{
const uint32_t SENTINEL = 0xFFFFFFFF;
{
uint32_t before = SENTINEL;
uint32_t* single = rjd_mem_alloc_stack_noclear(uint32_t);
uint32_t after = SENTINEL;
*single = 0;
expect_uint32(SENTINEL, before);
expect_uint32(SENTINEL, after);
}
{
uint32_t before = SENTINEL;
const size_t count = 9;
uint32_t* array = rjd_mem_alloc_stack_array_noclear(uint32_t, 9);
uint32_t after = SENTINEL;
for (size_t i = 0; i < count; ++i) {
array[i] = 0;
}
expect_uint32(SENTINEL, before);
expect_uint32(SENTINEL, after);
}
}
// mem_swap
{
char test1[] = { 'm','y','t','e','s','t','\0' };
char test2[] = { 'o','h','n','o','e','s','\0' };
RJD_STATIC_ASSERT(sizeof(test1) == sizeof(test2));
rjd_mem_swap(test1, test2, sizeof(test1));
expect_str("ohnoes", test1);
expect_str("mytest", test2);
}
}
////////////////////////////////////////////////////////////////////////////////
// rjd_array
void test_array()
{
struct rjd_mem_allocator allocator = rjd_mem_allocator_init_default();
// rjd_countof
{
int a[3];
expect_uint32(3, rjd_countof(a));
unsigned long long b[64];
expect_uint32(64, rjd_countof(b));
struct { uint32_t a[4]; const char* b; unsigned c[20]; } c[20];
expect_uint32(20, rjd_countof(c));
}
// general functionality
{
struct test {
int a;
int b;
int c;
int d;
};
struct test* a = rjd_array_alloc(struct test, RJD_ARRAY_DEFAULT_CAPACITY, &allocator);
expect_uint32(0, rjd_array_count(a));
expect_true(rjd_array_capacity(a) >= 1);
rjd_array_free(a);
a = rjd_array_alloc(struct test, 32, &allocator);
expect_uint32(0, rjd_array_count(a));
expect_uint32(32, rjd_array_capacity(a));
expect_true(rjd_array_empty(a));
expect_false(rjd_array_full(a));
expect_uint32(0, rjd_array_count(NULL));
expect_uint32(0, rjd_array_capacity(NULL));
rjd_array_resize(a, 16);
expect_uint32(16, rjd_array_count(a));
expect_uint32(32, rjd_array_capacity(a));
for (size_t i = 0; i < rjd_array_count(a); ++i) {
expect_int32(0, a[i].a);
expect_int32(0, a[i].b);
expect_int32(0, a[i].c);
expect_int32(0, a[i].d);
}
rjd_array_resize(a, 50);
expect_uint32(50, rjd_array_count(a));
expect_uint32(50, rjd_array_capacity(a));
for (size_t i = 0; i < rjd_array_count(a); ++i) {
expect_int32(0, a[i].a);
expect_int32(0, a[i].b);
expect_int32(0, a[i].c);
expect_int32(0, a[i].d);
}
rjd_array_resize(a, 51);
expect_uint32(51, rjd_array_count(a));
expect_uint32(51, rjd_array_capacity(a));
expect_int32(0, a[50].a);
expect_int32(0, a[50].b);
expect_int32(0, a[50].c);
expect_int32(0, a[50].d);
rjd_array_resize(a, 50);
expect_uint32(50, rjd_array_count(a));
expect_uint32(51, rjd_array_capacity(a));
rjd_array_trim(a);
expect_uint32(50, rjd_array_capacity(a));
for (size_t i = 0; i < rjd_array_count(a); ++i) {
struct test v = { (int)i, 0, 0, 0 };
a[i] = v;
}
expect_false(rjd_array_empty(a));
expect_true(rjd_array_full(a));
for (uint32_t i = 0; i < rjd_array_count(a); ++i) {
expect_uint32(i, rjd_array_get(a, i)->a);
}
rjd_array_erase(a, 0);
expect_int32(1, a[0].a);
rjd_array_erase(a, 1);
expect_int32(3, a[1].a);
expect_false(rjd_array_empty(a));
expect_false(rjd_array_full(a));
struct test end = rjd_array_pop(a);
expect_int32(49, end.a);
rjd_array_resize(a, 0);
expect_true(rjd_array_empty(a));
expect_uint32(0, rjd_array_count(a));
expect_uint32(50, rjd_array_capacity(a));
rjd_array_push(a, end);
expect_int32(end.a, a[0].a);
rjd_array_clear(a);
expect_int32(0, rjd_array_count(a));
expect_int32(50, rjd_array_capacity(a));
rjd_array_free(a);
}
expect_no_leaks(&allocator);
// clone
{
uint32_t* a = rjd_array_alloc(uint32_t, 128, &allocator);
for (uint32_t i = 0; i < 128; ++i) {
rjd_array_push(a, i * 2);
}
uint32_t* b = rjd_array_clone(a, &allocator);
expect_uint32(rjd_array_count(a), rjd_array_count(b));
for (uint32_t i = 0; i < 128; ++i) {
expect_uint32(a[i], b[i]);
}
rjd_array_free(a);
rjd_array_free(b);
}
expect_no_leaks(&allocator);
// growing from push test
{
uint32_t* a = rjd_array_alloc(uint32_t, 3, &allocator);
for (uint32_t i = 0; i < 15; ++i) {
rjd_array_push(a, i * 2);
}
for (uint32_t i = 0; i < rjd_array_count(a); ++i) {
expect_uint32(a[i], i * 2);
}
expect_uint32(24, rjd_array_capacity(a));
rjd_array_free(a);
}
expect_no_leaks(&allocator);
// reserve
{
uint32_t* a = rjd_array_alloc(uint32_t, 3, &allocator);
rjd_array_reserve(a, 20);
expect_uint32(20, rjd_array_capacity(a));
for (uint32_t i = 0; i < 20; ++i) {
rjd_array_push(a, i * 2);
}
expect_uint32(20, rjd_array_capacity(a));
for (uint32_t i = 0; i < rjd_array_count(a); ++i) {
expect_uint32(a[i], i * 2);
}
expect_uint32(20, rjd_array_capacity(a));
rjd_array_free(a);
}
expect_no_leaks(&allocator);
// insert
{
int32_t* a = rjd_array_alloc(int32_t, 20, &allocator);
for (int32_t i = 0; i < 20; ++i) {
rjd_array_push(a, 22);
}
int32_t insert_value = 77;
rjd_array_insert(a, &insert_value, 0);
rjd_array_insert(a, &insert_value, 5);
rjd_array_insert(a, &insert_value, 10);
rjd_array_insert(a, &insert_value, 15);
rjd_array_insert(a, &insert_value, 20);
expect_uint32(25, rjd_array_count(a));
rjd_array_insert(a, &insert_value, 25);// this is the array count + 1
expect_uint32(26, rjd_array_count(a));
for (uint32_t i = 0; i < rjd_array_count(a); ++i) {
if (i % 5 == 0) {
expect_int32(insert_value, a[i]);
} else {
expect_int32(22, a[i]);
}
}
rjd_array_free(a);
}
// first/last
{
int32_t* a = rjd_array_alloc(int32_t, 16, &allocator);
for (int32_t i = 0; i < (int32_t)rjd_array_capacity(a); ++i) {
rjd_array_push(a, 0xD00D + i);
}
const int32_t first = rjd_array_first(a);
expect_int32(0xD00D + 0, first);
const int32_t last = rjd_array_last(a);
expect_int32(0xD00D + 15, last);
rjd_array_free(a);
}
expect_no_leaks(&allocator);
// find and sort tests
{
int32_t* shuffled = rjd_array_alloc(int32_t, 8, &allocator);
rjd_array_push(shuffled, 5);
rjd_array_push(shuffled, 0);
rjd_array_push(shuffled, 3);
rjd_array_push(shuffled, 4);
rjd_array_push(shuffled, 7);
rjd_array_push(shuffled, 6);
rjd_array_push(shuffled, 2);
rjd_array_push(shuffled, 1);
int32_t* sorted = rjd_array_alloc(int32_t, 256, &allocator);
for (uint32_t i = 0; i < rjd_array_count(sorted); ++i)
{
rjd_array_push(sorted, i);
}
int32_t* holes = rjd_array_alloc(int32_t, 8, &allocator);
rjd_array_push(holes, 0);
rjd_array_push(holes, 3);
rjd_array_push(holes, 7);
rjd_array_push(holes, 11);
rjd_array_push(holes, 21);
rjd_array_push(holes, 37);
rjd_array_push(holes, 50);
// find linear
{
int32_t needle = 0;
expect_int32(1, rjd_array_find(shuffled, &needle));
needle = 100;
expect_int32(RJD_ARRAY_NOT_FOUND, rjd_array_find(shuffled, &needle));
needle = 1;
expect_int32(7, rjd_array_find(shuffled, &needle));
}
// lowerbound
{
int32_t needle = -1;
expect_int32(0, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 0;
expect_int32(0, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 1;
expect_int32(1, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 2;
expect_int32(1, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 3;
expect_int32(1, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 4;
expect_int32(2, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 15;
expect_int32(4, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 21;
expect_int32(4, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 50;
expect_int32(6, rjd_array_lowerbound(holes, &needle, compare_int32));
needle = 51;
expect_int32(7, rjd_array_lowerbound(holes, &needle, compare_int32)); // note this is after the last element
}
// find sorted
for (int32_t i = 0; i < (int32_t)rjd_array_count(sorted); ++i)
{
expect_int32(i, rjd_array_find_sorted(sorted, &i, compare_int32));
}
// contains
{
int32_t two = 2;
bool has2 = rjd_array_contains(shuffled, &two);
expect_true(has2);
int32_t twenty = 20;
bool has10 = rjd_array_contains(shuffled, &twenty);
expect_false(has10);
}
// sort
int32_t* shuffled2 = rjd_array_clone(shuffled, &allocator);
rjd_array_sort(shuffled, compare_int32);
for (uint32_t i = 0; i < rjd_array_count(shuffled); ++i) {
expect_int32(i, shuffled[i]);
}
// context-versions
{
int32_t context = 0;
int32_t needle = -1;
expect_int32(0, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 0;
expect_int32(0, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 1;
expect_int32(1, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 2;
expect_int32(1, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 3;
expect_int32(1, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 4;
expect_int32(2, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 15;
expect_int32(4, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 21;
expect_int32(4, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 50;
expect_int32(6, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context));
needle = 51;
expect_int32(7, rjd_array_lowerbound_c(holes, &needle, compare_int32_c, &context)); // note this is after the last element
// find sorted
for (int32_t i = 0; i < (int32_t)rjd_array_count(sorted); ++i)
{
expect_int32(i, rjd_array_find_sorted_c(sorted, &i, compare_int32_c, &context));
}
rjd_array_sort_c(shuffled2, compare_int32_c, &context);
for (uint32_t i = 0; i < rjd_array_count(shuffled2); ++i) {
expect_int32(i, shuffled2[i]);
}
}
rjd_array_free(holes);
rjd_array_free(sorted);
rjd_array_free(shuffled);
rjd_array_free(shuffled2);
}
expect_no_leaks(&allocator);
// functional-style tests
{
int32_t* b = rjd_array_alloc(int32_t, 16, &allocator);
for (int32_t i = 0; i < 16; ++i) {
rjd_array_push(b, i);
}
// filter
#define testfilter(element) (element < 8)
rjd_array_filter(b, testfilter, NULL);
expect_uint32(8, rjd_array_count(b));
#undef testfilter
// reduce / sum
int32_t sum1 = 0;
rjd_array_reduce(b, sum1, rjd_array_sum_predicate);
expect_int32(1 + 2 + 3 + 4 + 5 + 6 + 7, sum1);
int32_t sum2 = 0;
rjd_array_sum(b, sum2);
expect_int32(sum1, sum2);
// map
rjd_array_clear(b);
rjd_array_push(b, 0);
rjd_array_push(b, 1);
rjd_array_push(b, 2);
rjd_array_push(b, 3);
rjd_array_push(b, 4);
#define TEST_PRED(v) (v * 2)
int32_t* mapped = rjd_array_alloc(int32_t, 16, &allocator);
rjd_array_map(b, mapped, TEST_PRED);
#undef TEST_PRED
expect_int32(0, mapped[0]);
expect_int32(2, mapped[1]);
expect_int32(4, mapped[2]);
expect_int32(6, mapped[3]);
expect_int32(8, mapped[4]);
// reverse
rjd_array_reverse(b);
expect_int32(4, b[0]);
expect_int32(3, b[1]);
expect_int32(2, b[2]);
expect_int32(1, b[3]);
expect_int32(0, b[4]);
rjd_array_free(b);
rjd_array_free(mapped);
}
expect_no_leaks(&allocator);
// rng tests
{
struct rjd_rng rng = rjd_rng_init(0x1337C0DE);
int32_t* a = rjd_array_alloc(int32_t, 8, &allocator);
for (int32_t i = 0; i < 8; ++i) {
rjd_array_push(a, i);
}
expect_int32(5, rjd_array_sample(a, &rng));
rjd_array_shuffle(a, &rng);
expect_int32(3, a[0]);
expect_int32(6, a[1]);
expect_int32(0, a[2]);
expect_int32(7, a[3]);
expect_int32(2, a[4]);
expect_int32(4, a[5]);
expect_int32(1, a[6]);
expect_int32(5, a[7]);
rjd_array_free(a);
}
expect_no_leaks(&allocator);
}
////////////////////////////////////////////////////////////////////////////////
// rjd_math
void expect_vec4(rjd_math_vec4 expected, rjd_math_vec4 actual)
{
if (!rjd_math_vec4_eq(expected, actual)) {
RJD_ASSERTFAIL("Expected (%.2f, %.2f, %.2f, %.2f), but got: (%.2f, %.2f, %.2f, %.2f)",