Mercurial > hg > th-libs
annotate tests.c @ 288:89c7696acb82
Add some tests.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 22 Feb 2016 14:28:31 +0200 |
parents | f0cb48b34463 |
children | 7122026b0ece |
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 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #define SET_BUF_SIZE 128 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
8 #define SET_MAX_TESTS 64 |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
259 | 10 char *test_str_header = NULL, |
11 *test_str_res = NULL; | |
12 | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
13 int tests_failed, tests_passed, tests_total, tests_sets, tests_nenabled; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
14 int tests_enabled[SET_MAX_TESTS]; |
259 | 15 |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 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
|
17 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
19 // Define option arguments |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
20 static const th_optarg_t arg_opts[] = |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
21 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
22 { 0, '?', "help", "Show this help", OPT_NONE }, |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
23 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
24 { 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
|
25 }; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
26 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
27 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
|
28 |
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 void arg_show_help(void) |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
31 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
32 th_print_banner(stdout, th_prog_name, "[options]"); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
33 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
|
34 } |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
35 |
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 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
|
38 { |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
39 switch (optN) |
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 case 0: |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
42 arg_show_help(); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
43 exit(0); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
44 break; |
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 case 1: |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
47 th_verbosityLevel++; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
48 break; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
49 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
50 case 2: |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
51 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
52 BOOL ret = TRUE; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
53 char *pos, *pstr, *next; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
54 pos = pstr = th_strdup(optArg); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
55 memset(tests_enabled, 0, sizeof(tests_enabled)); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
56 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
57 do { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
58 next = strchr(pos, ','); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
59 if (next != NULL) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
60 *next = 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
61 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
62 char *tmp = th_strdup_trim(pos, TH_TRIM_BOTH); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
63 if (tmp != NULL) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
64 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
65 int val = atoi(tmp); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
66 if (val > 0 && val <= SET_MAX_TESTS) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
67 tests_enabled[val] = 1; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
68 else |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
69 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
70 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
|
71 ret = FALSE; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
72 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
73 th_free(tmp); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
74 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
75 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
76 if (next != NULL) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
77 pos = next + 1; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
78 } while (next != NULL); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
79 th_free(pstr); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
80 return ret; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
81 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
82 break; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
83 |
274
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
84 default: |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
85 THERR("Unknown option '%s'.\n", currArg); |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
86 return FALSE; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
87 } |
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 return TRUE; |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
90 } |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
91 |
f875db8634b6
Implement commandline options for test driver.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
92 |
259 | 93 void test_start_v(const char *fmt, va_list ap) |
94 { | |
95 tests_total++; | |
96 | |
97 th_free_r(&test_str_header); | |
98 th_free_r(&test_str_res); | |
99 | |
100 test_str_header = th_strdup_vprintf(fmt, ap); | |
101 } | |
102 | |
103 | |
104 void test_start(const char *fmt, ...) | |
105 { | |
106 va_list ap; | |
107 va_start(ap, fmt); | |
108 test_start_v(fmt, ap); | |
109 va_end(ap); | |
110 } | |
111 | |
112 | |
113 void test_result_msg_v(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
|
114 { |
259 | 115 if (check) |
116 { | |
276
56b0de9f9d44
Improve tests output per verbosity level.
Matti Hamalainen <ccr@tnsp.org>
parents:
274
diff
changeset
|
117 THPRINT(2, "%s: OK\n", test_str_header); |
259 | 118 tests_passed++; |
119 } | |
120 else | |
121 { | |
263 | 122 THPRINT(0, "%s: FAIL\n", test_str_header); |
259 | 123 if (fmt != NULL) |
263 | 124 { |
125 THPRINT(0, " - "); | |
259 | 126 THPRINT_V(0, fmt, ap); |
263 | 127 THPRINT(0, "\n"); |
128 } | |
129 if (test_str_res != NULL) | |
130 THPRINT(0, "%s\n", test_str_res); | |
259 | 131 tests_failed++; |
132 } | |
133 } | |
134 | |
135 | |
136 void test_result_msg(BOOL check, const char *fmt, ...) | |
137 { | |
138 va_list ap; | |
139 va_start(ap, fmt); | |
140 test_result_msg_v(check, fmt, ap); | |
141 va_end(ap); | |
142 } | |
143 | |
144 | |
145 void test_result(BOOL check) | |
146 { | |
147 test_result_msg_v(check, NULL, NULL); | |
148 } | |
149 | |
150 | |
151 #define SET_SENTINEL_BYTE 0x0e5 | |
152 | |
153 void test_snprintf_do(size_t len, const char *fmt, va_list ap, const char *msg) | |
154 { | |
155 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
|
156 va_list tmp; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 |
263 | 158 // Test basic *printf() functionality |
259 | 159 test_start("th_vsnprintf('%s', %s)", fmt, msg); |
160 | |
161 memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0; | |
162 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
|
163 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 va_copy(tmp, ap); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 ret1 = th_vsnprintf(buf1, len, fmt, ap); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 ret2 = vsnprintf(buf2, len, fmt, tmp); |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 |
259 | 169 test_result_msg(ret1 == ret2, "retval mismatch %d != %d", ret1, ret2); |
170 test_result_msg(strcmp(buf1, buf2) == 0, "result mismatch '%s' != '%s'", buf1, buf2); | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
171 |
259 | 172 test_result_msg((unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]); |
173 test_result_msg((unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]); | |
261 | 174 |
263 | 175 // Test th_strdup_vprintf() |
261 | 176 test_start("th_strdup_vprintf('%s')", fmt); |
177 char *str = th_strdup_vprintf(fmt, ap); | |
178 test_result_msg(str != NULL, "result NULL"); | |
179 th_free(str); | |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 void test_snprintf(const char *fmt, ...) |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 { |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 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
|
186 va_start(ap, fmt); |
259 | 187 va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0"); |
188 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1"); | |
189 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2"); | |
190 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16"); | |
191 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE"); | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
192 va_end(ap); |
276
56b0de9f9d44
Improve tests output per verbosity level.
Matti Hamalainen <ccr@tnsp.org>
parents:
274
diff
changeset
|
193 THPRINT(2, "----------------------------------------------\n"); |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 } |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
197 BOOL test_set_start(const char *str) |
264
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
198 { |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
199 if (tests_enabled[tests_sets++]) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
200 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
201 tests_nenabled++; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
202 THPRINT(1, |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
203 "======================================================\n" |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
204 " Set #%d : %s tests\n" |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
205 "======================================================\n", |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
206 tests_sets, str); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
207 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
208 return TRUE; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
209 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
210 else |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
211 return FALSE; |
264
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
212 } |
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
213 |
9b6fd2953d8c
Add test header printing function.
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
214 |
263 | 215 #define TEST2(fun, str1, str2, ret) do { \ |
216 test_start(# fun "('%s', '%s')", str1, str2); \ | |
217 test_result(( fun (str1, str2) == 0) == ret); \ | |
218 } while (0) | |
219 | |
266 | 220 #define TEST2B(fun, str1, str2, ret) do { \ |
221 test_start(# fun "('%s', '%s')", str1, str2); \ | |
222 test_result( fun (str1, str2) == ret); \ | |
223 } while (0) | |
224 | |
263 | 225 #define TEST3(fun, str1, str2, len, ret) do { \ |
226 test_start(# fun "('%s', '%s', %d)", str1, str2, len); \ | |
227 test_result(( fun (str1, str2, len) == 0) == ret); \ | |
228 } while (0) | |
229 | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
230 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
231 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
232 int main(int argc, char *argv[]) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
233 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
234 size_t i1, i2; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
235 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
236 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
237 // Initialization |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
238 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
239 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
|
240 th_verbosityLevel = 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
241 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
242 if (sizeof(char) != sizeof(unsigned char)) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
243 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
244 THERR("sizeof(char) != sizeof(unsigned char)???\n"); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
245 return -1; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
246 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
247 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
248 tests_failed = tests_passed = tests_total = tests_sets = tests_nenabled = 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
249 for (i1 = 0; i1 < SET_MAX_TESTS; i1++) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
250 tests_enabled[i1] = 1; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
251 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
252 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
253 // Parse command line arguments |
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 if (!th_args_process(argc, argv, arg_opts, arg_nopts, |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
256 arg_handle_opt, NULL, 0)) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
257 return 0; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
258 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
259 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
260 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
261 // Test series #1 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
262 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
263 if (test_set_start("printf() function family #1")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
264 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
265 int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, }; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
266 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", "", }; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
267 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
268 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
269 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
270 test_snprintf(i_fmts[i2], i_vals); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
271 } |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
272 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
273 if (test_set_start("printf() function family #2")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
274 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
275 char *s_vals[] = { "", "asdf", "xxx yyy zzz ppp fff", NULL, "X", "abcde", }; |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
276 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", }; |
265 | 277 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
278 for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
279 for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
280 test_snprintf(s_fmts[i2], s_vals); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
281 |
288 | 282 } |
283 | |
284 if (test_set_start("printf() function family #3")) | |
285 { | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
286 test_snprintf("a%cBC", 'x'); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
287 test_snprintf("%c", 'x'); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
288 test_snprintf("", 'x'); |
288 | 289 test_snprintf("%0c", 'x'); |
290 test_snprintf("%1c", 'x'); | |
291 test_snprintf("% c", 'x'); | |
292 test_snprintf("%-3c", 'x'); | |
293 test_snprintf("%3c", 'x'); | |
282
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 |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
296 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
297 // String matching functions |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
298 // |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
299 if (test_set_start("String matching #1")) |
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 TEST2(th_strcasecmp, "aSdFq", "asdfq", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
302 TEST2(th_strcasecmp, "aSdFq", "asFfq", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
303 TEST2(th_strcasecmp, "abcde", "abcde", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
304 TEST2(th_strcasecmp, "öäå", "öäå", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
305 TEST2(th_strcasecmp, "aöäå", "aöäå", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
306 } |
255 | 307 |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
308 if (test_set_start("String matching #2")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
309 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
310 TEST3(th_strncasecmp, "aSdFq", "asFfq", 4, FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
311 TEST3(th_strncasecmp, "aSdFq", "asFfq", 2, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
312 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
313 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
314 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
315 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
316 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
317 } |
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 (test_set_start("String matching #3")) |
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 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
322 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
323 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
324 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
325 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
326 TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
327 TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
328 } |
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 if (test_set_start("String matching #4")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
331 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
332 TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
333 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
334 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", FALSE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
335 TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", TRUE); |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
336 } |
266 | 337 |
265 | 338 // Tests that test for things that do not work correctly yet |
339 // Unicode / multibyte UTF-8 causes problems here | |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
340 if (test_set_start("Invalid")) |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
341 { |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
342 TEST2(th_strcasecmp, "ÖÄÅ", "öäå", FALSE); // SHOULD match |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
343 TEST3(th_strncasecmp, "Aäöå", "aöå", 2, TRUE); // should NOT match |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
344 TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", FALSE); // should match |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
345 } |
266 | 346 |
255 | 347 // |
348 // Print summary and exit | |
349 // | |
276
56b0de9f9d44
Improve tests output per verbosity level.
Matti Hamalainen <ccr@tnsp.org>
parents:
274
diff
changeset
|
350 THPRINT(1, |
56b0de9f9d44
Improve tests output per verbosity level.
Matti Hamalainen <ccr@tnsp.org>
parents:
274
diff
changeset
|
351 "======================================================\n"); |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
352 |
272 | 353 THPRINT(0, |
282
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
354 "%d tests failed, %d passed (%d main tests), %d test sets of %d sets total.\n", |
f0cb48b34463
Implement selectable tests.
Matti Hamalainen <ccr@tnsp.org>
parents:
276
diff
changeset
|
355 tests_failed, tests_passed, tests_total, tests_nenabled, tests_sets); |
255 | 356 |
254
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
357 return 0; |
3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
358 } |