2022-03-22 18:13:06 +00:00
|
|
|
|
|
|
|
Platform_Thread_Result
|
|
|
|
thread_proc(Platform_Thread_Data* td)
|
|
|
|
{
|
2022-03-27 10:47:18 +00:00
|
|
|
//Sleep(100);
|
2022-03-22 18:13:06 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-06 18:29:32 +00:00
|
|
|
void
|
|
|
|
memory_allocator_tests(Allocator* a, bool run_free_tests)
|
|
|
|
{
|
|
|
|
// TestGroup("Allocator Push")
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
u8* buf0 = allocator_alloc(a, 256);
|
|
|
|
buf0[0] = 200;
|
|
|
|
buf0[255] = 199;
|
|
|
|
assert(buf0[0] == 200);
|
|
|
|
assert(buf0[255] == 199);
|
|
|
|
|
|
|
|
u8* buf1 = allocator_alloc(a, 256);
|
|
|
|
buf1[0] = 201;
|
|
|
|
buf1[255] = 202;
|
|
|
|
assert(buf1 >= (buf0 + 256));
|
|
|
|
assert(buf0[0] == 200);
|
|
|
|
assert(buf0[255] == 199);
|
|
|
|
assert(buf1[0] == 201);
|
|
|
|
assert(buf1[255] == 202);
|
|
|
|
|
|
|
|
allocator_clear(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGroup("Allocator Free")
|
|
|
|
if (run_free_tests)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
u8* buf0 = allocator_alloc(a, KB(4));
|
|
|
|
u8* buf1 = allocator_alloc(a, KB(4));
|
|
|
|
u8* buf2 = allocator_alloc(a, KB(4));
|
|
|
|
u8* buf3 = allocator_alloc(a, KB(4));
|
|
|
|
u8* buf4 = allocator_alloc(a, KB(4));
|
|
|
|
assert((buf1 - buf0) >= KB(4));
|
|
|
|
assert((buf2 - buf0) >= KB(8));
|
|
|
|
assert((buf3 - buf0) >= KB(12));
|
|
|
|
assert((buf4 - buf0) >= KB(16));
|
|
|
|
|
|
|
|
allocator_free(a, buf1, KB(4));
|
|
|
|
allocator_free(a, buf2, KB(4));
|
|
|
|
u8* buf5 = allocator_alloc(a, KB(7));
|
|
|
|
// buf5 should get put in the place of buf1 since buf1 and 2 get
|
|
|
|
// merged
|
|
|
|
assert(buf5 == buf1);
|
|
|
|
|
|
|
|
allocator_free(a, buf4, KB(4));
|
|
|
|
allocator_free(a, buf3, KB(4));
|
|
|
|
allocator_free(a, buf0, KB(4));
|
|
|
|
u8* buf6 = allocator_alloc(a, KB(4));
|
|
|
|
assert(buf0 == buf6);
|
|
|
|
|
|
|
|
allocator_clear(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
memory_tests()
|
|
|
|
{
|
|
|
|
// TestGroup("Platform Allocation")
|
|
|
|
{
|
|
|
|
u8* base = platform_mem_reserve(GB(32));
|
|
|
|
platform_mem_commit(base, KB(4));
|
|
|
|
base[4095] = 200;
|
|
|
|
assert(base[4095] == 200);
|
|
|
|
platform_mem_commit(base + KB(4), KB(4));
|
|
|
|
base[5000] = 200;
|
|
|
|
assert(base[5000] == 200);
|
|
|
|
platform_mem_decommit(base, KB(8));
|
|
|
|
platform_mem_release(base, GB(32));
|
|
|
|
}
|
|
|
|
|
|
|
|
Allocator* bump = bump_allocator_create_reserve(KB(32));
|
|
|
|
memory_allocator_tests(bump, false);
|
|
|
|
allocator_destroy(bump);
|
|
|
|
|
|
|
|
Allocator* paged = paged_allocator_create_reserve(KB(32));
|
|
|
|
memory_allocator_tests(paged, true);
|
|
|
|
allocator_destroy(paged);
|
|
|
|
}
|
|
|
|
|
2022-03-22 18:13:06 +00:00
|
|
|
internal void
|
|
|
|
run_tests()
|
|
|
|
{
|
2022-03-27 10:47:18 +00:00
|
|
|
// memory tests
|
|
|
|
|
|
|
|
u8* a0 = allocator_alloc_array(scratch, u8, 32);
|
|
|
|
u8* a1 = allocator_alloc_array(scratch, u8, 32);
|
|
|
|
assert(a0 != a1);
|
|
|
|
assert((a0 + 32) <= a1);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
a0[i] = (u8)i;
|
|
|
|
a1[i] = (u8)(100 + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (u32 i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
assert(a0[i] == i);
|
|
|
|
assert(a1[i] == (100 + i));
|
|
|
|
}
|
|
|
|
|
2022-04-06 18:29:32 +00:00
|
|
|
memory_tests();
|
|
|
|
|
2022-03-27 10:47:18 +00:00
|
|
|
#if defined(PLATFORM_wasm)
|
|
|
|
// NOTE(PS): the tests below this point don't make sense on a web assembly
|
|
|
|
// platform
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2022-03-22 18:13:06 +00:00
|
|
|
// testing strings and exe path
|
|
|
|
String exe_file_path = platform_get_exe_path(scratch);
|
|
|
|
assert(exe_file_path.str != 0);
|
|
|
|
u64 run_tree_start = string_find_substring(exe_file_path, lit_str("run_tree"), 0, StringMatch_FindLast);
|
|
|
|
u64 run_tree_end = run_tree_start + lit_str("run_tree").len;
|
|
|
|
assert(run_tree_start < exe_file_path.len);
|
|
|
|
String run_tree_path = string_get_prefix(exe_file_path, run_tree_end);
|
|
|
|
String run_tree_path_nullterm = string_copy(run_tree_path, scratch);
|
|
|
|
assert(run_tree_path_nullterm.len > 0);
|
|
|
|
assert(platform_pwd_set(run_tree_path_nullterm));
|
|
|
|
|
|
|
|
// testing file io
|
|
|
|
Platform_File_Handle f = platform_file_open(lit_str("text.txt"), FileAccess_Read | FileAccess_Write, FileCreate_OpenExisting);
|
|
|
|
Platform_File_Info i = platform_file_get_info(f, scratch);
|
|
|
|
|
|
|
|
Data d0 = platform_file_read_all(f, scratch);
|
|
|
|
assert(d0.size > 0);
|
|
|
|
|
|
|
|
String s = lit_str("foooooooooobbbbbbaaaarrrrrr");
|
|
|
|
Data d1 = { s.str, s.len };
|
|
|
|
bool r = platform_file_write_all(f, d1);
|
|
|
|
assert(r);
|
|
|
|
|
2022-03-27 10:47:18 +00:00
|
|
|
#if 0
|
|
|
|
// TODO(PS): these were causing startup problems but you weren't focusing on
|
|
|
|
// threads/ When you build something multithreaded come back here and
|
|
|
|
// make tests that actually work
|
|
|
|
|
2022-03-22 18:13:06 +00:00
|
|
|
// testing threads
|
|
|
|
Platform_Thread_Handle threads[8];
|
|
|
|
for (u32 j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
threads[j] = platform_thread_begin(thread_proc, 0);
|
|
|
|
}
|
2022-03-27 10:47:18 +00:00
|
|
|
|
2022-03-22 18:13:06 +00:00
|
|
|
for (u32 j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
platform_thread_end(threads[j]);
|
|
|
|
}
|
2022-03-27 10:47:18 +00:00
|
|
|
#endif
|
2022-03-22 18:13:06 +00:00
|
|
|
|
|
|
|
allocator_clear(scratch);
|
2022-03-27 10:47:18 +00:00
|
|
|
|
2022-03-22 18:13:06 +00:00
|
|
|
}
|