update testing framework

This commit is contained in:
2025-06-13 00:51:53 +02:00
parent 2489ecbb0b
commit 3a9fc7e0a4
4 changed files with 89 additions and 53 deletions

34
test/dat.h Normal file
View File

@@ -0,0 +1,34 @@
#include <stdint.h>
#include <stdlib.h>
#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 },
};

View File

@@ -4,63 +4,14 @@
#include <glad/gl.h>
#undef GLAD_GL_IMPLEMENTATION
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#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]);

46
test/t_util.h Normal file
View File

@@ -0,0 +1,46 @@
#pragma once
#include <math.h>
#include <stdint.h>
#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;
}

View File

@@ -3,13 +3,15 @@
#pragma once
#include <stdio.h>
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)