Remove Cxxtest dependency
[zynaddsubfx-code.git] / src / Tests / common.h
blob4b0a79127fe44f14744a99d31fab57cd71f46c26
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <stdlib.h>
6 int tap_quiet = 0;
7 int global_err = 0;
8 int test_counter = 0;
9 //expect a
10 //actual b
11 int assert_int_eq(int a, int b, const char *testcase, int line)
13 test_counter++;
14 int err = a!=b;
15 if(err) {
16 printf("not ok %d - %s...\n", test_counter, testcase);
17 printf("# Expected %d, but observed %d instead (line %d)\n", a, b, line);
18 global_err++;
19 } else if(!tap_quiet)
20 printf("ok %d - %s...\n", test_counter, testcase);
21 return err;
24 int assert_char_eq(char a, char b, const char *testcase, int line)
26 test_counter++;
27 int err = a!=b;
28 if(err) {
29 printf("not ok %d - %s...\n", test_counter, testcase);
30 printf("# Expected %c, but observed %c instead (line %d)\n", a, b, line);
31 global_err++;
32 } else if(!tap_quiet)
33 printf("ok %d - %s...\n", test_counter, testcase);
34 return err;
37 int assert_ptr_eq(const void* a, const void* b, const char *testcase, int line)
39 test_counter++;
40 int err = a!=b;
41 if(err) {
42 printf("not ok %d - %s...\n", test_counter, testcase);
43 printf("# Expected %p, but observed %p instead (line %d)\n", a, b, line);
44 global_err++;
45 } else if(!tap_quiet)
46 printf("ok %d - %s...\n", test_counter, testcase);
47 return err;
50 int assert_true(int a, const char *testcase, int line)
52 test_counter++;
53 int err = !a;
54 if(err) {
55 printf("not ok %d - %s...\n", test_counter, testcase);
56 printf("# Failure on line %d\n", line);
57 global_err++;
58 } else if(!tap_quiet)
59 printf("ok %d - %s...\n", test_counter, testcase);
60 return err;
63 int assert_false(int a, const char *testcase, int line)
65 return assert_true(!a, testcase, line);
69 int assert_str_eq(const char *a, const char *b, const char *testcase, int line)
71 test_counter++;
72 int err = strcmp(a,b);
73 if(err) {
74 printf("not ok %d - %s...\n", test_counter, testcase);
75 printf("# Expected '%s', but observed '%s' instead (line %d)\n", a, b, line);
76 global_err++;
77 } else if(!tap_quiet)
78 printf("ok %d - %s...\n", test_counter, testcase);
79 return err;
82 int assert_null(const void *v, const char *testcase, int line)
84 test_counter++;
85 int err = !!v;
86 if(err) {
87 printf("not ok %d - %s...\n", test_counter, testcase);
88 printf("# Expected NULL value, but observed Non-NULL instead (line %d)\n", line);
89 global_err++;
90 } else if(!tap_quiet)
91 printf("ok %d - %s...\n", test_counter, testcase);
92 return err;
95 int assert_non_null(const void *v, const char *testcase, int line)
97 test_counter++;
98 int err = !v;
99 if(err) {
100 printf("not ok %d - %s...\n", test_counter, testcase);
101 printf("# Expected Non-NULL value, but observed NULL instead (line %d)\n", line);
102 global_err++;
103 } else if(!tap_quiet)
104 printf("ok %d - %s...\n", test_counter, testcase);
105 return err;
108 int assert_f32_eq(float a, float b,const char *testcase, int line)
110 test_counter++;
111 int err = a!=b;
112 if(err) {
113 printf("not ok %d - %s...\n", test_counter, testcase);
114 printf("# Expected %f, but observed %f instead (line %d)\n", a, b, line);
115 global_err++;
116 } else if(!tap_quiet)
117 printf("ok %d - %s...\n", test_counter, testcase);
118 return err;
121 int assert_f32_sim(float a, float b, float t, const char *testcase, int line)
123 test_counter++;
124 float tmp = (a-b);
125 tmp = tmp<0?-tmp:tmp;
126 int err = tmp > t;
127 if(err) {
128 printf("not ok %d - %s...\n", test_counter, testcase);
129 printf("# Expected %f+-%f, but observed %f instead (line %d)\n", a, t, b, line);
130 global_err++;
131 } else if(!tap_quiet)
132 printf("ok %d - %s...\n", test_counter, testcase);
133 return err;
136 //produce a xxd style hexdump with sections highlighted using escape sequences
138 //e.g.
139 //0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e #include <stdio.
140 //0000010: 683e 0a23 696e 636c 7564 6520 3c73 7472 h>.#include <str
141 //0000020: 696e 672e 683e 0a0a 696e 7420 676c 6f62 ing.h>..int glob
142 //0000030: 616c 5f65 7272 203d 2030 3b0a 696e 7420 al_err = 0;.int
143 //0000040: 7465 7374 5f63 6f75 6e74 6572 203d 2030 test_counter = 0
144 //0000050: 3b0a 2f2f 6578 7065 6374 2061 0a2f 2f61 ;.//expect a.//a
146 void hexdump(const char *data, const char *mask, size_t len)
148 const char *bold_gray = "\x1b[30;1m";
149 const char *reset = "\x1b[0m";
150 int offset = 0;
151 while(1)
153 //print line
154 printf("#%07x: ", offset);
156 int char_covered = 0;
158 //print hex groups (8)
159 for(int i=0; i<8; ++i) {
161 //print doublet
162 for(int j=0; j<2; ++j) {
163 int loffset = offset + 2*i + j;
164 if(loffset >= (int)len)
165 goto escape;
167 //print hex
169 //start highlight
170 if(mask && mask[loffset]){printf("%s", bold_gray);}
172 //print chars
173 printf("%02x", 0xff&data[loffset]);
175 //end highlight
176 if(mask && mask[loffset]){printf("%s", reset);}
177 char_covered += 2;
180 printf(" ");
181 char_covered += 1;
183 escape:
185 //print filler if needed
186 for(int i=char_covered; i<41; ++i)
187 printf(" ");
189 //print ascii (16)
190 for(int i=0; i<16; ++i) {
191 if(isprint(data[offset+i]))
192 printf("%c", data[offset+i]);
193 else
194 printf(".");
196 printf("\n");
197 offset += 16;
198 if(offset >= (int)len)
199 return;
204 int assert_hex_eq(const char *a, const char *b, size_t size_a, size_t size_b,
205 const char *testcase, int line)
207 test_counter++;
208 int err = (size_a != size_b) || memcmp(a, b, size_a);
209 if(err) {
210 printf("not ok %d - %s...\n", test_counter, testcase);
211 printf("# Error on line %d\n", line);
212 //printf("# Expected '%s', but observed '%s' instead (line %d)\n", a, b, line);
214 //create difference mask
215 const int longer = size_a > size_b ? size_a : size_b;
216 const int shorter = size_a < size_b ? size_a : size_b;
217 char mask[longer];
218 memset(mask, 0, longer);
219 for(int i=0; i<shorter; ++i)
220 if(a[i] != b[i])
221 mask[i] = 1;
223 printf("#\n");
224 printf("# Expected:\n");
225 hexdump(a, mask, size_a);
226 printf("#\n");
227 printf("# Observed:\n");
228 hexdump(b, mask, size_b);
230 global_err++;
231 } else if(!tap_quiet)
232 printf("ok %d - %s...\n", test_counter, testcase);
233 return err;
236 int assert_flt_eq(float a, float b, const char *testcase, int line)
238 int ret = assert_hex_eq((char*)&a, (char*)&b, sizeof(float), sizeof(float),
239 testcase, line);
240 if(ret)
241 printf("#expected=%f actual %f\n", a, b);
242 return ret;
245 int test_summary(void)
247 printf("# %d test(s) failed out of %d (currently passing %f%% tests)\n",
248 global_err, test_counter, 100.0-global_err*100./test_counter);
249 return global_err ? EXIT_FAILURE : EXIT_SUCCESS;