view dowa/dowa_test.c @ 60:d64a8c189a77

Merged
author June Park <me@mrjunejune.com>
date Sat, 20 Dec 2025 13:56:01 -0500
parents 947b81010aba
children a30944e5719e
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "dowa.h"

int main(void)
{
  // --- Test Arena Allocator ---
  Dowa_PArena arena = Dowa_Arena_Create(64);
  assert(arena && "Arena creation failed");

  char *arena_mem1 = (char *)Dowa_Arena_Allocate(arena, 16);
  assert(arena_mem1 && "Arena allocation #1 failed");
  strcpy(arena_mem1, "hello arena");
  printf("[Arena Allocate] mem1 = \"%s\"\n", arena_mem1);

  char *arena_mem2 = (char *)Dowa_Arena_Allocate(arena, 8);
  assert(arena_mem2 && "Arena allocation #2 failed");
  strcpy(arena_mem2, "data");
  printf("[Arena Allocate] mem2 = \"%s\"\n", arena_mem2);

  Dowa_Arena_Destroy(arena);
  printf("[Arena] destroyed\n\n");

  // --- Test HashMap Basic Operations ---
  Dowa_PHashMap map = Dowa_HashMap_Create(8);
  assert(map && "HashMap_Create failed");

  // Push raw buffer (default type: BUFFER)
  const char raw_buf[] = {0x01, 0x02, 0x03, 0x04};
  Dowa_HashMap_Push_Value(map, "raw", raw_buf, sizeof(raw_buf));

  // Push string with explicit STRING type
  const char *hello = "hello, world";
  Dowa_HashMap_Push_Value_With_Type(map, "greeting", hello, strlen(hello) + 1, DOWA_HASH_MAP_TYPE_STRING);

  // Push nested hashmap (no copy)
  Dowa_PHashMap inner = Dowa_HashMap_Create(4);
  Dowa_HashMap_Push_Value(inner, "inner_key", "inner_val", strlen("inner_val") + 1);
  Dowa_HashMap_Push_Value_With_Type_NoCopy(map, "nested", inner, sizeof(inner), DOWA_HASH_MAP_TYPE_HASHMAP);

  // Push integer with INT type
  int32 number = 42;
  Dowa_HashMap_Push_Value_With_Type(map, "answer", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT);

  // Print full map
  printf("=== Map After Inserts ===\n");
  Dowa_HashMap_Print(map);

  // Retrieve and validate values
  {
    // raw buffer
    uint8 *r = Dowa_HashMap_Get(map, "raw");
    printf("[Get raw] bytes:");
    for (size_t i = 0; i < sizeof(raw_buf); ++i) {
      printf(" %02X", r[i]);
    }
    printf("\n");

    // greeting
    char *g = Dowa_HashMap_Get(map, "greeting");
    printf("[Get greeting] \"%s\"\n", g);

    // nested hashmap
    Dowa_PHashMap ni = Dowa_HashMap_Get(map, "nested");
    printf("[Get nested] Inner map contents:\n");
    Dowa_HashMap_Print(ni);

    // answer
    int32 *ans = Dowa_HashMap_Get(map, "answer");
    printf("[Get answer] %d\n", *ans);
  }

  // Test Get_Position
  printf("[Get_Position] 'greeting' at index %d\n",
       Dowa_HashMap_Get_Position(map, "greeting"));
  printf("[Get_Position] 'missing' at index %d\n",
       Dowa_HashMap_Get_Position(map, "missing"));

  // Pop a key
  Dowa_HashMap_Pop_Key(map, "raw");
  printf("=== Map After Pop 'raw' ===\n");
  Dowa_HashMap_Print(map);

  // --- Test HashMap with Arena ---
  Dowa_PArena mapArena = Dowa_Arena_Create(256);
  Dowa_PHashMap map2 = Dowa_HashMap_Create_With_Arena(8, mapArena);
  assert(map2 && "HashMap_Create_With_Arena failed");
  char *test = "bar";

  Dowa_HashMap_Push_Value_With_Type(map2, "foo", test, 4, DOWA_HASH_MAP_TYPE_STRING);
  Dowa_HashMap_Push_Value_With_Type(map2, "num", &number, sizeof(number), DOWA_HASH_MAP_TYPE_INT);

  printf("=== Map2 (with arena) ===\n");
  Dowa_HashMap_Print(map2);

  Dowa_HashMap_Destroy(map2);
  printf("[Map2] destroyed (no change)\n\n");
  Dowa_Arena_Destroy(mapArena);
  printf("[Arena] destroyed (all null)\n\n");

  // --- Test Cache_Folder ---
  // Ensure there is a directory "./dowa/test_folder" with some files for this test to succeed.
  int cache_result = Dowa_HashMap_Cache_Folder(map, "dowa/test_folder");
  printf("[Cache_Folder] returned %d\n", cache_result);
  if (cache_result == 0)
  {
    printf("=== Map After Caching 'dowa/test_folder' ===\n");
    Dowa_HashMap_Print(map);
  }else
  {
    printf("Cache_Folder failed (ensure 'dowa/test_folder' exists with files)\n");
  }

  // Cleanup
  Dowa_HashMap_Destroy(map);
  printf("[Map] destroyed\n");

  return 0;
}