Mercurial
comparison dowa/dowa_test.c @ 65:ecb6ee6a22c3
[Misc] I will no longer drink cool aids of capital P.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Dec 2025 06:22:59 -0800 |
| parents | a30944e5719e |
| children | 75de5903355c |
comparison
equal
deleted
inserted
replaced
| 64:a30944e5719e | 65:ecb6ee6a22c3 |
|---|---|
| 6 #include "dowa.h" | 6 #include "dowa.h" |
| 7 | 7 |
| 8 int main(void) | 8 int main(void) |
| 9 { | 9 { |
| 10 // --- Test Arena Allocator --- | 10 // --- Test Arena Allocator --- |
| 11 Dowa_PArena arena = Dowa_Arena_Create(64); | 11 Dowa_Arena *arena = Dowa_Arena_Create(64); |
| 12 assert(arena && "Arena creation failed"); | 12 assert(arena && "Arena creation failed"); |
| 13 | 13 |
| 14 char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16); | 14 char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16); |
| 15 assert(arena_mem1 && "Arena allocation #1 failed"); | 15 assert(arena_mem1 && "Arena allocation #1 failed"); |
| 16 strcpy(arena_mem1, "hello arena"); | 16 strcpy(arena_mem1, "hello arena"); |
| 23 | 23 |
| 24 Dowa_Arena_Destroy(arena); | 24 Dowa_Arena_Destroy(arena); |
| 25 printf("[Arena] destroyed\n\n"); | 25 printf("[Arena] destroyed\n\n"); |
| 26 | 26 |
| 27 // --- Test HashMap Basic Operations --- | 27 // --- Test HashMap Basic Operations --- |
| 28 Dowa_PHashMap map = Dowa_HashMap_Create(8); | 28 Dowa_HashMap *map = Dowa_HashMap_Create(8); |
| 29 assert(map && "HashMap_Create failed"); | 29 assert(map && "HashMap_Create failed"); |
| 30 | 30 |
| 31 // Push raw buffer (default type: BUFFER) | 31 // Push raw buffer (default type: BUFFER) |
| 32 const char raw_buf[] = {0x01, 0x02, 0x03, 0x04}; | 32 const char raw_buf[] = {0x01, 0x02, 0x03, 0x04}; |
| 33 Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf)); | 33 Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf)); |
| 35 // Push string with explicit STRING type | 35 // Push string with explicit STRING type |
| 36 const char *hello = "hello, world"; | 36 const char *hello = "hello, world"; |
| 37 Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING); | 37 Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING); |
| 38 | 38 |
| 39 // Push nested hashmap (no copy) | 39 // Push nested hashmap (no copy) |
| 40 Dowa_PHashMap inner = Dowa_HashMap_Create(4); | 40 Dowa_HashMap *inner = Dowa_HashMap_Create(4); |
| 41 Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1); | 41 Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1); |
| 42 Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP); | 42 Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP); |
| 43 | 43 |
| 44 // Push integer with INT type | 44 // Push integer with INT type |
| 45 int32 number = 42; | 45 int32 number = 42; |
| 62 // greeting | 62 // greeting |
| 63 char *g = Dowa_HashMap_Get(map, "greeting"); | 63 char *g = Dowa_HashMap_Get(map, "greeting"); |
| 64 printf("[Get greeting] \"%s\"\n", g); | 64 printf("[Get greeting] \"%s\"\n", g); |
| 65 | 65 |
| 66 // nested hashmap | 66 // nested hashmap |
| 67 Dowa_PHashMap ni = Dowa_HashMap_Get(map, "nested"); | 67 Dowa_HashMap *ni = Dowa_HashMap_Get(map, "nested"); |
| 68 printf("[Get nested] Inner map contents:\n"); | 68 printf("[Get nested] Inner map contents:\n"); |
| 69 Dowa_HashMap_Print(ni); | 69 Dowa_HashMap_Print(ni); |
| 70 | 70 |
| 71 // answer | 71 // answer |
| 72 int32 *ans = Dowa_HashMap_Get(map, "answer"); | 72 int32 *ans = Dowa_HashMap_Get(map, "answer"); |
| 83 Dowa_HashMap_Pop_Key(map, "raw"); | 83 Dowa_HashMap_Pop_Key(map, "raw"); |
| 84 printf("=== Map After Pop 'raw' ===\n"); | 84 printf("=== Map After Pop 'raw' ===\n"); |
| 85 Dowa_HashMap_Print(map); | 85 Dowa_HashMap_Print(map); |
| 86 | 86 |
| 87 // --- Test HashMap with Arena --- | 87 // --- Test HashMap with Arena --- |
| 88 Dowa_PArena mapArena = Dowa_Arena_Create(256); | 88 Dowa_Arena *mapArena = Dowa_Arena_Create(256); |
| 89 Dowa_PHashMap map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); | 89 Dowa_HashMap *map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); |
| 90 assert(map2 && "HashMap_Create_With_Arena failed"); | 90 assert(map2 && "HashMap_Create_With_Arena failed"); |
| 91 char *test = "bar"; | 91 char *test = "bar"; |
| 92 | 92 |
| 93 Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING); | 93 Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING); |
| 94 Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); | 94 Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); |