From a9c9fc392bf2c3803251e5f63c944cd221a1ca67 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 12 Jan 2024 12:01:07 -0500 Subject: tests: Merge unit test executables into one --- tests/alloc.c | 6 ++-- tests/bfstd.c | 5 +-- tests/bit.c | 5 +-- tests/main.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.h | 26 ++++++++++++++ tests/trie.c | 5 +-- tests/xtime.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/xtimegm.c | 94 ------------------------------------------------- 8 files changed, 239 insertions(+), 103 deletions(-) create mode 100644 tests/main.c create mode 100644 tests/tests.h create mode 100644 tests/xtime.c delete mode 100644 tests/xtimegm.c (limited to 'tests') diff --git a/tests/alloc.c b/tests/alloc.c index 37b70bf..e14f131 100644 --- a/tests/alloc.c +++ b/tests/alloc.c @@ -1,13 +1,14 @@ // Copyright © Tavian Barnes // SPDX-License-Identifier: 0BSD +#include "tests.h" #include "../src/alloc.h" #include "../src/diag.h" #include #include #include -int main(void) { +bool check_alloc(void) { // Check sizeof_flex() struct flexible { alignas(64) int foo[8]; @@ -44,6 +45,5 @@ int main(void) { } varena_destroy(&varena); - - return EXIT_SUCCESS; + return true; } diff --git a/tests/bfstd.c b/tests/bfstd.c index c386279..d385c6b 100644 --- a/tests/bfstd.c +++ b/tests/bfstd.c @@ -1,6 +1,7 @@ // Copyright © Tavian Barnes // SPDX-License-Identifier: 0BSD +#include "tests.h" #include "../src/bfstd.h" #include "../src/config.h" #include "../src/diag.h" @@ -34,7 +35,7 @@ static void check_wordesc(const char *str, const char *exp, enum wesc_flags flag bfs_verify(strcmp(buf, exp) == 0, "wordesc(%s) == %s (!= %s)", str, buf, exp); } -int main(void) { +bool check_bfstd(void) { // Try to set a UTF-8 locale if (!setlocale(LC_ALL, "C.UTF-8")) { setlocale(LC_ALL, ""); @@ -69,5 +70,5 @@ int main(void) { check_wordesc("\xF0\x9F\x98\x80", "\xF0\x9F\x98\x80", WESC_SHELL | WESC_TTY); } - return EXIT_SUCCESS; + return true; } diff --git a/tests/bit.c b/tests/bit.c index f9071be..1c6a4de 100644 --- a/tests/bit.c +++ b/tests/bit.c @@ -1,6 +1,7 @@ // Copyright © Tavian Barnes // SPDX-License-Identifier: 0BSD +#include "tests.h" #include "../src/bit.h" #include "../src/diag.h" #include @@ -54,7 +55,7 @@ bfs_static_assert(INTMAX_MAX == IWIDTH_MAX(INTMAX_WIDTH)); #define verify_eq(a, b) \ bfs_verify((a) == (b), "(0x%jX) %s != %s (0x%jX)", (uintmax_t)(a), #a, #b, (uintmax_t)(b)) -int main(void) { +bool check_bit(void) { verify_eq(bswap((uint8_t)0x12), 0x12); verify_eq(bswap((uint16_t)0x1234), 0x3412); verify_eq(bswap((uint32_t)0x12345678), 0x78563412); @@ -121,5 +122,5 @@ int main(void) { bfs_verify(!has_single_bit(UINT32_MAX)); bfs_verify(has_single_bit((uint32_t)1 << (UINT_WIDTH - 1))); - return EXIT_SUCCESS; + return true; } diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..b7292b4 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,106 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +/** + * Entry point for unit tests. + */ + +#include "tests.h" +#include "../src/bfstd.h" +#include "../src/color.h" +#include "../src/config.h" +#include "../src/diag.h" +#include +#include +#include +#include + +/** + * Test context. + */ +struct test_ctx { + /** Number of command line arguments. */ + int argc; + /** The arguments themselves. */ + char **argv; + + /** Parsed colors. */ + struct colors *colors; + /** Colorized output stream. */ + CFILE *cout; + + /** Eventual exit status. */ + int ret; +}; + +/** Initialize the test context. */ +static int test_init(struct test_ctx *ctx, int argc, char **argv) { + ctx->argc = argc; + ctx->argv = argv; + + ctx->colors = parse_colors(); + ctx->cout = cfwrap(stdout, ctx->colors, false); + if (!ctx->cout) { + ctx->ret = EXIT_FAILURE; + return -1; + } + + ctx->ret = EXIT_SUCCESS; + return 0; +} + +/** Finalize the test context. */ +static int test_fini(struct test_ctx *ctx) { + if (ctx->cout) { + cfclose(ctx->cout); + } + + free_colors(ctx->colors); + + return ctx->ret; +} + +/** Check if a test case is enabled for this run. */ +static bool should_run(const struct test_ctx *ctx, const char *test) { + // Run all tests by default + if (ctx->argc < 2) { + return true; + } + + // With args, run only specified tests + for (int i = 1; i < ctx->argc; ++i) { + if (strcmp(test, ctx->argv[i]) == 0) { + return true; + } + } + + return false; +} + +/** Run a test if it's enabled. */ +static void run_test(struct test_ctx *ctx, const char *test, test_fn *fn) { + if (should_run(ctx, test)) { + if (fn()) { + cfprintf(ctx->cout, "${grn}[PASS]${rs} ${bld}%s${rs}\n", test); + } else { + cfprintf(ctx->cout, "${red}[FAIL]${rs} ${bld}%s${rs}\n", test); + ctx->ret = EXIT_FAILURE; + } + } +} + +int main(int argc, char *argv[]) { + struct test_ctx ctx; + if (test_init(&ctx, argc, argv) != 0) { + goto done; + } + + run_test(&ctx, "alloc", check_alloc); + run_test(&ctx, "bfstd", check_bfstd); + run_test(&ctx, "bit", check_bit); + run_test(&ctx, "trie", check_trie); + run_test(&ctx, "xtime", check_xtime); + +done: + return test_fini(&ctx); +} diff --git a/tests/tests.h b/tests/tests.h new file mode 100644 index 0000000..34c58b7 --- /dev/null +++ b/tests/tests.h @@ -0,0 +1,26 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +/** + * Unit tests. + */ + +#include "../src/config.h" + +/** Unit test function type. */ +typedef bool test_fn(void); + +/** Memory allocation tests. */ +bool check_alloc(void); + +/** Standard library wrapper tests. */ +bool check_bfstd(void); + +/** Bit manipulation tests. */ +bool check_bit(void); + +/** Trie tests. */ +bool check_trie(void); + +/** Time tests. */ +bool check_xtime(void); diff --git a/tests/trie.c b/tests/trie.c index 656fd85..f94d7c8 100644 --- a/tests/trie.c +++ b/tests/trie.c @@ -1,6 +1,7 @@ // Copyright © Tavian Barnes // SPDX-License-Identifier: 0BSD +#include "tests.h" #include "../src/trie.h" #include "../src/config.h" #include "../src/diag.h" @@ -38,7 +39,7 @@ const char *keys[] = { const size_t nkeys = countof(keys); -int main(void) { +bool check_trie(void) { struct trie trie; trie_init(&trie); @@ -130,5 +131,5 @@ int main(void) { free(longstr); trie_destroy(&trie); - return EXIT_SUCCESS; + return true; } diff --git a/tests/xtime.c b/tests/xtime.c new file mode 100644 index 0000000..53ecbc4 --- /dev/null +++ b/tests/xtime.c @@ -0,0 +1,95 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include "tests.h" +#include "../src/xtime.h" +#include "../src/config.h" +#include +#include +#include +#include + +static bool tm_equal(const struct tm *tma, const struct tm *tmb) { + if (tma->tm_year != tmb->tm_year) { + return false; + } + if (tma->tm_mon != tmb->tm_mon) { + return false; + } + if (tma->tm_mday != tmb->tm_mday) { + return false; + } + if (tma->tm_hour != tmb->tm_hour) { + return false; + } + if (tma->tm_min != tmb->tm_min) { + return false; + } + if (tma->tm_sec != tmb->tm_sec) { + return false; + } + if (tma->tm_wday != tmb->tm_wday) { + return false; + } + if (tma->tm_yday != tmb->tm_yday) { + return false; + } + if (tma->tm_isdst != tmb->tm_isdst) { + return false; + } + + return true; +} + +static void tm_print(FILE *file, const struct tm *tm) { + fprintf(file, "Y%d M%d D%d h%d m%d s%d wd%d yd%d%s\n", + tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec, + tm->tm_wday, tm->tm_yday, + tm->tm_isdst ? (tm->tm_isdst < 0 ? " (DST?)" : " (DST)") : ""); +} + +bool check_xtime(void) { + if (setenv("TZ", "UTC0", true) != 0) { + perror("setenv()"); + return false; + } + + struct tm tm = { + .tm_isdst = -1, + }; + + for (tm.tm_year = 10; tm.tm_year <= 200; tm.tm_year += 10) + for (tm.tm_mon = -3; tm.tm_mon <= 15; tm.tm_mon += 3) + for (tm.tm_mday = -31; tm.tm_mday <= 61; tm.tm_mday += 4) + for (tm.tm_hour = -1; tm.tm_hour <= 24; tm.tm_hour += 5) + for (tm.tm_min = -1; tm.tm_min <= 60; tm.tm_min += 31) + for (tm.tm_sec = -60; tm.tm_sec <= 120; tm.tm_sec += 5) { + struct tm tma = tm, tmb = tm; + time_t ta, tb; + ta = mktime(&tma); + if (xtimegm(&tmb, &tb) != 0) { + tb = -1; + } + + bool fail = false; + if (ta != tb) { + printf("Mismatch: %jd != %jd\n", (intmax_t)ta, (intmax_t)tb); + fail = true; + } + if (ta != -1 && !tm_equal(&tma, &tmb)) { + printf("mktime(): "); + tm_print(stdout, &tma); + printf("xtimegm(): "); + tm_print(stdout, &tmb); + fail = true; + } + if (fail) { + printf("Input: "); + tm_print(stdout, &tm); + return false; + } + } + + return true; +} diff --git a/tests/xtimegm.c b/tests/xtimegm.c deleted file mode 100644 index 973b2eb..0000000 --- a/tests/xtimegm.c +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright © Tavian Barnes -// SPDX-License-Identifier: 0BSD - -#include "../src/xtime.h" -#include "../src/config.h" -#include -#include -#include -#include - -static bool tm_equal(const struct tm *tma, const struct tm *tmb) { - if (tma->tm_year != tmb->tm_year) { - return false; - } - if (tma->tm_mon != tmb->tm_mon) { - return false; - } - if (tma->tm_mday != tmb->tm_mday) { - return false; - } - if (tma->tm_hour != tmb->tm_hour) { - return false; - } - if (tma->tm_min != tmb->tm_min) { - return false; - } - if (tma->tm_sec != tmb->tm_sec) { - return false; - } - if (tma->tm_wday != tmb->tm_wday) { - return false; - } - if (tma->tm_yday != tmb->tm_yday) { - return false; - } - if (tma->tm_isdst != tmb->tm_isdst) { - return false; - } - - return true; -} - -static void tm_print(FILE *file, const struct tm *tm) { - fprintf(file, "Y%d M%d D%d h%d m%d s%d wd%d yd%d%s\n", - tm->tm_year, tm->tm_mon, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, - tm->tm_wday, tm->tm_yday, - tm->tm_isdst ? (tm->tm_isdst < 0 ? " (DST?)" : " (DST)") : ""); -} - -int main(void) { - if (setenv("TZ", "UTC0", true) != 0) { - perror("setenv()"); - return EXIT_FAILURE; - } - - struct tm tm = { - .tm_isdst = -1, - }; - - for (tm.tm_year = 10; tm.tm_year <= 200; tm.tm_year += 10) - for (tm.tm_mon = -3; tm.tm_mon <= 15; tm.tm_mon += 3) - for (tm.tm_mday = -31; tm.tm_mday <= 61; tm.tm_mday += 4) - for (tm.tm_hour = -1; tm.tm_hour <= 24; tm.tm_hour += 5) - for (tm.tm_min = -1; tm.tm_min <= 60; tm.tm_min += 31) - for (tm.tm_sec = -60; tm.tm_sec <= 120; tm.tm_sec += 5) { - struct tm tma = tm, tmb = tm; - time_t ta, tb; - ta = mktime(&tma); - if (xtimegm(&tmb, &tb) != 0) { - tb = -1; - } - - bool fail = false; - if (ta != tb) { - printf("Mismatch: %jd != %jd\n", (intmax_t)ta, (intmax_t)tb); - fail = true; - } - if (ta != -1 && !tm_equal(&tma, &tmb)) { - printf("mktime(): "); - tm_print(stdout, &tma); - printf("xtimegm(): "); - tm_print(stdout, &tmb); - fail = true; - } - if (fail) { - printf("Input: "); - tm_print(stdout, &tm); - return EXIT_FAILURE; - } - } - - return EXIT_SUCCESS; -} -- cgit v1.2.3