#include "test_str8.h" #include "wapp.h" #define ARRLEN(ARR) (sizeof(ARR) / sizeof(ARR[0])) WpTestFuncResult test_str8_lit(void) { b8 result; WpStr8 s1 = wpStr8Lit("Hello world"); result = s1.capacity == 22 && s1.capacity != s1.size; WpStr8 s2 = wpStr8Lit("Different strokes for different folks"); result = result && s2.capacity == 74 && s2.capacity != s2.size; WpStr8 s3 = wpStr8Lit("Discretion is the better part of valour"); result = result && s3.capacity == 78 && s3.capacity != s3.size; WpStr8 s4 = wpStr8Lit("Distance lends enchantment to the view"); result = result && s4.capacity == 76 && s4.capacity != s4.size; WpStr8 s5 = wpStr8Lit("Do as I say, not as I do"); result = result && s5.capacity == 48 && s5.capacity != s5.size; WpStr8 s6 = wpStr8Lit("Do as you would be done by"); result = result && s6.capacity == 52 && s6.capacity != s6.size; WpStr8 s7 = wpStr8Lit("Do unto others as you would have them do to you"); result = result && s7.capacity == 94 && s7.capacity != s7.size; return wpTesterResult(result); } WpTestFuncResult test_str8_lit_ro(void) { b8 result; WpStr8RO s1 = wpStr8LitRo("Hello world"); result = s1.capacity == 11 && s1.capacity == s1.size; WpStr8RO s2 = wpStr8LitRo("Different strokes for different folks"); result = result && s2.capacity == 37 && s2.capacity == s2.size; WpStr8RO s3 = wpStr8LitRo("Discretion is the better part of valour"); result = result && s3.capacity == 39 && s3.capacity == s3.size; WpStr8RO s4 = wpStr8LitRo("Distance lends enchantment to the view"); result = result && s4.capacity == 38 && s4.capacity == s4.size; WpStr8RO s5 = wpStr8LitRo("Do as I say, not as I do"); result = result && s5.capacity == 24 && s5.capacity == s5.size; WpStr8RO s6 = wpStr8LitRo("Do as you would be done by"); result = result && s6.capacity == 26 && s6.capacity == s6.size; WpStr8RO s7 = wpStr8LitRo("Do unto others as you would have them do to you"); result = result && s7.capacity == 47 && s7.capacity == s7.size; return wpTesterResult(result); } WpTestFuncResult test_str8_buf(void) { b8 result; WpStr8 s1 = wpStr8Buf(1024); result = s1.capacity == 1024 && s1.size == 0; WpStr8 s2 = wpStr8Buf(2048); result = result && s2.capacity == 2048 && s2.size == 0; WpStr8 s3 = wpStr8Buf(4096); result = result && s3.capacity == 4096 && s3.size == 0; WpStr8 s4 = wpStr8Buf(8192); result = result && s4.capacity == 8192 && s4.size == 0; return wpTesterResult(result); } WpTestFuncResult test_str8_alloc_buf(void) { b8 result; WpAllocator allocator = wpMemArenaAllocatorInit(KiB(100)); if (wpMemAllocatorInvalid(&allocator)) { return wpTesterResult(false); } u64 capacity = 4096; WpStr8 *s = wpStr8AllocBuf(&allocator, capacity); if (!s) { result = false; goto TEST_ALLOC_BUF_CLEANUP; } result = s->capacity == capacity; const char *cstr = "My name is Abdelrahman"; wpStr8CopyCstrCapped(s, cstr); result = result && s->capacity == capacity && s->size == strlen(cstr) && memcmp(s->buf, cstr, s->size) == 0; TEST_ALLOC_BUF_CLEANUP: wpMemArenaAllocatorDestroy(&allocator); return wpTesterResult(result); } WpTestFuncResult test_str8_alloc_cstr(void) { b8 result; WpAllocator allocator = wpMemArenaAllocatorInit(KiB(100)); if (wpMemAllocatorInvalid(&allocator)) { return wpTesterResult(false); } char *str = "Abdelrahman"; u64 length = strlen(str); WpStr8 *s = wpStr8AllocCstr(&allocator, str); if (!s) { return wpTesterResult(false); } result = s->size == length && memcmp(s->buf, str, length) == 0; wpMemArenaAllocatorDestroy(&allocator); return wpTesterResult(result); } WpTestFuncResult test_str8_alloc_str8(void) { b8 result; WpAllocator allocator = wpMemArenaAllocatorInit(KiB(100)); if (wpMemAllocatorInvalid(&allocator)) { return wpTesterResult(false); } WpStr8 str = wpStr8Lit("Abdelrahman"); WpStr8 *s = wpStr8AllocStr8(&allocator, &str); if (!s) { return wpTesterResult(false); } result = wpStr8Equal(s, &str); wpMemArenaAllocatorDestroy(&allocator); return wpTesterResult(result); } WpTestFuncResult test_str8_alloc_substr(void) { b8 result; WpAllocator allocator = wpMemArenaAllocatorInit(KiB(100)); if (wpMemAllocatorInvalid(&allocator)) { return wpTesterResult(false); } WpStr8 str = wpStr8Lit("Abdelrahman"); WpStr8 *s = wpStr8AllocSubstr(&allocator, &str, 3, 8); if (!s) { return wpTesterResult(false); } result = s->size == 5 && memcmp(s->buf, "elrah", s->size) == 0; wpMemArenaAllocatorDestroy(&allocator); return wpTesterResult(result); } WpTestFuncResult test_str8_get_index_within_bounds(void) { b8 result; WpStr8RO s1 = wpStr8LitRo("Hello world"); result = wpStr8Get(&s1, 4) == 'o'; WpStr8RO s2 = wpStr8LitRo("Different strokes for different folks"); result = result && wpStr8Get(&s2, 0) == 'D'; WpStr8RO s3 = wpStr8LitRo("Discretion is the better part of valour"); result = result && wpStr8Get(&s3, 13) == ' '; WpStr8RO s4 = wpStr8LitRo("Distance lends enchantment to the view"); result = result && wpStr8Get(&s4, 20) == 'n'; WpStr8RO s5 = wpStr8LitRo("Do as I say, not as I do"); result = result && wpStr8Get(&s5, 11) == ','; WpStr8RO s6 = wpStr8LitRo("Do as you would be done by"); result = result && wpStr8Get(&s6, 25) == 'y'; WpStr8RO s7 = wpStr8LitRo("Do unto others as you would have them do to you"); result = result && wpStr8Get(&s7, 16) == 's'; return wpTesterResult(result); } WpTestFuncResult test_str8_get_index_out_of_bounds(void) { WpStr8 s1 = wpStr8Lit("Hello world"); b8 result = wpStr8Get(&s1, 20) == '\0'; return wpTesterResult(result); } WpTestFuncResult test_str8_set(void) { b8 result; WpStr8 s1 = wpStr8Lit("Hello world"); wpStr8Set(&s1, 4, 'f'); result = wpStr8Get(&s1, 4) == 'f'; WpStr8 s2 = wpStr8Lit("Different strokes for different folks"); wpStr8Set(&s2, 0, 'A'); result = result && wpStr8Get(&s2, 0) == 'A'; WpStr8 s3 = wpStr8Lit("Discretion is the better part of valour"); wpStr8Set(&s3, 13, 'u'); result = result && wpStr8Get(&s3, 13) == 'u'; WpStr8 s4 = wpStr8Lit("Distance lends enchantment to the view"); wpStr8Set(&s4, 20, 'R'); result = result && wpStr8Get(&s4, 20) == 'R'; WpStr8 s5 = wpStr8Lit("Do as I say, not as I do"); wpStr8Set(&s5, 11, '.'); result = result && wpStr8Get(&s5, 11) == '.'; WpStr8 s6 = wpStr8Lit("Do as you would be done by"); wpStr8Set(&s6, 25, 'w'); result = result && wpStr8Get(&s6, 25) == 'w'; WpStr8 s7 = wpStr8Lit("Do unto others as you would have them do to you"); wpStr8Set(&s7, 16, 'i'); result = result && wpStr8Get(&s7, 16) == 'i'; return wpTesterResult(result); } WpTestFuncResult test_str8_push_back(void) { b8 result; WpStr8 expected = wpStr8Lit("Abdelrahman"); WpStr8 buf = wpStr8Buf(64); wpStr8PushBack(&buf, 'A'); wpStr8PushBack(&buf, 'b'); wpStr8PushBack(&buf, 'd'); wpStr8PushBack(&buf, 'e'); wpStr8PushBack(&buf, 'l'); wpStr8PushBack(&buf, 'r'); wpStr8PushBack(&buf, 'a'); wpStr8PushBack(&buf, 'h'); wpStr8PushBack(&buf, 'm'); wpStr8PushBack(&buf, 'a'); wpStr8PushBack(&buf, 'n'); result = wpStr8Equal(&buf, &expected); return wpTesterResult(result); } WpTestFuncResult test_str8_equal(void) { b8 result; WpStr8RO s1 = wpStr8LitRo("hello"); WpStr8RO s2 = wpStr8LitRo("hell"); WpStr8RO s3 = wpStr8LitRo("hello"); WpStr8RO s4 = wpStr8LitRo("goodbye"); result = wpStr8Equal(&s1, &s2) == false; result = result && wpStr8Equal(&s1, &s3) == true; result = result && wpStr8Equal(&s1, &s4) == false; return wpTesterResult(result); } WpTestFuncResult test_str8_slice(void) { b8 result; WpStr8 s = wpStr8Lit("Different strokes for different folks"); WpStr8RO sub1 = wpStr8Slice(&s, 3, 9); result = sub1.size == 6 && sub1.capacity == 6; WpStr8RO sub2 = wpStr8Slice(&s, 18, 21); result = result && sub2.size == 3 && sub2.capacity == 3; WpStr8RO sub3 = wpStr8Slice(&s, 5, 1); result = result && sub3.size == 0 && sub3.capacity == 0; WpStr8RO sub4 = wpStr8Slice(&s, 70, 80); result = result && sub4.size == 0 && sub4.capacity == 0; return wpTesterResult(result); } WpTestFuncResult test_str8_alloc_concat(void) { b8 result; WpAllocator arena = wpMemArenaAllocatorInit(KiB(100)); WpStr8 str = wpStr8Lit("Hello world"); WpStr8 suffix1 = wpStr8Lit(" from me."); WpStr8 suffix2 = wpStr8Lit(" This is my code."); WpStr8 concat1 = wpStr8Lit("Hello world from me."); WpStr8 concat2 = wpStr8Lit("Hello world from me. This is my code."); WpStr8 *output; output = wpStr8AllocConcat(&arena, &str, &suffix1); result = output->size == concat1.size && wpStr8Equal(output, &concat1); output = wpStr8AllocConcat(&arena, output, &suffix2); result = result && output->size == concat2.size && wpStr8Equal(output, &concat2); wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_str8_concat_capped(void) { b8 result; WpStr8 str = wpStr8Lit("Hello world"); WpStr8 suffix1 = wpStr8Lit(" from me."); WpStr8 suffix2 = wpStr8Lit(" This is my code."); WpStr8 concat1 = wpStr8Lit("Hello world from me."); WpStr8 concat2 = wpStr8Lit("Hello world from me. T"); wpStr8ConcatCapped(&str, &suffix1); result = str.size == concat1.size && wpStr8Equal(&str, &concat1); wpStr8ConcatCapped(&str, &suffix2); result = result && str.size == concat2.size && wpStr8Equal(&str, &concat2); return wpTesterResult(result); } WpTestFuncResult test_str8_copy_cstr_capped(void) { b8 result; WpStr8 buf = wpStr8Buf(32); const char *src1 = "Hello world"; const char *src2 = "Hello world from the Wizard Apprentice standard library"; WpStr8RO src1_cp = wpStr8LitRo("Hello world"); WpStr8RO src2_cp = wpStr8LitRo("Hello world from the Wizard Appr"); wpStr8CopyCstrCapped(&buf, src1); result = buf.size == src1_cp.size && wpStr8Equal(&buf, &src1_cp); wpStr8CopyCstrCapped(&buf, src2); result = result && buf.size == src2_cp.size && buf.size == buf.capacity && wpStr8Equal(&buf, &src2_cp); return wpTesterResult(result); } WpTestFuncResult test_str8_copy_str8_capped(void) { b8 result; WpStr8 buf = wpStr8Buf(32); WpStr8RO src1 = wpStr8LitRo("Hello world"); WpStr8RO src2 = wpStr8LitRo("Hello world from the Wizard Apprentice standard library"); WpStr8RO src2_cp = wpStr8LitRo("Hello world from the Wizard Appr"); wpStr8CopyStr8Capped(&buf, &src1); result = buf.size == src1.size && wpStr8Equal(&buf, &src1); wpStr8CopyStr8Capped(&buf, &src2); result = result && buf.size < src2.size && buf.size == buf.capacity && wpStr8Equal(&buf, &src2_cp); return wpTesterResult(result); } WpTestFuncResult test_str8_format(void) { b8 result; WpStr8 buf = wpStr8Buf(128); WpStr8 expected = wpStr8Lit("My name is Abdelrahman and I am 35 years old"); wpStr8Format(&buf, "My name is %s and I am %u years old", "Abdelrahman", 35); result = wpStr8Equal(&buf, &expected); return wpTesterResult(result); } WpTestFuncResult test_str8_find(void) { b8 result; WpStr8RO s = wpStr8Lit("Do as I say, not as I do"); result = wpStr8Find(&s, wpStr8LitRo("d")) != -1; result = result && (wpStr8Find(&s, wpStr8LitRo("not")) != -1); result = result && (wpStr8Find(&s, wpStr8LitRo("as I say")) != -1); result = result && (wpStr8Find(&s, wpStr8LitRo("f")) == -1); result = result && (wpStr8Find(&s, wpStr8LitRo("hello")) == -1); result = result && (wpStr8Find(&s, wpStr8LitRo("not sa I")) == -1); result = result && (wpStr8Find(&s, wpStr8LitRo("Do unto others as you would have them do to you")) == -1); return wpTesterResult(result); } WpTestFuncResult test_str8_rfind(void) { b8 result; WpStr8RO s = wpStr8Lit("Do as I say, not as I do"); result = wpStr8Rfind(&s, wpStr8LitRo("d")) != -1; result = result && (wpStr8Rfind(&s, wpStr8LitRo("not")) != -1); result = result && (wpStr8Rfind(&s, wpStr8LitRo("as I say")) != -1); result = result && (wpStr8Rfind(&s, wpStr8LitRo("f")) == -1); result = result && (wpStr8Rfind(&s, wpStr8LitRo("hello")) == -1); result = result && (wpStr8Rfind(&s, wpStr8LitRo("not sa I")) == -1); result = result && (wpStr8Rfind(&s, wpStr8LitRo("Do unto others as you would have them do to you")) == -1); return wpTesterResult(result); } WpTestFuncResult test_str8_split(void) { b8 result; WpAllocator arena = wpMemArenaAllocatorInit(KiB(100)); WpStr8 str = wpStr8Lit("hello world from me"); WpStr8 delim1 = wpStr8Lit(" "); WpStr8 delim2 = wpStr8Lit("from"); WpStr8List *list1 = wpStr8Split(&arena, &str, &delim1); WpStr8List *list2 = wpStr8Split(&arena, &str, &delim2); WpStr8RO splits1[] = { wpStr8Slice(&str, 0, 5), wpStr8Slice(&str, 6, 11), wpStr8Slice(&str, 12, 16), wpStr8Slice(&str, 17, 19), }; WpStr8RO splits2[] = { wpStr8Slice(&str, 0, 12), wpStr8Slice(&str, 16, 19), }; u64 index1 = 0; u64 count1 = ARRLEN(splits1); b8 running1 = true; u64 index2 = 0; u64 count2 = ARRLEN(splits2); b8 running2 = true; result = list1->node_count == count1 && wpStr8ListTotalSize(list1) == str.size - 3; result = result && list2->node_count == count2 && wpStr8ListTotalSize(list2) == str.size - 4; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running1) { WpStr8 *node = wpDblListGet(WpStr8, list1, index1); result = result && wpStr8Equal(node, &(splits1[index1])); ++index1; running1 = index1 < count1; } // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running2) { WpStr8 *node = wpDblListGet(WpStr8, list2, index2); result = result && wpStr8Equal(node, &(splits2[index2])); ++index2; running2 = index2 < count2; } wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_str8_split_with_max(void) { b8 result; WpAllocator arena = wpMemArenaAllocatorInit(KiB(100)); WpStr8 str = wpStr8Lit("hello world from me"); WpStr8 delim = wpStr8Lit(" "); WpStr8List *list = wpStr8SplitWithMax(&arena, &str, &delim, 2); WpStr8RO splits[] = { wpStr8Slice(&str, 0, 5), wpStr8Slice(&str, 6, 11), wpStr8Slice(&str, 12, 19), }; u64 index = 0; u64 count = ARRLEN(splits); b8 running = true; result = list->node_count == count && wpStr8ListTotalSize(list) == str.size - 2; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running) { WpStr8 *node = wpDblListGet(WpStr8, list, index); result = result && wpStr8Equal(node, &(splits[index])); ++index; running = index < count; } wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_str8_rsplit(void) { b8 result; WpAllocator arena = wpMemArenaAllocatorInit(KiB(100)); WpStr8 str = wpStr8Lit("hello world from me"); WpStr8 delim1 = wpStr8Lit(" "); WpStr8 delim2 = wpStr8Lit("from"); WpStr8List *list1 = wpStr8Rsplit(&arena, &str, &delim1); WpStr8List *list2 = wpStr8Rsplit(&arena, &str, &delim2); WpStr8RO splits1[] = { wpStr8Slice(&str, 0, 5), wpStr8Slice(&str, 6, 11), wpStr8Slice(&str, 12, 16), wpStr8Slice(&str, 17, 19), }; WpStr8RO splits2[] = { wpStr8Slice(&str, 0, 12), wpStr8Slice(&str, 16, 19), }; u64 index1 = 0; u64 count1 = ARRLEN(splits1); b8 running1 = true; u64 index2 = 0; u64 count2 = ARRLEN(splits2); b8 running2 = true; result = list1->node_count == count1 && wpStr8ListTotalSize(list1) == str.size - 3; result = result && list2->node_count == count2 && wpStr8ListTotalSize(list2) == str.size - 4; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running1) { WpStr8 *node = wpDblListGet(WpStr8, list1, index1); result = result && wpStr8Equal(node, &(splits1[index1])); ++index1; running1 = index1 < count1; } // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running2) { WpStr8 *node = wpDblListGet(WpStr8, list2, index2); result = result && wpStr8Equal(node, &(splits2[index2])); ++index2; running2 = index2 < count2; } wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_str8_rsplit_with_max(void) { b8 result; WpAllocator arena = wpMemArenaAllocatorInit(KiB(100)); WpStr8 str = wpStr8Lit("hello world from me"); WpStr8 delim = wpStr8Lit(" "); WpStr8List *list = wpStr8RsplitWithMax(&arena, &str, &delim, 2); WpStr8RO splits[] = { wpStr8Slice(&str, 0, 11), wpStr8Slice(&str, 12, 16), wpStr8Slice(&str, 17, 19), }; u64 index = 0; u64 count = ARRLEN(splits); b8 running = true; result = list->node_count == count && wpStr8ListTotalSize(list) == str.size - 2; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings while (running) { WpStr8 *node = wpDblListGet(WpStr8, list, index); result = result && wpStr8Equal(node, &(splits[index])); ++index; running = index < count; } wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_str8_join(void) { b8 result; WpAllocator arena = wpMemArenaAllocatorInit(KiB(100)); WpStr8 str = wpStr8Lit("hello world from me"); WpStr8 delim1 = wpStr8Lit(" "); WpStr8 delim2 = wpStr8Lit("from"); WpStr8List *list1 = wpStr8Rsplit(&arena, &str, &delim1); WpStr8List *list2 = wpStr8Rsplit(&arena, &str, &delim2); WpStr8 *join1 = wpStr8Join(&arena, list1, &delim1); WpStr8 *join2 = wpStr8Join(&arena, list2, &delim2); result = join1->size == str.size && wpStr8Equal(join1, &str); result = result && join2->size == str.size && wpStr8Equal(join2, &str); wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_str8_from_bytes(void) { b8 result; WpStr8 str = wpStr8Buf(1024); WpU8Array bytes = wpArray(u8, 'W', 'A', 'P', 'P'); wpStr8FromBytes(&str, bytes); result = str.size == wpArrayCount(bytes) * wpArrayItemSize(bytes); result = result && wpStr8Equal(&str, &wpStr8LitRo("WAPP")); return wpTesterResult(result); }