From f0db9d67cb33b9cc6c469f69bbf452b88d558e3f Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 24 Oct 2024 21:33:19 +0100 Subject: [PATCH] add all old corelib tests --- CMakeLists.txt | 28 +++-- core-test/{core-test.c => core-units.c} | 0 core-test/randomise.c | 39 ++++++ object-test/arrays.c | 19 +++ object-test/numbers.c | 10 ++ object-test/{object-test.c => object-units.c} | 0 object-test/strings.c | 24 ++++ object-test/trees.c | 116 ++++++++++++++++++ object-test/uuids.c | 12 ++ 9 files changed, 241 insertions(+), 7 deletions(-) rename core-test/{core-test.c => core-units.c} (100%) create mode 100644 core-test/randomise.c create mode 100644 object-test/arrays.c create mode 100644 object-test/numbers.c rename object-test/{object-test.c => object-units.c} (100%) create mode 100644 object-test/strings.c create mode 100644 object-test/trees.c create mode 100644 object-test/uuids.c diff --git a/CMakeLists.txt b/CMakeLists.txt index dfdbed8..2f8cf6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,17 +6,31 @@ set(b_modules core object term cmd) set(b_system_name ${CMAKE_SYSTEM_NAME}) string(TOLOWER ${b_system_name} b_system_name) +message(STATUS "System name: ${b_system_name}") + foreach (module ${b_modules}) add_subdirectory(${module}) if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${module}-test) message(STATUS "Building unit tests for module ${module}") - add_executable(blue-${module}-test - ${module}-test/${module}-test.c - misc/AllTests.c - misc/CuTest.c - misc/CuTest.h) - target_link_libraries(blue-${module}-test blue-${module}) - target_include_directories(blue-${module}-test PRIVATE misc/) + if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${module}-test/${module}-units.c) + add_executable(blue-${module}-units + ${module}-test/${module}-units.c + misc/AllTests.c + misc/CuTest.c + misc/CuTest.h) + target_link_libraries(blue-${module}-units blue-${module}) + target_include_directories(blue-${module}-units PRIVATE misc/) + endif () + + file(GLOB test_sources ${module}-test/*.c) + list(REMOVE_ITEM test_sources "${CMAKE_CURRENT_SOURCE_DIR}/${module}-test/${module}-units.c") + + foreach (test_file ${test_sources}) + get_filename_component(test_name ${test_file} NAME_WE) + add_executable(blue-${module}-${test_name} ${test_file}) + + target_link_libraries(blue-${module}-${test_name} blue-${module}) + endforeach (test_file) endif () endforeach (module) diff --git a/core-test/core-test.c b/core-test/core-units.c similarity index 100% rename from core-test/core-test.c rename to core-test/core-units.c diff --git a/core-test/randomise.c b/core-test/randomise.c new file mode 100644 index 0000000..2f3abcd --- /dev/null +++ b/core-test/randomise.c @@ -0,0 +1,39 @@ +#include +#include + +#define NRAND_NUMBERS 12 +#define NRAND_BYTES 128 +#define NRAND_DOUBLES 8 + +int main(void) +{ + b_random_ctx random; + b_random_init(&random, B_RANDOM_SECURE | B_RANDOM_MT19937); + + printf("generating %d random numbers:\n", NRAND_NUMBERS); + for (int i = 0; i < NRAND_NUMBERS; i++) { + unsigned long long v = b_random_next_int64(&random); + printf(" %llu\n", v); + } + + printf("\ngenerating %d random bytes:", NRAND_BYTES); + unsigned char bytes[16]; + for (int i = 0; i < NRAND_BYTES; i++) { + if (i == 0 || (i % 16) == 0) { + printf("\n "); + b_random_next_bytes(&random, bytes, sizeof bytes); + } else if ((i % 4) == 0) { + printf(" "); + } + + printf("%02x", bytes[i % 16]); + } + + printf("\n\ngenerating %d random doubles:\n", NRAND_DOUBLES); + for (int i = 0; i < NRAND_DOUBLES; i++) { + double v = b_random_next_double(&random); + printf(" %lf\n", v); + } + + return 0; +} diff --git a/object-test/arrays.c b/object-test/arrays.c new file mode 100644 index 0000000..60626a4 --- /dev/null +++ b/object-test/arrays.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int main(void) +{ + b_array *array = b_array_create(); + b_array_append(array, B_RV_INT(32)); + b_array_append(array, B_RV_INT(64)); + b_array_append(array, B_RV_INT(128)); + + b_array_iterator it; + b_array_foreach(&it, array) + { + printf("object %p\n", it.value); + } + + return 0; +} diff --git a/object-test/numbers.c b/object-test/numbers.c new file mode 100644 index 0000000..d1f6c1c --- /dev/null +++ b/object-test/numbers.c @@ -0,0 +1,10 @@ +#include +#include + +int main(void) +{ + b_number *number = b_number_create_float(6.8); + + printf("number=%zd\n", B_NUMBER_IVAL(number)); + return 0; +} diff --git a/object-test/object-test.c b/object-test/object-units.c similarity index 100% rename from object-test/object-test.c rename to object-test/object-units.c diff --git a/object-test/strings.c b/object-test/strings.c new file mode 100644 index 0000000..12f9fd7 --- /dev/null +++ b/object-test/strings.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int main(void) +{ + b_string *str = b_string_create_from_cstr("Hello, world!\n"); + b_string_insert_cstr(str, "WOW!", 4); + + printf("%s\n", b_string_ptr(str)); + + b_string_release(str); + + b_stringstream strv; + b_stringstream_begin_dynamic(&strv); + b_stringstream_add_many(&strv, "Hello", ", world", "!", NULL); + char *s = b_stringstream_end(&strv); + + printf("%s\n", s); + free(s); + + return 0; +} diff --git a/object-test/trees.c b/object-test/trees.c new file mode 100644 index 0000000..6a1508a --- /dev/null +++ b/object-test/trees.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include + +#define NITEMS 16 + +struct tree_item { + int value; + b_tree_node node; +}; + +struct btree_item { + int value; + b_btree_node node; +}; + +B_BTREE_DEFINE_SIMPLE_GET(struct btree_item, int, node, value, get_node) +B_BTREE_DEFINE_SIMPLE_INSERT(struct btree_item, node, value, put_node) + +int main(void) +{ + b_dict_item items[] + = {B_DICT_ITEM("hello", B_RV_INT(32)), + B_DICT_ITEM("world", B_RV_INT(64)), + B_DICT_ITEM("more", B_RV_INT(128)), + B_DICT_ITEM("other", B_RV_INT(256)), B_DICT_ITEM_END}; + + b_dict *dict = b_dict_create_with_items(items); + + b_dict_iterator it; + b_dict_foreach(&it, dict) + { + printf("item %zu: %s=%d\n", it.i, it.key, + b_number_get_int(B_NUMBER(it.value))); + } + + b_tree *tree = b_tree_create(); + struct tree_item items2[NITEMS]; + + for (int i = 0; i < NITEMS; i++) { + items2[i].value = i; + items2[i].node = B_TREE_NODE_INIT; + } + + b_tree_set_root(tree, &items2[0].node); + + b_tree_node_add_child(&items2[0].node, &items2[1].node); + b_tree_node_add_child(&items2[0].node, &items2[2].node); + b_tree_node_add_child(&items2[0].node, &items2[3].node); + b_tree_node_add_child(&items2[0].node, &items2[7].node); + b_tree_node_add_child(&items2[1].node, &items2[4].node); + b_tree_node_add_child(&items2[1].node, &items2[5].node); + b_tree_node_add_child(&items2[4].node, &items2[6].node); + + b_tree_iterator it2; + b_tree_foreach(&it2, tree) + { + struct tree_item *item = b_unbox(struct tree_item, it2.node, node); + + for (size_t i = 0; i < it2.depth; i++) { + fputs(" ", stdout); + } + + printf("%u\n", item->value); + } + + b_btree btree = {}; + struct btree_item items3[NITEMS] = {}; + for (int i = 0; i < NITEMS; i++) { + items3[i].value = i; + put_node(&btree, &items3[i]); + } + + printf("\n\n"); + + b_btree_iterator it3; + b_btree_foreach (&it3, &btree) { + struct btree_item *item + = b_unbox(struct btree_item, it3.node, node); + + for (size_t i = 0; i < it3.depth; i++) { + fputs(" ", stdout); + } + + printf("%d\n", item->value); + } + + b_btree_iterator_begin(&btree, &it3); + while (b_btree_iterator_is_valid(&it3)) { + struct btree_item *item + = b_unbox(struct btree_item, it3.node, node); + + if (item->value == 9) { + b_btree_iterator_erase(&it3); + } else { + b_btree_iterator_next(&it3); + } + } + + printf("\n\n"); + + b_btree_foreach (&it3, &btree) { + struct btree_item *item + = b_unbox(struct btree_item, it3.node, node); + + for (size_t i = 0; i < it3.depth; i++) { + fputs(" ", stdout); + } + + printf("%d\n", item->value); + } + + return 0; +} diff --git a/object-test/uuids.c b/object-test/uuids.c new file mode 100644 index 0000000..506b358 --- /dev/null +++ b/object-test/uuids.c @@ -0,0 +1,12 @@ +#include +#include + +int main(void) +{ + b_uuid *uuid = b_uuid_create_from_cstr( + "5b80ad1f-367f-4a1f-88f3-b3a6f8d1f63d"); + char str[B_UUID_STRING_MAX]; + b_uuid_to_cstr(uuid, str); + printf("%s\n", str); + return 0; +}