Mercurial
view dowa/dowa_test.c @ 2:8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Sep 2025 13:15:32 -0700 |
| parents | |
| children | 3e12bf044589 |
line wrap: on
line source
#include "dowa.h" static void TestArena() { const size_t capacity = 64; Dowa_PArena arena = Dowa_Arena_Initialize(capacity); assert(arena != NULL); assert(arena->offset == 0); assert(arena->capacity == capacity); // Allocate within capacity void *p1 = Dowa_Arena_Allocate(arena, 16); // assert(p1 != NULL); assert(arena->offset == 16); // Allocate more void *p2 = Dowa_Arena_Allocate(arena, 32); assert(p2 != NULL); assert(arena->offset == 48); // Overflow allocation should return NULL and not advance offset void *p3 = Dowa_Arena_Allocate(arena, 20); assert(p3 == NULL); assert(arena->offset == 48); Dowa_Arena_Free(arena); } static void TestHashMap() { const size_t capacity = 10; Dowa_PHashMap map = Dowa_HashMap_Create(capacity); assert(map != NULL); assert(map->capacity == capacity); assert(map->current_capacity == 0); // Insert "foo" -> 42 int val1 = 42; Dowa_HashMap_PushValue(map, "foo", &val1, sizeof(val1)); assert(map->current_capacity == 1); int idx_foo = Dowa_HashMap_GetPosition(map, "foo"); Dowa_PHashEntry e_foo = map->entries[idx_foo]; assert(e_foo != NULL); assert(strcmp(e_foo->key, "foo") == 0); assert(*(int*)e_foo->buffer == 42); // Overwrite "foo" -> 100 int val2 = 100; Dowa_HashMap_PushValue(map, "foo", &val2, sizeof(val2)); // current_capacity increments again according to your implementation assert(map->current_capacity == 2); Dowa_PHashEntry e_foo2 = map->entries[idx_foo]; assert(e_foo2 != NULL); assert(strcmp(e_foo2->key, "foo") == 0); assert(*(int*)e_foo2->buffer == 100); // Insert "bar" -> -7 int val3 = -7; Dowa_HashMap_PushValue(map, "bar", &val3, sizeof(val3)); assert(map->current_capacity == 3); int idx_bar = Dowa_HashMap_GetPosition(map, "bar"); Dowa_PHashEntry e_bar = map->entries[idx_bar]; assert(e_bar != NULL); assert(strcmp(e_bar->key, "bar") == 0); assert(*(int*)e_bar->buffer == -7); // Pop "foo" Dowa_HashMap_PopKey(map, "foo"); assert(map->entries[idx_foo] == NULL); assert(map->current_capacity == 2); // Pop "bar" Dowa_HashMap_PopKey(map, "bar"); assert(map->entries[idx_bar] == NULL); assert(map->current_capacity == 1); // Clean up remaining entries and the map itself for (size_t i = 0; i < map->capacity; ++i) { Dowa_PHashEntry ent = map->entries[i]; if (ent) { free(ent->key); free(ent->buffer); free(ent); } } free(map->entries); free(map); } int main(void) { TestArena(); printf("Arena tests passed.\n"); TestHashMap(); printf("HashMap tests passed.\n"); return 0; }