Mercurial
annotate 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 |
| rev | line source |
|---|---|
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
1 #include <stdio.h> |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
2 #include <stdlib.h> |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
3 #include <string.h> |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
4 #include <assert.h> |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
5 #include "dowa.h" |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
6 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
7 int main(void) |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
8 { |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
9 // --- Test Arena Allocator --- |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
10 Dowa_PArena arena = Dowa_Arena_Create(64); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
11 assert(arena && "Arena creation failed"); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
12 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
13 char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
14 assert(arena_mem1 && "Arena allocation #1 failed"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
15 strcpy(arena_mem1, "hello arena"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
16 printf("[Arena Allocate] mem1 = \"%s\"\n", arena_mem1); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
17 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
18 char *arena_mem2 = (char *)Dowa_Arena_Allocate(arena, 8); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
19 assert(arena_mem2 && "Arena allocation #2 failed"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
20 strcpy(arena_mem2, "data"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
21 printf("[Arena Allocate] mem2 = \"%s\"\n", arena_mem2); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
22 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
23 Dowa_Arena_Destroy(arena); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
24 printf("[Arena] destroyed\n\n"); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
25 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
26 // --- Test HashMap Basic Operations --- |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
27 Dowa_PHashMap map = Dowa_HashMap_Create(8); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
28 assert(map && "HashMap_Create failed"); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
29 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
30 // Push raw buffer (default type: BUFFER) |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
31 const char raw_buf[] = {0x01, 0x02, 0x03, 0x04}; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
32 Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf)); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
33 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
34 // Push string with explicit STRING type |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
35 const char *hello = "hello, world"; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
36 Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
37 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
38 // Push nested hashmap (no copy) |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
39 Dowa_PHashMap inner = Dowa_HashMap_Create(4); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
40 Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
41 Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
42 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
43 // Push integer with INT type |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
44 int32 number = 42; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
45 Dowa_HashMap_Push_Value_With_Type(map, "answer", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
46 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
47 // Print full map |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
48 printf("=== Map After Inserts ===\n"); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
49 Dowa_HashMap_Print(map); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
50 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
51 // Retrieve and validate values |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
52 { |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
53 // raw buffer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
54 uint8 *r = Dowa_HashMap_Get(map, "raw"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
55 printf("[Get raw] bytes:"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
56 for (size_t i = 0; i < sizeof(raw_buf); ++i) { |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
57 printf(" %02X", r[i]); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
58 } |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
59 printf("\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
60 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
61 // greeting |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
62 char *g = Dowa_HashMap_Get(map, "greeting"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
63 printf("[Get greeting] \"%s\"\n", g); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
64 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
65 // nested hashmap |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
66 Dowa_PHashMap ni = Dowa_HashMap_Get(map, "nested"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
67 printf("[Get nested] Inner map contents:\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
68 Dowa_HashMap_Print(ni); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
69 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
70 // answer |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
71 int32 *ans = Dowa_HashMap_Get(map, "answer"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
72 printf("[Get answer] %d\n", *ans); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
73 } |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
74 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
75 // Test Get_Position |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
76 printf("[Get_Position] 'greeting' at index %d\n", |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
77 Dowa_HashMap_Get_Position(map, "greeting")); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
78 printf("[Get_Position] 'missing' at index %d\n", |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
79 Dowa_HashMap_Get_Position(map, "missing")); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
80 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
81 // Pop a key |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
82 Dowa_HashMap_Pop_Key(map, "raw"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
83 printf("=== Map After Pop 'raw' ===\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
84 Dowa_HashMap_Print(map); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
85 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
86 // --- Test HashMap with Arena --- |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
87 Dowa_PArena mapArena = Dowa_Arena_Create(256); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
88 Dowa_PHashMap map2 = Dowa_HashMap_Create_With_Arena(8, mapArena); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
89 assert(map2 && "HashMap_Create_With_Arena failed"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
90 char *test = "bar"; |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
91 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
92 Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
93 Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
94 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
95 printf("=== Map2 (with arena) ===\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
96 Dowa_HashMap_Print(map2); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
97 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
98 Dowa_HashMap_Destroy(map2); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
99 Dowa_Arena_Destroy(mapArena); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
100 printf("[Map2 & Arena] destroyed\n\n"); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
101 |
|
21
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
102 // --- Test Cache_Folder --- |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
103 // Ensure there is a directory "./dowa/test_folder" with some files for this test to succeed. |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
104 int cache_result = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
105 printf("[Cache_Folder] returned %d\n", cache_result); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
106 if (cache_result == 0) { |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
107 printf("=== Map After Caching 'dowa/test_folder' ===\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
108 Dowa_HashMap_Print(map); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
109 } else { |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
110 printf("Cache_Folder failed (ensure 'dowa/test_folder' exists with files)\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
111 } |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
112 |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
113 // Cleanup |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
114 Dowa_HashMap_Destroy(map); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
115 printf("[Map] destroyed\n"); |
|
09def63429b9
[Dowa] Updated the naming scheme and tests.
June Park <parkjune1995@gmail.com>
parents:
5
diff
changeset
|
116 |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
117 return 0; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
118 } |