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