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