From 3a9fc7e0a4171703ca4ba94eee270f03fb61beef Mon Sep 17 00:00:00 2001 From: Quinn Date: Fri, 13 Jun 2025 00:51:53 +0200 Subject: [PATCH] update testing framework --- test/dat.h | 34 ++++++++++++++++++++++++++++++++++ test/main.c | 51 +-------------------------------------------------- test/t_util.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test/test.h | 11 ++++++++--- 4 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 test/dat.h create mode 100644 test/t_util.h diff --git a/test/dat.h b/test/dat.h new file mode 100644 index 0000000..d728094 --- /dev/null +++ b/test/dat.h @@ -0,0 +1,34 @@ +#include +#include + +#include "../src/util/conf.h" +#include "../src/util/vec/float3.h" +#include "t_conf.h" +#include "t_util.h" +#include "test.h" + +testdat tests[] = { + {"", test_float3_norm, &(float3){2.0F, 0.67F, 5.0F} }, + {"", test_float3_norm, &(float3){0.2F, 0.4F, 0.1F} }, + {"", test_compat_endianess, (uint32_t[]){0x15F59267} }, + {"k=v", test_procbuf, &(struct test_procbuf){"key=val", "key", "val", 0} }, + {"sometxt", test_procbuf, &(struct test_procbuf){"sometxt", "sometxt", "", CONF_ESYNTAX}}, + {"comment", test_procbuf, &(struct test_procbuf){"# comment", "", "", CONF_ENODAT} }, + {"empty", test_procbuf, &(struct test_procbuf){"", "", "", CONF_ENODAT} }, + {"LF", test_procbuf, &(struct test_procbuf){"\n", "", "", CONF_ENODAT} }, + {"CRLF", test_procbuf, &(struct test_procbuf){"\r\n", "", "", CONF_ENODAT} }, + {"k=v (LF)", test_procbuf, &(struct test_procbuf){"k=v\na", "k", "v", 0} }, + {"k=v (CRLF)", test_procbuf, &(struct test_procbuf){"k=v\r\na", "k", "v", 0} }, + {"get", test_matchopt, &(struct test_matchopt){"key3", 2} }, + {"invalid", test_matchopt, &(struct test_matchopt){"nono", -1} }, + {"", test_colour32_endianess, NULL }, + {"", test_procval_i32, NULL }, + {"", test_procval_u64, NULL }, + {"", test_procval_f32, NULL }, + {"", test_procval_str, NULL }, + {"", test_procval_str_predef, NULL }, + {"", test_procval_fstr, NULL }, + {"", test_procval_fstr_trunc, NULL }, + {"", test_procval_eparse, NULL }, + {"", test_getpat, NULL }, +}; diff --git a/test/main.c b/test/main.c index 52210b8..7956fee 100644 --- a/test/main.c +++ b/test/main.c @@ -4,63 +4,14 @@ #include #undef GLAD_GL_IMPLEMENTATION -#include #include #include #include -#include "../src/util/colour32.h" -#include "../src/util/compat/endian.h" -#include "../src/util/vec/float3.h" +#include "dat.h" // contains the test data #include "test.h" -static int test_float3_norm(void* d) { - float* arg = d; - float3 v = {arg[0], arg[1], arg[2]}; - float3 r = float3_norm(v); - float n = r.x * r.x + r.y * r.y + r.z * r.z; - - // check if the value is within 1 millionth of the one we expect - return assert_true(fabsf(n - 1.0F) < 1e-6F); -} - -static int test_colour32_endianess(void* d) { - (void)d; - colour32 c = {0xFF000000}; // setting just the red channel - return assert_true(c.ch.r == 0xFF); -} - -static int test_compat_endianess(void* d) { - uint32_t x = *((uint32_t*)d + 0); - int res = 0; - -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - res |= assert_false(be32ton(x) == x); - res |= assert_false(ntobe32(x) == x); - res |= assert_true(be32ton(ntobe32(x)) == x); - res |= assert_true(le32ton(x) == x); - res |= assert_true(ntole32(x) == x); -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - res |= assert_false(le32ton(x) == x); - res |= assert_false(ntole32(x) == x); - res |= assert_true(le32ton(ntole32(x)) == x); - res |= assert_true(be32ton(x) == x); - res |= assert_true(ntobe32(x) == x); -#else - res |= assert_false("platform unsupported!!"); -#endif - return res; -} - int main(void) { - // tests that should be performed - testdat tests[] = { - {&test_float3_norm, &(float3){2.0F, 0.67F, 5.0F}}, - {&test_float3_norm, &(float3){0.2F, 0.4F, 0.1F} }, - {&test_colour32_endianess, (uint32_t[]){0, 1} }, - {&test_compat_endianess, (uint32_t[]){0x15F59267} } - }; - // get test count size_t n = sizeof(tests) / sizeof(tests[0]); diff --git a/test/t_util.h b/test/t_util.h new file mode 100644 index 0000000..f3e7ba2 --- /dev/null +++ b/test/t_util.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include + +#include "../src/util/colour32.h" +#include "../src/util/compat/endian.h" +#include "../src/util/vec/float3.h" +#include "test.h" + +int test_float3_norm(void* d) { + float* arg = d; + float3 v = {arg[0], arg[1], arg[2]}; + float3 r = float3_norm(v); + float n = r.x * r.x + r.y * r.y + r.z * r.z; + + // check if the value is within 1 millionth of the one we expect + return assert_true(fabsf(n - 1.0F) < 1e-6F); +} + +int test_colour32_endianess(void* d) { + (void)d; + colour32 c = {0xFF000000}; // setting just the red channel + return assert_true(c.ch.r == 0xFF); +} + +int test_compat_endianess(void* d) { + uint32_t x = *((uint32_t*)d + 0); + int res = 0; + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + res |= assert_false(be32ton(x) == x); + res |= assert_false(ntobe32(x) == x); + res |= assert_true(be32ton(ntobe32(x)) == x); + res |= assert_true(le32ton(x) == x); + res |= assert_true(ntole32(x) == x); +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + res |= assert_false(le32ton(x) == x); + res |= assert_false(ntole32(x) == x); + res |= assert_true(le32ton(ntole32(x)) == x); + res |= assert_true(be32ton(x) == x); + res |= assert_true(ntobe32(x) == x); +#else + res |= assert_false("platform unsupported!!"); +#endif + return res; +} diff --git a/test/test.h b/test/test.h index 375a43a..aefa8fd 100644 --- a/test/test.h +++ b/test/test.h @@ -3,13 +3,15 @@ #pragma once #include +char const* test_ctest; + // evaluates the test // returns 1 upon error static inline int assert_helper(int cond, char const* restrict fname, unsigned ln, char const* restrict fnname, char const* restrict expr) { if (cond) - printf("[\033[32;1m OK \033[0m] %s -> %s:%u (%s)\n", fnname, fname, ln, expr); + printf("[\033[32;1m OK \033[0m] %s %s -> %s:%u (%s)\n", test_ctest, fnname, fname, ln, expr); else - printf("[\033[31;1m FAIL \033[0m] %s -> %s:%u (%s)\n", fnname, fname, ln, expr); + printf("[\033[31;1m FAIL \033[0m] %s %s -> %s:%u (%s)\n", test_ctest, fnname, fname, ln, expr); return !cond; } @@ -18,6 +20,7 @@ static inline int assert_helper(int cond, char const* restrict fname, unsigned l // contains the data for executing a single test struct testdat { + char const* name; // test name int (*test)(void*); // test, returns 0 upon success, non-zero upon failure void* args; // arguments to the test }; @@ -29,8 +32,10 @@ static inline size_t exec_tests(testdat* dat, size_t ntests) { size_t err = 0; // perform tests and count the error state - for (i = 0; i < ntests; i++) + for (i = 0; i < ntests; i++) { + test_ctest = dat[i].name; err += !!(dat[i].test(dat[i].args)); + } // give final score if (!err)