diff --git a/TODO b/TODO index 994addc..ba73615 100644 --- a/TODO +++ b/TODO @@ -41,7 +41,7 @@ - Arenas - +- Dynamic array - Needs testing: - Audio format channel conversions diff --git a/build.c b/build.c index afdc585..fbb44aa 100644 --- a/build.c +++ b/build.c @@ -3,8 +3,6 @@ /// // Build config stuff -#define RUN_TESTS 1 - #define INITIAL_PROGRAM_MEMORY_SIZE MB(5) // You might want to increase this if you get a log warning saying the temporary storage was overflown. @@ -39,10 +37,11 @@ typedef struct Context_Extra { // #include "oogabooga/examples/text_rendering.c" // #include "oogabooga/examples/custom_logger.c" -#include "oogabooga/examples/renderer_stress_test.c" +// #include "oogabooga/examples/renderer_stress_test.c" // #include "oogabooga/examples/tile_game.c" // #include "oogabooga/examples/audio_test.c" // #include "oogabooga/examples/custom_shader.c" +#include "oogabooga/examples/growing_array_example.c" // This is where you swap in your own project! // #include "entry_yourepicgamename.c" diff --git a/oogabooga/oogabooga.c b/oogabooga/oogabooga.c index 12a6c33..171288c 100644 --- a/oogabooga/oogabooga.c +++ b/oogabooga/oogabooga.c @@ -289,6 +289,7 @@ typedef u8 bool; #include "utility.c" #include "hash_table.c" +#include "growing_array.c" #include "os_interface.c" diff --git a/oogabooga/os_interface.c b/oogabooga/os_interface.c index 5345fb2..e2d36e5 100644 --- a/oogabooga/os_interface.c +++ b/oogabooga/os_interface.c @@ -81,8 +81,6 @@ Os_Window window; #endif // NOT OOGABOOGA_LINK_EXTERNAL_INSTANCE -inline bool bytes_match(void *a, void *b, u64 count) { return memcmp(a, b, count) == 0; } - inline int vsnprintf(char* buffer, size_t n, const char* fmt, va_list args) { return os.crt_vsnprintf(buffer, n, fmt, args); } diff --git a/oogabooga/tests.c b/oogabooga/tests.c index 31dc736..be108bc 100644 --- a/oogabooga/tests.c +++ b/oogabooga/tests.c @@ -1223,9 +1223,78 @@ void test_sort() { } #endif /* OOGABOOGA_HEADLESS */ +typedef struct Test_Thing { + int foo; + float bar; +} Test_Thing; +void test_growing_array() { + Test_Thing *things = 0; + + growing_array_init((void**)&things, sizeof(Test_Thing), get_heap_allocator()); + + Test_Thing new_thing; + new_thing.foo = 5; + new_thing.bar = 420.69; + growing_array_add((void**)&things, &new_thing); + + assert(growing_array_get_valid_count(things) == 1, "Failed: growing_array_get_valid_count"); + + new_thing.foo = 1; + new_thing.bar = 123.45; + growing_array_add((void**)&things, &new_thing); + + assert(growing_array_get_valid_count(things) == 2, "Failed: growing_array_get_valid_count"); + + assert(things[0].foo == 5 && floats_roughly_match(things[0].bar, 420.69), "Failed: growing_array_add"); + assert(things[1].foo == 1 && floats_roughly_match(things[1].bar, 123.45), "Failed: growing_array_add"); + + growing_array_ordered_remove_by_index((void**)&things, 0); + assert(things[0].foo == 1 && floats_roughly_match(things[0].bar, 123.45), "Failed: growing_array_ordered_remove_by_index"); + assert(growing_array_get_valid_count(things) == 1, "Failed: growing_array_get_valid_count"); + + new_thing.foo = 5; + new_thing.bar = 420.69; + growing_array_add((void**)&things, &new_thing); + assert(things[1].foo == 5 && floats_roughly_match(things[1].bar, 420.69), "Failed: growing_array_add"); + + assert(growing_array_get_valid_count(things) == 2, "Failed: growing_array_get_valid_count"); + + growing_array_unordered_remove_by_index((void**)&things, 0); + assert(things[0].foo == 5 && floats_roughly_match(things[0].bar, 420.69), "Failed: growing_array_unordered_remove_by_index"); + assert(growing_array_get_valid_count(things) == 1, "Failed: growing_array_get_valid_count"); + + + for (u32 i = 0; i < 100; i += 1) { + new_thing.foo = i; + new_thing.bar = i * 4.0; + growing_array_add((void**)&things, &new_thing); + } + + assert(growing_array_get_valid_count(things) == 101, "Failed: growing_array_get_valid_count"); + + // Unordered remove by pointer + Test_Thing *thing = &things[50]; + Test_Thing copy = *thing; + bool found = growing_array_unordered_remove_by_pointer((void**)&things, thing); + assert(found, "Failed: growing_array_unordered_remove_by_pointer"); + assert(!bytes_match(©, thing, sizeof(Test_Thing)), "Failed: growing_array_unordered_remove_by_pointer"); + + // Ordered remove by pointer + thing = &things[50]; + copy = *thing; + found = growing_array_ordered_remove_by_pointer((void**)&things, thing); + assert(found, "Failed: growing_array_unordered_remove_by_pointer"); + assert(!bytes_match(©, thing, sizeof(Test_Thing)), "Failed: growing_array_unordered_remove_by_pointer"); + + assert(growing_array_get_valid_count(things) == 99, "Failed: growing_array_get_valid_count"); +} + void oogabooga_run_tests() { - + print("Testing growing array... "); + test_growing_array(); + print("OK!\n"); + print("Testing allocator... "); test_allocator(true); print("OK!\n"); @@ -1267,6 +1336,8 @@ void oogabooga_run_tests() { test_sort(); print("OK!\n"); #endif + + print("All tests ok!\n"); } \ No newline at end of file diff --git a/oogabooga/utility.c b/oogabooga/utility.c index e2b8b07..32ef337 100644 --- a/oogabooga/utility.c +++ b/oogabooga/utility.c @@ -98,3 +98,4 @@ void merge_sort(void *collection, void *help_buffer, u64 item_count, u64 item_si } } +inline bool bytes_match(void *a, void *b, u64 count) { return memcmp(a, b, count) == 0; } \ No newline at end of file