Mercurial
diff dowa/dowa_test.c @ 71:75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sun, 28 Dec 2025 20:34:22 -0800 |
| parents | ecb6ee6a22c3 |
| children | 4532ce6d9eb8 |
line wrap: on
line diff
--- a/dowa/dowa_test.c Thu Dec 25 20:10:46 2025 -0800 +++ b/dowa/dowa_test.c Sun Dec 28 20:34:22 2025 -0800 @@ -7,116 +7,211 @@ int main(void) { - // --- Test Arena Allocator --- - Dowa_Arena *arena = Dowa_Arena_Create(64); - assert(arena && "Arena creation failed"); + printf("=== Testing NEW dowa API (stb_ds style) ===\n\n"); - 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); + // --- Test Arena Allocator --- + printf("Testing Arena Allocator...\n"); + Dowa_Arena *p_arena = Dowa_Arena_Create(64); + assert(p_arena && "Arena creation failed"); - 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); + char *p_arena_mem1 = (char *)Dowa_Arena_Allocate(p_arena, 16); + assert(p_arena_mem1 && "Arena allocation #1 failed"); + strcpy(p_arena_mem1, "hello arena"); + printf(" [Arena Allocate] mem1 = \"%s\"\n", p_arena_mem1); - Dowa_Arena_Destroy(arena); - printf("[Arena] destroyed\n\n"); + char *p_arena_mem2 = (char *)Dowa_Arena_Allocate(p_arena, 8); + assert(p_arena_mem2 && "Arena allocation #2 failed"); + strcpy(p_arena_mem2, "data"); + printf(" [Arena Allocate] mem2 = \"%s\"\n", p_arena_mem2); - // --- Test HashMap Basic Operations --- - Dowa_HashMap *map = Dowa_HashMap_Create(8); - assert(map && "HashMap_Create failed"); + Dowa_Arena_Destroy(p_arena); + printf(" [Arena] destroyed\n\n"); - // 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)); + // --- Test Array Operations --- + printf("Testing Array Operations...\n"); + int32* p_numbers = NULL; - // 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); + Dowa_Array_Push(p_numbers, 10); + Dowa_Array_Push(p_numbers, 20); + Dowa_Array_Push(p_numbers, 30); + Dowa_Array_Push(p_numbers, 40); - // Push nested hashmap (no copy) - Dowa_HashMap *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); + printf(" Array length: %zu\n", Dowa_Array_Length(p_numbers)); + printf(" Array capacity: %zu\n", Dowa_Array_Capacity(p_numbers)); + printf(" Array contents:"); + for (size_t i = 0; i < Dowa_Array_Length(p_numbers); i++) + printf(" %d", p_numbers[i]); + printf("\n"); + + int32 popped = Dowa_Array_Pop(p_numbers); + printf(" Popped value: %d\n", popped); + printf(" Array length after pop: %zu\n", Dowa_Array_Length(p_numbers)); + + Dowa_Array_Clear(p_numbers); + printf(" Array length after clear: %zu\n", Dowa_Array_Length(p_numbers)); - // Push integer with INT type - int32 number = 42; - Dowa_HashMap_Push_Value_With_Type(map, "answer", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); + Dowa_Array_Free(p_numbers); + printf(" Array freed\n\n"); + + // --- Test HashMap Basic Operations (String Keys) --- + printf("Testing HashMap with String Keys...\n"); + Dowa_KV(char*, char*)* p_map = NULL; - // Print full map - printf("=== Map After Inserts ===\n"); - Dowa_HashMap_Print(map); + Dowa_HashMap_Push(p_map, "name", "John"); + Dowa_HashMap_Push(p_map, "city", "New York"); + Dowa_HashMap_Push(p_map, "country", "USA"); - // Retrieve and validate values + printf(" HashMap count: %zu\n", Dowa_HashMap_Count(p_map)); + + void* p_kv = Dowa_HashMap_Get_Ptr(p_map, "name"); + if (p_kv) { - // 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"); + Dowa_KV(char*, char*)* p_pair = (Dowa_KV(char*, char*)*)p_kv; + printf(" HashMap['name']: %s\n", p_pair->value); + } - // greeting - char *g = Dowa_HashMap_Get(map, "greeting"); - printf("[Get greeting] \"%s\"\n", g); - - // nested hashmap - Dowa_HashMap *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); + p_kv = Dowa_HashMap_Get_Ptr(p_map, "city"); + if (p_kv) + { + Dowa_KV(char*, char*)* p_pair = (Dowa_KV(char*, char*)*)p_kv; + printf(" HashMap['city']: %s\n", p_pair->value); } - // 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")); + boolean has_name = Dowa_HashMap_Has_Key(p_map, "name"); + printf(" Has key 'name': %d\n", has_name); + + boolean has_missing = Dowa_HashMap_Has_Key(p_map, "missing"); + printf(" Has key 'missing': %d\n", has_missing); + + printf(" Iterating over HashMap:\n"); + size_t map_length = Dowa_Array_Length(p_map); + for (size_t i = 0; i < map_length; i++) + printf(" [%zu] '%s' => '%s'\n", i, p_map[i].key, p_map[i].value); + + Dowa_HashMap_Delete(p_map, "city"); + printf(" After deleting 'city', count: %zu\n", Dowa_HashMap_Count(p_map)); + + Dowa_HashMap_Clear(p_map); + printf(" After clear, count: %zu\n", Dowa_HashMap_Count(p_map)); + + Dowa_HashMap_Free(p_map); + printf(" HashMap freed\n\n"); + + // --- Test HashMap with Int Keys (Binary) --- + printf("Testing HashMap with Int Keys...\n"); + Dowa_KV(int32, char*)* p_int_map = NULL; + + int32 key1 = 1; + int32 key2 = 2; + int32 key3 = 3; - // Pop a key - Dowa_HashMap_Pop_Key(map, "raw"); - printf("=== Map After Pop 'raw' ===\n"); - Dowa_HashMap_Print(map); + Dowa_HashMap_Push_Binary(p_int_map, &key1, sizeof(int32), "one"); + Dowa_HashMap_Push_Binary(p_int_map, &key2, sizeof(int32), "two"); + Dowa_HashMap_Push_Binary(p_int_map, &key3, sizeof(int32), "three"); + + printf(" Int map count: %zu\n", Dowa_HashMap_Count(p_int_map)); + + p_kv = Dowa_HashMap_Get_Ptr_Binary(p_int_map, &key1, sizeof(int32)); + if (p_kv) + { + Dowa_KV(int32, char*)* p_pair = (Dowa_KV(int32, char*)*)p_kv; + printf(" Int map[1]: '%s'\n", p_pair->value); + } + + p_kv = Dowa_HashMap_Get_Ptr_Binary(p_int_map, &key2, sizeof(int32)); + if (p_kv) + { + Dowa_KV(int32, char*)* p_pair = (Dowa_KV(int32, char*)*)p_kv; + printf(" Int map[2]: '%s'\n", p_pair->value); + } + + printf(" Iterating over Int HashMap:\n"); + map_length = Dowa_Array_Length(p_int_map); + for (size_t i = 0; i < map_length; i++) + printf(" [%zu] %d => '%s'\n", i, p_int_map[i].key, p_int_map[i].value); + + Dowa_HashMap_Free(p_int_map); + printf(" Int map freed\n\n"); // --- Test HashMap with Arena --- - Dowa_Arena *mapArena = Dowa_Arena_Create(256); - Dowa_HashMap *map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); - assert(map2 && "HashMap_Create_With_Arena failed"); - char *test = "bar"; + printf("Testing HashMap with Arena...\n"); + Dowa_Arena *p_map_arena = Dowa_Arena_Create(1024); + Dowa_KV(char*, int32) *p_arena_map = NULL; - 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); + Dowa_HashMap_Push_Arena(p_arena_map, "x", 100, p_map_arena); + Dowa_HashMap_Push_Arena(p_arena_map, "y", 200, p_map_arena); + Dowa_HashMap_Push_Arena(p_arena_map, "z", 300, p_map_arena); - printf("=== Map2 (with arena) ===\n"); - Dowa_HashMap_Print(map2); + printf(" Arena map count: %zu\n", Dowa_HashMap_Count(p_arena_map)); - Dowa_HashMap_Destroy(map2); - printf("[Map2] destroyed (no change)\n\n"); - Dowa_Arena_Destroy(mapArena); - printf("[Arena] destroyed (all null)\n\n"); + p_kv = Dowa_HashMap_Get_Ptr(p_arena_map, "x"); + if (p_kv) + { + Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv; + printf(" Arena map['x']: %d\n", p_pair->value); + } - // --- 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) + p_kv = Dowa_HashMap_Get_Ptr(p_arena_map, "y"); + if (p_kv) { - 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"); + Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv; + printf(" Arena map['y']: %d\n", p_pair->value); } - // Cleanup - Dowa_HashMap_Destroy(map); - printf("[Map] destroyed\n"); + printf(" Iterating over Arena HashMap:\n"); + map_length = Dowa_Array_Length(p_arena_map); + for (size_t i = 0; i < map_length; i++) + printf(" [%zu] '%s' => %d\n", i, p_arena_map[i].key, p_arena_map[i].value); + + Dowa_Arena_Destroy(p_map_arena); + printf(" Arena destroyed (including map)\n\n"); + + // --- Test Array with Arena --- + printf("Testing Array with Arena...\n"); + Dowa_Arena *p_array_arena = Dowa_Arena_Create(1024); + int32* p_arena_numbers = NULL; + + Dowa_Array_Push_Arena(p_arena_numbers, 5, p_array_arena); + Dowa_Array_Push_Arena(p_arena_numbers, 10, p_array_arena); + Dowa_Array_Push_Arena(p_arena_numbers, 15, p_array_arena); + + printf(" Arena array length: %zu\n", Dowa_Array_Length(p_arena_numbers)); + printf(" Arena array contents:"); + for (size_t i = 0; i < Dowa_Array_Length(p_arena_numbers); i++) + printf(" %d", p_arena_numbers[i]); + printf("\n"); + + Dowa_Arena_Destroy(p_array_arena); + printf(" Arena destroyed (including array)\n\n"); + // --- Test Medium HashMap (Stress Test) --- + printf("Testing Medium HashMap (Stress Test)...\n"); + Dowa_KV(char*, int32)* p_large_map = NULL; + + char key_buffer[32]; + for (int32 i = 0; i < 100; i++) + { + sprintf(key_buffer, "key_%d", i); + Dowa_HashMap_Push(p_large_map, key_buffer, i * 10); + } + + printf(" Medium map count: %zu\n", Dowa_HashMap_Count(p_large_map)); + + sprintf(key_buffer, "key_50"); + p_kv = Dowa_HashMap_Get_Ptr(p_large_map, key_buffer); + if (p_kv) + { + Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv; + printf(" Medium map['key_50']: %d\n", p_pair->value); + } + + sprintf(key_buffer, "key_99"); + boolean has_99 = Dowa_HashMap_Has_Key(p_large_map, key_buffer); + printf(" Has key 'key_99': %d\n", has_99); + + Dowa_HashMap_Free(p_large_map); + printf(" Medium map freed\n\n"); + + printf("=== All tests passed! ===\n"); return 0; }