comparison 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
comparison
equal deleted inserted replaced
70:4bc56e88e1f3 71:75de5903355c
5 #define DIRECTORY 5 #define DIRECTORY
6 #include "dowa.h" 6 #include "dowa.h"
7 7
8 int main(void) 8 int main(void)
9 { 9 {
10 printf("=== Testing NEW dowa API (stb_ds style) ===\n\n");
11
10 // --- Test Arena Allocator --- 12 // --- Test Arena Allocator ---
11 Dowa_Arena *arena = Dowa_Arena_Create(64); 13 printf("Testing Arena Allocator...\n");
12 assert(arena && "Arena creation failed"); 14 Dowa_Arena *p_arena = Dowa_Arena_Create(64);
13 15 assert(p_arena && "Arena creation failed");
14 char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16); 16
15 assert(arena_mem1 && "Arena allocation #1 failed"); 17 char *p_arena_mem1 = (char *)Dowa_Arena_Allocate(p_arena, 16);
16 strcpy(arena_mem1, "hello arena"); 18 assert(p_arena_mem1 && "Arena allocation #1 failed");
17 printf("[Arena Allocate] mem1 = \"%s\"\n", arena_mem1); 19 strcpy(p_arena_mem1, "hello arena");
18 20 printf(" [Arena Allocate] mem1 = \"%s\"\n", p_arena_mem1);
19 char *arena_mem2 = (char *)Dowa_Arena_Allocate(arena, 8); 21
20 assert(arena_mem2 && "Arena allocation #2 failed"); 22 char *p_arena_mem2 = (char *)Dowa_Arena_Allocate(p_arena, 8);
21 strcpy(arena_mem2, "data"); 23 assert(p_arena_mem2 && "Arena allocation #2 failed");
22 printf("[Arena Allocate] mem2 = \"%s\"\n", arena_mem2); 24 strcpy(p_arena_mem2, "data");
23 25 printf(" [Arena Allocate] mem2 = \"%s\"\n", p_arena_mem2);
24 Dowa_Arena_Destroy(arena); 26
25 printf("[Arena] destroyed\n\n"); 27 Dowa_Arena_Destroy(p_arena);
26 28 printf(" [Arena] destroyed\n\n");
27 // --- Test HashMap Basic Operations --- 29
28 Dowa_HashMap *map = Dowa_HashMap_Create(8); 30 // --- Test Array Operations ---
29 assert(map && "HashMap_Create failed"); 31 printf("Testing Array Operations...\n");
30 32 int32* p_numbers = NULL;
31 // Push raw buffer (default type: BUFFER) 33
32 const char raw_buf[] = {0x01, 0x02, 0x03, 0x04}; 34 Dowa_Array_Push(p_numbers, 10);
33 Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf)); 35 Dowa_Array_Push(p_numbers, 20);
34 36 Dowa_Array_Push(p_numbers, 30);
35 // Push string with explicit STRING type 37 Dowa_Array_Push(p_numbers, 40);
36 const char *hello = "hello, world"; 38
37 Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING); 39 printf(" Array length: %zu\n", Dowa_Array_Length(p_numbers));
38 40 printf(" Array capacity: %zu\n", Dowa_Array_Capacity(p_numbers));
39 // Push nested hashmap (no copy) 41 printf(" Array contents:");
40 Dowa_HashMap *inner = Dowa_HashMap_Create(4); 42 for (size_t i = 0; i < Dowa_Array_Length(p_numbers); i++)
41 Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1); 43 printf(" %d", p_numbers[i]);
42 Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP); 44 printf("\n");
43 45
44 // Push integer with INT type 46 int32 popped = Dowa_Array_Pop(p_numbers);
45 int32 number = 42; 47 printf(" Popped value: %d\n", popped);
46 Dowa_HashMap_Push_Value_With_Type(map, "answer", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); 48 printf(" Array length after pop: %zu\n", Dowa_Array_Length(p_numbers));
47 49
48 // Print full map 50 Dowa_Array_Clear(p_numbers);
49 printf("=== Map After Inserts ===\n"); 51 printf(" Array length after clear: %zu\n", Dowa_Array_Length(p_numbers));
50 Dowa_HashMap_Print(map); 52
51 53 Dowa_Array_Free(p_numbers);
52 // Retrieve and validate values 54 printf(" Array freed\n\n");
53 { 55
54 // raw buffer 56 // --- Test HashMap Basic Operations (String Keys) ---
55 uint8 *r = Dowa_HashMap_Get(map, "raw"); 57 printf("Testing HashMap with String Keys...\n");
56 printf("[Get raw] bytes:"); 58 Dowa_KV(char*, char*)* p_map = NULL;
57 for (size_t i = 0; i < sizeof(raw_buf); ++i) { 59
58 printf(" %02X", r[i]); 60 Dowa_HashMap_Push(p_map, "name", "John");
59 } 61 Dowa_HashMap_Push(p_map, "city", "New York");
60 printf("\n"); 62 Dowa_HashMap_Push(p_map, "country", "USA");
61 63
62 // greeting 64 printf(" HashMap count: %zu\n", Dowa_HashMap_Count(p_map));
63 char *g = Dowa_HashMap_Get(map, "greeting"); 65
64 printf("[Get greeting] \"%s\"\n", g); 66 void* p_kv = Dowa_HashMap_Get_Ptr(p_map, "name");
65 67 if (p_kv)
66 // nested hashmap 68 {
67 Dowa_HashMap *ni = Dowa_HashMap_Get(map, "nested"); 69 Dowa_KV(char*, char*)* p_pair = (Dowa_KV(char*, char*)*)p_kv;
68 printf("[Get nested] Inner map contents:\n"); 70 printf(" HashMap['name']: %s\n", p_pair->value);
69 Dowa_HashMap_Print(ni); 71 }
70 72
71 // answer 73 p_kv = Dowa_HashMap_Get_Ptr(p_map, "city");
72 int32 *ans = Dowa_HashMap_Get(map, "answer"); 74 if (p_kv)
73 printf("[Get answer] %d\n", *ans); 75 {
74 } 76 Dowa_KV(char*, char*)* p_pair = (Dowa_KV(char*, char*)*)p_kv;
75 77 printf(" HashMap['city']: %s\n", p_pair->value);
76 // Test Get_Position 78 }
77 printf("[Get_Position] 'greeting' at index %d\n", 79
78 Dowa_HashMap_Get_Position(map, "greeting")); 80 boolean has_name = Dowa_HashMap_Has_Key(p_map, "name");
79 printf("[Get_Position] 'missing' at index %d\n", 81 printf(" Has key 'name': %d\n", has_name);
80 Dowa_HashMap_Get_Position(map, "missing")); 82
81 83 boolean has_missing = Dowa_HashMap_Has_Key(p_map, "missing");
82 // Pop a key 84 printf(" Has key 'missing': %d\n", has_missing);
83 Dowa_HashMap_Pop_Key(map, "raw"); 85
84 printf("=== Map After Pop 'raw' ===\n"); 86 printf(" Iterating over HashMap:\n");
85 Dowa_HashMap_Print(map); 87 size_t map_length = Dowa_Array_Length(p_map);
88 for (size_t i = 0; i < map_length; i++)
89 printf(" [%zu] '%s' => '%s'\n", i, p_map[i].key, p_map[i].value);
90
91 Dowa_HashMap_Delete(p_map, "city");
92 printf(" After deleting 'city', count: %zu\n", Dowa_HashMap_Count(p_map));
93
94 Dowa_HashMap_Clear(p_map);
95 printf(" After clear, count: %zu\n", Dowa_HashMap_Count(p_map));
96
97 Dowa_HashMap_Free(p_map);
98 printf(" HashMap freed\n\n");
99
100 // --- Test HashMap with Int Keys (Binary) ---
101 printf("Testing HashMap with Int Keys...\n");
102 Dowa_KV(int32, char*)* p_int_map = NULL;
103
104 int32 key1 = 1;
105 int32 key2 = 2;
106 int32 key3 = 3;
107
108 Dowa_HashMap_Push_Binary(p_int_map, &key1, sizeof(int32), "one");
109 Dowa_HashMap_Push_Binary(p_int_map, &key2, sizeof(int32), "two");
110 Dowa_HashMap_Push_Binary(p_int_map, &key3, sizeof(int32), "three");
111
112 printf(" Int map count: %zu\n", Dowa_HashMap_Count(p_int_map));
113
114 p_kv = Dowa_HashMap_Get_Ptr_Binary(p_int_map, &key1, sizeof(int32));
115 if (p_kv)
116 {
117 Dowa_KV(int32, char*)* p_pair = (Dowa_KV(int32, char*)*)p_kv;
118 printf(" Int map[1]: '%s'\n", p_pair->value);
119 }
120
121 p_kv = Dowa_HashMap_Get_Ptr_Binary(p_int_map, &key2, sizeof(int32));
122 if (p_kv)
123 {
124 Dowa_KV(int32, char*)* p_pair = (Dowa_KV(int32, char*)*)p_kv;
125 printf(" Int map[2]: '%s'\n", p_pair->value);
126 }
127
128 printf(" Iterating over Int HashMap:\n");
129 map_length = Dowa_Array_Length(p_int_map);
130 for (size_t i = 0; i < map_length; i++)
131 printf(" [%zu] %d => '%s'\n", i, p_int_map[i].key, p_int_map[i].value);
132
133 Dowa_HashMap_Free(p_int_map);
134 printf(" Int map freed\n\n");
86 135
87 // --- Test HashMap with Arena --- 136 // --- Test HashMap with Arena ---
88 Dowa_Arena *mapArena = Dowa_Arena_Create(256); 137 printf("Testing HashMap with Arena...\n");
89 Dowa_HashMap *map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); 138 Dowa_Arena *p_map_arena = Dowa_Arena_Create(1024);
90 assert(map2 && "HashMap_Create_With_Arena failed"); 139 Dowa_KV(char*, int32) *p_arena_map = NULL;
91 char *test = "bar"; 140
92 141 Dowa_HashMap_Push_Arena(p_arena_map, "x", 100, p_map_arena);
93 Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING); 142 Dowa_HashMap_Push_Arena(p_arena_map, "y", 200, p_map_arena);
94 Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); 143 Dowa_HashMap_Push_Arena(p_arena_map, "z", 300, p_map_arena);
95 144
96 printf("=== Map2 (with arena) ===\n"); 145 printf(" Arena map count: %zu\n", Dowa_HashMap_Count(p_arena_map));
97 Dowa_HashMap_Print(map2); 146
98 147 p_kv = Dowa_HashMap_Get_Ptr(p_arena_map, "x");
99 Dowa_HashMap_Destroy(map2); 148 if (p_kv)
100 printf("[Map2] destroyed (no change)\n\n"); 149 {
101 Dowa_Arena_Destroy(mapArena); 150 Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv;
102 printf("[Arena] destroyed (all null)\n\n"); 151 printf(" Arena map['x']: %d\n", p_pair->value);
103 152 }
104 // --- Test Cache_Folder --- 153
105 // Ensure there is a directory "./dowa/test_folder" with some files for this test to succeed. 154 p_kv = Dowa_HashMap_Get_Ptr(p_arena_map, "y");
106 int cache_result = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); 155 if (p_kv)
107 printf("[Cache_Folder] returned %d\n", cache_result); 156 {
108 if (cache_result == 0) 157 Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv;
109 { 158 printf(" Arena map['y']: %d\n", p_pair->value);
110 printf("=== Map After Caching 'dowa/test_folder' ===\n"); 159 }
111 Dowa_HashMap_Print(map); 160
112 }else 161 printf(" Iterating over Arena HashMap:\n");
113 { 162 map_length = Dowa_Array_Length(p_arena_map);
114 printf("Cache_Folder failed (ensure 'dowa/test_folder' exists with files)\n"); 163 for (size_t i = 0; i < map_length; i++)
115 } 164 printf(" [%zu] '%s' => %d\n", i, p_arena_map[i].key, p_arena_map[i].value);
116 165
117 // Cleanup 166 Dowa_Arena_Destroy(p_map_arena);
118 Dowa_HashMap_Destroy(map); 167 printf(" Arena destroyed (including map)\n\n");
119 printf("[Map] destroyed\n"); 168
120 169 // --- Test Array with Arena ---
170 printf("Testing Array with Arena...\n");
171 Dowa_Arena *p_array_arena = Dowa_Arena_Create(1024);
172 int32* p_arena_numbers = NULL;
173
174 Dowa_Array_Push_Arena(p_arena_numbers, 5, p_array_arena);
175 Dowa_Array_Push_Arena(p_arena_numbers, 10, p_array_arena);
176 Dowa_Array_Push_Arena(p_arena_numbers, 15, p_array_arena);
177
178 printf(" Arena array length: %zu\n", Dowa_Array_Length(p_arena_numbers));
179 printf(" Arena array contents:");
180 for (size_t i = 0; i < Dowa_Array_Length(p_arena_numbers); i++)
181 printf(" %d", p_arena_numbers[i]);
182 printf("\n");
183
184 Dowa_Arena_Destroy(p_array_arena);
185 printf(" Arena destroyed (including array)\n\n");
186
187 // --- Test Medium HashMap (Stress Test) ---
188 printf("Testing Medium HashMap (Stress Test)...\n");
189 Dowa_KV(char*, int32)* p_large_map = NULL;
190
191 char key_buffer[32];
192 for (int32 i = 0; i < 100; i++)
193 {
194 sprintf(key_buffer, "key_%d", i);
195 Dowa_HashMap_Push(p_large_map, key_buffer, i * 10);
196 }
197
198 printf(" Medium map count: %zu\n", Dowa_HashMap_Count(p_large_map));
199
200 sprintf(key_buffer, "key_50");
201 p_kv = Dowa_HashMap_Get_Ptr(p_large_map, key_buffer);
202 if (p_kv)
203 {
204 Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv;
205 printf(" Medium map['key_50']: %d\n", p_pair->value);
206 }
207
208 sprintf(key_buffer, "key_99");
209 boolean has_99 = Dowa_HashMap_Has_Key(p_large_map, key_buffer);
210 printf(" Has key 'key_99': %d\n", has_99);
211
212 Dowa_HashMap_Free(p_large_map);
213 printf(" Medium map freed\n\n");
214
215 printf("=== All tests passed! ===\n");
121 return 0; 216 return 0;
122 } 217 }