Mercurial
annotate dowa/dowa_test.c @ 12:de54585a40f1
Adding bun and node modules.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 02 Oct 2025 14:39:48 -0700 |
| parents | 3e12bf044589 |
| children | 09def63429b9 |
| rev | line source |
|---|---|
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
1 #include "dowa.h" |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
2 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
3 static void TestArena() |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
4 { |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
5 const size_t capacity = 64; |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
6 Dowa_PArena arena = Dowa_Arena_Create(capacity); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
7 assert(arena != NULL); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
8 assert(arena->offset == 0); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
9 assert(arena->capacity == capacity); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
10 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
11 // Allocate within capacity |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
12 void *p1 = Dowa_Arena_Allocate(arena, 16); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
13 assert(p1 != NULL); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
14 assert(arena->offset == 16); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
15 sprintf((char *)p1, "%i", 10); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
16 assert(strcmp(p1, "10") == 0); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
17 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
18 // Allocate more |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
19 void *p2 = Dowa_Arena_Allocate(arena, 32); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
20 assert(p2 != NULL); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
21 assert(arena->offset == 48); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
22 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
23 // Overflow allocation should return NULL and not advance offset |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
24 void *p3 = Dowa_Arena_Allocate(arena, 20); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
25 assert(p3 == NULL); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
26 assert(arena->offset == 48); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
27 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
28 Dowa_Arena_Free(arena); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
29 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
30 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
31 static void TestHashMap() |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
32 { |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
33 const size_t capacity = 10; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
34 Dowa_PHashMap map = Dowa_HashMap_Create(capacity); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
35 assert(map != NULL); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
36 assert(map->capacity == capacity); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
37 assert(map->current_capacity == 0); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
38 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
39 // Insert "foo" -> 42 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
40 int val1 = 42; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
41 Dowa_HashMap_PushValue(map, "foo", &val1, sizeof(val1)); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
42 assert(map->current_capacity == 1); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
43 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
44 int idx_foo = Dowa_HashMap_GetPosition(map, "foo"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
45 Dowa_PHashEntry e_foo = map->entries[idx_foo]; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
46 assert(e_foo != NULL); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
47 assert(strcmp(e_foo->key, "foo") == 0); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
48 assert(*(int*)e_foo->buffer == 42); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
49 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
50 // Overwrite "foo" -> 100 (capacity should not change) |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
51 int val2 = 100; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
52 Dowa_HashMap_PushValue(map, "foo", &val2, sizeof(val2)); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
53 assert(map->current_capacity == 1); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
54 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
55 e_foo = map->entries[idx_foo]; |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
56 assert(e_foo != NULL); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
57 assert(strcmp(e_foo->key, "foo") == 0); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
58 assert(*(int*)e_foo->buffer == 100); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
59 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
60 // Insert "bar" -> -7 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
61 int val3 = -7; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
62 Dowa_HashMap_PushValue(map, "bar", &val3, sizeof(val3)); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
63 assert(map->current_capacity == 2); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
64 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
65 int idx_bar = Dowa_HashMap_GetPosition(map, "bar"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
66 Dowa_PHashEntry e_bar = map->entries[idx_bar]; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
67 assert(e_bar != NULL); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
68 assert(strcmp(e_bar->key, "bar") == 0); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
69 assert(*(int*)e_bar->buffer == -7); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
70 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
71 // Pop "foo" |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
72 Dowa_HashMap_PopKey(map, "foo"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
73 assert(map->entries[idx_foo] == NULL); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
74 assert(map->current_capacity == 1); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
75 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
76 // Pop "bar" |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
77 Dowa_HashMap_PopKey(map, "bar"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
78 assert(map->entries[idx_bar] == NULL); |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
79 assert(map->current_capacity == 0); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
80 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
81 // Clean up map |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
82 Dowa_HashMap_Free(map); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
83 } |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
84 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
85 void Test_Dowa_HashMap_Cache_Folder() |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
86 { |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
87 Dowa_PHashMap map = Dowa_HashMap_Create(100); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
88 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
89 int res = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
90 assert(res == 0 && "Folder caching should succeed"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
91 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
92 const char *foo_val = (const char *)Dowa_HashMap_Get(map, "foo.txt"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
93 assert(foo_val != NULL); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
94 assert(strcmp("this is foo\n", foo_val)==0); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
95 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
96 Dowa_HashMap_Print(map); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
97 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
98 Dowa_PHashMap p_bar_map = (Dowa_PHashMap)Dowa_HashMap_Get(map, "bar"); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
99 assert(p_bar_map != NULL); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
100 assert(strcmp("this is bar\n", (const char *)Dowa_HashMap_Get(p_bar_map, "bar.txt"))==0); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
101 |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
102 Dowa_HashMap_Free(map); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
103 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
104 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
105 int main(void) { |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
106 TestArena(); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
107 printf("Arena tests passed.\n"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
108 |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
109 TestHashMap(); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
110 printf("HashMap tests passed.\n"); |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
111 |
|
5
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
112 Test_Dowa_HashMap_Cache_Folder(); |
|
3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents:
2
diff
changeset
|
113 printf("Test_Dowa_HashMap_Cache_Folder passed\n"); |
|
2
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
114 return 0; |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
115 } |
|
8a43dedbe530
[Dowa] Added HashMap and Updated Arena naming.
June Park <parkjune1995@gmail.com>
parents:
diff
changeset
|
116 |