Mercurial
diff dowa/dowa_test.c @ 21:09def63429b9
[Dowa] Updated the naming scheme and tests.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:57:30 -0700 |
| parents | 3e12bf044589 |
| children | 947b81010aba |
line wrap: on
line diff
--- a/dowa/dowa_test.c Mon Oct 06 10:13:41 2025 -0700 +++ b/dowa/dowa_test.c Mon Oct 06 10:57:30 2025 -0700 @@ -1,116 +1,118 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> #include "dowa.h" -static void TestArena() +int main(void) { - const size_t capacity = 64; - Dowa_PArena arena = Dowa_Arena_Create(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); - sprintf((char *)p1, "%i", 10); - assert(strcmp(p1, "10") == 0); - - // Allocate more - void *p2 = Dowa_Arena_Allocate(arena, 32); - assert(p2 != NULL); - assert(arena->offset == 48); + // --- Test Arena Allocator --- + Dowa_PArena arena = Dowa_Arena_Create(64); + assert(arena && "Arena creation failed"); - // 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); -} + char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16); + assert(arena_mem1 && "Arena allocation #1 failed"); + strcpy(arena_mem1, "hello arena"); + printf("[Arena Allocate] mem1 = \"%s\"\n", arena_mem1); -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); + char *arena_mem2 = (char *)Dowa_Arena_Allocate(arena, 8); + assert(arena_mem2 && "Arena allocation #2 failed"); + strcpy(arena_mem2, "data"); + printf("[Arena Allocate] mem2 = \"%s\"\n", arena_mem2); - // 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); + Dowa_Arena_Destroy(arena); + printf("[Arena] destroyed\n\n"); - // Overwrite "foo" -> 100 (capacity should not change) - int val2 = 100; - Dowa_HashMap_PushValue(map, "foo", &val2, sizeof(val2)); - assert(map->current_capacity == 1); - - e_foo = map->entries[idx_foo]; - assert(e_foo != NULL); - assert(strcmp(e_foo->key, "foo") == 0); - assert(*(int*)e_foo->buffer == 100); + // --- Test HashMap Basic Operations --- + Dowa_PHashMap map = Dowa_HashMap_Create(8); + assert(map && "HashMap_Create failed"); - // Insert "bar" -> -7 - int val3 = -7; - Dowa_HashMap_PushValue(map, "bar", &val3, sizeof(val3)); - assert(map->current_capacity == 2); + // Push raw buffer (default type: BUFFER) + const char raw_buf[] = {0x01, 0x02, 0x03, 0x04}; + Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf)); - 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); + // Push string with explicit STRING type + const char *hello = "hello, world"; + Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING); - // Pop "foo" - Dowa_HashMap_PopKey(map, "foo"); - assert(map->entries[idx_foo] == NULL); - assert(map->current_capacity == 1); - - // Pop "bar" - Dowa_HashMap_PopKey(map, "bar"); - assert(map->entries[idx_bar] == NULL); - assert(map->current_capacity == 0); + // Push nested hashmap (no copy) + Dowa_PHashMap inner = Dowa_HashMap_Create(4); + Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1); + Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP); - // Clean up map - Dowa_HashMap_Free(map); -} - -void Test_Dowa_HashMap_Cache_Folder() -{ - Dowa_PHashMap map = Dowa_HashMap_Create(100); + // Push integer with INT type + int32 number = 42; + Dowa_HashMap_Push_Value_With_Type(map, "answer", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); - int res = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); - assert(res == 0 && "Folder caching should succeed"); - - const char *foo_val = (const char *)Dowa_HashMap_Get(map, "foo.txt"); - assert(foo_val != NULL); - assert(strcmp("this is foo\n", foo_val)==0); - + // Print full map + printf("=== Map After Inserts ===\n"); Dowa_HashMap_Print(map); - Dowa_PHashMap p_bar_map = (Dowa_PHashMap)Dowa_HashMap_Get(map, "bar"); - assert(p_bar_map != NULL); - assert(strcmp("this is bar\n", (const char *)Dowa_HashMap_Get(p_bar_map, "bar.txt"))==0); + // Retrieve and validate values + { + // raw buffer + uint8 *r = Dowa_HashMap_Get(map, "raw"); + printf("[Get raw] bytes:"); + for (size_t i = 0; i < sizeof(raw_buf); ++i) { + printf(" %02X", r[i]); + } + printf("\n"); + + // greeting + char *g = Dowa_HashMap_Get(map, "greeting"); + printf("[Get greeting] \"%s\"\n", g); - Dowa_HashMap_Free(map); -} + // nested hashmap + Dowa_PHashMap ni = Dowa_HashMap_Get(map, "nested"); + printf("[Get nested] Inner map contents:\n"); + Dowa_HashMap_Print(ni); + + // answer + int32 *ans = Dowa_HashMap_Get(map, "answer"); + printf("[Get answer] %d\n", *ans); + } + + // Test Get_Position + printf("[Get_Position] 'greeting' at index %d\n", + Dowa_HashMap_Get_Position(map, "greeting")); + printf("[Get_Position] 'missing' at index %d\n", + Dowa_HashMap_Get_Position(map, "missing")); -int main(void) { - TestArena(); - printf("Arena tests passed.\n"); + // Pop a key + Dowa_HashMap_Pop_Key(map, "raw"); + printf("=== Map After Pop 'raw' ===\n"); + Dowa_HashMap_Print(map); + + // --- Test HashMap with Arena --- + Dowa_PArena mapArena = Dowa_Arena_Create(256); + Dowa_PHashMap map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); + assert(map2 && "HashMap_Create_With_Arena failed"); + char *test = "bar"; + + Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING); + Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); + + printf("=== Map2 (with arena) ===\n"); + Dowa_HashMap_Print(map2); - TestHashMap(); - printf("HashMap tests passed.\n"); + Dowa_HashMap_Destroy(map2); + Dowa_Arena_Destroy(mapArena); + printf("[Map2 & Arena] destroyed\n\n"); - Test_Dowa_HashMap_Cache_Folder(); - printf("Test_Dowa_HashMap_Cache_Folder passed\n"); + // --- Test Cache_Folder --- + // Ensure there is a directory "./dowa/test_folder" with some files for this test to succeed. + int cache_result = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); + printf("[Cache_Folder] returned %d\n", cache_result); + if (cache_result == 0) { + printf("=== Map After Caching 'dowa/test_folder' ===\n"); + Dowa_HashMap_Print(map); + } else { + printf("Cache_Folder failed (ensure 'dowa/test_folder' exists with files)\n"); + } + + // Cleanup + Dowa_HashMap_Destroy(map); + printf("[Map] destroyed\n"); + return 0; } -