Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 1:adcfad6e86fb | 2:8a43dedbe530 |
|---|---|
| 1 #include "dowa.h" | |
| 2 | |
| 3 static void TestArena() { | |
| 4 const size_t capacity = 64; | |
| 5 Dowa_PArena arena = Dowa_Arena_Initialize(capacity); | |
| 6 assert(arena != NULL); | |
| 7 assert(arena->offset == 0); | |
| 8 assert(arena->capacity == capacity); | |
| 9 | |
| 10 // Allocate within capacity | |
| 11 void *p1 = Dowa_Arena_Allocate(arena, 16); | |
| 12 // assert(p1 != NULL); | |
| 13 assert(arena->offset == 16); | |
| 14 | |
| 15 // Allocate more | |
| 16 void *p2 = Dowa_Arena_Allocate(arena, 32); | |
| 17 assert(p2 != NULL); | |
| 18 assert(arena->offset == 48); | |
| 19 | |
| 20 // Overflow allocation should return NULL and not advance offset | |
| 21 void *p3 = Dowa_Arena_Allocate(arena, 20); | |
| 22 assert(p3 == NULL); | |
| 23 assert(arena->offset == 48); | |
| 24 | |
| 25 Dowa_Arena_Free(arena); | |
| 26 } | |
| 27 | |
| 28 static void TestHashMap() { | |
| 29 const size_t capacity = 10; | |
| 30 Dowa_PHashMap map = Dowa_HashMap_Create(capacity); | |
| 31 assert(map != NULL); | |
| 32 assert(map->capacity == capacity); | |
| 33 assert(map->current_capacity == 0); | |
| 34 | |
| 35 // Insert "foo" -> 42 | |
| 36 int val1 = 42; | |
| 37 Dowa_HashMap_PushValue(map, "foo", &val1, sizeof(val1)); | |
| 38 assert(map->current_capacity == 1); | |
| 39 | |
| 40 int idx_foo = Dowa_HashMap_GetPosition(map, "foo"); | |
| 41 Dowa_PHashEntry e_foo = map->entries[idx_foo]; | |
| 42 assert(e_foo != NULL); | |
| 43 assert(strcmp(e_foo->key, "foo") == 0); | |
| 44 assert(*(int*)e_foo->buffer == 42); | |
| 45 | |
| 46 // Overwrite "foo" -> 100 | |
| 47 int val2 = 100; | |
| 48 Dowa_HashMap_PushValue(map, "foo", &val2, sizeof(val2)); | |
| 49 // current_capacity increments again according to your implementation | |
| 50 assert(map->current_capacity == 2); | |
| 51 | |
| 52 Dowa_PHashEntry e_foo2 = map->entries[idx_foo]; | |
| 53 assert(e_foo2 != NULL); | |
| 54 assert(strcmp(e_foo2->key, "foo") == 0); | |
| 55 assert(*(int*)e_foo2->buffer == 100); | |
| 56 | |
| 57 // Insert "bar" -> -7 | |
| 58 int val3 = -7; | |
| 59 Dowa_HashMap_PushValue(map, "bar", &val3, sizeof(val3)); | |
| 60 assert(map->current_capacity == 3); | |
| 61 | |
| 62 int idx_bar = Dowa_HashMap_GetPosition(map, "bar"); | |
| 63 Dowa_PHashEntry e_bar = map->entries[idx_bar]; | |
| 64 assert(e_bar != NULL); | |
| 65 assert(strcmp(e_bar->key, "bar") == 0); | |
| 66 assert(*(int*)e_bar->buffer == -7); | |
| 67 | |
| 68 // Pop "foo" | |
| 69 Dowa_HashMap_PopKey(map, "foo"); | |
| 70 assert(map->entries[idx_foo] == NULL); | |
| 71 assert(map->current_capacity == 2); | |
| 72 | |
| 73 // Pop "bar" | |
| 74 Dowa_HashMap_PopKey(map, "bar"); | |
| 75 assert(map->entries[idx_bar] == NULL); | |
| 76 assert(map->current_capacity == 1); | |
| 77 | |
| 78 // Clean up remaining entries and the map itself | |
| 79 for (size_t i = 0; i < map->capacity; ++i) { | |
| 80 Dowa_PHashEntry ent = map->entries[i]; | |
| 81 if (ent) { | |
| 82 free(ent->key); | |
| 83 free(ent->buffer); | |
| 84 free(ent); | |
| 85 } | |
| 86 } | |
| 87 free(map->entries); | |
| 88 free(map); | |
| 89 } | |
| 90 | |
| 91 int main(void) { | |
| 92 TestArena(); | |
| 93 printf("Arena tests passed.\n"); | |
| 94 | |
| 95 TestHashMap(); | |
| 96 printf("HashMap tests passed.\n"); | |
| 97 | |
| 98 return 0; | |
| 99 } | |
| 100 |