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

Function scope_assert

scripts/test_stringzilla.cpp:2023–2214  ·  view source on GitHub ↗

Same length replacements.

Source from the content-addressed store, hash-verified

2021
2022 // Same length replacements.
2023 scope_assert(str s = "hello", s.replace_all("xx", "xx"), s == "hello");
2024 scope_assert(str s = "hello", s.replace_all("l", "1"), s == "he11o");
2025 scope_assert(str s = "hello", s.replace_all("he", "al"), s == "alllo");
2026 scope_assert(str s = "hello", s.replace_all("x"_bs, "!"), s == "hello");
2027 scope_assert(str s = "hello", s.replace_all("o"_bs, "!"), s == "hell!");
2028 scope_assert(str s = "hello", s.replace_all("ho"_bs, "!"), s == "!ell!");
2029
2030 // Shorter replacements.
2031 scope_assert(str s = "hello", s.replace_all("xx", "x"), s == "hello");
2032 scope_assert(str s = "hello", s.replace_all("l", ""), s == "heo");
2033 scope_assert(str s = "hello", s.replace_all("h", ""), s == "ello");
2034 scope_assert(str s = "hello", s.replace_all("o", ""), s == "hell");
2035 scope_assert(str s = "hello", s.replace_all("llo", "!"), s == "he!");
2036 scope_assert(str s = "hello", s.replace_all("x"_bs, ""), s == "hello");
2037 scope_assert(str s = "hello", s.replace_all("lo"_bs, ""), s == "he");
2038
2039 // Longer replacements.
2040 scope_assert(str s = "hello", s.replace_all("xx", "xxx"), s == "hello");
2041 scope_assert(str s = "hello", s.replace_all("l", "ll"), s == "hellllo");
2042 scope_assert(str s = "hello", s.replace_all("h", "hh"), s == "hhello");
2043 scope_assert(str s = "hello", s.replace_all("o", "oo"), s == "helloo");
2044 scope_assert(str s = "hello", s.replace_all("llo", "llo!"), s == "hello!");
2045 scope_assert(str s = "hello", s.replace_all("x"_bs, "xx"), s == "hello");
2046 scope_assert(str s = "hello", s.replace_all("lo"_bs, "lo"), s == "helololo");
2047
2048 // Directly mapping bytes using a Look-Up Table.
2049 sz::look_up_table invert_case = sz::look_up_table::identity();
2050 for (char c = 'a'; c <= 'z'; c++) invert_case[c] = c - 'a' + 'A';
2051 for (char c = 'A'; c <= 'Z'; c++) invert_case[c] = c - 'A' + 'a';
2052 scope_assert(str s = "hello", s.lookup(invert_case), s == "HELLO");
2053 scope_assert(str s = "HeLLo", s.lookup(invert_case), s == "hEllO");
2054 scope_assert(str s = "H-lL0", s.lookup(invert_case), s == "h-Ll0");
2055
2056 // Concatenation.
2057 assert(str(str("a") | str("b")) == "ab");
2058 assert(str(str("a") | str("b") | str("ab")) == "abab");
2059
2060 assert(str(sz::concatenate("a"_sv, "b"_sv)) == "ab");
2061 assert(str(sz::concatenate("a"_sv, "b"_sv, "c"_sv)) == "abc");
2062
2063 // Randomization.
2064 assert(str::random(0).empty());
2065 assert(str::random(4).size() == 4);
2066 assert(str::random(4, 42).size() == 4);
2067}
2068
2069/**
2070 * @brief Tests copy constructor and copy-assignment constructor of `sz::string`.
2071 */
2072void test_constructors() {
2073 std::string alphabet {sz::ascii_printables(), sizeof(sz::ascii_printables())};
2074 std::vector<sz::string> strings;
2075 for (std::size_t alphabet_slice = 0; alphabet_slice != alphabet.size(); ++alphabet_slice)
2076 strings.push_back(alphabet.substr(0, alphabet_slice));
2077 std::vector<sz::string> copies {strings};
2078 assert(copies.size() == strings.size());
2079 for (size_t i = 0; i < copies.size(); ++i) {
2080 assert(copies[i].size() == strings[i].size());

Callers 2

Calls 10

concatenateFunction · 0.85
scale_iterationsFunction · 0.85
lookupMethod · 0.80
push_backMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
substrMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…