diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-11 20:39:41 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-12 12:30:58 -0400 |
| commit | 68ef51ed3247dc4e7bd5970b9279a7d6a938ca52 (patch) | |
| tree | 169f75207bca7650a390ed812476ff3037978dae /tests | |
| parent | eae4525970d5a689f09d82e1f73218cb16168b9b (diff) | |
pke: pke test scaffolding
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/pke-test-dummy.c | 22 | ||||
| -rw-r--r-- | tests/pke-test-dummy.h | 8 | ||||
| -rw-r--r-- | tests/pke-test-types.h | 30 | ||||
| -rw-r--r-- | tests/pke-test.c | 57 |
4 files changed, 117 insertions, 0 deletions
diff --git a/tests/pke-test-dummy.c b/tests/pke-test-dummy.c new file mode 100644 index 0000000..ea92c34 --- /dev/null +++ b/tests/pke-test-dummy.c @@ -0,0 +1,22 @@ + +#include "./pke-test-dummy.h" + +int pke_test_dummy_001() { + return 0; +} + +struct pke_test_group *pke_test_dummy_get_group() { + static struct pke_test tests[1] = { + { + .title = "test 001", + .func = pke_test_dummy_001, + .expected_result = 0, + } + }; + static struct pke_test_group group = {0}; + group.title = "dummy test"; + group.n_tests = 1; + group.tests = &tests[0]; + + return &group; +} diff --git a/tests/pke-test-dummy.h b/tests/pke-test-dummy.h new file mode 100644 index 0000000..b69609e --- /dev/null +++ b/tests/pke-test-dummy.h @@ -0,0 +1,8 @@ +#ifndef PKE_PKE_TEST_DUMMY_H +#define PKE_PKE_TEST_DUMMY_H + +#include "pke-test-types.h" + +struct pke_test_group *pke_test_dummy_get_group(); + +#endif /* PKE_PKE_TEST_DUMMY_H */ diff --git a/tests/pke-test-types.h b/tests/pke-test-types.h new file mode 100644 index 0000000..5278eae --- /dev/null +++ b/tests/pke-test-types.h @@ -0,0 +1,30 @@ +#ifndef PKE_PKE_TEST_TYPES_H +#define PKE_PKE_TEST_TYPES_H + +#include <setjmp.h> +#include <stdint.h> + +struct pke_test_long_jump { + uint8_t expected_exit; + uint8_t caught; + jmp_buf jump_env; +}; +extern struct pke_test_long_jump lj; + +typedef int (pke_test_func)(); +struct pke_test_group; +typedef struct pke_test_group *(pke_test_get_group)(); + +struct pke_test { + const char *title; + pke_test_func *func; + int expected_result; +}; + +struct pke_test_group { + const char *title; + struct pke_test *tests; + uint32_t n_tests; +}; + +#endif /* PKE_PKE_TEST_TYPES_H */ diff --git a/tests/pke-test.c b/tests/pke-test.c new file mode 100644 index 0000000..4f88dbf --- /dev/null +++ b/tests/pke-test.c @@ -0,0 +1,57 @@ + +#include "./pke-test-types.h" + +#include "./pke-test-dummy.h" + +#include "unistd.h" +#include <stdio.h> + +#define CLR_WHITE "\033[0m" +#define CLR_GREEN "\033[92m" +#define CLR_RED "\033[31m" + +// https://stackoverflow.com/questions/64190847/how-to-catch-a-call-to-exit-for-unit-testing +struct pke_test_long_jump lj; +void exit(int code) { + if (lj.expected_exit) { + lj.caught = 1; + longjmp(lj.jump_env, 1); + } + _exit(code); +} + +int main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + int i = 0; + uint32_t k, pass_count; + + pke_test_get_group *group_fns[] = { + pke_test_dummy_get_group, + NULL, + }; + + pke_test_get_group *fn = group_fns[i]; + while (fn != NULL) { + pass_count = 0; + struct pke_test_group *group = (fn)(); + fprintf(stdout, "[pke-test]:[%s] Begin.\n", group->title); + for (k = 0; k < group->n_tests; ++k) { + fprintf(stdout, "[pke-test]:[%s]:[%s] Begin.\n", group->title, group->tests[k].title); + lj.expected_exit = 0; + lj.caught = 0; + if (group->tests[k].expected_result == (group->tests[k].func)()){ + pass_count += 1; + fprintf(stdout, "[pke-test]:[%s]:[%s] %sPassed.%s\n", group->title, group->tests[k].title, CLR_GREEN, CLR_WHITE); + } else { + fprintf(stdout, "[pke-test]:[%s]:[%s] %sFailed.%s\n", group->title, group->tests[k].title, CLR_RED, CLR_WHITE); + } + } + fprintf(stdout, "[pke-test]:[%s] End. ( %s%03d%s / %03d ) Tests Completed.\n\n", group->title, pass_count == group->n_tests ? CLR_GREEN : CLR_RED, pass_count, CLR_WHITE, group->n_tests); + i += 1; + fn = group_fns[i]; + } + + return 0; +} |
