Mercurial > hg > th-libs
annotate tests.c @ 421:d8702bbab84f
Fix build.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 09 Mar 2016 15:11:06 +0200 |
parents | 4852ebc17575 |
children | 2991e6b52d95 |
rev | line source |
---|---|
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 #include "th_types.h" |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
2 #include "th_args.h" |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 #include "th_util.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 #include "th_string.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 #include "th_crypto.h" |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
364 | 7 |
8 #define SET_BUF_SIZE 128 | |
365 | 9 #define SET_BUF_SIZE_2 ((SET_BUF_SIZE) + 32) |
364 | 10 #define SET_MAX_TESTS 64 |
11 #define SET_SENTINEL_BYTE 0x0e5 | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
291 | 13 |
365 | 14 enum |
15 { | |
16 TST_SUPERFLUOUS = 0x0001, | |
17 TST_CORNERCASE = 0x0002, | |
18 TST_OVERFLOW = 0x0004, | |
19 TST_ALL = 0xffff, | |
20 }; | |
21 | |
22 | |
303 | 23 typedef struct |
24 { | |
25 char *header, *res; | |
363 | 26 BOOL shown; |
303 | 27 } test_ctx; |
28 | |
29 | |
291 | 30 // Globals |
365 | 31 int tests_failed, tests_passed, tests_total, sets_total, sets_nenabled; |
32 int sets_enabled[SET_MAX_TESTS]; | |
259 | 33 |
365 | 34 char buf1[SET_BUF_SIZE_2], buf2[SET_BUF_SIZE_2]; |
35 int optFlags = TST_ALL; | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
38 // Define option arguments |
380
ac10155d2b4a
Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents:
366
diff
changeset
|
39 static const th_optarg arg_opts[] = |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
40 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
41 { 0, '?', "help", "Show this help", OPT_NONE }, |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
42 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
365 | 43 { 2, 's', "sets", "Perform test sets -s <set>[,<set2>..]", OPT_ARGREQ }, |
44 { 3, 't', "tests", "Perform only tests (see below)", OPT_ARGREQ }, | |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
45 }; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
46 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
47 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
48 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
49 |
363 | 50 BOOL tprintv(const int level, const char *fmt, va_list ap) |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
51 { |
311
31668615083e
Fix test printing function :S
Matti Hamalainen <ccr@tnsp.org>
parents:
306
diff
changeset
|
52 if (level <= th_verbosityLevel) |
363 | 53 { |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
54 vfprintf(stdout, fmt, ap); |
363 | 55 return TRUE; |
56 } | |
57 else | |
58 return FALSE; | |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
59 } |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
60 |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
61 |
363 | 62 BOOL tprint(const int level, const char *fmt, ...) |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
63 { |
363 | 64 BOOL retv; |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
65 va_list ap; |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
66 va_start(ap, fmt); |
363 | 67 retv = tprintv(level, fmt, ap); |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
68 va_end(ap); |
363 | 69 return retv; |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
70 } |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
71 |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
72 |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
73 void arg_show_help(void) |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
74 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
75 th_print_banner(stdout, th_prog_name, "[options]"); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
76 th_args_help(stdout, arg_opts, arg_nopts, 0); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
77 } |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
78 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
79 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
80 BOOL arg_handle_opt(const int optN, char *optArg, char *currArg) |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
81 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
82 switch (optN) |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
83 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
84 case 0: |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
85 arg_show_help(); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
86 exit(0); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
87 break; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
88 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
89 case 1: |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
90 th_verbosityLevel++; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
91 break; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
92 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
93 case 2: |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
94 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
95 BOOL ret = TRUE; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
96 char *pos, *pstr, *next; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
97 pos = pstr = th_strdup(optArg); |
365 | 98 memset(sets_enabled, 0, sizeof(sets_enabled)); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
99 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
100 do { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
101 next = strchr(pos, ','); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
102 if (next != NULL) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
103 *next = 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
104 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
105 char *tmp = th_strdup_trim(pos, TH_TRIM_BOTH); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
106 if (tmp != NULL) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
107 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
108 int val = atoi(tmp); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
109 if (val > 0 && val <= SET_MAX_TESTS) |
365 | 110 sets_enabled[val - 1] = 1; |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
111 else |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
112 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
113 THERR("Invalid test number #%d, out of range [%d .. %d]\n", val, 1, SET_MAX_TESTS); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
114 ret = FALSE; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
115 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
116 th_free(tmp); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
117 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
118 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
119 if (next != NULL) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
120 pos = next + 1; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
121 } while (next != NULL); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
122 th_free(pstr); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
123 return ret; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
124 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
125 break; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
126 |
365 | 127 case 3: |
366
21bbb2dc4fac
Actually parse the -t option argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
365
diff
changeset
|
128 optFlags = atoi(optArg); |
365 | 129 break; |
130 | |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
131 default: |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
132 THERR("Unknown option '%s'.\n", currArg); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
133 return FALSE; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
134 } |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
135 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
136 return TRUE; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
137 } |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
138 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
139 |
303 | 140 void test_init(test_ctx *ctx) |
141 { | |
142 memset(ctx, 0, sizeof(test_ctx)); | |
143 } | |
144 | |
145 | |
342
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
146 void test_end(test_ctx *ctx) |
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
147 { |
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
148 th_free_r(&ctx->header); |
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
149 th_free_r(&ctx->res); |
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
150 } |
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
151 |
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
152 |
303 | 153 void test_start_v(test_ctx *ctx, const char *fmt, va_list ap) |
259 | 154 { |
155 tests_total++; | |
342
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
156 test_end(ctx); |
303 | 157 ctx->header = th_strdup_vprintf(fmt, ap); |
259 | 158 } |
159 | |
160 | |
303 | 161 void test_start(test_ctx *ctx, const char *fmt, ...) |
259 | 162 { |
163 va_list ap; | |
164 va_start(ap, fmt); | |
303 | 165 test_start_v(ctx, fmt, ap); |
259 | 166 va_end(ap); |
167 } | |
168 | |
169 | |
303 | 170 void test_result_msg_v(test_ctx *ctx, BOOL check, const char *fmt, va_list ap) |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 { |
259 | 172 if (check) |
173 { | |
363 | 174 if (!ctx->shown && tprint(2, "%s: OK\n", ctx->header)) |
175 ctx->shown = TRUE; | |
176 | |
259 | 177 tests_passed++; |
178 } | |
179 else | |
180 { | |
363 | 181 if (!ctx->shown && tprint(0, "%s: FAIL\n", ctx->header)) |
182 ctx->shown = TRUE; | |
183 | |
259 | 184 if (fmt != NULL) |
263 | 185 { |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
186 tprint(0, " - "); |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
187 tprintv(0, fmt, ap); |
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
188 tprint(0, "\n"); |
263 | 189 } |
303 | 190 if (ctx->res != NULL) |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
191 tprint(0, "%s\n", ctx->res); |
259 | 192 tests_failed++; |
193 } | |
194 } | |
195 | |
196 | |
305
5afd918cbd79
Have a return value for test result functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
304
diff
changeset
|
197 BOOL test_result_msg(test_ctx *ctx, BOOL check, const char *fmt, ...) |
259 | 198 { |
199 va_list ap; | |
200 va_start(ap, fmt); | |
303 | 201 test_result_msg_v(ctx, check, fmt, ap); |
259 | 202 va_end(ap); |
305
5afd918cbd79
Have a return value for test result functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
304
diff
changeset
|
203 return check; |
259 | 204 } |
205 | |
206 | |
305
5afd918cbd79
Have a return value for test result functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
304
diff
changeset
|
207 BOOL test_result(test_ctx *ctx, BOOL check) |
259 | 208 { |
303 | 209 test_result_msg_v(ctx, check, NULL, NULL); |
305
5afd918cbd79
Have a return value for test result functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
304
diff
changeset
|
210 return check; |
259 | 211 } |
212 | |
213 | |
214 | |
319 | 215 void test_snprintf_do(size_t len, const char *msg, const char *fmt, va_list ap) |
259 | 216 { |
217 int ret1, ret2; | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 va_list tmp; |
303 | 219 test_ctx ctx; |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 |
263 | 221 // Test basic *printf() functionality |
303 | 222 test_init(&ctx); |
395
bffd3caf2d2c
Rename TH_PRI{u,d,x}* macros to match with standard ISO C99 inttypes.h PRI*.
Matti Hamalainen <ccr@tnsp.org>
parents:
380
diff
changeset
|
223 test_start(&ctx, "th_vsnprintf(%" PRIu_SIZE_T ", \"%s\", %s)", len, fmt, msg); |
259 | 224 |
365 | 225 memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE_2); buf1[SET_BUF_SIZE_2-1] = 0; |
226 memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE_2); buf2[SET_BUF_SIZE_2-1] = 0; | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 |
339
46a0fab6ca1f
Fix snprintf() test running.
Matti Hamalainen <ccr@tnsp.org>
parents:
338
diff
changeset
|
228 va_copy(tmp, ap); ret1 = th_vsnprintf(buf1, len, fmt, tmp); |
46a0fab6ca1f
Fix snprintf() test running.
Matti Hamalainen <ccr@tnsp.org>
parents:
338
diff
changeset
|
229 va_copy(tmp, ap); ret2 = vsnprintf(buf2, len, fmt, tmp); |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
303 | 231 test_result_msg(&ctx, ret1 == ret2, "retval mismatch %d [th] != %d [libc]", ret1, ret2); |
232 test_result_msg(&ctx, strcmp(buf1, buf2) == 0, "result mismatch '%s' [th] != '%s' [libc]", buf1, buf2); | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
233 |
365 | 234 if (optFlags & TST_OVERFLOW) |
235 { | |
303 | 236 test_result_msg(&ctx, (unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]); |
237 test_result_msg(&ctx, (unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]); | |
365 | 238 } |
261 | 239 |
342
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
240 test_end(&ctx); |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 |
319 | 244 void test_snprintf(const char *msg, const char *fmt, ...) |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 { |
362 | 246 test_ctx ctx; |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 va_list ap, tmp; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 va_start(ap, fmt); |
365 | 249 |
250 if (optFlags & TST_CORNERCASE) | |
251 { | |
252 va_copy(tmp, ap); test_snprintf_do(0, msg, fmt, tmp); | |
253 va_copy(tmp, ap); test_snprintf_do(1, msg, fmt, tmp); | |
254 va_copy(tmp, ap); test_snprintf_do(2, msg, fmt, tmp); | |
255 va_copy(tmp, ap); test_snprintf_do(16, msg, fmt, tmp); | |
256 } | |
257 | |
319 | 258 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, msg, fmt, tmp); |
362 | 259 |
260 // Test th_strdup_vprintf() | |
365 | 261 if (optFlags & TST_SUPERFLUOUS) |
262 { | |
263 test_init(&ctx); | |
264 test_start(&ctx, "th_strdup_vprintf('%s')", fmt); | |
265 va_copy(tmp, ap); | |
266 char *str = th_strdup_vprintf(fmt, tmp); | |
267 test_result_msg(&ctx, str != NULL, "result NULL"); | |
268 th_free(str); | |
269 test_end(&ctx); | |
270 } | |
362 | 271 |
255 | 272 va_end(ap); |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
273 tprint(2, |
291 | 274 "-----------------------------------------------------\n"); |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
278 BOOL test_set_start(const char *str) |
264
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
279 { |
365 | 280 if (sets_enabled[sets_total++]) |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
281 { |
365 | 282 sets_nenabled++; |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
283 tprint(1, |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
284 "======================================================\n" |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
285 " Set #%d : %s tests\n" |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
286 "======================================================\n", |
365 | 287 sets_total, str); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
288 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
289 return TRUE; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
290 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
291 else |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
292 return FALSE; |
264
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
293 } |
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
294 |
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
295 |
327
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
296 #define NCOUNT(xxx) (sizeof(xxx) / sizeof(xxx[0])) |
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
297 |
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
298 |
263 | 299 #define TEST2(fun, str1, str2, ret) do { \ |
303 | 300 test_ctx ctx; test_init(&ctx); \ |
301 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ | |
302 test_result(&ctx, ( fun (str1, str2) == 0) == ret); \ | |
342
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
303 test_end(&ctx); \ |
263 | 304 } while (0) |
305 | |
266 | 306 #define TEST2B(fun, str1, str2, ret) do { \ |
303 | 307 test_ctx ctx; test_init(&ctx); \ |
308 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ | |
309 test_result(&ctx, fun (str1, str2) == ret); \ | |
342
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
310 test_end(&ctx); \ |
266 | 311 } while (0) |
312 | |
263 | 313 #define TEST3(fun, str1, str2, len, ret) do { \ |
303 | 314 test_ctx ctx; test_init(&ctx); \ |
315 test_start(&ctx, # fun "('%s', '%s', %d)", str1, str2, len); \ | |
316 test_result(&ctx, ( fun (str1, str2, len) == 0) == ret); \ | |
342
09c2f6abf694
Fix memory leaks in test suite.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
317 test_end(&ctx); \ |
263 | 318 } while (0) |
319 | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
320 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
321 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
322 int main(int argc, char *argv[]) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
323 { |
334
b90173719330
Add more tests (some of which will fail for now due to unimplemented features).
Matti Hamalainen <ccr@tnsp.org>
parents:
333
diff
changeset
|
324 size_t i1, i2, i3, i4; |
326
75b22d6f8a71
Simplify and improve integer sprintf() tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
325 char buf[64], buf2[64]; |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
326 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
327 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
328 // Initialization |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
329 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
330 th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
331 th_verbosityLevel = 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
332 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
333 if (sizeof(char) != sizeof(unsigned char)) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
334 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
335 THERR("sizeof(char) != sizeof(unsigned char)???\n"); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
336 return -1; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
337 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
338 |
365 | 339 tests_failed = tests_passed = tests_total = sets_total = sets_nenabled = 0; |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
340 for (i1 = 0; i1 < SET_MAX_TESTS; i1++) |
365 | 341 sets_enabled[i1] = 1; |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
342 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
343 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
344 // Parse command line arguments |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
345 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
346 if (!th_args_process(argc, argv, arg_opts, arg_nopts, |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
347 arg_handle_opt, NULL, 0)) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
348 return 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
349 |
365 | 350 tprint(1, "Enabled test types are 0x%04x.\n", optFlags); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
351 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
352 // |
405 | 353 // Test series for printf() |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
354 // |
404 | 355 char *i_fmts[] = { "", "05", "5", ".5", "8.5", "08.5", "3", "3.2", "3", ".0", "0" }; |
334
b90173719330
Add more tests (some of which will fail for now due to unimplemented features).
Matti Hamalainen <ccr@tnsp.org>
parents:
333
diff
changeset
|
356 char *i_mods[] = { "", "-", "+", "#", }; |
343 | 357 char *i_types[] = { "d", "u", "i", "x", "X", "o", }; |
295 | 358 if (test_set_start("printf() integer")) |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
359 { |
403 | 360 int i_vals[] = { 0, -0, -1, 2, -2, 512, -1024, 612342, -612342, 0x1fff, 0x8000000, -123456789 }; |
315 | 361 |
327
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
362 for (i1 = 0; i1 < NCOUNT(i_vals); i1++) |
315 | 363 { |
319 | 364 snprintf(buf, sizeof(buf), "%d", i_vals[i1]); |
326
75b22d6f8a71
Simplify and improve integer sprintf() tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
365 |
334
b90173719330
Add more tests (some of which will fail for now due to unimplemented features).
Matti Hamalainen <ccr@tnsp.org>
parents:
333
diff
changeset
|
366 for (i4 = 0; i4 < NCOUNT(i_mods); i4++) |
332 | 367 for (i3 = 0; i3 < NCOUNT(i_types); i3++) |
368 for (i2 = 0; i2 < NCOUNT(i_fmts); i2++) | |
326
75b22d6f8a71
Simplify and improve integer sprintf() tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
369 { |
334
b90173719330
Add more tests (some of which will fail for now due to unimplemented features).
Matti Hamalainen <ccr@tnsp.org>
parents:
333
diff
changeset
|
370 snprintf(buf2, sizeof(buf2), "%%%s%s%s", i_mods[i4], i_fmts[i2], i_types[i3]); |
326
75b22d6f8a71
Simplify and improve integer sprintf() tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
371 test_snprintf(buf, buf2, i_vals[i1]); |
75b22d6f8a71
Simplify and improve integer sprintf() tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
372 } |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
373 } |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
374 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
375 |
331
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
376 if (test_set_start("printf() integer 64bit")) |
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
377 { |
333 | 378 int64_t i_vals64[] = { 0, -0, -1, 2, -2, 612342, -612342, 0x3342344341fff, 0x1f8000000, }; |
331
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
379 |
333 | 380 for (i1 = 0; i1 < NCOUNT(i_vals64); i1++) |
331
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
381 { |
395
bffd3caf2d2c
Rename TH_PRI{u,d,x}* macros to match with standard ISO C99 inttypes.h PRI*.
Matti Hamalainen <ccr@tnsp.org>
parents:
380
diff
changeset
|
382 snprintf(buf, sizeof(buf), "%" PRId64, i_vals64[i1]); |
331
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
383 |
340 | 384 for (i4 = 0; i4 < NCOUNT(i_mods); i4++) |
332 | 385 for (i3 = 0; i3 < NCOUNT(i_types); i3++) |
386 for (i2 = 0; i2 < NCOUNT(i_fmts); i2++) | |
331
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
387 { |
402 | 388 snprintf(buf2, sizeof(buf2), "%%%s%sll%s", i_mods[i4], i_fmts[i2], i_types[i3]); |
333 | 389 test_snprintf(buf, buf2, i_vals64[i1]); |
331
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
390 } |
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
391 } |
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
392 } |
6e42d50c08c4
Add tests for 64bit int prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
328
diff
changeset
|
393 |
296
ec8357d02a52
Add floating point tests (which will fail for now).
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
394 if (test_set_start("printf() float")) |
ec8357d02a52
Add floating point tests (which will fail for now).
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
395 { |
345 | 396 double f_vals[] = { 1, 2, 3, 2.02, 612342.234, -2.07, -612342.12, 437692.9876543219, 0x1fff, 0x8000000, 0.15625 }; |
404 | 397 char *f_fmts[] = { "%f", "%1.1f", "%8.5f", "%5f", "%-5f", "", "%-5.2f", "%08.5f" }; |
296
ec8357d02a52
Add floating point tests (which will fail for now).
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
398 |
327
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
399 for (i1 = 0; i1 < NCOUNT(f_vals); i1++) |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
400 { |
319 | 401 snprintf(buf, sizeof(buf), "%f", f_vals[i1]); |
327
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
402 for (i2 = 0; i2 < NCOUNT(f_fmts); i2++) |
319 | 403 test_snprintf(buf, f_fmts[i2], f_vals[i1]); |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
404 } |
296
ec8357d02a52
Add floating point tests (which will fail for now).
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
405 } |
ec8357d02a52
Add floating point tests (which will fail for now).
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
406 |
295 | 407 if (test_set_start("printf() string")) |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
408 { |
407 | 409 char *s_vals[] = { "", "XYZXYZ", "xxx yyy zzz ppp fff", NULL, "X", "abcde", "dx", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", }; |
301 | 410 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", "% 2s", "%03s", "% -12s", "% 03s", "%-.15s", "%.8s" }; |
265 | 411 |
327
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
412 for (i1 = 0; i1 < NCOUNT(s_vals); i1++) |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
413 { |
327
b0f9f806c8c9
Implement NCOUNT() macro for getting array element counts (sizeof(arr) / sizeof(arr[0]))
Matti Hamalainen <ccr@tnsp.org>
parents:
326
diff
changeset
|
414 for (i2 = 0; i2 < NCOUNT(s_fmts); i2++) |
319 | 415 test_snprintf(s_vals[i1], s_fmts[i2], s_vals[i1]); |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
416 } |
288 | 417 } |
418 | |
295 | 419 if (test_set_start("printf() char")) |
288 | 420 { |
319 | 421 const char c_val = 'x'; |
422 const char *c_msg = "x"; | |
352 | 423 char *c_fmts[] = { "a%cBC", "%c", "", "%0c", "%1c", "% c", "%-3c", "%3c", "%.3c", "%-.3c", "%-3.3c", "%.c", "%05c", "%-05c", }; |
328
0146aa9a9524
Simplify char printf tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
327
diff
changeset
|
424 |
0146aa9a9524
Simplify char printf tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
327
diff
changeset
|
425 for (i1 = 0; i1 < NCOUNT(c_fmts); i1++) |
0146aa9a9524
Simplify char printf tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
327
diff
changeset
|
426 test_snprintf(c_msg, c_fmts[i1], c_val); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
427 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
428 |
356
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
429 if (test_set_start("printf() pointers")) |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
430 { |
407 | 431 char *p_fmts[] = { "%p", "%2p", "%.2p", "%03p", "%04p", "%-3p", "%0.3p", "%8p", "%32p", "%032p", "%-32p", "%-032p", "%16.8p", "%016.8p" }; |
356
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
432 void *p_vals[] = { NULL, (void *) 1, &p_fmts, }; |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
433 |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
434 for (i1 = 0; i1 < NCOUNT(p_vals); i1++) |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
435 { |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
436 snprintf(buf, sizeof(buf), "%p", p_vals[i1]); |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
437 for (i2 = 0; i2 < NCOUNT(p_fmts); i2++) |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
438 test_snprintf(buf, p_fmts[i2], p_vals[i1]); |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
439 } |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
440 } |
d7f43fce0dce
Add tests for printing pointers (%p formatters).
Matti Hamalainen <ccr@tnsp.org>
parents:
352
diff
changeset
|
441 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
442 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
443 // String matching functions |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
444 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
445 if (test_set_start("String matching #1")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
446 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
447 TEST2(th_strcasecmp, "aSdFq", "asdfq", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
448 TEST2(th_strcasecmp, "aSdFq", "asFfq", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
449 TEST2(th_strcasecmp, "abcde", "abcde", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
450 TEST2(th_strcasecmp, "öäå", "öäå", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
451 TEST2(th_strcasecmp, "aöäå", "aöäå", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
452 } |
255 | 453 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
454 if (test_set_start("String matching #2")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
455 { |
347 | 456 TEST3(th_strncasecmp, "aSdFq", "asFfqB", 4, FALSE); |
457 TEST3(th_strncasecmp, "aSdFq", "asFfqQ", 2, TRUE); | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
458 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
459 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
460 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
461 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
462 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
463 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
464 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
465 if (test_set_start("String matching #3")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
466 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
467 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
468 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
469 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
470 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", FALSE); |
412 | 471 TEST2B(th_strmatch, "abba ABBAkukka lol", "*bba*", TRUE); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
472 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
473 TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
474 TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
475 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
476 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
477 if (test_set_start("String matching #4")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
478 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
479 TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
480 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", TRUE); |
412 | 481 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*ab?ak*", TRUE); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
482 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
483 TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
484 } |
266 | 485 |
265 | 486 // Tests that test for things that do not work correctly yet |
487 // Unicode / multibyte UTF-8 causes problems here | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
488 if (test_set_start("Invalid")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
489 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
490 TEST2(th_strcasecmp, "ÖÄÅ", "öäå", FALSE); // SHOULD match |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
491 TEST3(th_strncasecmp, "Aäöå", "aöå", 2, TRUE); // should NOT match |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
492 TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", FALSE); // should match |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
493 } |
266 | 494 |
255 | 495 // |
496 // Print summary and exit | |
497 // | |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
498 tprint(1, |
276
56b0de9f9d44
Improve tests output per verbosity level.
Matti Hamalainen <ccr@tnsp.org>
parents:
274
diff
changeset
|
499 "======================================================\n"); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
500 |
304
3fcf42cce43d
Fix some tests and use stdout for output.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
501 tprint(0, |
312 | 502 "%d tests failed, %d passed (%d main tests), %d test sets of %d sets total.\n\n", |
365 | 503 tests_failed, tests_passed, tests_total, sets_nenabled, sets_total); |
255 | 504 |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
505 return 0; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
506 } |