From ab3edaa58eed4ff73410954ca094531d49eb5844 Mon Sep 17 00:00:00 2001 From: talha Date: Thu, 15 Feb 2024 09:58:42 +0500 Subject: added personal libraries to git tracking --- strings/test.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 strings/test.cpp (limited to 'strings/test.cpp') diff --git a/strings/test.cpp b/strings/test.cpp new file mode 100644 index 0000000..49f81c9 --- /dev/null +++ b/strings/test.cpp @@ -0,0 +1,65 @@ +#include +#include "strings.h" +#include "strings.cpp" + +void TestCstrSize() +{ + u64 size = CstrSize("hello sailor\n"); + assert(size == 13 && "test_cstr_size() failed" + "the cstr size was wrong\n"); +} + +void TestEmptyCstrSize() +{ + u64 size = CstrSize(""); + assert(size == 0 && "test_empty_cstr_size() failed" + "the cstr size was wrong\n"); +} + +void TestStr8InitCstr(u8* buffer, u64 buffer_size) +{ + struct arena a = {}; + arena_init(&a, buffer, buffer_size); + struct res_str8 qres = Str8InitCstr(&a, "hello sailor"); + assert((qres.bytes_count == 12) && "Error TestStr8InitCstr:\n" + "Reason: Invalid size of allocated string\n" + "Hint: check what is happening to the bytes_count and how it is set first\n"); +} + +void TestStr8InitCstrEmpty(u8* buffer, u64 buffer_size) +{ + struct arena a = {}; + arena_init(&a, buffer, buffer_size); + struct res_str8 qres = Str8InitCstr(&a, ""); + assert((qres.string.size == 0) && "Error TestStr8InitCstrEmpty:\n" + "Reason: Invalid size of allocated string\n" + "Hint: check what is happening to the bytes_count and how it is set first\n"); +} + +void TestStr8InitCstrNoSpace(u8* buffer, u8 buffer_size) +{ + struct arena a = {}; + arena_init(&a, buffer, buffer_size); + struct res_str8 qres = Str8InitCstr(&a, "hello sailor, looks like you've got no space here on this boat!"); + assert(qres.string.size == 0 && "Error TestStr8InitStrNoSpace:\n" + "Reason: the bytes count is not 0, even though the string is larger than the supposedly available\n" + "arena space. This is an error and should not occur.\n" + "Hint: Look at what is going on in the allocation since that arena helpers handle allocations\n."); +} + +void test_strings() +{ + printf("\n======= Testing Strings Library =======\n"); + printf("===== Testing Cstring Functions =====\n"); + TestCstrSize(); + TestEmptyCstrSize(); + + u64 buffer_size = KB(64); + u8* buffer = (u8*)malloc(sizeof(u8)*buffer_size); + + TestStr8InitCstr(buffer, buffer_size); + TestStr8InitCstrEmpty(buffer, buffer_size); + TestStr8InitCstrNoSpace(buffer, 8); + printf("Cstring tests passed successfully\n"); + free(buffer); +} -- cgit v1.2.3