Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 20:0a9e67c7039a | 21:09def63429b9 |
|---|---|
| 1 #include <stdio.h> | |
| 2 #include <stdlib.h> | |
| 3 #include <string.h> | |
| 4 #include <assert.h> | |
| 1 #include "dowa.h" | 5 #include "dowa.h" |
| 2 | 6 |
| 3 static void TestArena() | 7 int main(void) |
| 4 { | 8 { |
| 5 const size_t capacity = 64; | 9 // --- Test Arena Allocator --- |
| 6 Dowa_PArena arena = Dowa_Arena_Create(capacity); | 10 Dowa_PArena arena = Dowa_Arena_Create(64); |
| 7 assert(arena != NULL); | 11 assert(arena && "Arena creation failed"); |
| 8 assert(arena->offset == 0); | |
| 9 assert(arena->capacity == capacity); | |
| 10 | 12 |
| 11 // Allocate within capacity | 13 char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16); |
| 12 void *p1 = Dowa_Arena_Allocate(arena, 16); | 14 assert(arena_mem1 && "Arena allocation #1 failed"); |
| 13 assert(p1 != NULL); | 15 strcpy(arena_mem1, "hello arena"); |
| 14 assert(arena->offset == 16); | 16 printf("[Arena Allocate] mem1 = \"%s\"\n", arena_mem1); |
| 15 sprintf((char *)p1, "%i", 10); | |
| 16 assert(strcmp(p1, "10") == 0); | |
| 17 | 17 |
| 18 // Allocate more | 18 char *arena_mem2 = (char *)Dowa_Arena_Allocate(arena, 8); |
| 19 void *p2 = Dowa_Arena_Allocate(arena, 32); | 19 assert(arena_mem2 && "Arena allocation #2 failed"); |
| 20 assert(p2 != NULL); | 20 strcpy(arena_mem2, "data"); |
| 21 assert(arena->offset == 48); | 21 printf("[Arena Allocate] mem2 = \"%s\"\n", arena_mem2); |
| 22 | 22 |
| 23 // Overflow allocation should return NULL and not advance offset | 23 Dowa_Arena_Destroy(arena); |
| 24 void *p3 = Dowa_Arena_Allocate(arena, 20); | 24 printf("[Arena] destroyed\n\n"); |
| 25 assert(p3 == NULL); | |
| 26 assert(arena->offset == 48); | |
| 27 | 25 |
| 28 Dowa_Arena_Free(arena); | 26 // --- Test HashMap Basic Operations --- |
| 29 } | 27 Dowa_PHashMap map = Dowa_HashMap_Create(8); |
| 28 assert(map && "HashMap_Create failed"); | |
| 30 | 29 |
| 31 static void TestHashMap() | 30 // Push raw buffer (default type: BUFFER) |
| 32 { | 31 const char raw_buf[] = {0x01, 0x02, 0x03, 0x04}; |
| 33 const size_t capacity = 10; | 32 Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf)); |
| 34 Dowa_PHashMap map = Dowa_HashMap_Create(capacity); | |
| 35 assert(map != NULL); | |
| 36 assert(map->capacity == capacity); | |
| 37 assert(map->current_capacity == 0); | |
| 38 | 33 |
| 39 // Insert "foo" -> 42 | 34 // Push string with explicit STRING type |
| 40 int val1 = 42; | 35 const char *hello = "hello, world"; |
| 41 Dowa_HashMap_PushValue(map, "foo", &val1, sizeof(val1)); | 36 Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING); |
| 42 assert(map->current_capacity == 1); | |
| 43 | 37 |
| 44 int idx_foo = Dowa_HashMap_GetPosition(map, "foo"); | 38 // Push nested hashmap (no copy) |
| 45 Dowa_PHashEntry e_foo = map->entries[idx_foo]; | 39 Dowa_PHashMap inner = Dowa_HashMap_Create(4); |
| 46 assert(e_foo != NULL); | 40 Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1); |
| 47 assert(strcmp(e_foo->key, "foo") == 0); | 41 Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP); |
| 48 assert(*(int*)e_foo->buffer == 42); | |
| 49 | 42 |
| 50 // Overwrite "foo" -> 100 (capacity should not change) | 43 // Push integer with INT type |
| 51 int val2 = 100; | 44 int32 number = 42; |
| 52 Dowa_HashMap_PushValue(map, "foo", &val2, sizeof(val2)); | 45 Dowa_HashMap_Push_Value_With_Type(map, "answer", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); |
| 53 assert(map->current_capacity == 1); | |
| 54 | 46 |
| 55 e_foo = map->entries[idx_foo]; | 47 // Print full map |
| 56 assert(e_foo != NULL); | 48 printf("=== Map After Inserts ===\n"); |
| 57 assert(strcmp(e_foo->key, "foo") == 0); | |
| 58 assert(*(int*)e_foo->buffer == 100); | |
| 59 | |
| 60 // Insert "bar" -> -7 | |
| 61 int val3 = -7; | |
| 62 Dowa_HashMap_PushValue(map, "bar", &val3, sizeof(val3)); | |
| 63 assert(map->current_capacity == 2); | |
| 64 | |
| 65 int idx_bar = Dowa_HashMap_GetPosition(map, "bar"); | |
| 66 Dowa_PHashEntry e_bar = map->entries[idx_bar]; | |
| 67 assert(e_bar != NULL); | |
| 68 assert(strcmp(e_bar->key, "bar") == 0); | |
| 69 assert(*(int*)e_bar->buffer == -7); | |
| 70 | |
| 71 // Pop "foo" | |
| 72 Dowa_HashMap_PopKey(map, "foo"); | |
| 73 assert(map->entries[idx_foo] == NULL); | |
| 74 assert(map->current_capacity == 1); | |
| 75 | |
| 76 // Pop "bar" | |
| 77 Dowa_HashMap_PopKey(map, "bar"); | |
| 78 assert(map->entries[idx_bar] == NULL); | |
| 79 assert(map->current_capacity == 0); | |
| 80 | |
| 81 // Clean up map | |
| 82 Dowa_HashMap_Free(map); | |
| 83 } | |
| 84 | |
| 85 void Test_Dowa_HashMap_Cache_Folder() | |
| 86 { | |
| 87 Dowa_PHashMap map = Dowa_HashMap_Create(100); | |
| 88 | |
| 89 int res = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); | |
| 90 assert(res == 0 && "Folder caching should succeed"); | |
| 91 | |
| 92 const char *foo_val = (const char *)Dowa_HashMap_Get(map, "foo.txt"); | |
| 93 assert(foo_val != NULL); | |
| 94 assert(strcmp("this is foo\n", foo_val)==0); | |
| 95 | |
| 96 Dowa_HashMap_Print(map); | 49 Dowa_HashMap_Print(map); |
| 97 | 50 |
| 98 Dowa_PHashMap p_bar_map = (Dowa_PHashMap)Dowa_HashMap_Get(map, "bar"); | 51 // Retrieve and validate values |
| 99 assert(p_bar_map != NULL); | 52 { |
| 100 assert(strcmp("this is bar\n", (const char *)Dowa_HashMap_Get(p_bar_map, "bar.txt"))==0); | 53 // raw buffer |
| 54 uint8 *r = Dowa_HashMap_Get(map, "raw"); | |
| 55 printf("[Get raw] bytes:"); | |
| 56 for (size_t i = 0; i < sizeof(raw_buf); ++i) { | |
| 57 printf(" %02X", r[i]); | |
| 58 } | |
| 59 printf("\n"); | |
| 101 | 60 |
| 102 Dowa_HashMap_Free(map); | 61 // greeting |
| 103 } | 62 char *g = Dowa_HashMap_Get(map, "greeting"); |
| 63 printf("[Get greeting] \"%s\"\n", g); | |
| 104 | 64 |
| 105 int main(void) { | 65 // nested hashmap |
| 106 TestArena(); | 66 Dowa_PHashMap ni = Dowa_HashMap_Get(map, "nested"); |
| 107 printf("Arena tests passed.\n"); | 67 printf("[Get nested] Inner map contents:\n"); |
| 68 Dowa_HashMap_Print(ni); | |
| 108 | 69 |
| 109 TestHashMap(); | 70 // answer |
| 110 printf("HashMap tests passed.\n"); | 71 int32 *ans = Dowa_HashMap_Get(map, "answer"); |
| 72 printf("[Get answer] %d\n", *ans); | |
| 73 } | |
| 111 | 74 |
| 112 Test_Dowa_HashMap_Cache_Folder(); | 75 // Test Get_Position |
| 113 printf("Test_Dowa_HashMap_Cache_Folder passed\n"); | 76 printf("[Get_Position] 'greeting' at index %d\n", |
| 77 Dowa_HashMap_Get_Position(map, "greeting")); | |
| 78 printf("[Get_Position] 'missing' at index %d\n", | |
| 79 Dowa_HashMap_Get_Position(map, "missing")); | |
| 80 | |
| 81 // Pop a key | |
| 82 Dowa_HashMap_Pop_Key(map, "raw"); | |
| 83 printf("=== Map After Pop 'raw' ===\n"); | |
| 84 Dowa_HashMap_Print(map); | |
| 85 | |
| 86 // --- Test HashMap with Arena --- | |
| 87 Dowa_PArena mapArena = Dowa_Arena_Create(256); | |
| 88 Dowa_PHashMap map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); | |
| 89 assert(map2 && "HashMap_Create_With_Arena failed"); | |
| 90 char *test = "bar"; | |
| 91 | |
| 92 Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING); | |
| 93 Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); | |
| 94 | |
| 95 printf("=== Map2 (with arena) ===\n"); | |
| 96 Dowa_HashMap_Print(map2); | |
| 97 | |
| 98 Dowa_HashMap_Destroy(map2); | |
| 99 Dowa_Arena_Destroy(mapArena); | |
| 100 printf("[Map2 & Arena] destroyed\n\n"); | |
| 101 | |
| 102 // --- Test Cache_Folder --- | |
| 103 // Ensure there is a directory "./dowa/test_folder" with some files for this test to succeed. | |
| 104 int cache_result = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); | |
| 105 printf("[Cache_Folder] returned %d\n", cache_result); | |
| 106 if (cache_result == 0) { | |
| 107 printf("=== Map After Caching 'dowa/test_folder' ===\n"); | |
| 108 Dowa_HashMap_Print(map); | |
| 109 } else { | |
| 110 printf("Cache_Folder failed (ensure 'dowa/test_folder' exists with files)\n"); | |
| 111 } | |
| 112 | |
| 113 // Cleanup | |
| 114 Dowa_HashMap_Destroy(map); | |
| 115 printf("[Map] destroyed\n"); | |
| 116 | |
| 114 return 0; | 117 return 0; |
| 115 } | 118 } |
| 116 |