diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-12 17:26:48 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-12 17:26:48 -0400 |
| commit | 85a2ba407b1ae285b4080e14f8a18ecf4ec7da2c (patch) | |
| tree | 0abe7044b6af5820e9f3c143733047b254e78e15 /tests/pke-test.cpp | |
| parent | 68ef51ed3247dc4e7bd5970b9279a7d6a938ca52 (diff) | |
pke: more testing features + more ui flex work
Diffstat (limited to 'tests/pke-test.cpp')
| -rw-r--r-- | tests/pke-test.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/pke-test.cpp b/tests/pke-test.cpp new file mode 100644 index 0000000..61b6fb8 --- /dev/null +++ b/tests/pke-test.cpp @@ -0,0 +1,77 @@ + +#include "./pke-test-types.h" + +#include "./pke-test-dummy.h" +#include "./pke-test-static-ui.h" + +#include "pk.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; + int result; + uint32_t k, pass_count; + double nanoseconds; + double group_nanoseconds; + pk_tmr func_tmr; + + pke_test_get_group *group_fns[] = { + pke_test_dummy_get_group, + pke_test_static_ui_get_group, + NULL, + }; + + fprintf(stdout, "\r\n"); + pke_test_get_group *fn = group_fns[i]; + while (fn != NULL) { + pass_count = 0; + group_nanoseconds = 0; + struct pke_test_group *group = (fn)(); + fprintf(stdout, "[pke-test]:[%s] Begin.\n", group->title); + if (group->group_setup != NULL) (group->group_setup)(); + for (k = 0; k < group->n_tests; ++k) { + fprintf(stdout, "[pke-test]:[%s]:[%s] Begin.\n", group->title, group->tests[k].title); + if (group->test_setup != NULL) (group->test_setup)(); + lj.expected_exit = 0; + lj.caught = 0; + pk_tmr_start(func_tmr); + result = (group->tests[k].func)(); + pk_tmr_stop(func_tmr); + nanoseconds = pk_tmr_duration_double(func_tmr); + group_nanoseconds += nanoseconds; + if (result == group->tests[k].expected_result){ + pass_count += 1; + fprintf(stdout, "[pke-test]:[%s]:[%s] %sPassed.%s\n", group->title, group->tests[k].title, CLR_GREEN, CLR_WHITE); + fprintf(stdout, "[pke-test]:[%s]:[%s] Elapsed ms: '%f'.\n", group->title, group->tests[k].title, nanoseconds); + } else { + fprintf(stdout, "[pke-test]:[%s]:[%s] %sFailed.%s Expected: '%i' Got: '%i'.\n", group->title, group->tests[k].title, CLR_RED, CLR_WHITE, group->tests[k].expected_result, result); + } + if (group->test_teardown != NULL) (group->test_teardown)(); + } + if (group->group_teardown != NULL) (group->group_teardown)(); + fprintf(stdout, "[pke-test]:[%s] End. ( %s%03d%s / %03d ) Tests Completed.\n", group->title, pass_count == group->n_tests ? CLR_GREEN : CLR_RED, pass_count, CLR_WHITE, group->n_tests); + fprintf(stdout, "[pke-test]:[%s] End. Elapsed ms: '%f'.\n\n", group->title, group_nanoseconds); + i += 1; + fn = group_fns[i]; + } + + return 0; +} |
