From 46423619695190aec5e862f0577a26ff82aaa677 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 9 Feb 2025 16:35:42 +0000 Subject: [PATCH] Add tests for find and rfind --- tests/str8/test_str8.c | 60 ++++++++++++++++++++++++++++++++---------- tests/str8/test_str8.h | 2 ++ tests/wapptest.c | 31 +++++++++++++++++----- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 2445e83..f77b81f 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -78,26 +78,26 @@ TestFuncResult test_str8_buf(void) { TestFuncResult test_str8_get_index_within_bounds(void) { bool result; - Str8 s1 = wapp_str8_lit("Hello world"); - result = wapp_str8_get(&s1, 4) == 'o'; + Str8RO s1 = wapp_str8_lit_ro("Hello world"); + result = wapp_str8_get(&s1, 4) == 'o'; - Str8 s2 = wapp_str8_lit("Different strokes for different folks"); - result = result && wapp_str8_get(&s2, 0) == 'D'; + Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks"); + result = result && wapp_str8_get(&s2, 0) == 'D'; - Str8 s3 = wapp_str8_lit("Discretion is the better part of valour"); - result = result && wapp_str8_get(&s3, 13) == ' '; + Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour"); + result = result && wapp_str8_get(&s3, 13) == ' '; - Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view"); - result = result && wapp_str8_get(&s4, 20) == 'n'; + Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view"); + result = result && wapp_str8_get(&s4, 20) == 'n'; - Str8 s5 = wapp_str8_lit("Do as I say, not as I do"); - result = result && wapp_str8_get(&s5, 11) == ','; + Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do"); + result = result && wapp_str8_get(&s5, 11) == ','; - Str8 s6 = wapp_str8_lit("Do as you would be done by"); - result = result && wapp_str8_get(&s6, 25) == 'y'; + Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by"); + result = result && wapp_str8_get(&s6, 25) == 'y'; - Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you"); - result = result && wapp_str8_get(&s7, 16) == 's'; + 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); } @@ -141,3 +141,35 @@ TestFuncResult test_str8_set(void) { return wapp_tester_result(result); } + +TestFuncResult test_str8_find(void) { + bool result = true; + Str8RO s = wapp_str8_lit("Do as I say, not as I do"); + + result = 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 = true; + Str8RO s = wapp_str8_lit("Do as I say, not as I do"); + + result = 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); +} diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h index 84c7ebb..8ddec36 100644 --- a/tests/str8/test_str8.h +++ b/tests/str8/test_str8.h @@ -13,6 +13,8 @@ TestFuncResult test_str8_buf(void); TestFuncResult test_str8_get_index_within_bounds(void); TestFuncResult test_str8_get_index_out_of_bounds(void); TestFuncResult test_str8_set(void); +TestFuncResult test_str8_find(void); +TestFuncResult test_str8_rfind(void); #ifdef __cplusplus END_C_LINKAGE diff --git a/tests/wapptest.c b/tests/wapptest.c index 49f6703..1266e78 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -9,13 +9,30 @@ int main(void) { - wapp_tester_run_tests(test_arena_allocator, test_arena_init, test_arena_init_succeeds_when_reserving_very_large_size, - test_str8_lit, test_str8_lit_ro, test_str8_buf, test_str8_get_index_within_bounds, - test_str8_get_index_out_of_bounds, test_str8_set, test_cpath_join_path, test_cpath_dirname, - test_cpath_dirup, test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_fails_when_over_capacity, - test_arena_realloc_bigger_size, test_arena_realloc_smaller_size, test_arena_clear, - test_arena_destroy, test_commander_cmd_success, test_commander_cmd_failure, - test_commander_cmd_out_buf_success, test_commander_cmd_out_buf_failure); + wapp_tester_run_tests(test_arena_allocator, + test_arena_init, + test_arena_init_succeeds_when_reserving_very_large_size, + test_str8_lit, + test_str8_lit_ro, + test_str8_buf, + test_str8_get_index_within_bounds, + test_str8_get_index_out_of_bounds, + test_str8_set, + test_str8_find, + test_str8_rfind, + test_cpath_join_path, + test_cpath_dirname, + test_cpath_dirup, + test_arena_alloc_succeeds_when_within_capacity, + test_arena_alloc_fails_when_over_capacity, + test_arena_realloc_bigger_size, + test_arena_realloc_smaller_size, + test_arena_clear, + test_arena_destroy, + test_commander_cmd_success, + test_commander_cmd_failure, + test_commander_cmd_out_buf_success, + test_commander_cmd_out_buf_failure); return EXIT_SUCCESS; }