summaryrefslogtreecommitdiff
path: root/project-base/tests
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2024-11-23 22:33:43 +0100
committerAldrik Ramaekers <aldrikboy@gmail.com>2024-11-23 22:33:43 +0100
commitb1e857cf1471d1871a9396696b22fa531da98249 (patch)
tree3923008a8653057698cb339faf6dcfa92e18364b /project-base/tests
parent106bb7fcadf637cec883648916cc8d19529d6199 (diff)
add projbase to repo
Diffstat (limited to 'project-base/tests')
-rw-r--r--project-base/tests/main.c44
-rw-r--r--project-base/tests/test_array.c71
-rw-r--r--project-base/tests/test_assets.c71
-rw-r--r--project-base/tests/test_settings_config.c36
-rw-r--r--project-base/tests/test_string_utils.c60
-rw-r--r--project-base/tests/test_threads.c32
-rw-r--r--project-base/tests/test_window.c20
7 files changed, 334 insertions, 0 deletions
diff --git a/project-base/tests/main.c b/project-base/tests/main.c
new file mode 100644
index 0000000..d7cfb60
--- /dev/null
+++ b/project-base/tests/main.c
@@ -0,0 +1,44 @@
+#include <projectbase/project_base.h>
+#include <projectbase/addons/test_helper.h>
+
+#define CONFIG_DIRECTORY "test_program_config"
+
+#include "test_string_utils.c"
+#include "test_threads.c"
+#include "test_window.c"
+#include "test_assets.c"
+#include "test_array.c"
+#include "test_settings_config.c"
+
+int main(int argc, char** argv) {
+
+ print_h1("String utils");
+ print_result("String to number", test_string_to_number());
+ print_result("String contains", test_string_contains());
+
+ print_h1("Threads");
+ print_result("Detached thread", test_detached_thread(argc, argv));
+ print_result("Joined thread", test_joined_thread(argc, argv));
+
+ print_h1("Platform");
+ print_result("Open window", test_open_window(argc, argv));
+
+ print_h1("Array");
+ print_result("Write", test_array_write());
+ print_result("Read", test_array_read());
+ print_result("Delete", test_array_delete());
+ print_result("Swap", test_array_swap());
+ print_result("Multi-threaded Write", test_array_thread_write());
+
+ print_h1("Settings Config");
+ print_result("Write", test_settings_config_write(argc, argv));
+ print_result("Read", test_settings_config_read(argc, argv));
+
+ print_h1("Assets");
+ print_result("Load image embedded", test_assets_load_embedded(argc, argv));
+ print_result("Load image from file", test_assets_load_file(argc, argv));
+ print_result("Queue & post-process", test_assets_queue_and_postprocess_count(argc, argv));
+
+ if (failure) exit(EXIT_FAILURE);
+ return 0;
+} \ No newline at end of file
diff --git a/project-base/tests/test_array.c b/project-base/tests/test_array.c
new file mode 100644
index 0000000..ec5e842
--- /dev/null
+++ b/project-base/tests/test_array.c
@@ -0,0 +1,71 @@
+array arr;
+s32 vals[] = {1,2,3,4,5,6,7,8,9};
+
+s32 test_array_write() {
+
+ arr = array_create(sizeof(s32));
+ error_if(!array_exists(&arr));
+
+ array_push(&arr, &vals[0]);
+ array_push(&arr, &vals[1]);
+ array_push(&arr, &vals[2]);
+ array_push(&arr, &vals[3]);
+ array_push(&arr, &vals[4]);
+ array_push(&arr, &vals[8]);
+
+ error_if(arr.length != 6);
+
+ success;
+}
+
+s32 test_array_read() {
+ error_if(*(s32*)array_at(&arr, 0) != 1);
+ error_if(*(s32*)array_at(&arr, 1) != 2);
+ error_if(*(s32*)array_at(&arr, 5) != 9);
+
+ success;
+}
+
+s32 test_array_delete() {
+ array_remove_at(&arr, 0);
+ error_if(*(s32*)array_at(&arr, 0) != 2);
+ error_if(arr.length != 5);
+
+ array_remove_by(&arr, &vals[1]);
+ error_if(*(s32*)array_at(&arr, 0) != 3);
+ error_if(arr.length != 4);
+
+ success;
+}
+
+s32 test_array_swap() {
+ array_swap(&arr, 0, 3);
+ error_if(*(s32*)array_at(&arr, 0) != 9);
+ error_if(*(s32*)array_at(&arr, 3) != 3);
+
+ success;
+}
+
+void *array_thread_write_imp(void *temp) {
+ s32 num = 3;
+
+ for (s32 i = 0; i < 2000; i++) {
+ array_push((array*)temp, &num);
+ }
+
+ return 0;
+}
+
+s32 test_array_thread_write() {
+ array temp = array_create(sizeof(s32));
+
+ thread t1 = thread_start(array_thread_write_imp, &temp);
+ thread t2 = thread_start(array_thread_write_imp, &temp);
+
+ thread_join(&t1);
+ thread_join(&t2);
+
+ error_if(temp.length != 4000);
+
+ success;
+} \ No newline at end of file
diff --git a/project-base/tests/test_assets.c b/project-base/tests/test_assets.c
new file mode 100644
index 0000000..22e0a3f
--- /dev/null
+++ b/project-base/tests/test_assets.c
@@ -0,0 +1,71 @@
+extern unsigned char _binary_examples_logo_64_bmp_start[];
+extern unsigned char _binary_examples_logo_64_bmp_end[];
+
+extern unsigned char _binary_examples_logo_64_png_start[];
+extern unsigned char _binary_examples_logo_64_png_end[];
+
+extern unsigned char _binary_examples_mono_ttf_start[];
+extern unsigned char _binary_examples_mono_ttf_end[];
+
+s32 test_assets_queue_and_postprocess_count(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ image* img = assets_load_bitmap(_binary_examples_logo_64_bmp_start, _binary_examples_logo_64_bmp_end);
+ image* img2 = assets_load_image(_binary_examples_logo_64_png_start, _binary_examples_logo_64_png_end);
+
+ thread_sleep(1000 * 50); // 50ms
+
+ error_if(global_asset_collection.images.length == 0);
+ error_if(global_asset_collection.post_process_queue.length != 2);
+ assets_do_post_process();
+ error_if(global_asset_collection.post_process_queue.length != 0);
+
+ platform_destroy();
+
+ success;
+}
+
+s32 test_assets_load_embedded(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ image* img = assets_load_bitmap(_binary_examples_logo_64_bmp_start, _binary_examples_logo_64_bmp_end);
+ image* img2 = assets_load_image(_binary_examples_logo_64_png_start, _binary_examples_logo_64_png_end);
+ font* fnt = assets_load_font(_binary_examples_mono_ttf_start, _binary_examples_mono_ttf_end, 36);
+ error_if(!img);
+ error_if(!img2);
+ error_if(!fnt);
+
+ thread_sleep(1000 * 50); // 50ms
+ assets_do_post_process();
+
+ error_if(!img->loaded);
+ error_if(!img2->loaded);
+ error_if(!fnt->loaded);
+
+ platform_destroy();
+
+ success;
+}
+
+s32 test_assets_load_file(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ image* img = assets_load_bitmap_from_file("logo_64.bmp");
+ image* img2 = assets_load_image_from_file("logo_64.png");
+ font* fnt = assets_load_font_from_file("mono.ttf", 48);
+ error_if(!img);
+ error_if(!img2);
+ error_if(!fnt);
+
+ thread_sleep(1000 * 50); // 50ms
+ assets_do_post_process();
+
+ error_if(!img->loaded);
+ error_if(!img2->loaded);
+ error_if(!fnt->loaded);
+
+ platform_destroy();
+
+ success;
+}
+
diff --git a/project-base/tests/test_settings_config.c b/project-base/tests/test_settings_config.c
new file mode 100644
index 0000000..b24e864
--- /dev/null
+++ b/project-base/tests/test_settings_config.c
@@ -0,0 +1,36 @@
+s32 test_settings_config_write(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ settings_set_number("number1", 1);
+ settings_set_number("number2", 2);
+ settings_set_string("string1", "String 123");
+ settings_set_string("string2", "321 String");
+
+ settings_write_to_file();
+
+ error_if(_settings_file.settings.length != 4);
+
+ platform_destroy();
+ success;
+}
+
+s32 test_settings_config_read(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ int num1 = settings_get_number("number1");
+ int num2 = settings_get_number("number2");
+
+ char* string1 = settings_get_string("string1");
+ char* string2 = settings_get_string("string2");
+
+ int def = settings_get_number_or_default("wrong", 15);
+
+ error_if(num1 != 1);
+ error_if(num2 != 2);
+ error_if(!string_equals(string1, "String 123"));
+ error_if(!string_equals(string2, "321 String"));
+ error_if(def != 15);
+
+ platform_destroy();
+ success;
+} \ No newline at end of file
diff --git a/project-base/tests/test_string_utils.c b/project-base/tests/test_string_utils.c
new file mode 100644
index 0000000..99cea65
--- /dev/null
+++ b/project-base/tests/test_string_utils.c
@@ -0,0 +1,60 @@
+
+s32 test_string_to_number()
+{
+ error_if(string_to_u64("5294967295") != 5294967295);
+ error_if(string_to_u32("294967295") != 294967295);
+ error_if(string_to_u16("2544") != 2544);
+ error_if(string_to_u8("244") != 244);
+
+ error_if(string_to_s64("-12") != -12);
+ error_if(string_to_s32("-12") != -12);
+ error_if(string_to_s16("-12") != -12);
+ error_if(string_to_s8("-12") != -12);
+
+ error_if(string_to_f32("-1.259223") != -1.259223f);
+ error_if(string_to_f64("346.12323") != 346.12323);
+
+ success;
+}
+
+s32 test_string_contains()
+{
+ error_if(string_contains("lll", "llll*"));
+ error_if(string_contains("lllll", "l*lop"));
+ error_if(string_contains("hello world", "h?lo"));
+ error_if(string_contains("hello world", "h*lo")); // The first L(1) matches with the first L(2), so the next check would be l == o, hence failure.
+ error_if(string_contains("lllll", "llll*l"));
+ error_if(string_contains("lllll", "*llllll*"));
+
+ error_if(!string_contains("22lllll", "l*l"));
+ error_if(!string_contains(" wsdf asd \"hello sailor\" asdf asdf ", "sailor"));
+ error_if(!string_contains(" wsdf asd \"hello sailor\" asdf asdf ", "*sailor"));
+ error_if(!string_contains(" wsdf asd \"hello sailor\" asdf asdf ", "*sailor\""));
+ error_if(!string_contains(" wsdf asd \"hello sailor\" asdf asdf ", "*sailor*"));
+ error_if(!string_contains(" wsdf asd \"hello sailor\" asdf asdf ", "sailor*"));
+ error_if(!string_contains("22lllll pi23hjp rbksje LSKJDh l", "LS*"));
+ error_if(!string_contains("22lllll lal", "l*l"));
+ error_if(!string_contains("lllll", "*l*l"));
+ error_if(!string_contains("hello world", "hello"));
+ error_if(!string_contains("hello world", "h?llo"));
+ error_if(!string_contains("hello world", "h????"));
+ error_if(!string_contains("hello world", "*"));
+ error_if(!string_contains("hello world", "h*"));
+ error_if(!string_contains("hello world", "*o"));
+ error_if(!string_contains("hello world", "h*o"));
+ error_if(!string_contains("hello world", "*lo"));
+ error_if(!string_contains("hello world", "hello"));
+ error_if(!string_contains("lllll", "l*l"));
+ error_if(!string_contains("lllll", "lllll"));
+ error_if(!string_contains("lllll", "l*lll"));
+ error_if(!string_contains("lllll", "*"));
+ error_if(!string_contains("lllll", "l?lll"));
+ error_if(!string_contains("lllll", "lllll"));
+ error_if(!string_contains("lllll", "*llll"));
+ error_if(!string_contains("lllll", "llll*"));
+ error_if(!string_contains("lllll", "*llll*"));
+ error_if(!string_contains("lllll", "*lllll*"));
+ error_if(!string_contains("lllll", "*ll*"));
+
+ success;
+} \ No newline at end of file
diff --git a/project-base/tests/test_threads.c b/project-base/tests/test_threads.c
new file mode 100644
index 0000000..869afdf
--- /dev/null
+++ b/project-base/tests/test_threads.c
@@ -0,0 +1,32 @@
+
+volatile s32 val = 0;
+
+static void* test_t(void *args) {
+ thread_sleep(2000);
+ val = 50;
+}
+
+s32 test_detached_thread(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ val = 0;
+ thread t = thread_start(test_t, 0);
+ thread_detach(&t);
+ error_if(val == 50);
+ thread_stop(&t);
+
+ platform_destroy();
+ success;
+}
+
+s32 test_joined_thread(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ val = 0;
+ thread t = thread_start(test_t, 0);
+ thread_join(&t);
+ error_if(val != 50);
+
+ platform_destroy();
+ success;
+} \ No newline at end of file
diff --git a/project-base/tests/test_window.c b/project-base/tests/test_window.c
new file mode 100644
index 0000000..537ac52
--- /dev/null
+++ b/project-base/tests/test_window.c
@@ -0,0 +1,20 @@
+static void update_render_main(platform_window* window)
+{
+
+}
+
+s32 test_open_window(int argc, char** argv) {
+ platform_init(argc, argv, CONFIG_DIRECTORY);
+
+ platform_window *window = platform_open_window("Example1",
+ 500, 500, 800, 600, 500, 500, update_render_main);
+
+ if (platform_is_graphical()) {
+ error_if(!platform_window_is_valid(window));
+ }
+
+ platform_destroy_window(window);
+ platform_destroy();
+
+ success;
+} \ No newline at end of file