#include "test_str8.h" #include "mem_allocator.h" #include "mem_arena_allocator.h" #include "misc_utils.h" #include "str8.h" #include "tester.h" #include TestFuncResult test_str8_lit(void) { bool result; Str8 s1 = wapp_str8_lit("Hello world"); result = s1.capacity == 22 && s1.capacity != s1.size; Str8 s2 = wapp_str8_lit("Different strokes for different folks"); result = result && s2.capacity == 74 && s2.capacity != s2.size; Str8 s3 = wapp_str8_lit("Discretion is the better part of valour"); result = result && s3.capacity == 78 && s3.capacity != s3.size; Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view"); result = result && s4.capacity == 76 && s4.capacity != s4.size; Str8 s5 = wapp_str8_lit("Do as I say, not as I do"); result = result && s5.capacity == 48 && s5.capacity != s5.size; Str8 s6 = wapp_str8_lit("Do as you would be done by"); result = result && s6.capacity == 52 && s6.capacity != s6.size; Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you"); result = result && s7.capacity == 94 && s7.capacity != s7.size; return wapp_tester_result(result); } TestFuncResult test_str8_lit_ro(void) { bool result; Str8RO s1 = wapp_str8_lit_ro("Hello world"); result = s1.capacity == 11 && s1.capacity == s1.size; Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks"); result = result && s2.capacity == 37 && s2.capacity == s2.size; Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour"); result = result && s3.capacity == 39 && s3.capacity == s3.size; Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view"); result = result && s4.capacity == 38 && s4.capacity == s4.size; Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do"); result = result && s5.capacity == 24 && s5.capacity == s5.size; Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by"); result = result && s6.capacity == 26 && s6.capacity == s6.size; Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you"); result = result && s7.capacity == 47 && s7.capacity == s7.size; return wapp_tester_result(result); } TestFuncResult test_str8_buf(void) { bool result; Str8 s1 = wapp_str8_buf(1024); result = s1.capacity == 1024 && s1.size == 0; Str8 s2 = wapp_str8_buf(2048); result = result && s2.capacity == 2048 && s2.size == 0; Str8 s3 = wapp_str8_buf(4096); result = result && s3.capacity == 4096 && s3.size == 0; Str8 s4 = wapp_str8_buf(8192); result = result && s4.capacity == 8192 && s4.size == 0; return wapp_tester_result(result); } TestFuncResult test_str8_buf_alloc(void) { bool result; Allocator allocator = wapp_mem_arena_allocator_init(MB(10)); if (wapp_mem_allocator_invalid(&allocator)) { return wapp_tester_result(false); } Str8 *s = wapp_str8_buf_alloc(&allocator, 4096); result = s != NULL && s->capacity == 4096; wapp_mem_arena_allocator_destroy(&allocator); return wapp_tester_result(result); } TestFuncResult test_str8_alloc_cstr(void) { bool result; Allocator allocator = wapp_mem_arena_allocator_init(MB(10)); if (wapp_mem_allocator_invalid(&allocator)) { return wapp_tester_result(false); } char *str = "Abdelrahman"; u64 length = strlen(str); Str8 *s = wapp_str8_alloc_cstr(&allocator, str); if (!s) { return wapp_tester_result(false); } result = s->size == length && memcmp(s->buf, str, length) == 0; wapp_mem_arena_allocator_destroy(&allocator); return wapp_tester_result(result); } TestFuncResult test_str8_alloc_str8(void) { bool result; Allocator allocator = wapp_mem_arena_allocator_init(MB(10)); if (wapp_mem_allocator_invalid(&allocator)) { return wapp_tester_result(false); } Str8 str = wapp_str8_lit("Abdelrahman"); Str8 *s = wapp_str8_alloc_str8(&allocator, &str); if (!s) { return wapp_tester_result(false); } result = s->size == str.size && memcmp(s->buf, str.buf, str.size) == 0; wapp_mem_arena_allocator_destroy(&allocator); return wapp_tester_result(result); } TestFuncResult test_str8_get_index_within_bounds(void) { bool result; Str8RO s1 = wapp_str8_lit_ro("Hello world"); result = wapp_str8_get(&s1, 4) == 'o'; Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks"); result = result && wapp_str8_get(&s2, 0) == 'D'; Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour"); result = result && wapp_str8_get(&s3, 13) == ' '; Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view"); result = result && wapp_str8_get(&s4, 20) == 'n'; Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do"); result = result && wapp_str8_get(&s5, 11) == ','; Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by"); result = result && wapp_str8_get(&s6, 25) == 'y'; Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you"); result = result && wapp_str8_get(&s7, 16) == 's'; return wapp_tester_result(result); } TestFuncResult test_str8_get_index_out_of_bounds(void) { Str8 s1 = wapp_str8_lit("Hello world"); bool result = wapp_str8_get(&s1, 20) == '\0'; return wapp_tester_result(result); } TestFuncResult test_str8_set(void) { bool result; Str8 s1 = wapp_str8_lit("Hello world"); wapp_str8_set(&s1, 4, 'f'); result = wapp_str8_get(&s1, 4) == 'f'; Str8 s2 = wapp_str8_lit("Different strokes for different folks"); wapp_str8_set(&s2, 0, 'A'); result = result && wapp_str8_get(&s2, 0) == 'A'; Str8 s3 = wapp_str8_lit("Discretion is the better part of valour"); wapp_str8_set(&s3, 13, 'u'); result = result && wapp_str8_get(&s3, 13) == 'u'; Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view"); wapp_str8_set(&s4, 20, 'R'); result = result && wapp_str8_get(&s4, 20) == 'R'; Str8 s5 = wapp_str8_lit("Do as I say, not as I do"); wapp_str8_set(&s5, 11, '.'); result = result && wapp_str8_get(&s5, 11) == '.'; Str8 s6 = wapp_str8_lit("Do as you would be done by"); wapp_str8_set(&s6, 25, 'w'); result = result && wapp_str8_get(&s6, 25) == 'w'; Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you"); wapp_str8_set(&s7, 16, 'i'); result = result && wapp_str8_get(&s7, 16) == 'i'; return wapp_tester_result(result); } TestFuncResult test_str8_substr(void) { bool result; Str8 s = wapp_str8_lit("Different strokes for different folks"); Str8RO sub1 = wapp_str8_substr(&s, 3, 9); result = sub1.size == 6 && sub1.capacity == 6; Str8RO sub2 = wapp_str8_substr(&s, 18, 21); result = result && sub2.size == 3 && sub2.capacity == 3; Str8RO sub3 = wapp_str8_substr(&s, 5, 1); result = result && sub3.size == 0 && sub3.capacity == 0; Str8RO sub4 = wapp_str8_substr(&s, 70, 80); result = result && sub4.size == 0 && sub4.capacity == 0; return wapp_tester_result(result); } TestFuncResult test_str8_find(void) { bool result; Str8RO s = wapp_str8_lit("Do as I say, not as I do"); result = wapp_str8_find(&s, wapp_str8_lit_ro("d")) != -1; result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("not")) != -1); result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("as I say")) != -1); result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("f")) == -1); result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("hello")) == -1); result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("not sa I")) == -1); result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("Do unto others as you would have them do to you")) == -1); return wapp_tester_result(result); } TestFuncResult test_str8_rfind(void) { bool result; Str8RO s = wapp_str8_lit("Do as I say, not as I do"); result = wapp_str8_rfind(&s, wapp_str8_lit_ro("d")) != -1; result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("not")) != -1); result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("as I say")) != -1); result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("f")) == -1); result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("hello")) == -1); result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("not sa I")) == -1); result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("Do unto others as you would have them do to you")) == -1); return wapp_tester_result(result); }