Mercurial > hg > th-libs
comparison tests.c @ 457:85fa3d333556
Actually, revert the boolean changes .. meh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Jan 2018 23:09:29 +0200 |
parents | efd33accdc81 |
children | 6d44592cdab1 |
comparison
equal
deleted
inserted
replaced
456:1bf886fa9db5 | 457:85fa3d333556 |
---|---|
21 | 21 |
22 | 22 |
23 typedef struct | 23 typedef struct |
24 { | 24 { |
25 char *header, *res; | 25 char *header, *res; |
26 bool shown; | 26 BOOL shown; |
27 } test_ctx; | 27 } test_ctx; |
28 | 28 |
29 | 29 |
30 // Globals | 30 // Globals |
31 int tests_failed, tests_passed, tests_total, sets_total, sets_nenabled; | 31 int tests_failed, tests_passed, tests_total, sets_total, sets_nenabled; |
45 }; | 45 }; |
46 | 46 |
47 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]); | 47 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]); |
48 | 48 |
49 | 49 |
50 bool tprintv(const int level, const char *fmt, va_list ap) | 50 BOOL tprintv(const int level, const char *fmt, va_list ap) |
51 { | 51 { |
52 if (level <= th_verbosityLevel) | 52 if (level <= th_verbosityLevel) |
53 { | 53 { |
54 vfprintf(stdout, fmt, ap); | 54 vfprintf(stdout, fmt, ap); |
55 return true; | 55 return TRUE; |
56 } | 56 } |
57 else | 57 else |
58 return false; | 58 return FALSE; |
59 } | 59 } |
60 | 60 |
61 | 61 |
62 bool tprint(const int level, const char *fmt, ...) | 62 BOOL tprint(const int level, const char *fmt, ...) |
63 { | 63 { |
64 bool retv; | 64 BOOL retv; |
65 va_list ap; | 65 va_list ap; |
66 va_start(ap, fmt); | 66 va_start(ap, fmt); |
67 retv = tprintv(level, fmt, ap); | 67 retv = tprintv(level, fmt, ap); |
68 va_end(ap); | 68 va_end(ap); |
69 return retv; | 69 return retv; |
75 th_print_banner(stdout, th_prog_name, "[options]"); | 75 th_print_banner(stdout, th_prog_name, "[options]"); |
76 th_args_help(stdout, arg_opts, arg_nopts, 0); | 76 th_args_help(stdout, arg_opts, arg_nopts, 0); |
77 } | 77 } |
78 | 78 |
79 | 79 |
80 bool arg_handle_opt(const int optN, char *optArg, char *currArg) | 80 BOOL arg_handle_opt(const int optN, char *optArg, char *currArg) |
81 { | 81 { |
82 switch (optN) | 82 switch (optN) |
83 { | 83 { |
84 case 0: | 84 case 0: |
85 arg_show_help(); | 85 arg_show_help(); |
90 th_verbosityLevel++; | 90 th_verbosityLevel++; |
91 break; | 91 break; |
92 | 92 |
93 case 2: | 93 case 2: |
94 { | 94 { |
95 bool ret = true; | 95 BOOL ret = TRUE; |
96 char *pos, *pstr, *next; | 96 char *pos, *pstr, *next; |
97 pos = pstr = th_strdup(optArg); | 97 pos = pstr = th_strdup(optArg); |
98 memset(sets_enabled, 0, sizeof(sets_enabled)); | 98 memset(sets_enabled, 0, sizeof(sets_enabled)); |
99 | 99 |
100 do { | 100 do { |
109 if (val > 0 && val <= SET_MAX_TESTS) | 109 if (val > 0 && val <= SET_MAX_TESTS) |
110 sets_enabled[val - 1] = 1; | 110 sets_enabled[val - 1] = 1; |
111 else | 111 else |
112 { | 112 { |
113 THERR("Invalid test number #%d, out of range [%d .. %d]\n", val, 1, SET_MAX_TESTS); | 113 THERR("Invalid test number #%d, out of range [%d .. %d]\n", val, 1, SET_MAX_TESTS); |
114 ret = false; | 114 ret = FALSE; |
115 } | 115 } |
116 th_free(tmp); | 116 th_free(tmp); |
117 } | 117 } |
118 | 118 |
119 if (next != NULL) | 119 if (next != NULL) |
128 optFlags = atoi(optArg); | 128 optFlags = atoi(optArg); |
129 break; | 129 break; |
130 | 130 |
131 default: | 131 default: |
132 THERR("Unknown option '%s'.\n", currArg); | 132 THERR("Unknown option '%s'.\n", currArg); |
133 return false; | 133 return FALSE; |
134 } | 134 } |
135 | 135 |
136 return true; | 136 return TRUE; |
137 } | 137 } |
138 | 138 |
139 | 139 |
140 void test_init(test_ctx *ctx) | 140 void test_init(test_ctx *ctx) |
141 { | 141 { |
165 test_start_v(ctx, fmt, ap); | 165 test_start_v(ctx, fmt, ap); |
166 va_end(ap); | 166 va_end(ap); |
167 } | 167 } |
168 | 168 |
169 | 169 |
170 void test_result_msg_v(test_ctx *ctx, bool check, const char *fmt, va_list ap) | 170 void test_result_msg_v(test_ctx *ctx, BOOL check, const char *fmt, va_list ap) |
171 { | 171 { |
172 if (check) | 172 if (check) |
173 { | 173 { |
174 if (!ctx->shown && tprint(2, "%s: OK\n", ctx->header)) | 174 if (!ctx->shown && tprint(2, "%s: OK\n", ctx->header)) |
175 ctx->shown = true; | 175 ctx->shown = TRUE; |
176 | 176 |
177 tests_passed++; | 177 tests_passed++; |
178 } | 178 } |
179 else | 179 else |
180 { | 180 { |
181 if (!ctx->shown && tprint(0, "%s: FAIL\n", ctx->header)) | 181 if (!ctx->shown && tprint(0, "%s: FAIL\n", ctx->header)) |
182 ctx->shown = true; | 182 ctx->shown = TRUE; |
183 | 183 |
184 if (fmt != NULL) | 184 if (fmt != NULL) |
185 { | 185 { |
186 tprint(0, " - "); | 186 tprint(0, " - "); |
187 tprintv(0, fmt, ap); | 187 tprintv(0, fmt, ap); |
192 tests_failed++; | 192 tests_failed++; |
193 } | 193 } |
194 } | 194 } |
195 | 195 |
196 | 196 |
197 bool test_result_msg(test_ctx *ctx, bool check, const char *fmt, ...) | 197 BOOL test_result_msg(test_ctx *ctx, BOOL check, const char *fmt, ...) |
198 { | 198 { |
199 va_list ap; | 199 va_list ap; |
200 va_start(ap, fmt); | 200 va_start(ap, fmt); |
201 test_result_msg_v(ctx, check, fmt, ap); | 201 test_result_msg_v(ctx, check, fmt, ap); |
202 va_end(ap); | 202 va_end(ap); |
203 return check; | 203 return check; |
204 } | 204 } |
205 | 205 |
206 | 206 |
207 bool test_result(test_ctx *ctx, bool check) | 207 BOOL test_result(test_ctx *ctx, BOOL check) |
208 { | 208 { |
209 test_result_msg_v(ctx, check, NULL, NULL); | 209 test_result_msg_v(ctx, check, NULL, NULL); |
210 return check; | 210 return check; |
211 } | 211 } |
212 | 212 |
273 tprint(2, | 273 tprint(2, |
274 "-----------------------------------------------------\n"); | 274 "-----------------------------------------------------\n"); |
275 } | 275 } |
276 | 276 |
277 | 277 |
278 bool test_set_start(const char *str) | 278 BOOL test_set_start(const char *str) |
279 { | 279 { |
280 if (sets_enabled[sets_total++]) | 280 if (sets_enabled[sets_total++]) |
281 { | 281 { |
282 sets_nenabled++; | 282 sets_nenabled++; |
283 tprint(1, | 283 tprint(1, |
284 "======================================================\n" | 284 "======================================================\n" |
285 " Set #%d : %s tests\n" | 285 " Set #%d : %s tests\n" |
286 "======================================================\n", | 286 "======================================================\n", |
287 sets_total, str); | 287 sets_total, str); |
288 | 288 |
289 return true; | 289 return TRUE; |
290 } | 290 } |
291 else | 291 else |
292 return false; | 292 return FALSE; |
293 } | 293 } |
294 | 294 |
295 | 295 |
296 #define NCOUNT(xxx) (sizeof(xxx) / sizeof(xxx[0])) | 296 #define NCOUNT(xxx) (sizeof(xxx) / sizeof(xxx[0])) |
297 | 297 |
442 // | 442 // |
443 // String matching functions | 443 // String matching functions |
444 // | 444 // |
445 if (test_set_start("String matching #1")) | 445 if (test_set_start("String matching #1")) |
446 { | 446 { |
447 TEST2(th_strcasecmp, "aSdFq", "asdfq", true); | 447 TEST2(th_strcasecmp, "aSdFq", "asdfq", TRUE); |
448 TEST2(th_strcasecmp, "aSdFq", "asFfq", false); | 448 TEST2(th_strcasecmp, "aSdFq", "asFfq", FALSE); |
449 TEST2(th_strcasecmp, "abcde", "abcde", true); | 449 TEST2(th_strcasecmp, "abcde", "abcde", TRUE); |
450 TEST2(th_strcasecmp, "öäå", "öäå", true); | 450 TEST2(th_strcasecmp, "öäå", "öäå", TRUE); |
451 TEST2(th_strcasecmp, "aöäå", "aöäå", true); | 451 TEST2(th_strcasecmp, "aöäå", "aöäå", TRUE); |
452 } | 452 } |
453 | 453 |
454 if (test_set_start("String matching #2")) | 454 if (test_set_start("String matching #2")) |
455 { | 455 { |
456 TEST3(th_strncasecmp, "aSdFq", "asFfqB", 4, false); | 456 TEST3(th_strncasecmp, "aSdFq", "asFfqB", 4, FALSE); |
457 TEST3(th_strncasecmp, "aSdFq", "asFfqQ", 2, true); | 457 TEST3(th_strncasecmp, "aSdFq", "asFfqQ", 2, TRUE); |
458 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, true); | 458 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, TRUE); |
459 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, true); | 459 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, TRUE); |
460 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, true); | 460 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, TRUE); |
461 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, true); | 461 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, TRUE); |
462 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, false); | 462 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, FALSE); |
463 } | 463 } |
464 | 464 |
465 if (test_set_start("String matching #3")) | 465 if (test_set_start("String matching #3")) |
466 { | 466 { |
467 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", true); | 467 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", TRUE); |
468 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", true); | 468 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", TRUE); |
469 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", false); | 469 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", FALSE); |
470 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", false); | 470 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", FALSE); |
471 TEST2B(th_strmatch, "abba ABBAkukka lol", "*bba*", true); | 471 TEST2B(th_strmatch, "abba ABBAkukka lol", "*bba*", TRUE); |
472 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", true); | 472 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", TRUE); |
473 TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", false); | 473 TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", FALSE); |
474 TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", false); | 474 TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", FALSE); |
475 } | 475 } |
476 | 476 |
477 if (test_set_start("String matching #4")) | 477 if (test_set_start("String matching #4")) |
478 { | 478 { |
479 TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", false); | 479 TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", FALSE); |
480 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", true); | 480 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", TRUE); |
481 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*ab?ak*", true); | 481 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*ab?ak*", TRUE); |
482 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", false); | 482 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", FALSE); |
483 TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", true); | 483 TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", TRUE); |
484 } | 484 } |
485 | 485 |
486 // Tests that test for things that do not work correctly yet | 486 // Tests that test for things that do not work correctly yet |
487 // Unicode / multibyte UTF-8 causes problems here | 487 // Unicode / multibyte UTF-8 causes problems here |
488 if (test_set_start("Invalid")) | 488 if (test_set_start("Invalid")) |
489 { | 489 { |
490 TEST2(th_strcasecmp, "ÖÄÅ", "öäå", false); // SHOULD match | 490 TEST2(th_strcasecmp, "ÖÄÅ", "öäå", FALSE); // SHOULD match |
491 TEST3(th_strncasecmp, "Aäöå", "aöå", 2, true); // should NOT match | 491 TEST3(th_strncasecmp, "Aäöå", "aöå", 2, TRUE); // should NOT match |
492 TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false); // should match | 492 TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", FALSE); // should match |
493 } | 493 } |
494 | 494 |
495 // | 495 // |
496 // Print summary and exit | 496 // Print summary and exit |
497 // | 497 // |