comparison dowa/dowa_test.c @ 5:3e12bf044589

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