diff options
| author | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-28 17:29:31 +0100 |
|---|---|---|
| committer | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-28 17:29:31 +0100 |
| commit | 1f0d290bc303ac8f039963c4eb6421536d36827c (patch) | |
| tree | ed08c2da4524e4ca106b9ff78baf119b405d1076 | |
| parent | a6f93bf8df0dbfa7d19aa1168bfc8b052e41c42f (diff) | |
| download | teachos-1f0d290bc303ac8f039963c4eb6421536d36827c.tar.xz teachos-1f0d290bc303ac8f039963c4eb6421536d36827c.zip | |
string tests
| -rw-r--r-- | libs/kstd/tests/src/string.cpp | 84 |
1 files changed, 49 insertions, 35 deletions
diff --git a/libs/kstd/tests/src/string.cpp b/libs/kstd/tests/src/string.cpp index 43e9a6b..a94f0f6 100644 --- a/libs/kstd/tests/src/string.cpp +++ b/libs/kstd/tests/src/string.cpp @@ -386,60 +386,74 @@ SCENARIO("String iteration", "[string]") REQUIRE(str.back() == 'b'); } } - } - - GIVEN("A const string") - { - auto const str = kstd::string{"Blub"}; - WHEN("iterating over the characters of the string as string_view using a range-based for loop") + GIVEN("A non-empty string") { - kstd::string result; - - for (auto ch : str.view()) - { - result.push_back(ch); - } + auto str = kstd::string{"Hello"}; - THEN("the iterated characters are the same as the characters in the string") + WHEN("using const end()") { - REQUIRE(result == str.view()); + auto it = str.end(); // calls const end() + THEN("it points past the last character") + { + REQUIRE(*std::prev(it) == 'o'); + REQUIRE(std::distance(str.begin(), it) == str.size()); + } } } - WHEN("using front and back to access the first and last characters of the string") + GIVEN("A const string") { - THEN("front returns the first character of the string") + auto const str = kstd::string{"Blub"}; + + WHEN("iterating over the characters of the string as string_view using a range-based for loop") { - REQUIRE(str.front() == 'B'); + kstd::string result; + + for (auto ch : str.view()) + { + result.push_back(ch); + } + + THEN("the iterated characters are the same as the characters in the string") + { + REQUIRE(result == str.view()); + } } - THEN("back returns the last character of the string") + WHEN("using front and back to access the first and last characters of the string") { - REQUIRE(str.back() == 'b'); + THEN("front returns the first character of the string") + { + REQUIRE(str.front() == 'B'); + } + + THEN("back returns the last character of the string") + { + REQUIRE(str.back() == 'b'); + } } } - } - - GIVEN("An empty string") - { - auto str = kstd::string{}; - WHEN("iterating over the characters of an empty string") + GIVEN("An empty string") { - kstd::string result; + auto str = kstd::string{}; - for (auto ch : str.view()) + WHEN("iterating over the characters of an empty string") { - result.push_back(ch); - } + kstd::string result; - THEN("no characters are iterated and the result is an empty string") - { - REQUIRE(result.empty()); - REQUIRE(result.size() == 0); - REQUIRE(result.view() == std::string_view{}); + for (auto ch : str.view()) + { + result.push_back(ch); + } + + THEN("no characters are iterated and the result is an empty string") + { + REQUIRE(result.empty()); + REQUIRE(result.size() == 0); + REQUIRE(result.view() == std::string_view{}); + } } } } -} |
