-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutest.h
1512 lines (1357 loc) · 88.5 KB
/
utest.h
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
/*
The latest version of this library is available on GitHub;
https://github.com/sheredom/utest.h
*/
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#ifndef SHEREDOM_UTEST_H_INCLUDED
#define SHEREDOM_UTEST_H_INCLUDED
#ifdef _MSC_VER
/*
Disable warning about not inlining 'inline' functions.
*/
#pragma warning(disable : 4710)
/*
Disable warning about inlining functions that are not marked 'inline'.
*/
#pragma warning(disable : 4711)
/*
Disable warning for alignment padding added
*/
#pragma warning(disable : 4820)
#if _MSC_VER > 1900
/*
Disable warning about preprocessor macros not being defined in MSVC headers.
*/
#pragma warning(disable : 4668)
/*
Disable warning about no function prototype given in MSVC headers.
*/
#pragma warning(disable : 4255)
/*
Disable warning about pointer or reference to potentially throwing function.
*/
#pragma warning(disable : 5039)
#endif
#pragma warning(push, 1)
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1920)
typedef __int64 utest_int64_t;
typedef unsigned __int64 utest_uint64_t;
typedef unsigned __int32 utest_uint32_t;
#else
#include <stdint.h>
typedef int64_t utest_int64_t;
typedef uint64_t utest_uint64_t;
typedef uint32_t utest_uint32_t;
#endif
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__cplusplus)
#include <stdexcept>
#endif
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(__cplusplus)
#define UTEST_C_FUNC extern "C"
#else
#define UTEST_C_FUNC
#endif
#define UTEST_TEST_PASSED (0)
#define UTEST_TEST_FAILURE (1)
#define UTEST_TEST_SKIPPED (2)
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
#if defined(__MINGW64__) || defined(__MINGW32__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
// define UTEST_USE_OLD_QPC before #include "utest.h" to use old
// QueryPerformanceCounter
#ifndef UTEST_USE_OLD_QPC
#pragma warning(push, 0)
#include <Windows.h>
#pragma warning(pop)
typedef LARGE_INTEGER utest_large_integer;
#else
// use old QueryPerformanceCounter definitions (not sure is this needed in some
// edge cases or not) on Win7 with VS2015 these extern declaration cause "second
// C linkage of overloaded function not allowed" error
typedef union {
struct
{
unsigned long LowPart;
long HighPart;
} DUMMYSTRUCTNAME;
struct
{
unsigned long LowPart;
long HighPart;
} u;
utest_int64_t QuadPart;
} utest_large_integer;
UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceCounter(utest_large_integer *);
UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceFrequency(utest_large_integer *);
#if defined(__MINGW64__) || defined(__MINGW32__)
#pragma GCC diagnostic pop
#endif
#endif
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
defined(__DragonFly__) || defined(__sun__) || defined(__HAIKU__)
/*
slightly obscure include here - we need to include glibc's features.h, but
we don't want to just include a header that might not be defined for other
c libraries like musl. Instead we include limits.h, which we know on all
glibc distributions includes features.h
*/
#include <limits.h>
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
#include <time.h>
#if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__)))
/* glibc is version 2.17 or above, so we can just use clock_gettime */
#define UTEST_USE_CLOCKGETTIME
#else
#include <sys/syscall.h>
#include <unistd.h>
#endif
#else // Other libc implementations
#include <time.h>
#define UTEST_USE_CLOCKGETTIME
#endif
#elif defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1920)
#define UTEST_PRId64 "I64d"
#define UTEST_PRIu64 "I64u"
#else
#include <inttypes.h>
#define UTEST_PRId64 PRId64
#define UTEST_PRIu64 PRIu64
#endif
#if defined(__cplusplus)
#define UTEST_INLINE inline
#if defined(__clang__)
#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS \
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"")
#define UTEST_INITIALIZER_END_DISABLE_WARNINGS _Pragma("clang diagnostic pop")
#else
#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS
#define UTEST_INITIALIZER_END_DISABLE_WARNINGS
#endif
#define UTEST_INITIALIZER(f) \
struct f##_cpp_struct \
{ \
f##_cpp_struct(); \
}; \
UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS static f##_cpp_struct f##_cpp_global \
UTEST_INITIALIZER_END_DISABLE_WARNINGS; \
f##_cpp_struct::f##_cpp_struct()
#elif defined(_MSC_VER)
#define UTEST_INLINE __forceinline
#if defined(_WIN64)
#define UTEST_SYMBOL_PREFIX
#else
#define UTEST_SYMBOL_PREFIX "_"
#endif
#if defined(__clang__)
#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS \
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wmissing-variable-declarations\"")
#define UTEST_INITIALIZER_END_DISABLE_WARNINGS _Pragma("clang diagnostic pop")
#else
#define UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS
#define UTEST_INITIALIZER_END_DISABLE_WARNINGS
#endif
#pragma section(".CRT$XCU", read)
#define UTEST_INITIALIZER(f) \
static void __cdecl f(void); \
UTEST_INITIALIZER_BEGIN_DISABLE_WARNINGS \
__pragma(comment(linker, "/include:" UTEST_SYMBOL_PREFIX #f "_")) \
UTEST_C_FUNC __declspec(allocate(".CRT$XCU")) void(__cdecl * f##_)(void) = f; \
UTEST_INITIALIZER_END_DISABLE_WARNINGS \
static void __cdecl f(void)
#else
#if defined(__linux__)
#if defined(__clang__)
#if __has_warning("-Wreserved-id-macro")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif
#endif
#define __STDC_FORMAT_MACROS 1
#if defined(__clang__)
#if __has_warning("-Wreserved-id-macro")
#pragma clang diagnostic pop
#endif
#endif
#endif
#define UTEST_INLINE inline
#define UTEST_INITIALIZER(f) \
static void f(void) __attribute__((constructor)); \
static void f(void)
#endif
#if defined(__cplusplus)
#define UTEST_CAST(type, x) static_cast<type>(x)
#define UTEST_PTR_CAST(type, x) reinterpret_cast<type>(x)
#define UTEST_EXTERN extern "C"
#define UTEST_NULL NULL
#else
#define UTEST_CAST(type, x) ((type)(x))
#define UTEST_PTR_CAST(type, x) ((type)(x))
#define UTEST_EXTERN extern
#define UTEST_NULL 0
#endif
#ifdef _MSC_VER
/*
io.h contains definitions for some structures with natural padding. This is
uninteresting, but for some reason MSVC's behaviour is to warn about
including this system header. That *is* interesting
*/
#pragma warning(disable : 4820)
#pragma warning(push, 1)
#include <io.h>
#pragma warning(pop)
#define UTEST_COLOUR_OUTPUT() (_isatty(_fileno(stdout)))
#else
#if defined(__EMSCRIPTEN__)
#include <emscripten/html5.h>
#define UTEST_COLOUR_OUTPUT() false
#else
#include <unistd.h>
#define UTEST_COLOUR_OUTPUT() (isatty(STDOUT_FILENO))
#endif
#endif
static UTEST_INLINE void *utest_realloc(void *const pointer, size_t new_size)
{
void *const new_pointer = realloc(pointer, new_size);
if (UTEST_NULL == new_pointer)
{
free(new_pointer);
}
return new_pointer;
}
static UTEST_INLINE utest_int64_t utest_ns(void)
{
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
utest_large_integer counter;
utest_large_integer frequency;
QueryPerformanceCounter(&counter);
QueryPerformanceFrequency(&frequency);
return UTEST_CAST(utest_int64_t, (counter.QuadPart * 1000000000) / frequency.QuadPart);
#elif defined(__linux__) && defined(__STRICT_ANSI__)
return UTEST_CAST(utest_int64_t, clock()) * 1000000000 / CLOCKS_PER_SEC;
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
defined(__DragonFly__) || defined(__sun__) || defined(__HAIKU__)
struct timespec ts;
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && !defined(__HAIKU__)
timespec_get(&ts, TIME_UTC);
#else
const clockid_t cid = CLOCK_REALTIME;
#if defined(UTEST_USE_CLOCKGETTIME)
clock_gettime(cid, &ts);
#else
syscall(SYS_clock_gettime, cid, &ts);
#endif
#endif
return UTEST_CAST(utest_int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec;
#elif __APPLE__
return UTEST_CAST(utest_int64_t, mach_absolute_time());
#elif __EMSCRIPTEN__
return emscripten_performance_now() * 1000000.0;
#else
#error Unsupported platform!
#endif
}
typedef void (*utest_testcase_t)(int *, size_t);
struct utest_test_state_s
{
utest_testcase_t func;
size_t index;
char *name;
};
struct utest_state_s
{
struct utest_test_state_s *tests;
size_t tests_length;
FILE *output;
};
/* extern to the global state utest needs to execute */
UTEST_EXTERN struct utest_state_s utest_state;
#if defined(_MSC_VER)
#define UTEST_WEAK __forceinline
#else
#define UTEST_WEAK __attribute__((weak))
#endif
#if defined(_MSC_VER)
#define UTEST_UNUSED
#else
#define UTEST_UNUSED __attribute__((unused))
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvariadic-macros"
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif
#define UTEST_PRINTF(...) \
if (utest_state.output) \
{ \
fprintf(utest_state.output, __VA_ARGS__); \
} \
printf(__VA_ARGS__)
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvariadic-macros"
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif
#ifdef _MSC_VER
#define UTEST_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__)
#else
#define UTEST_SNPRINTF(...) snprintf(__VA_ARGS__)
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if defined(__cplusplus)
/* if we are using c++ we can use overloaded methods (its in the language) */
#define UTEST_OVERLOADABLE
#elif defined(__clang__)
/* otherwise, if we are using clang with c - use the overloadable attribute */
#define UTEST_OVERLOADABLE __attribute__((overloadable))
#endif
#if defined(UTEST_OVERLOADABLE)
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f)
{
UTEST_PRINTF("%f", UTEST_CAST(double, f));
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d)
{
UTEST_PRINTF("%f", d);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d)
{
UTEST_PRINTF("%Lf", d);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i)
{
UTEST_PRINTF("%d", i);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i)
{
UTEST_PRINTF("%u", i);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i)
{
UTEST_PRINTF("%ld", i);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i)
{
UTEST_PRINTF("%lu", i);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const void *p)
{
UTEST_PRINTF("%p", p);
}
/*
long long is a c++11 extension
*/
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__cplusplus) && (__cplusplus >= 201103L)
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i)
{
UTEST_PRINTF("%lld", i);
}
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i);
UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i)
{
UTEST_PRINTF("%llu", i);
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
#define utest_type_printer(val) \
UTEST_PRINTF(_Generic((val), signed char \
: "%d", unsigned char \
: "%u", short \
: "%d", unsigned short \
: "%u", int \
: "%d", long \
: "%ld", long long \
: "%lld", unsigned \
: "%u", unsigned long \
: "%lu", unsigned long long \
: "%llu", float \
: "%f", double \
: "%f", long double \
: "%Lf", default \
: _Generic((val - val), ptrdiff_t \
: "%p", default \
: "undef")), \
(val))
#else
/*
we don't have the ability to print the values we got, so we create a macro
to tell our users we can't do anything fancy
*/
#define utest_type_printer(...) UTEST_PRINTF("undef")
#endif
#ifdef _MSC_VER
#define UTEST_SURPRESS_WARNING_BEGIN \
__pragma(warning(push)) __pragma(warning(disable : 4127)) __pragma(warning(disable : 4571))
#define UTEST_SURPRESS_WARNING_END __pragma(warning(pop))
#else
#define UTEST_SURPRESS_WARNING_BEGIN
#define UTEST_SURPRESS_WARNING_END
#endif
#if defined(__cplusplus) && (__cplusplus >= 201103L)
#define UTEST_AUTO(x) auto
#elif !defined(__cplusplus)
#if defined(__clang__)
/* clang-format off */
/* had to disable clang-format here because it malforms the pragmas */
#define UTEST_AUTO(x) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wgnu-auto-type\"") __auto_type \
_Pragma("clang diagnostic pop")
/* clang-format on */
#else
#define UTEST_AUTO(x) __typeof__(x + 0)
#endif
#else
#define UTEST_AUTO(x) typeof(x + 0)
#endif
#if defined(__clang__)
#define UTEST_STRNCMP(x, y, size) \
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wdisabled-macro-expansion\"") \
strncmp(x, y, size) _Pragma("clang diagnostic pop")
#else
#define UTEST_STRNCMP(x, y, size) strncmp(x, y, size)
#endif
#define UTEST_SKIP(msg) \
do \
{ \
UTEST_PRINTF(" Skipped : '%s'\n", (msg)); \
*utest_result = UTEST_TEST_SKIPPED; \
return; \
} while (0)
#if defined(__clang__)
#define UTEST_EXPECT(x, y, cond) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \
_Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \
_Pragma("clang diagnostic ignored \"-Wfloat-equal\"") UTEST_AUTO(x) xEval = (x); \
UTEST_AUTO(y) yEval = (y); \
if (!((xEval)cond(yEval))) \
{ \
_Pragma("clang diagnostic pop") UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : ("); \
UTEST_PRINTF(#x ") " #cond " (" #y); \
UTEST_PRINTF(")\n"); \
UTEST_PRINTF(" Actual : "); \
utest_type_printer(xEval); \
UTEST_PRINTF(" vs "); \
utest_type_printer(yEval); \
UTEST_PRINTF("\n"); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#elif defined(__GNUC__)
#define UTEST_EXPECT(x, y, cond) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
UTEST_AUTO(x) xEval = (x); \
UTEST_AUTO(y) yEval = (y); \
if (!((xEval)cond(yEval))) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : ("); \
UTEST_PRINTF(#x ") " #cond " (" #y); \
UTEST_PRINTF(")\n"); \
UTEST_PRINTF(" Actual : "); \
utest_type_printer(xEval); \
UTEST_PRINTF(" vs "); \
utest_type_printer(yEval); \
UTEST_PRINTF("\n"); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#else
#define UTEST_EXPECT(x, y, cond) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (!((x)cond(y))) \
{ \
UTEST_PRINTF("%s:%u: Failure (Expected " #cond " Actual)\n", __FILE__, __LINE__); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif
#define EXPECT_TRUE(x) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (!(x)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : true\n"); \
UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define EXPECT_FALSE(x) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : false\n"); \
UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define EXPECT_EQ(x, y) UTEST_EXPECT(x, y, ==)
#define EXPECT_NE(x, y) UTEST_EXPECT(x, y, !=)
#define EXPECT_LT(x, y) UTEST_EXPECT(x, y, <)
#define EXPECT_LE(x, y) UTEST_EXPECT(x, y, <=)
#define EXPECT_GT(x, y) UTEST_EXPECT(x, y, >)
#define EXPECT_GE(x, y) UTEST_EXPECT(x, y, >=)
#define EXPECT_STREQ(x, y) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 != strcmp(x, y)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%s\"\n", x); \
UTEST_PRINTF(" Actual : \"%s\"\n", y); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define EXPECT_STRNE(x, y) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 == strcmp(x, y)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%s\"\n", x); \
UTEST_PRINTF(" Actual : \"%s\"\n", y); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define EXPECT_STRNEQ(x, y, n) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 != UTEST_STRNCMP(x, y, n)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \
UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define EXPECT_STRNNE(x, y, n) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 == UTEST_STRNCMP(x, y, n)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \
UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define EXPECT_NEAR(x, y, epsilon) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (utest_fabs(UTEST_CAST(double, x) - UTEST_CAST(double, y)) > UTEST_CAST(double, epsilon)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %f\n", UTEST_CAST(double, x)); \
UTEST_PRINTF(" Actual : %f\n", UTEST_CAST(double, y)); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#if defined(__cplusplus)
#define EXPECT_EXCEPTION(x, exception_type) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
int exception_caught = 0; \
try \
{ \
x; \
} \
catch (const exception_type &) \
{ \
exception_caught = 1; \
} \
catch (...) \
{ \
exception_caught = 2; \
} \
if (exception_caught != 1) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \
UTEST_PRINTF(" Actual : %s\n", (exception_caught == 2) ? "Unexpected exception" : "No exception"); \
*utest_result = UTEST_TEST_FAILURE; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif
#if defined(__clang__)
#define UTEST_ASSERT(x, y, cond) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \
_Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \
_Pragma("clang diagnostic ignored \"-Wfloat-equal\"") UTEST_AUTO(x) xEval = (x); \
UTEST_AUTO(y) yEval = (y); \
if (!((xEval)cond(yEval))) \
{ \
_Pragma("clang diagnostic pop") UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : ("); \
UTEST_PRINTF(#x ") " #cond " (" #y); \
UTEST_PRINTF(")\n"); \
UTEST_PRINTF(" Actual : "); \
utest_type_printer(xEval); \
UTEST_PRINTF(" vs "); \
utest_type_printer(yEval); \
UTEST_PRINTF("\n"); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#elif defined(__GNUC__)
#define UTEST_ASSERT(x, y, cond) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
UTEST_AUTO(x) xEval = (x); \
UTEST_AUTO(y) yEval = (y); \
if (!((xEval)cond(yEval))) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : ("); \
UTEST_PRINTF(#x ") " #cond " (" #y); \
UTEST_PRINTF(")\n"); \
UTEST_PRINTF(" Actual : "); \
utest_type_printer(xEval); \
UTEST_PRINTF(" vs "); \
utest_type_printer(yEval); \
UTEST_PRINTF("\n"); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#else
#define UTEST_ASSERT(x, y, cond) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (!((x)cond(y))) \
{ \
UTEST_PRINTF("%s:%u: Failure (Expected " #cond " Actual)\n", __FILE__, __LINE__); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif
#define ASSERT_TRUE(x) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (!(x)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : true\n"); \
UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define ASSERT_FALSE(x) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : false\n"); \
UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define ASSERT_EQ(x, y) UTEST_ASSERT(x, y, ==)
#define ASSERT_NE(x, y) UTEST_ASSERT(x, y, !=)
#define ASSERT_LT(x, y) UTEST_ASSERT(x, y, <)
#define ASSERT_LE(x, y) UTEST_ASSERT(x, y, <=)
#define ASSERT_GT(x, y) UTEST_ASSERT(x, y, >)
#define ASSERT_GE(x, y) UTEST_ASSERT(x, y, >=)
#define ASSERT_STREQ(x, y) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 != strcmp(x, y)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%s\"\n", x); \
UTEST_PRINTF(" Actual : \"%s\"\n", y); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define ASSERT_STRNE(x, y) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 == strcmp(x, y)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%s\"\n", x); \
UTEST_PRINTF(" Actual : \"%s\"\n", y); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define ASSERT_STRNEQ(x, y, n) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 != UTEST_STRNCMP(x, y, n)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \
UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define ASSERT_STRNNE(x, y, n) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (x == NULL || y == NULL || 0 == UTEST_STRNCMP(x, y, n)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \
UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#define ASSERT_NEAR(x, y, epsilon) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
if (utest_fabs(UTEST_CAST(double, x) - UTEST_CAST(double, y)) > UTEST_CAST(double, epsilon)) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %f\n", UTEST_CAST(double, x)); \
UTEST_PRINTF(" Actual : %f\n", UTEST_CAST(double, y)); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#if defined(__cplusplus)
#define ASSERT_EXCEPTION(x, exception_type) \
UTEST_SURPRESS_WARNING_BEGIN do \
{ \
int exception_caught = 0; \
try \
{ \
x; \
} \
catch (const exception_type &) \
{ \
exception_caught = 1; \
} \
catch (...) \
{ \
exception_caught = 2; \
} \
if (exception_caught != 1) \
{ \
UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \
UTEST_PRINTF(" Actual : %s\n", (exception_caught == 2) ? "Unexpected exception" : "No exception"); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif
#define UTEST(SET, NAME) \
UTEST_EXTERN struct utest_state_s utest_state; \
static void utest_run_##SET##_##NAME(int *utest_result); \
static void utest_##SET##_##NAME(int *utest_result, size_t utest_index) \
{ \
(void)utest_index; \
utest_run_##SET##_##NAME(utest_result); \
} \
UTEST_INITIALIZER(utest_register_##SET##_##NAME) \
{ \
const size_t index = utest_state.tests_length++; \
const char *name_part = #SET "." #NAME; \
const size_t name_size = strlen(name_part) + 1; \
char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \
utest_state.tests = UTEST_PTR_CAST( \
struct utest_test_state_s *, utest_realloc(UTEST_PTR_CAST(void *, utest_state.tests), \
sizeof(struct utest_test_state_s) * utest_state.tests_length)); \
if (utest_state.tests) \
{ \
utest_state.tests[index].func = &utest_##SET##_##NAME; \
utest_state.tests[index].name = name; \
utest_state.tests[index].index = 0; \
} \
UTEST_SNPRINTF(name, name_size, "%s", name_part); \
} \
void utest_run_##SET##_##NAME(int *utest_result)
#define UTEST_F_SETUP(FIXTURE) static void utest_f_setup_##FIXTURE(int *utest_result, struct FIXTURE *utest_fixture)
#define UTEST_F_TEARDOWN(FIXTURE) \
static void utest_f_teardown_##FIXTURE(int *utest_result, struct FIXTURE *utest_fixture)
#if defined(__GNUC__) && __GNUC__ >= 8 && defined(__cplusplus)
#define UTEST_FIXTURE_SURPRESS_WARNINGS_BEGIN \
_Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wclass-memaccess\"")
#define UTEST_FIXTURE_SURPRESS_WARNINGS_END _Pragma("GCC diagnostic pop")
#else
#define UTEST_FIXTURE_SURPRESS_WARNINGS_BEGIN
#define UTEST_FIXTURE_SURPRESS_WARNINGS_END
#endif
#define UTEST_F(FIXTURE, NAME) \
UTEST_FIXTURE_SURPRESS_WARNINGS_BEGIN \
UTEST_EXTERN struct utest_state_s utest_state; \