From 2c74b727ef2cada01e300e4efbbfe9a8c7150b43 Mon Sep 17 00:00:00 2001 From: Robear Selwans Date: Mon, 4 May 2026 21:44:32 +0300 Subject: [PATCH] Added more tests + fixed evstr issues --- ev_str.h | 27 +++++++++++++-------- tests/ev_str/find_all_count_only.c | 15 ++++++++++++ tests/ev_str/find_all_exact_match.c | 18 ++++++++++++++ tests/ev_str/find_all_no_matches.c | 16 ++++++++++++ tests/ev_str/find_all_overlapping.c | 23 ++++++++++++++++++ tests/ev_str/find_all_short_suffix.c | 18 ++++++++++++++ tests/ev_str/find_first_empty_text.c | 17 +++++++++++++ tests/ev_str/find_first_query_too_long.c | 17 +++++++++++++ tests/ev_str/meson.build | 8 ++++++ tests/ev_str/replace_first_query_too_long.c | 18 ++++++++++++++ 10 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 tests/ev_str/find_all_count_only.c create mode 100644 tests/ev_str/find_all_exact_match.c create mode 100644 tests/ev_str/find_all_no_matches.c create mode 100644 tests/ev_str/find_all_overlapping.c create mode 100644 tests/ev_str/find_all_short_suffix.c create mode 100644 tests/ev_str/find_first_empty_text.c create mode 100644 tests/ev_str/find_first_query_too_long.c create mode 100644 tests/ev_str/replace_first_query_too_long.c diff --git a/ev_str.h b/ev_str.h index 5ff50a3..2617602 100644 --- a/ev_str.h +++ b/ev_str.h @@ -588,7 +588,7 @@ __evstring_findFirst_impl( .offset = ~0ull }; - if(query.len == 0) + if(query.len == 0 || query.len > text.len) return result; for(u64 l = text.offset; l <= text.offset + text.len - query.len; l++) @@ -731,18 +731,25 @@ evstring_findAll( if(text_len == 0 || query_len == 0 || query_len > text_len) { return 0; } - bool check_run = (results == NULL); + + evstring_view query_view = evstring_slice(query, 0, -1); + u64 count = 0; - for(evstring_view v = evstring_findFirst(text, query); - v.len != 0; - v = __evstring_findFirst_impl( - evstring_slice(text, v.offset + v.len, -1), - evstring_slice(query, 0, -1))) { - if(!check_run) { - results[count] = v; + + for(u64 l = 0; l <= text_len - query_len; l++) + { + evstring_view curr_view = { + .data = text, + .len = query_len, + .offset = l + }; + if(EV_EQUAL(evstring_view)(&curr_view, &query_view)) + { + if(results) results[count] = curr_view; + count++; } - count++; } + return count; } diff --git a/tests/ev_str/find_all_count_only.c b/tests/ev_str/find_all_count_only.c new file mode 100644 index 0000000..45140ab --- /dev/null +++ b/tests/ev_str/find_all_count_only.c @@ -0,0 +1,15 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring text = evstr("Hello, this is me saying `Hello` like someone who says 'Hello'"); + + assert(evstring_findAll(text, evstr("Hello"), NULL) == 3); + assert(evstring_findAll(evstr("abc"), evstr("abc"), NULL) == 1); + assert(evstring_findAll(evstr("abcx"), evstr("abc"), NULL) == 1); + + return 0; +} diff --git a/tests/ev_str/find_all_exact_match.c b/tests/ev_str/find_all_exact_match.c new file mode 100644 index 0000000..da88df8 --- /dev/null +++ b/tests/ev_str/find_all_exact_match.c @@ -0,0 +1,18 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring_view search_results[1]; + evstring text = evstr("abc"); + evstring query = evstr("abc"); + + assert(evstring_findAll(text, query, search_results) == 1); + assert(search_results[0].data == text); + assert(search_results[0].len == 3); + assert(search_results[0].offset == 0); + + return 0; +} diff --git a/tests/ev_str/find_all_no_matches.c b/tests/ev_str/find_all_no_matches.c new file mode 100644 index 0000000..44b0a9d --- /dev/null +++ b/tests/ev_str/find_all_no_matches.c @@ -0,0 +1,16 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring_view search_results[1]; + evstring text = evstr("Hello, this is me saying `Hello` like someone who says 'Hello'"); + + assert(evstring_findAll(text, evstr(""), search_results) == 0); + assert(evstring_findAll(text, evstr("Goodbye"), search_results) == 0); + assert(evstring_findAll(evstr("ab"), evstr("abc"), search_results) == 0); + + return 0; +} diff --git a/tests/ev_str/find_all_overlapping.c b/tests/ev_str/find_all_overlapping.c new file mode 100644 index 0000000..94ef86a --- /dev/null +++ b/tests/ev_str/find_all_overlapping.c @@ -0,0 +1,23 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring_view search_results[2]; + evstring text = evstr("aaa"); + evstring query = evstr("aa"); + + assert(evstring_findAll(text, query, search_results) == 2); + + assert(search_results[0].data == text); + assert(search_results[0].len == 2); + assert(search_results[0].offset == 0); + + assert(search_results[1].data == text); + assert(search_results[1].len == 2); + assert(search_results[1].offset == 1); + + return 0; +} diff --git a/tests/ev_str/find_all_short_suffix.c b/tests/ev_str/find_all_short_suffix.c new file mode 100644 index 0000000..e183c76 --- /dev/null +++ b/tests/ev_str/find_all_short_suffix.c @@ -0,0 +1,18 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring_view search_results[1]; + evstring text = evstr("abcx"); + evstring query = evstr("abc"); + + assert(evstring_findAll(text, query, search_results) == 1); + assert(search_results[0].data == text); + assert(search_results[0].len == 3); + assert(search_results[0].offset == 0); + + return 0; +} diff --git a/tests/ev_str/find_first_empty_text.c b/tests/ev_str/find_first_empty_text.c new file mode 100644 index 0000000..59d5418 --- /dev/null +++ b/tests/ev_str/find_first_empty_text.c @@ -0,0 +1,17 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring text = evstr(""); + evstring query = evstr("a"); + + evstring_view match = evstring_findFirst(text, query); + + assert(match.len == 0); + assert(match.offset == ~0ull); + + return 0; +} diff --git a/tests/ev_str/find_first_query_too_long.c b/tests/ev_str/find_first_query_too_long.c new file mode 100644 index 0000000..f11a545 --- /dev/null +++ b/tests/ev_str/find_first_query_too_long.c @@ -0,0 +1,17 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring text = evstr("ab"); + evstring query = evstr("abc"); + + evstring_view match = evstring_findFirst(text, query); + + assert(match.len == 0); + assert(match.offset == ~0ull); + + return 0; +} diff --git a/tests/ev_str/meson.build b/tests/ev_str/meson.build index 3e95197..fbb370a 100644 --- a/tests/ev_str/meson.build +++ b/tests/ev_str/meson.build @@ -5,12 +5,20 @@ tests = [ 'new_format', 'replace_first', 'find_all', + 'find_all_exact_match', + 'find_all_short_suffix', + 'find_all_count_only', + 'find_all_no_matches', + 'find_all_overlapping', 'push_fmt_float', 'find_first_mismatch', 'find_first_overlapping_prefix', 'stack_get_space', 'overlapping_push', 'find_first_empty_query', + 'find_first_query_too_long', + 'find_first_empty_text', + 'replace_first_query_too_long', ] foreach t : tests diff --git a/tests/ev_str/replace_first_query_too_long.c b/tests/ev_str/replace_first_query_too_long.c new file mode 100644 index 0000000..15355cd --- /dev/null +++ b/tests/ev_str/replace_first_query_too_long.c @@ -0,0 +1,18 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring text = evstr("ab"); + evstring replacement = evstr("x"); + evstring query = evstr("abc"); + + evstring result = evstring_replaceFirst(text, query, replacement); + + assert(EV_EQUAL(evstring)(&result, &text)); + + evstring_free(result); + return 0; +}