diff 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
line wrap: on
line diff
--- a/dowa/dowa_test.c	Fri Sep 26 15:14:46 2025 -0700
+++ b/dowa/dowa_test.c	Sat Sep 27 16:23:04 2025 -0700
@@ -1,16 +1,19 @@
 #include "dowa.h"
 
-static void TestArena() {
+static void TestArena()
+{
   const size_t capacity = 64;
-  Dowa_PArena arena = Dowa_Arena_Initialize(capacity);
+  Dowa_PArena arena = Dowa_Arena_Create(capacity);
   assert(arena != NULL);
   assert(arena->offset == 0);
   assert(arena->capacity == capacity);
 
   // Allocate within capacity
   void *p1 = Dowa_Arena_Allocate(arena, 16);
-  // assert(p1 != NULL);
+  assert(p1 != NULL);
   assert(arena->offset == 16);
+  sprintf((char *)p1, "%i", 10);
+  assert(strcmp(p1, "10") == 0);
 
   // Allocate more
   void *p2 = Dowa_Arena_Allocate(arena, 32);
@@ -25,7 +28,8 @@
   Dowa_Arena_Free(arena);
 }
 
-static void TestHashMap() {
+static void TestHashMap()
+{
   const size_t capacity = 10;
   Dowa_PHashMap map = Dowa_HashMap_Create(capacity);
   assert(map != NULL);
@@ -43,21 +47,20 @@
   assert(strcmp(e_foo->key, "foo") == 0);
   assert(*(int*)e_foo->buffer == 42);
 
-  // Overwrite "foo" -> 100
+  // Overwrite "foo" -> 100 (capacity should not change)
   int val2 = 100;
   Dowa_HashMap_PushValue(map, "foo", &val2, sizeof(val2));
-  // current_capacity increments again according to your implementation
-  assert(map->current_capacity == 2);
+  assert(map->current_capacity == 1);
 
-  Dowa_PHashEntry e_foo2 = map->entries[idx_foo];
-  assert(e_foo2 != NULL);
-  assert(strcmp(e_foo2->key, "foo") == 0);
-  assert(*(int*)e_foo2->buffer == 100);
+  e_foo = map->entries[idx_foo];
+  assert(e_foo != NULL);
+  assert(strcmp(e_foo->key, "foo") == 0);
+  assert(*(int*)e_foo->buffer == 100);
 
   // Insert "bar" -> -7
   int val3 = -7;
   Dowa_HashMap_PushValue(map, "bar", &val3, sizeof(val3));
-  assert(map->current_capacity == 3);
+  assert(map->current_capacity == 2);
 
   int idx_bar = Dowa_HashMap_GetPosition(map, "bar");
   Dowa_PHashEntry e_bar = map->entries[idx_bar];
@@ -68,24 +71,35 @@
   // Pop "foo"
   Dowa_HashMap_PopKey(map, "foo");
   assert(map->entries[idx_foo] == NULL);
-  assert(map->current_capacity == 2);
+  assert(map->current_capacity == 1);
 
   // Pop "bar"
   Dowa_HashMap_PopKey(map, "bar");
   assert(map->entries[idx_bar] == NULL);
-  assert(map->current_capacity == 1);
+  assert(map->current_capacity == 0);
+
+  // Clean up map
+  Dowa_HashMap_Free(map);
+}
+
+void Test_Dowa_HashMap_Cache_Folder()
+{
+  Dowa_PHashMap map = Dowa_HashMap_Create(100);
 
-  // Clean up remaining entries and the map itself
-  for (size_t i = 0; i < map->capacity; ++i) {
-    Dowa_PHashEntry ent = map->entries[i];
-    if (ent) {
-      free(ent->key);
-      free(ent->buffer);
-      free(ent);
-    }
-  }
-  free(map->entries);
-  free(map);
+  int res = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder");
+  assert(res == 0 && "Folder caching should succeed");
+
+  const char *foo_val = (const char *)Dowa_HashMap_Get(map, "foo.txt");
+  assert(foo_val != NULL);
+  assert(strcmp("this is foo\n", foo_val)==0);
+
+  Dowa_HashMap_Print(map);
+
+  Dowa_PHashMap p_bar_map = (Dowa_PHashMap)Dowa_HashMap_Get(map, "bar");
+  assert(p_bar_map != NULL); 
+  assert(strcmp("this is bar\n", (const char *)Dowa_HashMap_Get(p_bar_map, "bar.txt"))==0);
+
+  Dowa_HashMap_Free(map);
 }
 
 int main(void) {
@@ -95,6 +109,8 @@
   TestHashMap();
   printf("HashMap tests passed.\n");
 
+  Test_Dowa_HashMap_Cache_Folder();
+  printf("Test_Dowa_HashMap_Cache_Folder passed\n");
   return 0;
 }