Restructuring
This commit is contained in:
1
tests/data/append_file
Normal file
1
tests/data/append_file
Normal file
@@ -0,0 +1 @@
|
||||
WRITE OK APPEND OK
|
||||
1
tests/data/read_file
Normal file
1
tests/data/read_file
Normal file
@@ -0,0 +1 @@
|
||||
OK
|
||||
1
tests/data/write_file
Normal file
1
tests/data/write_file
Normal file
@@ -0,0 +1 @@
|
||||
WRITE2 OK
|
||||
8
tests/main_core_as_header.cpp
Normal file
8
tests/main_core_as_header.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "../core_library/core.h"
|
||||
|
||||
int main() {
|
||||
MA_Arena arena = {};
|
||||
int *a = MA_PushStruct(&arena, int);
|
||||
*a = 10;
|
||||
return 0;
|
||||
}
|
||||
109
tests/test_arena.cpp
Normal file
109
tests/test_arena.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "../core_library/core.c"
|
||||
|
||||
void TestBootstrapArenaClear() {
|
||||
MA_Arena *arena = MA_Bootstrap();
|
||||
IO_Assert(arena->base_len != 0);
|
||||
|
||||
MA_PushSize(arena, 1024);
|
||||
MA_Reset(arena);
|
||||
|
||||
int *vals = MA_PushArray(arena, int, 1024);
|
||||
for (int i = 0; i < 1024; i += 1) {
|
||||
vals[i] = i;
|
||||
}
|
||||
|
||||
int *a = MA_PushStruct(arena, int);
|
||||
*a = 1;
|
||||
|
||||
for (int i = 0; i < 1024; i += 1) {
|
||||
IO_Assert(vals[i] == i);
|
||||
}
|
||||
|
||||
size_t len = arena->len;
|
||||
MA_PopSize(arena, 512);
|
||||
IO_Assert(len == arena->len + 512);
|
||||
((char *)arena->memory.data)[arena->len - 1] = 0;
|
||||
|
||||
MA_PopToPos(arena, 512);
|
||||
IO_Assert(arena->len == 512);
|
||||
((char *)arena->memory.data)[arena->len - 1] = 0;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void TestBootstrap() {
|
||||
MA_Arena *arena = MA_Bootstrap();
|
||||
void *r0 = MA_PushSize(arena, 1024);
|
||||
void *r1 = MA_PushSize(arena, 1024);
|
||||
IO_Assert(arena->len >= 2048);
|
||||
IO_Assert(r0 != r1);
|
||||
MA_DeallocateArena(arena);
|
||||
}
|
||||
|
||||
void TestBootstrapExclusive() {
|
||||
Array<int> v = {MA_BootstrapExclusive()};
|
||||
v.add(0);
|
||||
int *ptr = &v[0];
|
||||
for (int i = 1; i < 1000; i += 1) {
|
||||
IO_Assert(&v[0] == ptr);
|
||||
v.add(i);
|
||||
}
|
||||
int i = 0;
|
||||
For(v) IO_Assert(it == i++);
|
||||
v.dealloc();
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestScratch();
|
||||
TestBuffer();
|
||||
TestCreateAllocate();
|
||||
TestBootstrap();
|
||||
TestBootstrapExclusive();
|
||||
TestBootstrapArenaClear();
|
||||
}
|
||||
129
tests/test_array.cpp
Normal file
129
tests/test_array.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "../core_library/core.c"
|
||||
void TestExclusiveArenaBackedArray() {
|
||||
MA_Scratch scratch;
|
||||
MA_Arena ex = MA_Create();
|
||||
Array<int> array = {MA_GetExclusiveAllocator(&ex)};
|
||||
Array<int *> ptrs = {scratch};
|
||||
array.reserve(16);
|
||||
ptrs.reserve(16);
|
||||
void *initial_p0 = array.data;
|
||||
void *initial_p1 = ptrs.data;
|
||||
|
||||
for (int i = 0; i < 1000; i += 1) {
|
||||
array.add(i);
|
||||
ptrs.add(&array[i]);
|
||||
}
|
||||
for (int i = 0; i < 1000; i += 1) {
|
||||
IO_Assert(array[i] == i);
|
||||
IO_Assert(&array[i] == ptrs[i]);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
For(array) {
|
||||
IO_Assert(it == i++);
|
||||
}
|
||||
IO_Assert(initial_p0 == array.data);
|
||||
IO_Assert(initial_p1 != ptrs.data);
|
||||
|
||||
array.dealloc();
|
||||
}
|
||||
|
||||
Array<int> GenArray() {
|
||||
Array<int> result = {M_GetSystemAllocator()};
|
||||
for (int i = 0; i < 100; i += 1) result.add(i);
|
||||
return result;
|
||||
}
|
||||
|
||||
void TestRemoveForLoop() {
|
||||
Array<int> array = GenArray();
|
||||
IO_Assert(array.len == 100);
|
||||
IO_Assert(array[4] == 4);
|
||||
ForArrayRemovable(array) {
|
||||
ForArrayRemovablePrepare(array);
|
||||
if (it == 4) ForArrayRemovableDeclare();
|
||||
}
|
||||
IO_Assert(array[4] != 4);
|
||||
IO_Assert(array[4] == 5);
|
||||
IO_Assert(array[5] == 6);
|
||||
IO_Assert(array[3] == 3);
|
||||
IO_Assert(array.len == 99);
|
||||
array.dealloc();
|
||||
}
|
||||
|
||||
void TestBasic() {
|
||||
Array<int> array = GenArray();
|
||||
array.unordered_remove_index(40);
|
||||
IO_Assert(array.len == 99);
|
||||
IO_Assert(array[40] != 40);
|
||||
IO_Assert(array[40] == 99);
|
||||
|
||||
array.ordered_remove_index(35);
|
||||
IO_Assert(array.len == 98);
|
||||
IO_Assert(array[35] == 36);
|
||||
|
||||
array.ordered_remove_index(array[34]);
|
||||
IO_Assert(array.len == 97);
|
||||
IO_Assert(array[34] == 36);
|
||||
|
||||
array.unordered_remove(array[30]);
|
||||
IO_Assert(array.len == 96);
|
||||
IO_Assert(array[30] != 30);
|
||||
IO_Assert(array[30] == 98);
|
||||
|
||||
array.insert(101, 20);
|
||||
IO_Assert(array[20] == 101);
|
||||
IO_Assert(array[21] == 20);
|
||||
IO_Assert(array.len == 97);
|
||||
|
||||
IO_Assert(array.contains(array[20]));
|
||||
IO_Assert(array.contains(array[96]));
|
||||
IO_Assert(array.contains(array[0]));
|
||||
IO_Assert(!array.contains(array.data[97]));
|
||||
|
||||
IO_Assert(array.is_first(array[0]));
|
||||
IO_Assert(!array.is_first(array[1]));
|
||||
|
||||
IO_Assert(!array.is_last(array[0]));
|
||||
IO_Assert(array.is_last(array[array.len - 1]));
|
||||
|
||||
array.reset();
|
||||
IO_Assert(array.len == 0);
|
||||
array.dealloc();
|
||||
}
|
||||
|
||||
void TestReverseLoop() {
|
||||
Array<int> array = GenArray();
|
||||
|
||||
int i = 99;
|
||||
For(array.reverse()) {
|
||||
IO_Assert(it == i--);
|
||||
}
|
||||
|
||||
array.dealloc();
|
||||
}
|
||||
|
||||
void TestCopy() {
|
||||
MA_Scratch scratch;
|
||||
auto a = GenArray();
|
||||
auto b = a.tight_copy(scratch);
|
||||
auto c = a.copy(scratch);
|
||||
|
||||
int i = 0;
|
||||
For(b) IO_Assert(it == i++);
|
||||
i = 0;
|
||||
For(c) IO_Assert(it == i++);
|
||||
|
||||
IO_Assert(b.cap == b.len && b.len == a.len);
|
||||
IO_Assert(a.len == c.len && a.cap == c.cap);
|
||||
|
||||
a.dealloc();
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestExclusiveArenaBackedArray();
|
||||
TestRemoveForLoop();
|
||||
TestBasic();
|
||||
TestReverseLoop();
|
||||
TestCopy();
|
||||
return 0;
|
||||
}
|
||||
101
tests/test_filesystem.c
Normal file
101
tests/test_filesystem.c
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "../core_library/core.c"
|
||||
|
||||
int main() {
|
||||
MA_Arena arena = {0};
|
||||
S8_String read_file_path = S8_Lit("../tests/data/read_file");
|
||||
|
||||
// Read file test
|
||||
{
|
||||
S8_String file = OS_ReadFile(&arena, read_file_path);
|
||||
IO_Assert(S8_AreEqual(file, S8_Lit("OK"), 0));
|
||||
}
|
||||
|
||||
// Write file test
|
||||
{
|
||||
S8_String path = S8_Lit("../tests/data/write_file");
|
||||
{
|
||||
S8_String data = S8_Lit("WRITE1 OK");
|
||||
OS_Result result = OS_WriteFile(path, data);
|
||||
IO_Assert(result == OS_SUCCESS);
|
||||
|
||||
S8_String read = OS_ReadFile(&arena, path);
|
||||
IO_Assert(S8_AreEqual(read, data, 0));
|
||||
}
|
||||
S8_String data = S8_Lit("WRITE2 OK");
|
||||
|
||||
OS_Result result = OS_WriteFile(path, data);
|
||||
IO_Assert(result == OS_SUCCESS);
|
||||
|
||||
S8_String read = OS_ReadFile(&arena, path);
|
||||
IO_Assert(S8_AreEqual(read, data, 0));
|
||||
}
|
||||
|
||||
// Append file test
|
||||
{
|
||||
S8_String path = S8_Lit("../tests/data/append_file");
|
||||
{
|
||||
S8_String data = S8_Lit("WRITE OK");
|
||||
OS_Result result = OS_WriteFile(path, data);
|
||||
IO_Assert(result == OS_SUCCESS);
|
||||
}
|
||||
S8_String data = S8_Lit(" APPEND OK");
|
||||
OS_Result result = OS_AppendFile(path, data);
|
||||
IO_Assert(result == OS_SUCCESS);
|
||||
|
||||
S8_String read = OS_ReadFile(&arena, path);
|
||||
IO_Assert(S8_AreEqual(read, S8_Lit("WRITE OK APPEND OK"), 0));
|
||||
}
|
||||
|
||||
IO_Assert(OS_FileExists(read_file_path));
|
||||
IO_Assert(!OS_FileExists(S8_Lit("121ffsadasd.random")));
|
||||
|
||||
// Fetching dirs
|
||||
{
|
||||
S8_String exe_path = OS_GetExePath(&arena);
|
||||
S8_String dir_path = OS_GetExeDir(&arena);
|
||||
S8_String work_path = OS_GetWorkingDir(&arena);
|
||||
S8_String abs_path = OS_GetAbsolutePath(&arena, read_file_path);
|
||||
|
||||
IO_Assert(OS_IsDir(dir_path));
|
||||
IO_Assert(!OS_IsFile(dir_path));
|
||||
|
||||
IO_Assert(OS_IsFile(exe_path));
|
||||
IO_Assert(!OS_IsDir(exe_path));
|
||||
|
||||
IO_Assert(OS_IsAbsolute(exe_path));
|
||||
IO_Assert(OS_IsAbsolute(dir_path));
|
||||
IO_Assert(OS_IsAbsolute(work_path));
|
||||
IO_Assert(OS_IsAbsolute(abs_path));
|
||||
|
||||
IO_Assert(S8_Find(exe_path, S8_Lit("/test_filesystem"), 0, 0));
|
||||
IO_Assert(S8_Find(exe_path, S8_Lit("/build"), 0, 0));
|
||||
IO_Assert(S8_Find(dir_path, S8_Lit("/build"), 0, 0));
|
||||
IO_Assert(S8_Find(work_path, S8_Lit("/build"), 0, 0));
|
||||
IO_Assert(S8_Find(abs_path, S8_Lit("/tests/data"), 0, 0));
|
||||
IO_Assert(!S8_Find(abs_path, S8_Lit("../"), 0, 0));
|
||||
}
|
||||
|
||||
// List dir test
|
||||
{
|
||||
S8_List list = OS_ListDir(&arena, S8_Lit("../tests"), 0);
|
||||
IO_Assert(list.node_count > 4);
|
||||
int dir_count = 0;
|
||||
S8_For(it, list) {
|
||||
if (OS_IsDir(it->string)) {
|
||||
IO_Assert(it->string.str[it->string.len - 1] == '/');
|
||||
dir_count += 1;
|
||||
}
|
||||
IO_Assert(S8_Find(it->string, S8_Lit("/tests"), 0, 0));
|
||||
IO_Assert(!S8_Find(it->string, S8_Lit("../tests"), 0, 0));
|
||||
}
|
||||
IO_Assert(dir_count > 0);
|
||||
|
||||
// relative
|
||||
{
|
||||
S8_List list = OS_ListDir(&arena, S8_Lit("../tests"), OS_RELATIVE_PATHS);
|
||||
S8_For(it, list) {
|
||||
IO_Assert(S8_Find(it->string, S8_Lit("../tests"), 0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
tests/test_table.cpp
Normal file
44
tests/test_table.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "../core_library/core.c"
|
||||
|
||||
void TestSimpleInsertAndIntegrity() {
|
||||
MA_Scratch scratch;
|
||||
Table<uint64_t> table = {scratch};
|
||||
table.reserve(64);
|
||||
|
||||
for (uint64_t i = 0; i < 10000; i += 1) {
|
||||
table.insert(i, i);
|
||||
}
|
||||
for (uint64_t i = 0; i < 10000; i += 1) {
|
||||
uint64_t *v = table.get(i);
|
||||
IO_Assert(*v == i);
|
||||
}
|
||||
IO_Assert(table.len == 10000);
|
||||
IO_Assert(table.cap > table.len);
|
||||
IO_Assert(MA_IS_POW2(table.cap));
|
||||
table.remove(32);
|
||||
table.reset();
|
||||
table.dealloc();
|
||||
}
|
||||
|
||||
void TestStrings() {
|
||||
struct Data {
|
||||
int a[32];
|
||||
int i;
|
||||
};
|
||||
|
||||
Table<Data> table = {};
|
||||
table.puts("1", Data{{}, 1});
|
||||
table.puts("2", Data{{}, 2});
|
||||
table.puts("3", Data{{}, 3});
|
||||
|
||||
IO_Assert(table.gets("1")->i == 1);
|
||||
IO_Assert(table.gets("2")->i == 2);
|
||||
IO_Assert(table.gets("3")->i == 3);
|
||||
table.dealloc();
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestSimpleInsertAndIntegrity();
|
||||
TestStrings();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user