Add arena tests
This commit is contained in:
55
test/test_arena.cpp
Normal file
55
test/test_arena.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "../core.c"
|
||||
|
||||
void TestScratch() {
|
||||
MA_Arena *scratch_arena_test = NULL;
|
||||
{
|
||||
MA_Scratch scratch;
|
||||
IO_Assert(scratch.checkpoint.arena->len == 0);
|
||||
IO_Assert(scratch.checkpoint.arena->memory.data == NULL);
|
||||
int *a = MA_PushStruct(scratch, int);
|
||||
IO_Assert(scratch.checkpoint.arena->memory.data);
|
||||
IO_Assert(scratch.checkpoint.arena->len == sizeof(int));
|
||||
IO_Assert(scratch.checkpoint.arena);
|
||||
scratch_arena_test = scratch.checkpoint.arena;
|
||||
|
||||
int b = 10;
|
||||
IO_Assert(MA_IsPointerInside(scratch, a));
|
||||
IO_Assert(!MA_IsPointerInside(scratch, &b));
|
||||
}
|
||||
|
||||
{
|
||||
MA_Scratch scratch;
|
||||
IO_Assert(scratch_arena_test == scratch.checkpoint.arena);
|
||||
IO_Assert(scratch.checkpoint.arena->len == 0);
|
||||
|
||||
MA_Scratch scratch2(scratch.checkpoint);
|
||||
IO_Assert(scratch.checkpoint.arena != scratch2.checkpoint.arena);
|
||||
|
||||
MA_Scratch scratch3(scratch.checkpoint, scratch2.checkpoint);
|
||||
IO_Assert(scratch3.checkpoint.arena != scratch2.checkpoint.arena);
|
||||
}
|
||||
}
|
||||
|
||||
void TestBuffer() {
|
||||
char buffer[1024];
|
||||
MA_Arena buffer_arena = MA_MakeFromBuffer(buffer, 1024);
|
||||
void *result = MA_PushSize(&buffer_arena, 1024);
|
||||
IO_Assert(buffer == result);
|
||||
IO_Assert(buffer_arena.len == 1024);
|
||||
IO_Assert(buffer_arena.memory.commit == 1024);
|
||||
IO_Assert(buffer_arena.memory.reserve == 1024);
|
||||
}
|
||||
|
||||
void TestCreateAllocate() {
|
||||
MA_Arena created_arena = MA_Create();
|
||||
IO_Assert(MA_GetTop(&created_arena) != 0);
|
||||
MA_PushSize(&created_arena, MA_MIB(64));
|
||||
IO_Assert(created_arena.len == MA_MIB(64));
|
||||
MA_DeallocateArena(&created_arena);
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestScratch();
|
||||
TestBuffer();
|
||||
TestCreateAllocate();
|
||||
}
|
||||
@@ -1,14 +1,4 @@
|
||||
#include "../io.c"
|
||||
#define MA_ASSERT(x) IO_Assert(x)
|
||||
#include "../arena.c"
|
||||
#include "../defer.hpp"
|
||||
#define ARRAY_REALLOCATE(allocator, p, size, old_size) M_ReallocNonZeroed(allocator, p, size, old_size)
|
||||
#define ARRAY_DEALLOCATE(allocator, p) M_Dealloc(allocator, p)
|
||||
#define ARRAY_Allocator M_Allocator
|
||||
#define ARRAY_SET_DEFAULT_ALLOCATOR \
|
||||
if (!allocator.p) allocator = M_GetSystemAllocator();
|
||||
#include "../array.hpp"
|
||||
|
||||
#include "../core.c"
|
||||
void TestExclusiveArenaBackedArray() {
|
||||
MA_Scratch scratch;
|
||||
MA_Arena ex = MA_Create();
|
||||
@@ -105,7 +95,7 @@ void TestReverseLoop() {
|
||||
|
||||
int i = 99;
|
||||
For(array.reverse()) {
|
||||
assert(it == i--);
|
||||
IO_Assert(it == i--);
|
||||
}
|
||||
|
||||
array.dealloc();
|
||||
|
||||
Reference in New Issue
Block a user