Files
wizapp-stdlib/tests/arena/test_arena.c
T
abdelrahman a998f6b981
Release / release (push) Successful in 8s
Standardize naming conventions (#12)
## Summary

Standardize naming conventions across the entire wizapp-stdlib codebase by replacing inconsistent prefixes and snake_case with a unified `wp` prefix + CamelCase scheme.

## Changes

### Naming convention applied

| Pattern | Before | After |
|---|---|---|
| Public functions | `wapp_module_function` | `wpModuleFunction` |
| Public types | `GenericXxx`, bare `Xxx` | `WpXxx` |
| Constants / enum values | `WAPP_XXX`, `SHELL_XXX` | `WP_XXX`, `WP_SHELL_XXX` |
| Internal functions | `_module_function` | `_moduleFunction` |
| Storage-class macros | `wapp_extern`, `wapp_intern` | `wp_extern`, `wp_intern` |

### Modules affected

All 20 modules were renamed: `arena`, `array`, `dbl_list`, `queue`, `str8`, `mem_allocator`, `mem_utils`, `mem_os`, `file`, `cpath`, `log`, `shell_commander`, `shell_termcolour`, `shell_utils`, `prng/xorshift`, `uuid`, `tester`, `aliases`, `assert`, `misc_utils`, `platform` — plus their test files.

### Backward compatibility

Added `src/oldnames.h` with `#define OLD_NAME NEW_NAME` for every renamed symbol, organized by module under Constants → Types → Functions sections. Existing code that includes this file will compile without changes.

Reviewed-on: #12
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
2026-06-26 17:17:27 +00:00

169 lines
4.3 KiB
C

#include "test_arena.h"
#include "wapp.h"
#include <stdlib.h>
#define ARENA_CAPACITY KiB(16)
#define ARENA_BUF_SIZE KiB(4)
// NOTE (Abdelrahman): Cannot query size of WpArena here so it's hardcoded. Similarly, since arena
// allocation are aligned to power of 2 boundaries, the number of i32 values needed is hardcoded.
#define TEMP_BUF_SIZE (40 + sizeof(i32) * 8)
wp_intern WpArena *arena = NULL;
wp_intern i32 count = 20;
wp_intern i32 *array = NULL;
wp_intern WpArena *buf_arena = NULL;
wp_intern u8 buf[ARENA_BUF_SIZE] = {0};
wp_intern WpArena *temp_arena = NULL;
wp_intern u8 temp_buf[TEMP_BUF_SIZE] = {0};
WpTestFuncResult test_arena_init_buffer(void) {
b8 result = wpMemArenaInitBuffer(&buf_arena, buf, ARENA_BUF_SIZE);
return wpTesterResult(result);
}
WpTestFuncResult test_arena_init_allocated(void) {
b8 result = wpMemArenaInitAllocated(&arena, ARENA_CAPACITY);
return wpTesterResult(result);
}
WpTestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) {
WpArena *large_arena = NULL;
u64 capacity = GiB(512);
b8 result = wpMemArenaInitAllocated(&large_arena, capacity);
if (result) {
wpMemArenaDestroy(&large_arena);
}
return wpTesterResult(result);
}
WpTestFuncResult test_arena_alloc_with_buffer(void) {
i32 *arr = (i32 *)wpMemArenaAlloc(arena, count * sizeof(i32));
b8 result = arr != NULL;
for (i32 i = 0; i < count; ++i) {
arr[i] = i * 10;
}
return wpTesterResult(result);
}
WpTestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) {
array = wpMemArenaAlloc(arena, count * sizeof(i32));
b8 result = array != NULL;
for (i32 i = 0; i < count; ++i) {
array[i] = i * 10;
}
return wpTesterResult(result);
}
WpTestFuncResult test_arena_alloc_fails_when_over_capacity(void) {
u8 *bytes = wpMemArenaAlloc(arena, ARENA_CAPACITY * 2);
b8 result = bytes == NULL;
return wpTesterResult(result);
}
WpTestFuncResult test_arena_realloc_bigger_size(void) {
u64 old_count = 10;
u64 new_count = 20;
i32 *bytes = wpMemArenaAlloc(arena, old_count * sizeof(i32));
for (u64 i = 0; i < old_count; ++i) {
bytes[i] = (i32)i;
}
i32 *new_bytes = wpMemArenaRealloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32));
if (!new_bytes) {
return wpTesterResult(false);
}
for (u64 i = 0; i < new_count; ++i) {
if (i < old_count && new_bytes[i] != bytes[i]) {
return wpTesterResult(false);
}
}
return wpTesterResult(true);
}
WpTestFuncResult test_arena_realloc_smaller_size(void) {
u64 old_count = 10;
u64 new_count = 5;
i32 *bytes = wpMemArenaAlloc(arena, old_count * sizeof(i32));
for (u64 i = 0; i < old_count; ++i) {
bytes[i] = (i32)i;
}
i32 *new_bytes = wpMemArenaRealloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32));
if (!new_bytes) {
return wpTesterResult(false);
}
for (u64 i = 0; i < new_count; ++i) {
if (i < new_count && new_bytes[i] != bytes[i]) {
return wpTesterResult(false);
}
}
return wpTesterResult(true);
}
WpTestFuncResult test_arena_temp_begin(void) {
b8 result = wpMemArenaInitBuffer(&temp_arena, temp_buf, TEMP_BUF_SIZE);
i32 *num1 = (i32 *)wpMemArenaAlloc(temp_arena, sizeof(i32));
result = result && num1 != NULL;
wpMemArenaTempBegin(temp_arena);
i32 *num2 = (i32 *)wpMemArenaAlloc(temp_arena, sizeof(i32));
result = result && num2 != NULL;
i32 *num3 = (i32 *)wpMemArenaAlloc(temp_arena, sizeof(i32));
result = result && num3 == NULL;
return wpTesterResult(result);
}
WpTestFuncResult test_arena_temp_end(void) {
wpMemArenaTempEnd(temp_arena);
i32 *num1 = (i32 *)wpMemArenaAlloc(temp_arena, sizeof(i32));
b8 result = num1 != NULL;
i32 *num2 = (i32 *)wpMemArenaAlloc(temp_arena, sizeof(i32));
result = result && num2 == NULL;
wpMemArenaDestroy(&temp_arena);
return wpTesterResult(result);
}
WpTestFuncResult test_arena_clear(void) {
wpMemArenaClear(arena);
b8 result = true;
for (i32 i = 0; i < count; ++i) {
if (array[i] != 0) {
result = false;
break;
}
}
return wpTesterResult(result);
}
WpTestFuncResult test_arena_destroy_buffer(void) {
wpMemArenaDestroy(&buf_arena);
b8 result = buf_arena == NULL;
return wpTesterResult(result);
}
WpTestFuncResult test_arena_destroy_allocated(void) {
wpMemArenaDestroy(&arena);
b8 result = arena == NULL;
return wpTesterResult(result);
}