Mercurial > hg > th-libs
changeset 303:54ea7a73e5fa
Implement test contexts.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 22 Feb 2016 19:39:14 +0200 |
parents | 3f05f6fb5dfd |
children | 3fcf42cce43d |
files | tests.c |
diffstat | 1 files changed, 45 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/tests.c Mon Feb 22 18:43:46 2016 +0200 +++ b/tests.c Mon Feb 22 19:39:14 2016 +0200 @@ -9,10 +9,13 @@ #define SET_SENTINEL_BYTE 0x0e5 +typedef struct +{ + char *header, *res; +} test_ctx; + + // Globals -char *test_str_header = NULL, - *test_str_res = NULL; - int tests_failed, tests_passed, tests_total, tests_sets, tests_nenabled; int tests_enabled[SET_MAX_TESTS]; @@ -93,61 +96,67 @@ } -void test_start_v(const char *fmt, va_list ap) +void test_init(test_ctx *ctx) +{ + memset(ctx, 0, sizeof(test_ctx)); +} + + +void test_start_v(test_ctx *ctx, const char *fmt, va_list ap) { tests_total++; - th_free_r(&test_str_header); - th_free_r(&test_str_res); + th_free_r(&ctx->header); + th_free_r(&ctx->res); - test_str_header = th_strdup_vprintf(fmt, ap); + ctx->header = th_strdup_vprintf(fmt, ap); } -void test_start(const char *fmt, ...) +void test_start(test_ctx *ctx, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - test_start_v(fmt, ap); + test_start_v(ctx, fmt, ap); va_end(ap); } -void test_result_msg_v(BOOL check, const char *fmt, va_list ap) +void test_result_msg_v(test_ctx *ctx, BOOL check, const char *fmt, va_list ap) { if (check) { - THPRINT(2, "%s: OK\n", test_str_header); + THPRINT(2, "%s: OK\n", ctx->header); tests_passed++; } else { - THPRINT(0, "%s: FAIL\n", test_str_header); + THPRINT(0, "%s: FAIL\n", ctx->header); if (fmt != NULL) { THPRINT(0, " - "); THPRINT_V(0, fmt, ap); THPRINT(0, "\n"); } - if (test_str_res != NULL) - THPRINT(0, "%s\n", test_str_res); + if (ctx->res != NULL) + THPRINT(0, "%s\n", ctx->res); tests_failed++; } } -void test_result_msg(BOOL check, const char *fmt, ...) +void test_result_msg(test_ctx *ctx, BOOL check, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - test_result_msg_v(check, fmt, ap); + test_result_msg_v(ctx, check, fmt, ap); va_end(ap); } -void test_result(BOOL check) +void test_result(test_ctx *ctx, BOOL check) { - test_result_msg_v(check, NULL, NULL); + test_result_msg_v(ctx, check, NULL, NULL); } @@ -156,9 +165,11 @@ { int ret1, ret2; va_list tmp; + test_ctx ctx; // Test basic *printf() functionality - test_start("th_vsnprintf('%s', %s)", fmt, msg); + test_init(&ctx); + test_start(&ctx, "th_vsnprintf('%s', %s)", fmt, msg); memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0; memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0; @@ -168,16 +179,16 @@ ret1 = th_vsnprintf(buf1, len, fmt, ap); ret2 = vsnprintf(buf2, len, fmt, tmp); - test_result_msg(ret1 == ret2, "retval mismatch %d != %d", ret1, ret2); - test_result_msg(strcmp(buf1, buf2) == 0, "result mismatch '%s' != '%s'", buf1, buf2); + test_result_msg(&ctx, ret1 == ret2, "retval mismatch %d [th] != %d [libc]", ret1, ret2); + test_result_msg(&ctx, strcmp(buf1, buf2) == 0, "result mismatch '%s' [th] != '%s' [libc]", buf1, buf2); - test_result_msg((unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]); - test_result_msg((unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]); + test_result_msg(&ctx, (unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]); + test_result_msg(&ctx, (unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]); // Test th_strdup_vprintf() - test_start("th_strdup_vprintf('%s')", fmt); + test_start(&ctx, "th_strdup_vprintf('%s')", fmt); char *str = th_strdup_vprintf(fmt, ap); - test_result_msg(str != NULL, "result NULL"); + test_result_msg(&ctx, str != NULL, "result NULL"); th_free(str); } @@ -216,18 +227,21 @@ #define TEST2(fun, str1, str2, ret) do { \ - test_start(# fun "('%s', '%s')", str1, str2); \ - test_result(( fun (str1, str2) == 0) == ret); \ + test_ctx ctx; test_init(&ctx); \ + test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ + test_result(&ctx, ( fun (str1, str2) == 0) == ret); \ } while (0) #define TEST2B(fun, str1, str2, ret) do { \ - test_start(# fun "('%s', '%s')", str1, str2); \ - test_result( fun (str1, str2) == ret); \ + test_ctx ctx; test_init(&ctx); \ + test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ + test_result(&ctx, fun (str1, str2) == ret); \ } while (0) #define TEST3(fun, str1, str2, len, ret) do { \ - test_start(# fun "('%s', '%s', %d)", str1, str2, len); \ - test_result(( fun (str1, str2, len) == 0) == ret); \ + test_ctx ctx; test_init(&ctx); \ + test_start(&ctx, # fun "('%s', '%s', %d)", str1, str2, len); \ + test_result(&ctx, ( fun (str1, str2, len) == 0) == ret); \ } while (0)