Mercurial > hg > th-libs
comparison tests.c @ 319:f2af6049d958
Improve testing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 23 Feb 2016 01:39:49 +0200 |
parents | c532088b4f05 |
children | 75b22d6f8a71 |
comparison
equal
deleted
inserted
replaced
318:f8c385739571 | 319:f2af6049d958 |
---|---|
177 return check; | 177 return check; |
178 } | 178 } |
179 | 179 |
180 | 180 |
181 | 181 |
182 void test_snprintf_do(size_t len, const char *fmt, va_list ap, const char *msg) | 182 void test_snprintf_do(size_t len, const char *msg, const char *fmt, va_list ap) |
183 { | 183 { |
184 int ret1, ret2; | 184 int ret1, ret2; |
185 va_list tmp; | 185 va_list tmp; |
186 test_ctx ctx; | 186 test_ctx ctx; |
187 | 187 |
188 // Test basic *printf() functionality | 188 // Test basic *printf() functionality |
189 test_init(&ctx); | 189 test_init(&ctx); |
190 test_start(&ctx, "th_vsnprintf('%s', %s)", fmt, msg); | 190 test_start(&ctx, "th_vsnprintf(%" TH_PRIu_SIZE_T ", \"%s\", %s)", len, fmt, msg); |
191 | 191 |
192 memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0; | 192 memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0; |
193 memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0; | 193 memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0; |
194 | 194 |
195 va_copy(tmp, ap); | 195 va_copy(tmp, ap); |
209 test_result_msg(&ctx, str != NULL, "result NULL"); | 209 test_result_msg(&ctx, str != NULL, "result NULL"); |
210 th_free(str); | 210 th_free(str); |
211 } | 211 } |
212 | 212 |
213 | 213 |
214 void test_snprintf(const char *fmt, ...) | 214 void test_snprintf(const char *msg, const char *fmt, ...) |
215 { | 215 { |
216 va_list ap, tmp; | 216 va_list ap, tmp; |
217 va_start(ap, fmt); | 217 va_start(ap, fmt); |
218 va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0"); | 218 va_copy(tmp, ap); test_snprintf_do(0, msg, fmt, tmp); |
219 va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1"); | 219 va_copy(tmp, ap); test_snprintf_do(1, msg, fmt, tmp); |
220 va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2"); | 220 va_copy(tmp, ap); test_snprintf_do(2, msg, fmt, tmp); |
221 va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16"); | 221 va_copy(tmp, ap); test_snprintf_do(16, msg, fmt, tmp); |
222 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE"); | 222 va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, msg, fmt, tmp); |
223 va_end(ap); | 223 va_end(ap); |
224 tprint(2, | 224 tprint(2, |
225 "-----------------------------------------------------\n"); | 225 "-----------------------------------------------------\n"); |
226 } | 226 } |
227 | 227 |
265 | 265 |
266 | 266 |
267 int main(int argc, char *argv[]) | 267 int main(int argc, char *argv[]) |
268 { | 268 { |
269 size_t i1, i2; | 269 size_t i1, i2; |
270 char buf[64]; | |
270 | 271 |
271 // | 272 // |
272 // Initialization | 273 // Initialization |
273 // | 274 // |
274 th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL); | 275 th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL); |
300 int i_vals[] = { 0, -0, -1, 2, -2, 612342, -612342, 0x1fff, 0x8000000, }; | 301 int i_vals[] = { 0, -0, -1, 2, -2, 612342, -612342, 0x1fff, 0x8000000, }; |
301 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%.5d", "%05x", "%5x", "", "% 3d", "%+3d", "%05.5d", "%-8x", "%.x", }; | 302 char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%.5d", "%05x", "%5x", "", "% 3d", "%+3d", "%05.5d", "%-8x", "%.x", }; |
302 | 303 |
303 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) | 304 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) |
304 { | 305 { |
305 tprint(1, "Value %d\n", i_vals[i1]); | 306 snprintf(buf, sizeof(buf), "%d", i_vals[i1]); |
306 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) | 307 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) |
307 test_snprintf(i_fmts[i2], i_vals[i1]); | 308 test_snprintf(buf, i_fmts[i2], i_vals[i1]); |
308 } | 309 } |
309 } | 310 } |
310 | 311 |
311 if (test_set_start("printf() integer 2")) | 312 if (test_set_start("printf() integer 2")) |
312 { | 313 { |
313 int i_vals[] = { 0, -0, -1, 2, -2, 612342, -612342, 0x1fff, 0x8000000, }; | 314 int i_vals[] = { 0, -0, -1, 2, -2, 612342, -612342, 0x1fff, 0x8000000, }; |
314 char *i_fmts[] = { "% 3o", "%+3o", "%3o", "%.3o", "%3.3o", "%-4.2o", "%-4o", "%.o" }; | 315 char *i_fmts[] = { "% 3o", "%+3o", "%3o", "%.3o", "%3.3o", "%-4.2o", "%-4o", "%.o" }; |
315 | 316 |
316 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) | 317 for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) |
317 { | 318 { |
318 tprint(1, "Value %d\n", i_vals[i1]); | 319 snprintf(buf, sizeof(buf), "%d", i_vals[i1]); |
319 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) | 320 for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) |
320 test_snprintf(i_fmts[i2], i_vals[i1]); | 321 test_snprintf(buf, i_fmts[i2], i_vals[i1]); |
321 } | 322 } |
322 } | 323 } |
323 | 324 |
324 if (test_set_start("printf() float")) | 325 if (test_set_start("printf() float")) |
325 { | 326 { |
326 double f_vals[] = { 2.02, 612342.234, -2.07, -612342.12, 437692.9876543219, 0x1fff, 0x8000000, }; | 327 double f_vals[] = { 2.02, 612342.234, -2.07, -612342.12, 437692.9876543219, 0x1fff, 0x8000000, }; |
327 char *f_fmts[] = { "%f", "%1.1f", "%5.5f", "%5f", "%-5f", "", "%-2.2f", "%05.5f" }; | 328 char *f_fmts[] = { "%f", "%1.1f", "%5.5f", "%5f", "%-5f", "", "%-2.2f", "%05.5f" }; |
328 | 329 |
329 for (i1 = 0; i1 < sizeof(f_vals) / sizeof(f_vals[0]); i1++) | 330 for (i1 = 0; i1 < sizeof(f_vals) / sizeof(f_vals[0]); i1++) |
330 { | 331 { |
331 tprint(1, "Value %f\n", f_vals[i1]); | 332 snprintf(buf, sizeof(buf), "%f", f_vals[i1]); |
332 for (i2 = 0; i2 < sizeof(f_fmts) / sizeof(f_fmts[0]); i2++) | 333 for (i2 = 0; i2 < sizeof(f_fmts) / sizeof(f_fmts[0]); i2++) |
333 test_snprintf(f_fmts[i2], f_vals[i1]); | 334 test_snprintf(buf, f_fmts[i2], f_vals[i1]); |
334 } | 335 } |
335 } | 336 } |
336 | 337 |
337 if (test_set_start("printf() string")) | 338 if (test_set_start("printf() string")) |
338 { | 339 { |
339 char *s_vals[] = { "", "XYZXYZ", "xxx yyy zzz ppp fff", NULL, "X", "abcde", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", }; | 340 char *s_vals[] = { "", "XYZXYZ", "xxx yyy zzz ppp fff", NULL, "X", "abcde", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", }; |
340 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", "% 2s", "%03s", "% -12s", "% 03s", "%-.15s", "%.8s" }; | 341 char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", "%1s", "%-1s", "% 2s", "%03s", "% -12s", "% 03s", "%-.15s", "%.8s" }; |
341 | 342 |
342 for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) | 343 for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) |
343 { | 344 { |
344 tprint(1, "Value '%s'\n", s_vals[i1]); | |
345 for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) | 345 for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) |
346 test_snprintf(s_fmts[i2], s_vals[i1]); | 346 test_snprintf(s_vals[i1], s_fmts[i2], s_vals[i1]); |
347 } | 347 } |
348 } | 348 } |
349 | 349 |
350 if (test_set_start("printf() char")) | 350 if (test_set_start("printf() char")) |
351 { | 351 { |
352 test_snprintf("a%cBC", 'x'); | 352 const char c_val = 'x'; |
353 test_snprintf("%c", 'x'); | 353 const char *c_msg = "x"; |
354 test_snprintf("", 'x'); | 354 test_snprintf(c_msg, "a%cBC", c_val); |
355 test_snprintf("%0c", 'x'); | 355 test_snprintf(c_msg, "%c", c_val); |
356 test_snprintf("%1c", 'x'); | 356 test_snprintf(c_msg, "", c_val); |
357 test_snprintf("% c", 'x'); | 357 test_snprintf(c_msg, "%0c", c_val); |
358 test_snprintf("%-3c", 'x'); | 358 test_snprintf(c_msg, "%1c", c_val); |
359 test_snprintf("%3c", 'x'); | 359 test_snprintf(c_msg, "% c", c_val); |
360 test_snprintf("%.3c", 'x'); | 360 test_snprintf(c_msg, "%-3c", c_val); |
361 test_snprintf("%-.3c", 'x'); | 361 test_snprintf(c_msg, "%3c", c_val); |
362 test_snprintf(c_msg, "%.3c", c_val); | |
363 test_snprintf(c_msg, "%-.3c", c_val); | |
362 } | 364 } |
363 | 365 |
364 // | 366 // |
365 // String matching functions | 367 // String matching functions |
366 // | 368 // |