1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "test.h"
5 #include "../src/util/str-util.h"
6 
7 unsigned int failed;
8 
test_fail(const char * file,int line,const char * format,...)9 void test_fail(const char *file, int line, const char *format, ...)
10 {
11     fprintf(stderr, "%s:%d: ", file, line);
12     va_list ap;
13     va_start(ap, format);
14     vfprintf(stderr, format, ap);
15     va_end(ap);
16     putc('\n', stderr);
17     fflush(stderr);
18     failed += 1;
19 }
20 
expect_streq(const char * file,int line,const char * s1,const char * s2)21 void expect_streq(const char *file, int line, const char *s1, const char *s2)
22 {
23     if (unlikely(!xstreq(s1, s2))) {
24         s1 = s1 ? s1 : "(null)";
25         s2 = s2 ? s2 : "(null)";
26         test_fail(file, line, "Strings not equal: '%s', '%s'", s1, s2);
27     }
28 }
29 
expect_ptreq(const char * file,int line,const void * p1,const void * p2)30 void expect_ptreq(const char *file, int line, const void *p1, const void *p2)
31 {
32     if (unlikely(p1 != p2)) {
33         test_fail(file, line, "Pointers not equal: %p, %p", p1, p2);
34     }
35 }
36 
expect_eq(const char * file,int line,intmax_t a,intmax_t b)37 void expect_eq(const char *file, int line, intmax_t a, intmax_t b)
38 {
39     if (unlikely(a != b)) {
40         test_fail(file, line, "Values not equal: %jd, %jd", a, b);
41     }
42 }
43 
expect_uint_eq(const char * file,int line,uintmax_t a,uintmax_t b)44 void expect_uint_eq(const char *file, int line, uintmax_t a, uintmax_t b)
45 {
46     if (unlikely(a != b)) {
47         test_fail(file, line, "Values not equal: %ju, %ju", a, b);
48     }
49 }
50 
expect_true(const char * file,int line,bool x)51 void expect_true(const char *file, int line, bool x)
52 {
53     if (unlikely(!x)) {
54         test_fail(file, line, "Unexpected false value");
55     }
56 }
57 
expect_false(const char * file,int line,bool x)58 void expect_false(const char *file, int line, bool x)
59 {
60     if (unlikely(x)) {
61         test_fail(file, line, "Unexpected true value");
62     }
63 }
64 
expect_null(const char * file,int line,const void * ptr)65 void expect_null(const char *file, int line, const void *ptr)
66 {
67     if (unlikely(ptr != NULL)) {
68         test_fail(file, line, "Expected NULL, but got: %p", ptr);
69     }
70 }
71 
expect_nonnull(const char * file,int line,const void * ptr)72 void expect_nonnull(const char *file, int line, const void *ptr)
73 {
74     if (unlikely(ptr == NULL)) {
75         test_fail(file, line, "Unexpected NULL pointer");
76     }
77 }
78 
iexpect_streq(const char * f,int l,size_t i,const char * a,const char * b)79 void iexpect_streq(const char *f, int l, size_t i, const char *a, const char *b)
80 {
81     if (unlikely(!xstreq(a, b))) {
82         a = a ? a : "(null)";
83         b = b ? b : "(null)";
84         i++;
85         test_fail(f, l, "Test #%zu: strings not equal: '%s', '%s'", i, a, b);
86     }
87 }
88 
iexpect_eq(const char * file,int line,size_t i,intmax_t a,intmax_t b)89 void iexpect_eq(const char *file, int line, size_t i, intmax_t a, intmax_t b)
90 {
91     if (unlikely(a != b)) {
92         i++;
93         test_fail(file, line, "Test #%zu: values not equal: %jd, %jd", i, a, b);
94     }
95 }
96 
iexpect_true(const char * file,int line,size_t i,bool x)97 void iexpect_true(const char *file, int line, size_t i, bool x)
98 {
99     if (unlikely(!x)) {
100         i++;
101         test_fail(file, line, "Test #%zu: unexpected false value", i);
102     }
103 }
104 
assert_eq(const char * file,int line,intmax_t a,intmax_t b)105 void assert_eq(const char *file, int line, intmax_t a, intmax_t b)
106 {
107     if (unlikely(a != b)) {
108         test_fail(file, line, "ERROR: Values not equal: %jd, %jd", a, b);
109         abort();
110     }
111 }
112 
assert_true(const char * file,int line,bool x)113 void assert_true(const char *file, int line, bool x)
114 {
115     if (unlikely(!x)) {
116         test_fail(file, line, "ERROR: Unexpected false value");
117         abort();
118     }
119 }
120 
assert_nonnull(const char * file,int line,const void * ptr)121 void assert_nonnull(const char *file, int line, const void *ptr)
122 {
123     if (unlikely(ptr == NULL)) {
124         test_fail(file, line, "ERROR: Unexpected NULL pointer");
125         abort();
126     }
127 }
128