MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / test_search

Function test_search

scripts/test_stringzilla.cpp:2263–2400  ·  view source on GitHub ↗

* @brief Tests the correctness of the string class search methods, such as `find` and `find_first_of`. * This covers haystacks and needles of different lengths, as well as character-sets. */

Source from the content-addressed store, hash-verified

2261 * This covers haystacks and needles of different lengths, as well as character-sets.
2262 */
2263void test_search() {
2264
2265 // Searching for a set of characters
2266 assert(sz::string_view("a").find_first_of("az") == 0);
2267 assert(sz::string_view("a").find_last_of("az") == 0);
2268 assert(sz::string_view("a").find_first_of("xz") == sz::string_view::npos);
2269 assert(sz::string_view("a").find_last_of("xz") == sz::string_view::npos);
2270
2271 assert(sz::string_view("a").find_first_not_of("xz") == 0);
2272 assert(sz::string_view("a").find_last_not_of("xz") == 0);
2273 assert(sz::string_view("a").find_first_not_of("az") == sz::string_view::npos);
2274 assert(sz::string_view("a").find_last_not_of("az") == sz::string_view::npos);
2275
2276 assert(sz::string_view("aXbYaXbY").find_first_of("XY") == 1);
2277 assert(sz::string_view("axbYaxbY").find_first_of("Y") == 3);
2278 assert(sz::string_view("YbXaYbXa").find_last_of("XY") == 6);
2279 assert(sz::string_view("YbxaYbxa").find_last_of("Y") == 4);
2280 assert(sz::string_view(sz::base64(), sizeof(sz::base64())).find_first_of("_") == sz::string_view::npos);
2281 assert(sz::string_view(sz::base64(), sizeof(sz::base64())).find_first_of("+") == 62);
2282 assert(sz::string_view(sz::ascii_printables(), sizeof(sz::ascii_printables())).find_first_of("~") !=
2283 sz::string_view::npos);
2284
2285 assert("aabaa"_sv.remove_prefix("a") == "abaa");
2286 assert("aabaa"_sv.remove_suffix("a") == "aaba");
2287 assert("aabaa"_sv.lstrip("a"_bs) == "baa");
2288 assert("aabaa"_sv.rstrip("a"_bs) == "aab");
2289 assert("aabaa"_sv.strip("a"_bs) == "b");
2290
2291 // Check more advanced composite operations
2292 assert("abbccc"_sv.partition('b').before.size() == 1);
2293 assert("abbccc"_sv.partition("bb").before.size() == 1);
2294 assert("abbccc"_sv.partition("bb").match.size() == 2);
2295 assert("abbccc"_sv.partition("bb").after.size() == 3);
2296 assert("abbccc"_sv.partition("bb").before == "a");
2297 assert("abbccc"_sv.partition("bb").match == "bb");
2298 assert("abbccc"_sv.partition("bb").after == "ccc");
2299 assert("abb ccc"_sv.partition(sz::whitespaces_set()).after == "ccc");
2300
2301 // Check ranges of search matches
2302 assert("hello"_sv.find_all("l").size() == 2);
2303 assert("hello"_sv.rfind_all("l").size() == 2);
2304
2305 assert(""_sv.find_all(".", sz::include_overlaps_type {}).size() == 0);
2306 assert(""_sv.find_all(".", sz::exclude_overlaps_type {}).size() == 0);
2307 assert("."_sv.find_all(".", sz::include_overlaps_type {}).size() == 1);
2308 assert("."_sv.find_all(".", sz::exclude_overlaps_type {}).size() == 1);
2309 assert(".."_sv.find_all(".", sz::include_overlaps_type {}).size() == 2);
2310 assert(".."_sv.find_all(".", sz::exclude_overlaps_type {}).size() == 2);
2311 assert(""_sv.rfind_all(".", sz::include_overlaps_type {}).size() == 0);
2312 assert(""_sv.rfind_all(".", sz::exclude_overlaps_type {}).size() == 0);
2313 assert("."_sv.rfind_all(".", sz::include_overlaps_type {}).size() == 1);
2314 assert("."_sv.rfind_all(".", sz::exclude_overlaps_type {}).size() == 1);
2315 assert(".."_sv.rfind_all(".", sz::include_overlaps_type {}).size() == 2);
2316 assert(".."_sv.rfind_all(".", sz::exclude_overlaps_type {}).size() == 2);
2317
2318 assert("a.b.c.d"_sv.find_all(".").size() == 3);
2319 assert("a.,b.,c.,d"_sv.find_all(".,").size() == 3);
2320 assert("a.,b.,c.,d"_sv.rfind_all(".,").size() == 3);

Callers 1

mainFunction · 0.85

Calls 15

whitespaces_setFunction · 0.85
find_allFunction · 0.85
rfind_allFunction · 0.85
advancedFunction · 0.85
lstripMethod · 0.80
rstripMethod · 0.80
stripMethod · 0.80
partitionMethod · 0.80
find_allMethod · 0.80
rfind_allMethod · 0.80
splitMethod · 0.80
rsplitMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…