From 864bd70ece08b7d03bd3dfd6b242cf05e9687547 Mon Sep 17 00:00:00 2001 From: jun <83899451+zeichensystem@users.noreply.github.com> Date: Thu, 8 May 2025 20:38:25 +0200 Subject: [PATCH] Add more guf_str tests --- src/test/test_str.hpp | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/src/test/test_str.hpp b/src/test/test_str.hpp index 4e4562a..0cf08d8 100644 --- a/src/test/test_str.hpp +++ b/src/test/test_str.hpp @@ -140,6 +140,43 @@ private: guf_str_free(&s0, NULL); TEST_CHECK(guf_str_is_uninit(&s0)); } + + auto test_popsplit(std::string_view str, std::string_view delim) + { + std::vector result = {}; + + if (delim.size() > 0) { // NOTE: str.find with an empty delimiter returns 0, not std::string::npos + std::string_view src_cpp = str; + for (size_t idx = src_cpp.find(delim, 0); src_cpp.size() > 0; idx = src_cpp.find(delim, 0)) { + result.push_back(src_cpp.substr(0, idx)); + if (idx == std::string::npos) { + break; + } + src_cpp = src_cpp.substr(idx + delim.size()); + } + } else { + result.push_back(str); + } + + + const guf_str_view delim_sv = guf_str_view{.len = (ptrdiff_t)delim.size(), .str = delim.data()}; + guf_str_view src = guf_str_view{.len = (ptrdiff_t)str.size(), .str = str.data()}; + size_t n = 0; + do { + const guf_str_view popped = guf_str_view_pop_split(&src, delim_sv); + TEST_CHECK(n < result.size()); + TEST_CHECK(std::string_view(popped.str, (size_t)popped.len) == result.at(n)); + const guf_str_view res = {.str = result.at(n).data(), .len = (ptrdiff_t)result.at(n).size()}; + TEST_CHECK(guf_str_view_equal(&popped, &res)); + TEST_CHECK(guf_str_view_equal_val_arg(popped, res)); + // std::cout << "guf: " << std::string_view{popped.str, (size_t)popped.len} << "\n"; + // std::cout << "cpp: " << std::string_view{res.str, (size_t)res.len} << "\n"; + ++n; + } while (src.len > 0); + TEST_CHECK(n == result.size()); + + return result; + } public: bool run() @@ -205,6 +242,78 @@ public: } } + std::vector split = test_popsplit("1997-04-01", "-"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "1997" && split.at(1) == "04" && split.at(2) == "01"); + } + split = test_popsplit("1997-04-01-", "-"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "1997" && split.at(1) == "04" && split.at(2) == "01"); + } + + split = test_popsplit("2025/05/08", "/"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "2025" && split.at(1) == "05" && split.at(2) == "08"); + } + split = test_popsplit("2025/05/08/", "/"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "2025" && split.at(1) == "05" && split.at(2) == "08"); + } + split = test_popsplit("2025/05/08//", "/"); + if (TEST_CHECK(split.size() == 4)) { + TEST_CHECK(split.at(0) == "2025" && split.at(1) == "05" && split.at(2) == "08" && split.at(3) == ""); + } + + split = test_popsplit("/2025/05/08", "/"); + if (TEST_CHECK(split.size() == 4)) { + TEST_CHECK(split.at(0) == "" && split.at(1) == "2025" && split.at(2) == "05" && split.at(3) == "08"); + } + split = test_popsplit("//2025/05/08", "/"); + if (TEST_CHECK(split.size() == 5)) { + TEST_CHECK(split.at(0) == "" && split.at(1) == "" && split.at(2) == "2025" && split.at(3) == "05" && split.at(4) == "08"); + } + + split = test_popsplit("I eat formidable crayons, oof, for real", "foo"); + if (TEST_CHECK(split.size() == 1)) { + TEST_CHECK(split.at(0) == "I eat formidable crayons, oof, for real"); + } + + split = test_popsplit("Hej <<", "<<"); + if (TEST_CHECK(split.size() == 1)) { + TEST_CHECK(split.at(0) == "Hej "); + } + split = test_popsplit("Hej << verden", "<<"); + if (TEST_CHECK(split.size() == 2)) { + TEST_CHECK(split.at(0) == "Hej " && split.at(1) == " verden"); + } + split = test_popsplit("<< Hej << verden", "<<"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "" && split.at(1) == " Hej " && split.at(2) == " verden"); + } + split = test_popsplit("<< Hej << verden <<< foo<>", "<<"); + if (TEST_CHECK(split.size() == 4)) { + TEST_CHECK(split.at(0) == "" && split.at(1) == " Hej " && split.at(2) == " verden " && split.at(3) == "< foo<>"); + } + + split = test_popsplit("I eat tofu", ""); + if (TEST_CHECK(split.size() == 1)) { + TEST_CHECK(split.at(0) == "I eat tofu"); + } + + split = test_popsplit("At 3 a.m. during FULL-moon FULL-STOP Next to the public-library's -STOP sign FULL-STOP", "FULL-STOP"); + if (TEST_CHECK(split.size() == 2)) { + TEST_CHECK(split.at(0) == "At 3 a.m. during FULL-moon " && split.at(1) == " Next to the public-library's -STOP sign "); + } + split = test_popsplit("At 3 a.m. during FULL-moon FULL-STOP Next to the public-library's -STOP sign FULL-STOPI like trains, FULL-STO", "FULL-STOP"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "At 3 a.m. during FULL-moon " && split.at(1) == " Next to the public-library's -STOP sign " && split.at(2) == "I like trains, FULL-STO"); + } + split = test_popsplit("At 3 a.m. during FULL-moon FULL-STOP Next to the public-library's -STOP sign FULL-STOPI like trains, FULL-STO Poo", "FULL-STOP"); + if (TEST_CHECK(split.size() == 3)) { + TEST_CHECK(split.at(0) == "At 3 a.m. during FULL-moon " && split.at(1) == " Next to the public-library's -STOP sign " && split.at(2) == "I like trains, FULL-STO Poo"); + } + + done = true; passed = (num_failed_checks == 0); return passed;