Mercurial > hg > th-libs
comparison tests.c @ 304:3fcf42cce43d
Fix some tests and use stdout for output.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 22 Feb 2016 19:52:18 +0200 |
parents | 54ea7a73e5fa |
children | 5afd918cbd79 |
comparison
equal
deleted
inserted
replaced
303:54ea7a73e5fa | 304:3fcf42cce43d |
---|---|
29 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | 29 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
30 { 2, 't', "tests", "Perform tests -t <set>[,<set2>..]", OPT_ARGREQ }, | 30 { 2, 't', "tests", "Perform tests -t <set>[,<set2>..]", OPT_ARGREQ }, |
31 }; | 31 }; |
32 | 32 |
33 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]); | 33 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]); |
34 | |
35 | |
36 void tprintv(const int level, const char *fmt, va_list ap) | |
37 { | |
38 if (level >= th_verbosityLevel) | |
39 vfprintf(stdout, fmt, ap); | |
40 } | |
41 | |
42 | |
43 void tprint(const int level, const char *fmt, ...) | |
44 { | |
45 va_list ap; | |
46 va_start(ap, fmt); | |
47 tprintv(level, fmt, ap); | |
48 va_end(ap); | |
49 } | |
34 | 50 |
35 | 51 |
36 void arg_show_help(void) | 52 void arg_show_help(void) |
37 { | 53 { |
38 th_print_banner(stdout, th_prog_name, "[options]"); | 54 th_print_banner(stdout, th_prog_name, "[options]"); |
124 | 140 |
125 void test_result_msg_v(test_ctx *ctx, BOOL check, const char *fmt, va_list ap) | 141 void test_result_msg_v(test_ctx *ctx, BOOL check, const char *fmt, va_list ap) |
126 { | 142 { |
127 if (check) | 143 if (check) |
128 { | 144 { |
129 THPRINT(2, "%s: OK\n", ctx->header); | 145 tprint(2, "%s: OK\n", ctx->header); |
130 tests_passed++; | 146 tests_passed++; |
131 } | 147 } |
132 else | 148 else |
133 { | 149 { |
134 THPRINT(0, "%s: FAIL\n", ctx->header); | 150 tprint(0, "%s: FAIL\n", ctx->header); |
135 if (fmt != NULL) | 151 if (fmt != NULL) |
136 { | 152 { |
137 THPRINT(0, " - "); | 153 tprint(0, " - "); |
138 THPRINT_V(0, fmt, ap); | 154 tprintv(0, fmt, ap); |
139 THPRINT(0, "\n"); | 155 tprint(0, "\n"); |
140 } | 156 } |
141 if (ctx->res != NULL) | 157 if (ctx->res != NULL) |
142 THPRINT(0, "%s\n", ctx->res); | 158 tprint(0, "%s\n", ctx->res); |
143 tests_failed++; | 159 tests_failed++; |
144 } | 160 } |
145 } | 161 } |
146 | 162 |
147 | 163 |
201 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1"); | 217 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1"); |
202 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2"); | 218 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2"); |
203 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16"); | 219 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16"); |
204 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE"); | 220 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE"); |
205 va_end(ap); | 221 va_end(ap); |
206 THPRINT(2, | 222 tprint(2, |
207 "-----------------------------------------------------\n"); | 223 "-----------------------------------------------------\n"); |
208 } | 224 } |
209 | 225 |
210 | 226 |
211 BOOL test_set_start(const char *str) | 227 BOOL test_set_start(const char *str) |
212 { | 228 { |
213 if (tests_enabled[tests_sets++]) | 229 if (tests_enabled[tests_sets++]) |
214 { | 230 { |
215 tests_nenabled++; | 231 tests_nenabled++; |
216 THPRINT(1, | 232 tprint(1, |
217 "======================================================\n" | 233 "======================================================\n" |
218 " Set #%d : %s tests\n" | 234 " Set #%d : %s tests\n" |
219 "======================================================\n", | 235 "======================================================\n", |
220 tests_sets, str); | 236 tests_sets, str); |
221 | 237 |
281 { | 297 { |
282 int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, }; | 298 int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, }; |
283 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", "% 3d", "% 3o", "%+3o", "%+3d", }; | 299 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", "% 3d", "% 3o", "%+3o", "%+3d", }; |
284 | 300 |
285 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) | 301 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) |
286 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) | 302 { |
287 test_snprintf(i_fmts[i2], i_vals); | 303 tprint(1, "Value %d\n", i_vals[i1]); |
304 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) | |
305 test_snprintf(i_fmts[i2], i_vals[i1]); | |
306 } | |
288 } | 307 } |
289 | 308 |
290 if (test_set_start("printf() float")) | 309 if (test_set_start("printf() float")) |
291 { | 310 { |
292 int f_vals[] = { 2.02, 612342.234, -2.07, -612342.12, 0x1fff, 0x8000000, }; | 311 int f_vals[] = { 2.02, 612342.234, -2.07, -612342.12, 0x1fff, 0x8000000, }; |
293 char *f_fmts[] = { "%f", "%1.1f", "%5.5f", "%5f", "%-5f", "", }; | 312 char *f_fmts[] = { "%f", "%1.1f", "%5.5f", "%5f", "%-5f", "", }; |
294 | 313 |
295 for (i1 = 0; i1 < sizeof(f_vals) / sizeof(f_vals[0]); i1++) | 314 for (i1 = 0; i1 < sizeof(f_vals) / sizeof(f_vals[0]); i1++) |
296 for (i2 = 0; i2 < sizeof(f_fmts) / sizeof(f_fmts[0]); i2++) | 315 { |
297 test_snprintf(f_fmts[i2], f_vals); | 316 tprint(1, "Value %f\n", f_vals[i1]); |
317 for (i2 = 0; i2 < sizeof(f_fmts) / sizeof(f_fmts[0]); i2++) | |
318 test_snprintf(f_fmts[i2], f_vals[i1]); | |
319 } | |
298 } | 320 } |
299 | 321 |
300 if (test_set_start("printf() string")) | 322 if (test_set_start("printf() string")) |
301 { | 323 { |
302 char *s_vals[] = { "", "XYZXYZ", "xxx yyy zzz ppp fff", NULL, "X", "abcde", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", }; | 324 char *s_vals[] = { "", "XYZXYZ", "xxx yyy zzz ppp fff", NULL, "X", "abcde", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", }; |
303 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", "% 2s", "%03s", "% -12s", "% 03s", "%-.15s", "%.8s" }; | 325 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", "% 2s", "%03s", "% -12s", "% 03s", "%-.15s", "%.8s" }; |
304 | 326 |
305 for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) | 327 for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) |
306 for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) | 328 { |
307 test_snprintf(s_fmts[i2], s_vals); | 329 tprint(1, "Value '%s'\n", s_vals[i1]); |
308 | 330 for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) |
331 test_snprintf(s_fmts[i2], s_vals[i1]); | |
332 } | |
309 } | 333 } |
310 | 334 |
311 if (test_set_start("printf() char")) | 335 if (test_set_start("printf() char")) |
312 { | 336 { |
313 test_snprintf("a%cBC", 'x'); | 337 test_snprintf("a%cBC", 'x'); |
372 } | 396 } |
373 | 397 |
374 // | 398 // |
375 // Print summary and exit | 399 // Print summary and exit |
376 // | 400 // |
377 THPRINT(1, | 401 tprint(1, |
378 "======================================================\n"); | 402 "======================================================\n"); |
379 | 403 |
380 THPRINT(0, | 404 tprint(0, |
381 "%d tests failed, %d passed (%d main tests), %d test sets of %d sets total.\n", | 405 "%d tests failed, %d passed (%d main tests), %d test sets of %d sets total.\n", |
382 tests_failed, tests_passed, tests_total, tests_nenabled, tests_sets); | 406 tests_failed, tests_passed, tests_total, tests_nenabled, tests_sets); |
383 | 407 |
384 return 0; | 408 return 0; |
385 } | 409 } |