From 85a2ba407b1ae285b4080e14f8a18ecf4ec7da2c Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Wed, 12 Mar 2025 17:26:48 -0400 Subject: pke: more testing features + more ui flex work --- tests/pke-test-dummy.c | 22 --------- tests/pke-test-dummy.cpp | 23 ++++++++++ tests/pke-test-dummy.h | 8 ++++ tests/pke-test-static-ui.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++ tests/pke-test-static-ui.h | 16 +++++++ tests/pke-test-types.h | 11 +++++ tests/pke-test.c | 57 ----------------------- tests/pke-test.cpp | 77 +++++++++++++++++++++++++++++++ 8 files changed, 241 insertions(+), 79 deletions(-) delete mode 100644 tests/pke-test-dummy.c create mode 100644 tests/pke-test-dummy.cpp create mode 100644 tests/pke-test-static-ui.cpp create mode 100644 tests/pke-test-static-ui.h delete mode 100644 tests/pke-test.c create mode 100644 tests/pke-test.cpp (limited to 'tests') diff --git a/tests/pke-test-dummy.c b/tests/pke-test-dummy.c deleted file mode 100644 index ea92c34..0000000 --- a/tests/pke-test-dummy.c +++ /dev/null @@ -1,22 +0,0 @@ - -#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.cpp b/tests/pke-test-dummy.cpp new file mode 100644 index 0000000..a7e2ddc --- /dev/null +++ b/tests/pke-test-dummy.cpp @@ -0,0 +1,23 @@ + +#include "./pke-test-dummy.h" + +int pke_test_dummy_001() { + return 0; +} + +struct pke_test_group *pke_test_dummy_get_group() { + static const uint64_t test_count = 1; + static struct pke_test tests[test_count] = { + { + .title = "test 001", + .func = pke_test_dummy_001, + .expected_result = 0, + } + }; + static struct pke_test_group group = {}; + group.title = "dummy test"; + group.n_tests = test_count; + group.tests = &tests[0]; + + return &group; +} diff --git a/tests/pke-test-dummy.h b/tests/pke-test-dummy.h index b69609e..6abf8b7 100644 --- a/tests/pke-test-dummy.h +++ b/tests/pke-test-dummy.h @@ -3,6 +3,14 @@ #include "pke-test-types.h" +#ifdef __cplusplus +extern "C" { +#endif + struct pke_test_group *pke_test_dummy_get_group(); +#ifdef __cplusplus +} +#endif + #endif /* PKE_PKE_TEST_DUMMY_H */ diff --git a/tests/pke-test-static-ui.cpp b/tests/pke-test-static-ui.cpp new file mode 100644 index 0000000..6e6fb4b --- /dev/null +++ b/tests/pke-test-static-ui.cpp @@ -0,0 +1,106 @@ + +#include "./pke-test-static-ui.h" + +#include "window.hpp" +#include "dynamic-array.hpp" +#include "vendor-glm-include.hpp" +struct pke_ui_box_instance_buffer_item { + glm::mat4 pos_scale; + glm::vec2 px_scale; + float depth; + float padding[1]; +}; +struct pke_ui_flex_params { + float px_per_unit; + float unit_total; +}; +#define PKE_TEST_EXPOSE +#include "static-ui.hpp" + +void pke_test_static_ui_setup() { + Extent.width = 1920; + Extent.height = 1080; + pke_ui_init(); +} + +void pke_test_static_ui_teardown() { + pke_ui_teardown(); +} + +// test static +int pke_test_static_ui_000() { + DynArray arr{}; + float calculated_offset; + uint8_t err_index = 0; + + pke_ui_box *ui_box = pke_ui_box_new_root(); + ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC; + ui_box->pos_top_left_x = 10; + ui_box->pos_top_left_y = 10; + ui_box->max_width = 500; + ui_box->max_height = 500; + + pke_ui_box *c_ui_box = pke_ui_box_new_child(ui_box); + c_ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC; + c_ui_box->pos_top_left_x = 10; + c_ui_box->pos_top_left_y = 10; + c_ui_box->max_width = 100; + c_ui_box->max_height = 100; + + calculated_offset = ui_box->pos_top_left_x + c_ui_box->pos_top_left_x + built_in_offset; + + pke_ui_calc_px(arr, nullptr, ui_box); + pke_ui_recalc_sizes_recursive(arr, ui_box, 0); + + PKE_TEST_ASSERT(ui_box->internal.parent == nullptr, err_index); + PKE_TEST_ASSERT(c_ui_box->internal.parent != nullptr, err_index); + + PKE_TEST_ASSERT(ui_box->internal.px_corner_x == 10, err_index); + PKE_TEST_ASSERT(ui_box->internal.px_corner_y == 10, err_index); + PKE_TEST_ASSERT(c_ui_box->internal.px_corner_x == calculated_offset, err_index); + PKE_TEST_ASSERT(c_ui_box->internal.px_corner_y == calculated_offset, err_index); + + return 0; +} + +// test dynamic +int pke_test_static_ui_100() { + return 0; +} + + +// test flex +int pke_test_static_ui_200() { + return 0; +} + +pke_test_group *pke_test_static_ui_get_group() { + static const uint64_t test_count = 3; + static struct pke_test tests[test_count] = { + { + .title = "test 000", + .func = pke_test_static_ui_000, + .expected_result = 0, + }, + { + .title = "test 100", + .func = pke_test_static_ui_100, + .expected_result = 0, + }, + { + .title = "test 200", + .func = pke_test_static_ui_200, + .expected_result = 0, + }, + }; + static struct pke_test_group group = {}; + group.title = "static_ui"; + group.group_setup = NULL; + group.group_teardown = NULL; + group.test_setup = pke_test_static_ui_setup; + group.test_teardown = pke_test_static_ui_teardown; + group.tests = &tests[0]; + group.n_tests = test_count; + + return &group; +} diff --git a/tests/pke-test-static-ui.h b/tests/pke-test-static-ui.h new file mode 100644 index 0000000..673adf8 --- /dev/null +++ b/tests/pke-test-static-ui.h @@ -0,0 +1,16 @@ +#ifndef PKE_PKE_TEST_STATIC_UI_H +#define PKE_PKE_TEST_STATIC_UI_H + +#include "pke-test-types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct pke_test_group *pke_test_static_ui_get_group(); + +#ifdef __cplusplus +} +#endif + +#endif /* PKE_PKE_TEST_STATIC_UI_H */ diff --git a/tests/pke-test-types.h b/tests/pke-test-types.h index 5278eae..5a9ee24 100644 --- a/tests/pke-test-types.h +++ b/tests/pke-test-types.h @@ -15,6 +15,11 @@ typedef int (pke_test_func)(); struct pke_test_group; typedef struct pke_test_group *(pke_test_get_group)(); +typedef void (pke_test_group_setup()); +typedef void (pke_test_group_teardown()); +typedef void (pke_test_setup()); +typedef void (pke_test_teardown()); + struct pke_test { const char *title; pke_test_func *func; @@ -23,8 +28,14 @@ struct pke_test { struct pke_test_group { const char *title; + pke_test_group_setup *group_setup; + pke_test_group_teardown *group_teardown; + pke_test_setup *test_setup; + pke_test_teardown *test_teardown; struct pke_test *tests; uint32_t n_tests; }; +#define PKE_TEST_ASSERT(condition, index) if (!(condition)) { return ++index; } else { ++index; } + #endif /* PKE_PKE_TEST_TYPES_H */ diff --git a/tests/pke-test.c b/tests/pke-test.c deleted file mode 100644 index 4f88dbf..0000000 --- a/tests/pke-test.c +++ /dev/null @@ -1,57 +0,0 @@ - -#include "./pke-test-types.h" - -#include "./pke-test-dummy.h" - -#include "unistd.h" -#include - -#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; -} 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 + +#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; +} -- cgit v1.2.3