view dowa/dowa_test.c @ 71:75de5903355c

Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
author June Park <parkjune1995@gmail.com>
date Sun, 28 Dec 2025 20:34:22 -0800
parents ecb6ee6a22c3
children 4532ce6d9eb8
line wrap: on
line source

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

int main(void)
{
  printf("=== Testing NEW dowa API (stb_ds style) ===\n\n");

  // --- Test Arena Allocator ---
  printf("Testing Arena Allocator...\n");
  Dowa_Arena *p_arena = Dowa_Arena_Create(64);
  assert(p_arena && "Arena creation failed");

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

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

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

  // --- Test Array Operations ---
  printf("Testing Array Operations...\n");
  int32* p_numbers = NULL;

  Dowa_Array_Push(p_numbers, 10);
  Dowa_Array_Push(p_numbers, 20);
  Dowa_Array_Push(p_numbers, 30);
  Dowa_Array_Push(p_numbers, 40);

  printf("  Array length: %zu\n", Dowa_Array_Length(p_numbers));
  printf("  Array capacity: %zu\n", Dowa_Array_Capacity(p_numbers));
  printf("  Array contents:");
  for (size_t i = 0; i < Dowa_Array_Length(p_numbers); i++)
    printf(" %d", p_numbers[i]);
  printf("\n");

  int32 popped = Dowa_Array_Pop(p_numbers);
  printf("  Popped value: %d\n", popped);
  printf("  Array length after pop: %zu\n", Dowa_Array_Length(p_numbers));

  Dowa_Array_Clear(p_numbers);
  printf("  Array length after clear: %zu\n", Dowa_Array_Length(p_numbers));

  Dowa_Array_Free(p_numbers);
  printf("  Array freed\n\n");

  // --- Test HashMap Basic Operations (String Keys) ---
  printf("Testing HashMap with String Keys...\n");
  Dowa_KV(char*, char*)* p_map = NULL;

  Dowa_HashMap_Push(p_map, "name", "John");
  Dowa_HashMap_Push(p_map, "city", "New York");
  Dowa_HashMap_Push(p_map, "country", "USA");

  printf("  HashMap count: %zu\n", Dowa_HashMap_Count(p_map));

  void* p_kv = Dowa_HashMap_Get_Ptr(p_map, "name");
  if (p_kv)
  {
    Dowa_KV(char*, char*)* p_pair = (Dowa_KV(char*, char*)*)p_kv;
    printf("  HashMap['name']: %s\n", p_pair->value);
  }

  p_kv = Dowa_HashMap_Get_Ptr(p_map, "city");
  if (p_kv)
  {
    Dowa_KV(char*, char*)* p_pair = (Dowa_KV(char*, char*)*)p_kv;
    printf("  HashMap['city']: %s\n", p_pair->value);
  }

  boolean has_name = Dowa_HashMap_Has_Key(p_map, "name");
  printf("  Has key 'name': %d\n", has_name);

  boolean has_missing = Dowa_HashMap_Has_Key(p_map, "missing");
  printf("  Has key 'missing': %d\n", has_missing);

  printf("  Iterating over HashMap:\n");
  size_t map_length = Dowa_Array_Length(p_map);
  for (size_t i = 0; i < map_length; i++)
    printf("    [%zu] '%s' => '%s'\n", i, p_map[i].key, p_map[i].value);

  Dowa_HashMap_Delete(p_map, "city");
  printf("  After deleting 'city', count: %zu\n", Dowa_HashMap_Count(p_map));

  Dowa_HashMap_Clear(p_map);
  printf("  After clear, count: %zu\n", Dowa_HashMap_Count(p_map));

  Dowa_HashMap_Free(p_map);
  printf("  HashMap freed\n\n");

  // --- Test HashMap with Int Keys (Binary) ---
  printf("Testing HashMap with Int Keys...\n");
  Dowa_KV(int32, char*)* p_int_map = NULL;

  int32 key1 = 1;
  int32 key2 = 2;
  int32 key3 = 3;

  Dowa_HashMap_Push_Binary(p_int_map, &key1, sizeof(int32), "one");
  Dowa_HashMap_Push_Binary(p_int_map, &key2, sizeof(int32), "two");
  Dowa_HashMap_Push_Binary(p_int_map, &key3, sizeof(int32), "three");

  printf("  Int map count: %zu\n", Dowa_HashMap_Count(p_int_map));

  p_kv = Dowa_HashMap_Get_Ptr_Binary(p_int_map, &key1, sizeof(int32));
  if (p_kv)
  {
    Dowa_KV(int32, char*)* p_pair = (Dowa_KV(int32, char*)*)p_kv;
    printf("  Int map[1]: '%s'\n", p_pair->value);
  }

  p_kv = Dowa_HashMap_Get_Ptr_Binary(p_int_map, &key2, sizeof(int32));
  if (p_kv)
  {
    Dowa_KV(int32, char*)* p_pair = (Dowa_KV(int32, char*)*)p_kv;
    printf("  Int map[2]: '%s'\n", p_pair->value);
  }

  printf("  Iterating over Int HashMap:\n");
  map_length = Dowa_Array_Length(p_int_map);
  for (size_t i = 0; i < map_length; i++)
    printf("    [%zu] %d => '%s'\n", i, p_int_map[i].key, p_int_map[i].value);

  Dowa_HashMap_Free(p_int_map);
  printf("  Int map freed\n\n");

  // --- Test HashMap with Arena ---
  printf("Testing HashMap with Arena...\n");
  Dowa_Arena *p_map_arena = Dowa_Arena_Create(1024);
  Dowa_KV(char*, int32) *p_arena_map = NULL;

  Dowa_HashMap_Push_Arena(p_arena_map, "x", 100, p_map_arena);
  Dowa_HashMap_Push_Arena(p_arena_map, "y", 200, p_map_arena);
  Dowa_HashMap_Push_Arena(p_arena_map, "z", 300, p_map_arena);

  printf("  Arena map count: %zu\n", Dowa_HashMap_Count(p_arena_map));

  p_kv = Dowa_HashMap_Get_Ptr(p_arena_map, "x");
  if (p_kv)
  {
    Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv;
    printf("  Arena map['x']: %d\n", p_pair->value);
  }

  p_kv = Dowa_HashMap_Get_Ptr(p_arena_map, "y");
  if (p_kv)
  {
    Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv;
    printf("  Arena map['y']: %d\n", p_pair->value);
  }

  printf("  Iterating over Arena HashMap:\n");
  map_length = Dowa_Array_Length(p_arena_map);
  for (size_t i = 0; i < map_length; i++)
    printf("    [%zu] '%s' => %d\n", i, p_arena_map[i].key, p_arena_map[i].value);

  Dowa_Arena_Destroy(p_map_arena);
  printf("  Arena destroyed (including map)\n\n");

  // --- Test Array with Arena ---
  printf("Testing Array with Arena...\n");
  Dowa_Arena *p_array_arena = Dowa_Arena_Create(1024);
  int32* p_arena_numbers = NULL;

  Dowa_Array_Push_Arena(p_arena_numbers, 5, p_array_arena);
  Dowa_Array_Push_Arena(p_arena_numbers, 10, p_array_arena);
  Dowa_Array_Push_Arena(p_arena_numbers, 15, p_array_arena);

  printf("  Arena array length: %zu\n", Dowa_Array_Length(p_arena_numbers));
  printf("  Arena array contents:");
  for (size_t i = 0; i < Dowa_Array_Length(p_arena_numbers); i++)
    printf(" %d", p_arena_numbers[i]);
  printf("\n");

  Dowa_Arena_Destroy(p_array_arena);
  printf("  Arena destroyed (including array)\n\n");

  // --- Test Medium HashMap (Stress Test) ---
  printf("Testing Medium HashMap (Stress Test)...\n");
  Dowa_KV(char*, int32)* p_large_map = NULL;
  
  char key_buffer[32];
  for (int32 i = 0; i < 100; i++)
  {
    sprintf(key_buffer, "key_%d", i);
    Dowa_HashMap_Push(p_large_map, key_buffer, i * 10);
  }
  
  printf("  Medium map count: %zu\n", Dowa_HashMap_Count(p_large_map));
  
  sprintf(key_buffer, "key_50");
  p_kv = Dowa_HashMap_Get_Ptr(p_large_map, key_buffer);
  if (p_kv)
  {
    Dowa_KV(char*, int32)* p_pair = (Dowa_KV(char*, int32)*)p_kv;
    printf("  Medium map['key_50']: %d\n", p_pair->value);
  }
  
  sprintf(key_buffer, "key_99");
  boolean has_99 = Dowa_HashMap_Has_Key(p_large_map, key_buffer);
  printf("  Has key 'key_99': %d\n", has_99);
  
  Dowa_HashMap_Free(p_large_map);
  printf("  Medium map freed\n\n");

  printf("=== All tests passed! ===\n");
  return 0;
}