aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/README31
-rw-r--r--test/config.c18
-rw-r--r--test/gc.c34
-rw-r--r--test/identifier.c24
-rw-r--r--test/io.c57
-rw-r--r--test/memory.c33
-rw-r--r--test/string.c14
-rw-r--r--test/task.c24
-rw-r--r--test/test.c92
-rw-r--r--test/tree.c47
10 files changed, 0 insertions, 374 deletions
diff --git a/test/README b/test/README
deleted file mode 100644
index 5a0e197..0000000
--- a/test/README
+++ /dev/null
@@ -1,31 +0,0 @@
-Test Files
-
----
-
-Testing:
-
-Each test is a function wrapped in a macro
-
-KPL_TEST(my_test) {
- // code here
-}
-
-Each test passes by default
-
----
-
-Testing Macros:
-
-The testing framework provides two macros KPL_ASSERT and KPL_FAIL
-
-Both macros can only be used inside a KPL_TEST function
-
-Asserting:
-
-KPL_ASSERT(condition, "Fail Message");
-
----
-
-Failing:
-
-KPL_FAIL("The test failed");
diff --git a/test/config.c b/test/config.c
deleted file mode 100644
index eb5d0ca..0000000
--- a/test/config.c
+++ /dev/null
@@ -1,18 +0,0 @@
-
-#include "kpl_test.h"
-
-KPL_TEST(set_thread_count) {
- kpl_env env = {};
- int32_t argc = 4;
- char *argv[] = {"./kpl", "-t", "4", "file.kpl"};
- KPL_ASSERT(kpl_env_config(&env, argc, argv) == KPL_EXIT_CODE_SUCCESS, "Should return success");
- KPL_ASSERT(env.thread_count == 4, "Threads should not be -1");
-}
-
-KPL_TEST(set_io_uring_queue_pow2_size) {
- kpl_env env = {};
- int32_t argc = 4;
- char *argv[] = {"./kpl", "-i", "12", "file.kpl"};
- KPL_ASSERT(kpl_env_config(&env, argc, argv) == KPL_EXIT_CODE_SUCCESS, "Should return success");
- KPL_ASSERT(env.io_uring_queue_size_pow2 != KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT, "Should not be default");
-}
diff --git a/test/gc.c b/test/gc.c
deleted file mode 100644
index 6834ad2..0000000
--- a/test/gc.c
+++ /dev/null
@@ -1,34 +0,0 @@
-
-#include "kpl_test.h"
-
-#define TEST_OBJECT_COUNT 10
-
-KPL_TEST(basic_mark_and_sweep) {
- kpl_thread thread = {};
- kpl_memory_allocator_init(&thread.memory_allocator);
- kpl_gc_init(&thread.gc);
- kpl_object *objs[TEST_OBJECT_COUNT] = {};
- for (int32_t obj_index = 1; obj_index <= TEST_OBJECT_COUNT; obj_index++) {
- objs[obj_index - 1] = kpl_gc_new(&thread, NULL, sizeof(kpl_object_error));
- if (obj_index % 2 == 0)
- kpl_gc_mark(objs[obj_index - 1]->object_gc_parent);
- }
- int32_t counter = 0;
- kpl_object_gc *gc_head = thread.gc.gc_head;
- while (gc_head) {
- counter++;
- gc_head = gc_head->gc_next;
- }
- KPL_ASSERT(counter == TEST_OBJECT_COUNT, "should have TEST_OBJECT_COUNT Objects in gc list");
- kpl_gc_prepare_sweep(&thread.gc);
- kpl_gc_sweep(&thread);
- gc_head = thread.gc.gc_head;
- counter = 0;
- while (gc_head) {
- counter++;
- gc_head = gc_head->gc_next;
- }
- KPL_ASSERT(counter == TEST_OBJECT_COUNT / 2, "should have removed half of the objects");
- kpl_gc_free(&thread);
- kpl_memory_allocator_free(&thread.memory_allocator);
-}
diff --git a/test/identifier.c b/test/identifier.c
deleted file mode 100644
index abbcdac..0000000
--- a/test/identifier.c
+++ /dev/null
@@ -1,24 +0,0 @@
-
-#include "kpl_test.h"
-
-KPL_TEST(c_string_to_identifier) {
- const char *string = "aA0_t4_12w0abcdefghijklmnop456789Z";
- kpl_identifier identifier;
- KPL_ASSERT(kpl_identifier_encode(&identifier, string, strlen(string)), "should encode");
- char buffer[100] = {};
- const int32_t offset = 10;
- kpl_identifier_decode(&identifier, buffer + offset);
- KPL_ASSERT(strcmp(string, buffer + offset) == 0, "should decode to same string");
-}
-
-KPL_TEST(identifier_compare) {
- const char *string_a = "abc123456789", *string_b = "abc123456789", *string_c = "abd123456789";
- kpl_identifier identifier_a, identifier_b, identifier_c;
- KPL_ASSERT(kpl_identifier_encode(&identifier_a, string_a, strlen(string_a)), "should encode");
- KPL_ASSERT(kpl_identifier_encode(&identifier_b, string_b, strlen(string_b)), "should encode");
- KPL_ASSERT(kpl_identifier_encode(&identifier_c, string_c, strlen(string_c)), "should encode");
- KPL_ASSERT(kpl_identifier_compare(&identifier_a, &identifier_b) == 0, "should be 0");
- int32_t compare_value = strcmp(string_b, string_c);
- KPL_ASSERT(kpl_identifier_compare(&identifier_b, &identifier_c) == compare_value, "should equal strcmp");
- KPL_ASSERT(kpl_identifier_equal(&identifier_a, &identifier_b), "should be equal");
-}
diff --git a/test/io.c b/test/io.c
deleted file mode 100644
index 50077f6..0000000
--- a/test/io.c
+++ /dev/null
@@ -1,57 +0,0 @@
-
-#include "kpl_test.h"
-
-static const char *linux_version = "/proc/version";
-
-KPL_TEST(statx) {
- kpl_env env = {};
- kpl_thread thread = { .env = &env };
- kpl_io_uring *io_uring = &thread.io_uring;
- KPL_ASSERT(kpl_io_uring_init(&thread.io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success");
- struct statx statx_object = {};
- kpl_object_task *task = calloc(1, sizeof(kpl_object_task) + sizeof(kpl_class) * 1);
- task->io_res = 1;
- KPL_ASSERT(kpl_io_uring_statx(io_uring, task, AT_FDCWD, linux_version, 0, STATX_ALL, &statx_object),
- "should add statx operation");
- KPL_ASSERT(task->object_flags & KPL_OBJECT_FLAGS_IO_PENDING , "task should have io pending flag");
- KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation");
- KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task");
- KPL_ASSERT(!(task->object_flags & KPL_OBJECT_FLAGS_IO_PENDING) , "task should not have io pending flag");
- KPL_ASSERT(task->io_res == 0, "io_res should have success stax return");
- KPL_ASSERT(statx_object.stx_nlink > 0, "should have one or more hard link(s)");
- free(task);
- kpl_io_uring_free(io_uring);
-}
-
-#define TEST_TO_READ 5
-
-static const char *test_linix_compare = "Linux";
-
-KPL_TEST(open_read_close) {
- kpl_env env = {};
- kpl_thread thread = { .env = &env };
- kpl_io_uring *io_uring = &thread.io_uring;
- KPL_ASSERT(kpl_io_uring_init(io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success");
- char buffer[TEST_TO_READ + 1] = {};
- kpl_object_task *task = calloc(1, sizeof(kpl_object_task) + sizeof(kpl_class) * 1);
- task->io_res = 1;
- KPL_ASSERT(kpl_io_uring_openat(io_uring, task, AT_FDCWD, linux_version, O_RDONLY, 0),
- "should add open operation");
- KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation");
- KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task");
- KPL_ASSERT(task->io_res > 0, "io_res should have open fd");
- int32_t fd = task->io_res;
- task->io_res = 1;
- KPL_ASSERT(kpl_io_uring_read(io_uring, task, fd, &buffer, TEST_TO_READ, 0), "should add read operation");
- KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation");
- KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task");
- KPL_ASSERT(task->io_res == TEST_TO_READ, "io_res should equal the number of bytes read");
- KPL_ASSERT(strcmp(test_linix_compare, buffer) == 0, "should have Linux string in buffer");
- task->io_res = 1;
- KPL_ASSERT(kpl_io_uring_close(io_uring, task, fd), "should add close operation");
- KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation");
- KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task");
- KPL_ASSERT(task->io_res == 0, "io_res should have close success");
- free(task);
- kpl_io_uring_free(io_uring);
-}
diff --git a/test/memory.c b/test/memory.c
deleted file mode 100644
index 9308b2b..0000000
--- a/test/memory.c
+++ /dev/null
@@ -1,33 +0,0 @@
-
-#include "kpl_test.h"
-
-KPL_TEST(allocate_and_pool) {
- kpl_memory_allocator allocator = {};
- kpl_memory_allocator_init(&allocator);
- for (int32_t times = 0; times < 700; times++) {
- kpl_object *objects[10] = {};
- for (int32_t object = 0; object < 10; object++) {
- objects[object] = kpl_memory_allocator_allocate(&allocator, 32, KPL_THREAD_MAIN_ID);
- KPL_ASSERT(objects[object]->object_size == 32, "Invalid size for 32 byte object");
- }
- for (int32_t object = 0; object < 10; object++)
- kpl_memory_allocator_return(&allocator, objects[object]);
- }
- kpl_memory_allocator_slab_group *slab_group32 = &allocator.slabs[5];
- KPL_ASSERT(slab_group32->slab_head == slab_group32->slab_tail, "Only one slab should have been allocated");
- kpl_memory_allocator_free(&allocator);
-}
-
-KPL_TEST(allocate_multiple_slabs) {
- kpl_memory_allocator allocator = {};
- kpl_memory_allocator_init(&allocator);
- kpl_object *large1 = kpl_memory_allocator_allocate(&allocator, 1 << 13, KPL_THREAD_MAIN_ID);
- kpl_object *large2 = kpl_memory_allocator_allocate(&allocator, 1 << 13, KPL_THREAD_MAIN_ID);
- KPL_ASSERT(large1->object_size == 1 << 13, "Invalid object size");
- KPL_ASSERT(large1->object_size == large2->object_size, "Objects should have the same size");
- kpl_memory_allocator_slab_group *slab_group = &allocator.slabs[13];
- KPL_ASSERT(slab_group->slab_head != slab_group->slab_tail, "More the one slab should have been allocated");
- kpl_memory_allocator_return(&allocator, large1);
- kpl_memory_allocator_return(&allocator, large2);
- kpl_memory_allocator_free(&allocator);
-}
diff --git a/test/string.c b/test/string.c
deleted file mode 100644
index 8a6742c..0000000
--- a/test/string.c
+++ /dev/null
@@ -1,14 +0,0 @@
-
-#include "kpl_test.h"
-
-KPL_TEST(string_init) {
- kpl_thread thread = {};
- const char *c_string = "test string\n";
- kpl_memory_allocator_init(&thread.memory_allocator);
- kpl_gc_init(&thread.gc);
- kpl_object_string *string = kpl_object_string_from_c_string(&thread, c_string);
- KPL_ASSERT(string->bytes_length == (int32_t) strlen(c_string), "string should have same length as c string");
- KPL_ASSERT(strcmp(c_string, (char*) string->bytes) == 0, "new string should equal c string");
- kpl_gc_free(&thread);
- kpl_memory_allocator_free(&thread.memory_allocator);
-}
diff --git a/test/task.c b/test/task.c
deleted file mode 100644
index 8374989..0000000
--- a/test/task.c
+++ /dev/null
@@ -1,24 +0,0 @@
-
-#include "kpl_test.h"
-
-static int32_t run_task_value = 0;
-
-#define KPL_RUN_TASK_SET_VALUE 1234567
-
-void run_task_set_value(kpl_thread *thread, kpl_object_task *object_task) {
- run_task_value = KPL_RUN_TASK_SET_VALUE;
- kpl_object_task_done(thread, object_task);
-}
-
-KPL_TEST(run_task) {
- kpl_env env = {};
- kpl_env_config(&env, -1, NULL);
- env.thread_count = 1;
- env.io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_MIN;
- env.flags |= KPL_ENV_FLAG_RUN_FUNCTION;
- env.kpl_fn_entry = run_task_set_value;
- KPL_ASSERT(kpl_env_init(&env) == KPL_EXIT_CODE_SUCCESS, "env should init");
- KPL_ASSERT(kpl_env_run(&env) == KPL_EXIT_CODE_SUCCESS, "env should run");
- kpl_env_free(&env);
- KPL_ASSERT(run_task_value == KPL_RUN_TASK_SET_VALUE, "task should have set value");
-}
diff --git a/test/test.c b/test/test.c
deleted file mode 100644
index 0dcf302..0000000
--- a/test/test.c
+++ /dev/null
@@ -1,92 +0,0 @@
-
-#include "kpl_test.h"
-
-static int32_t kpl_test_tree_compare(const void *node_a, const void *node_b) {
- const kpl_test *test_a = node_a, *test_b = node_b;
- return strcmp(test_a->name, test_b->name);
-}
-
-static kpl_tree test_tree = KPL_TREE_INIT_STATIC(kpl_test_tree_compare, NULL);
-
-static kpl_test *test_head = NULL, *test_tail = NULL, *error_head = NULL, *error_tail = NULL;
-
-void _kpl_test_add(kpl_test *_test) {
- if (!test_head)
- test_head = test_tail = _test;
- else
- test_tail = test_tail->list_next = _test;
- kpl_tree_insert(&test_tree, _test);
-}
-
-static void kpl_test_report(const kpl_test *_test) {
- printf(KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_CYAN) "%s" KPL_COLOR(RESET),
- _test->source_file, _test->name);
- if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT) {
- printf(KPL_COLOR(LIGHT_GREEN) " PASS\n" KPL_COLOR(RESET));
- return;
- }
- 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 kpl_exit_code kpl_test_report_error(void) {
- if (!error_head) {
- printf(KPL_COLOR2(BOLD, GREEN) "PASSED\n" KPL_COLOR(RESET));
- return KPL_EXIT_CODE_SUCCESS;
- }
- printf(KPL_COLOR2(BOLD, RED) "FAILED\n" KPL_COLOR(RESET));
- while (error_head) {
- kpl_test *_test = error_head;
- error_head = error_head->list_next;
- kpl_test_report(_test);
- }
- return KPL_EXIT_CODE_ERROR;
-}
-
-static void kpl_test_run_one(kpl_test *_test) {
- _test->list_next = NULL;
- _test->test_fn(_test);
- kpl_test_report(_test);
- if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT)
- return;
- if (!error_head)
- error_head = error_tail = _test;
- else
- error_tail = error_tail->list_next = _test;
-}
-
-static kpl_exit_code kpl_test_run_all(void) {
- while (test_head) {
- kpl_test *_test = test_head;
- test_head = test_head->list_next;
- kpl_test_run_one(_test);
- }
- return kpl_test_report_error();
-}
-
-int32_t main(int32_t argc, char *argv[]) {
- if (argc < 2)
- return kpl_test_run_all();
- kpl_test find = {};
- for (int32_t arg_index = 1; arg_index < argc; arg_index++) {
- find.name = argv[arg_index];
- kpl_test *found = kpl_tree_find(&test_tree, &find);
- if (!found) {
- printf(KPL_COLOR2(BOLD, RED) "TEST: " KPL_COLOR(RESET) KPL_COLOR2(BOLD, WHITE) "%s " KPL_COLOR(RESET)
- KPL_COLOR2(BOLD, RED) "NOT FOUND\n" KPL_COLOR(RESET), find.name);
- return KPL_EXIT_CODE_ERROR;
- }
- kpl_test *duplicates = found->tree_duplicates;
- while (duplicates) {
- kpl_test_run_one(duplicates);
- duplicates = duplicates->tree_duplicates;
- }
- kpl_test_run_one(found);
- }
- return kpl_test_report_error();
-}
-
-KPL_TEST(pass_by_default) {
- // pass, uncomment below to fail
- // KPL_FAIL("example fail");
-}
diff --git a/test/tree.c b/test/tree.c
deleted file mode 100644
index aef4061..0000000
--- a/test/tree.c
+++ /dev/null
@@ -1,47 +0,0 @@
-
-#include "kpl_test.h"
-
-typedef struct _c_string_test_node {
- KPL_TREE_HEADER(_c_string_test_node);
- const char *c_string;
-} c_string_test_node;
-
-static int32_t c_string_test_compare(const void *node_a, const void *node_b) {
- const c_string_test_node *a = node_a, *b = node_b;
- return strcmp(a->c_string, b->c_string);
-}
-
-KPL_TEST(c_string_trees) {
- const char *c_strings[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", NULL };
- const int32_t c_string_counter[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
- kpl_tree c_string_tree = {};
- kpl_tree_init(&c_string_tree, c_string_test_compare, free);
- const char **c_strings_iterator = c_strings;
- const int32_t *c_string_counter_iterator = c_string_counter;
- while (*c_strings_iterator && *c_string_counter_iterator) {
- for (int32_t inserts = 0; inserts < *c_string_counter_iterator; inserts++) {
- c_string_test_node *node = calloc(1, sizeof(c_string_test_node));
- node->c_string = *c_strings_iterator;
- kpl_tree_insert(&c_string_tree, node);
- }
- c_strings_iterator++;
- c_string_counter_iterator++;
- }
- c_strings_iterator = c_strings;
- c_string_counter_iterator = c_string_counter;
- c_string_test_node find = {}, *found;
- while (*c_strings_iterator && *c_string_counter_iterator) {
- int32_t count = *c_string_counter_iterator;
- find.c_string = *c_strings_iterator;
- KPL_ASSERT(found = kpl_tree_find(&c_string_tree, &find), "c string node not found");
- while (count) {
- KPL_ASSERT(strcmp(found->c_string, *c_strings_iterator) == 0, "c string does not match");
- found = found->tree_duplicates;
- count--;
- }
- KPL_ASSERT(!found, "found pointer should be null on last duplicate");
- c_strings_iterator++;
- c_string_counter_iterator++;
- }
- kpl_tree_free(&c_string_tree);
-}