From de65adbc1b2d1b3efdbe6a023bb7435f84b2f3a7 Mon Sep 17 00:00:00 2001 From: nodist Date: Sat, 3 Oct 2026 08:50:07 -0400 Subject: redesign --- test/README | 62 ------------------------ test/config.c | 51 -------------------- test/queue.c | 96 ------------------------------------- test/test.c | 151 ---------------------------------------------------------- 4 files changed, 360 deletions(-) delete mode 100644 test/README delete mode 100644 test/config.c delete mode 100644 test/queue.c delete mode 100644 test/test.c (limited to 'test') diff --git a/test/README b/test/README deleted file mode 100644 index f1c86df..0000000 --- a/test/README +++ /dev/null @@ -1,62 +0,0 @@ -Testing - ---- - -File Layout: - -The test files mirror the src files, so a function in src/file.c would have a test in test/file.c - ---- - -Includes: - -#include "kpl_test.h" - -This is the only include a .c file in the test folder needs - ---- - -Writing Tests: - -KPL_TEST(test_name) { - // TEST CODE -} - -Tests pass by default - ---- - -Asserting: - -KPL_ASSERT(CONDITION, FAIL_MESSAGE); - ---- - -Failing: - -KPL_FAIL(FAIL_MESSAGE); - ---- - -Building: - -make kpl_test # build test exec -make kpl_test_memory # build test exec with address, leak and undefined sanitizer -make kpl_test_thread # build test exec with thread sanitizer - -All objects need to be built with the same sanitizer and do not run valgrind when sanitizers are added - ---- - -Running: - -./kpl_test # Run all tests -./kpl_test NAME # Run test named NAME -./kpl_test NAME1 NAME2 ... # Run selected tests named NAM1 NAME2 ... -./kpl_test -h # Show help - ---- - -Compiler Flags: - ---- diff --git a/test/config.c b/test/config.c deleted file mode 100644 index 978e003..0000000 --- a/test/config.c +++ /dev/null @@ -1,51 +0,0 @@ - -#include "kpl_test.h" - -KPL_TEST(show_help) { - kpl_config config; - const int32_t argc = 2; - const char *argv[] = { "./kpl", "--help", "not_used" }; - kpl_config_init(&config); - int32_t code = kpl_config_parse_args(&config, argc, argv); - KPL_ASSERT(code == KPL_EXIT_CODE_HELP, "Invalid help exit code"); -} - -KPL_TEST(change_module_extension) { - kpl_config config; - const int32_t argc = 3; - const char *argv[] = { "./kpl", "--module-extension=.l", "./file.l" }; - kpl_config_init(&config); - int32_t code = kpl_config_parse_args(&config, argc, argv); - KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); - KPL_ASSERT(strcmp(config.module_extension, ".l") == 0, "Invalid new module extension"); -} - -KPL_TEST(io_queue_pow2) { - kpl_config config; - const int32_t argc = 3; - const char *argv[] = { "./kpl", "--io-queue-pow2=14", "./file.kpl" }; - kpl_config_init(&config); - int32_t code = kpl_config_parse_args(&config, argc, argv); - KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); - KPL_ASSERT(config.io.queue_pow2 == 14, "Invalid io queue power of two"); -} - -KPL_TEST(thread_count) { - kpl_config config; - const int32_t argc = 3; - const char *argv[] = { "./kpl", "--thread-count=1", "./file.kpl" }; - kpl_config_init(&config); - int32_t code = kpl_config_parse_args(&config, argc, argv); - KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); - KPL_ASSERT(config.thread.count == 1, "Invalid thread count"); -} - -KPL_TEST(memory_pool_pow2) { - kpl_config config; - const int32_t argc = 3; - const char *argv[] = { "./kpl", "--memory-pool-pow2=6,7", "./file.kpl" }; - kpl_config_init(&config); - int32_t code = kpl_config_parse_args(&config, argc, argv); - KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); - KPL_ASSERT(config.thread.ring_pool_entries_pow2[6] == 7, "Invalid entries pow2"); -} diff --git a/test/queue.c b/test/queue.c deleted file mode 100644 index 0f63322..0000000 --- a/test/queue.c +++ /dev/null @@ -1,96 +0,0 @@ - -#include "kpl_test.h" - -typedef struct { - _Atomic int32_t increments; - bool pop_error; - kpl_task_queue task_queue; - pthread_t pthread; -} kpl_task_queue_thread_test; - -#define TASK_QUEUE_THREAD_COUNT 3 - -#define TASK_QUEUE_PER_THREAD_INCREMENTS 10 - -static const int32_t kpl_task_queue_total_increments = TASK_QUEUE_THREAD_COUNT * TASK_QUEUE_PER_THREAD_INCREMENTS; - -static kpl_task_queue_thread_test task_queue_threads[TASK_QUEUE_THREAD_COUNT] = {}; - -static void *task_queue_thread_fn(void *arg) { - const int32_t thread_id = (int32_t) (intptr_t) arg; - for (int32_t round = 0; round < TASK_QUEUE_PER_THREAD_INCREMENTS; round++) { - for (int32_t id = 0; id < TASK_QUEUE_THREAD_COUNT; id++) { - kpl_task *task = calloc(1, sizeof(kpl_task)); - kpl_task_queue_add(&task_queue_threads[id].task_queue, task); - } - kpl_task *task = kpl_task_queue_next(&task_queue_threads[thread_id].task_queue); - if (task) { - task_queue_threads[thread_id].increments++; - free(task); - } else { - task_queue_threads[thread_id].pop_error = true; - break; - } - } - return NULL; -} - -KPL_TEST(task_queue_threads) { - for (int32_t id = 0; id < TASK_QUEUE_THREAD_COUNT; id++) - kpl_task_queue_init(&task_queue_threads[id].task_queue); - for (int32_t id = 0; id < TASK_QUEUE_THREAD_COUNT; id++) - pthread_create(&task_queue_threads[id].pthread, NULL, task_queue_thread_fn, (void*) (intptr_t) id); - for (int32_t id = 0; id < TASK_QUEUE_THREAD_COUNT; id++) - pthread_join(task_queue_threads[id].pthread, NULL); - for (int32_t id = 0; id < TASK_QUEUE_THREAD_COUNT; id++) { - kpl_task *task = NULL; - while ((task = kpl_task_queue_next(&task_queue_threads[id].task_queue))) { - task_queue_threads[id].increments++; - free(task); - } - KPL_ASSERT(!task_queue_threads[id].pop_error, "Task queue pop failed"); - KPL_ASSERT(task_queue_threads[id].increments == kpl_task_queue_total_increments, - "Invalid number of increments"); - } -} - -#define RING_QUEUE_THREAD_COUNT 3 - -#define RING_QUEUE_SLOTS_PER_THREAD 1000 - -#define RING_QUEUE_SLOT_LENGTH (RING_QUEUE_THREAD_COUNT * RING_QUEUE_SLOTS_PER_THREAD) - -static pthread_t ring_queue_threads[RING_QUEUE_THREAD_COUNT] = {}; - -static bool ring_queue_test_slots[RING_QUEUE_SLOT_LENGTH] = {}; - -static kpl_ring_queue ring_queue; - -static void *ring_queue_thread_fn(void *arg) { - const int32_t thread_id = (int32_t) (intptr_t) arg; - int32_t slot_index; - for (int32_t round = 1; round < RING_QUEUE_SLOTS_PER_THREAD; round++) { - slot_index = thread_id * RING_QUEUE_SLOTS_PER_THREAD + round; - if (!kpl_ring_queue_add(&ring_queue, (void*) (intptr_t) slot_index)) - break; - if ((slot_index = (int32_t) (intptr_t) kpl_ring_queue_next(&ring_queue))) - ring_queue_test_slots[slot_index] = true; - } - while ((slot_index = (int32_t) (intptr_t) kpl_ring_queue_next(&ring_queue))) - ring_queue_test_slots[slot_index] = true; - return NULL; -} - -KPL_TEST(ring_queue_threads) { - for (int32_t skip_round = 0; skip_round < RING_QUEUE_THREAD_COUNT; skip_round++) - ring_queue_test_slots[skip_round * RING_QUEUE_SLOTS_PER_THREAD] = true; - KPL_ASSERT(kpl_ring_queue_init(&ring_queue, 12), "Ring queue should init"); - for (int32_t id = 0; id < RING_QUEUE_THREAD_COUNT; id++) - pthread_create(&ring_queue_threads[id], NULL, ring_queue_thread_fn, (void*) (intptr_t) id); - for (int32_t id = 0; id < RING_QUEUE_THREAD_COUNT; id++) - pthread_join(ring_queue_threads[id], NULL); - for (int32_t slot_index = 0; slot_index < RING_QUEUE_SLOT_LENGTH; slot_index++) { - KPL_ASSERT(ring_queue_test_slots[slot_index], "Slot should have been set"); - } - kpl_ring_queue_free(&ring_queue); -} diff --git a/test/test.c b/test/test.c deleted file mode 100644 index 04cade6..0000000 --- a/test/test.c +++ /dev/null @@ -1,151 +0,0 @@ - -#include "kpl_test.h" - -static kpl_test *_kpl_test_head = NULL, *_kpl_test_tail = NULL, *_kpl_error_head = NULL, *_kpl_error_tail = NULL; - -static int32_t _kpl_test_count = 0, _kpl_test_error_count = 0; - -void kpl_test_add(kpl_test *_test) { - _kpl_test_count++; - if (_kpl_test_head) - _kpl_test_tail = _kpl_test_tail->next_test = _test; - else - _kpl_test_head = _kpl_test_tail = _test; -} - -static void kpl_test_report(kpl_test *_test) { - printf(KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_CYAN) "%s" KPL_COLOR(RESET), - _test->file_name, _test->test_name); -} - -static void kpl_test_report_success(kpl_test *_test) { - printf(KPL_COLOR2(BOLD, LIGHT_GREEN) "PASSED " KPL_COLOR(RESET)); - kpl_test_report(_test); - putchar('\n'); -} - -static void kpl_test_report_error(kpl_test *_test, bool failed_status) { - if (failed_status) - printf(KPL_COLOR2(BOLD, LIGHT_RED) "FAILED " KPL_COLOR(RESET)); - kpl_test_report(_test); - printf(":" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) "::" - KPL_COLOR(LIGHT_RED) "%s\n" KPL_COLOR(RESET), _test->error_line_number, _test->error_message); -} - -static int32_t kpl_test_error_list(void) { - if (!_kpl_test_error_count) { - printf(KPL_COLOR2(BOLD, LIGHT_GREEN) "ALL TESTS PASSED\n" KPL_COLOR(RESET)); - return KPL_EXIT_CODE_SUCCESS; - } - printf(KPL_COLOR2(BOLD, LIGHT_RED) "FAILED TEST COUNT:" KPL_COLOR(RESET) - KPL_COLOR(WHITE) " %d\n\n" KPL_COLOR(RESET), _kpl_test_error_count); - while (_kpl_error_head) { - kpl_test_report_error(_kpl_error_head, false); - putchar('\n'); - _kpl_error_head = _kpl_error_head->next_test; - } - return KPL_EXIT_CODE_ERROR; -} - -static void kpl_test_run(kpl_test *_test) { - _test->test_function(_test); - if (_test->error_line_number == -1) { - kpl_test_report_success(_test); - return; - } - _kpl_test_error_count++; - kpl_test_report_error(_test, true); - if (_kpl_error_head) - _kpl_error_tail = _kpl_error_tail->next_test = _test; - else - _kpl_error_head = _kpl_error_tail = _test; -} - -static kpl_test *kpl_test_next(void) { - if (!_kpl_test_head) - return NULL; - kpl_test *_test = _kpl_test_head; - _kpl_test_head = _kpl_test_head->next_test; - _test->next_test = NULL; - return _test; -} - -static int32_t kpl_test_run_all(void) { - kpl_test *_test = NULL; - while ((_test = kpl_test_next())) - kpl_test_run(_test); - return kpl_test_error_list(); -} - -static int32_t kpl_test_help(const char *argv0) { - printf("\nUsage: " KPL_COLOR(BOLD) "%s " KPL_COLOR(RESET) - KPL_COLOR(WHITE) "[OPTIONS] [TEST NAMES]\n\n" KPL_COLOR(RESET), argv0); - printf("Where " KPL_COLOR(WHITE) "[OPTIONS]" KPL_COLOR(RESET) " are:\n"); - printf(KPL_COLOR(LIGHT_YELLOW) "\t-a " KPL_COLOR(RESET) "Run All Tests\n"); - printf(KPL_COLOR(LIGHT_YELLOW) "\t-h " KPL_COLOR(RESET) "Show Help\n"); - printf(KPL_COLOR(LIGHT_YELLOW) "\t-l " KPL_COLOR(RESET) "List Test Names\n"); - putchar('\n'); - return KPL_EXIT_CODE_HELP; -} - -static int32_t kpl_test_list(void) { - printf("\nTest Count: " KPL_COLOR(WHITE) "%d\n\n" - KPL_COLOR(RESET) KPL_COLOR(LIGHT_CYAN), _kpl_test_count); - while (_kpl_test_head) { - printf("%s ", _kpl_test_head->test_name); - _kpl_test_head = _kpl_test_head->next_test; - } - printf(KPL_COLOR(RESET) "\n\n"); - return KPL_EXIT_CODE_HELP; -} - -static int32_t kpl_test_run_by_name(const int32_t argc, const char *argv[], const int32_t named_arg_index) { - if (named_arg_index == argc) { - printf(KPL_COLOR(LIGHT_RED) "\nNo Tests Specified\n" KPL_COLOR(RESET)); - return kpl_test_help(argv[0]); - } - int32_t tests_ran = 0; - kpl_test *_test = NULL; - while ((_test = kpl_test_next())) - for (int32_t test_name_index = named_arg_index; test_name_index < argc; test_name_index++) - if (strcmp(argv[test_name_index], _test->test_name) == 0) { - kpl_test_run(_test); - tests_ran++; - } - if (!tests_ran) { - printf(KPL_COLOR(LIGHT_RED) "\nNo Matching Tests Found, for help run: " KPL_COLOR(RESET) - KPL_COLOR(BOLD) "%s -h\n\n" KPL_COLOR(RESET), argv[0]); - return KPL_EXIT_CODE_ERROR; - } - return kpl_test_error_list(); -} - -int32_t main(const int32_t argc, const char *argv[]) { - int32_t arg_index = 1; - while (arg_index < argc) { - if (argv[arg_index][0] != '-' || !argv[arg_index][1] || argv[arg_index][2]) - break; - switch (argv[arg_index][1]) { - case 'a': - return kpl_test_run_all(); - case 'h': - return kpl_test_help(argv[0]); - case 'l': - return kpl_test_list(); - default: - return kpl_test_run_by_name(argc, argv, arg_index); - } - arg_index++; - } - return kpl_test_run_by_name(argc, argv, arg_index); -} - -KPL_TEST(pass_by_default) { - // Pass by default - // Uncomment below to f ail - // KPL_FAIL("Failure message"); -} - -KPL_TEST(counting) { - KPL_ASSERT(_kpl_test_count > 0, "Test counter not incrementing"); -} -- cgit v1.2.3