Growing array + tests + growing_array_example.c

This commit is contained in:
Charlie Malmqvist 2024-07-23 21:18:27 +02:00
parent f36e2b73a7
commit c7daaac338
6 changed files with 77 additions and 7 deletions

2
TODO
View file

@ -41,7 +41,7 @@
- Arenas
- Dynamic array
- Needs testing:
- Audio format channel conversions

View file

@ -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"

View file

@ -289,6 +289,7 @@ typedef u8 bool;
#include "utility.c"
#include "hash_table.c"
#include "growing_array.c"
#include "os_interface.c"

View file

@ -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);
}

View file

@ -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(&copy, 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(&copy, 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");
}

View file

@ -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; }