From 6e884d865d077d86815f2e623c6c1875b995f9b1 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 1 Jul 2025 14:04:02 +0800 Subject: [PATCH] test: add testbench for patch (starts ends with, and contains) --- testbench/CMakeLists.txt | 2 + testbench/yycc/patch/contains.cpp | 58 ++++++++++++++++++++++ testbench/yycc/patch/starts_ends_with.cpp | 59 +++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 testbench/yycc/patch/contains.cpp create mode 100644 testbench/yycc/patch/starts_ends_with.cpp diff --git a/testbench/CMakeLists.txt b/testbench/CMakeLists.txt index 6692174..2021aa6 100644 --- a/testbench/CMakeLists.txt +++ b/testbench/CMakeLists.txt @@ -12,6 +12,8 @@ PRIVATE yycc/num/stringify.cpp yycc/rust/num/parse.cpp yycc/rust/num/stringify.cpp + yycc/patch/contains.cpp + yycc/patch/starts_ends_with.cpp ) # Setup headers target_include_directories(YYCCTestbench diff --git a/testbench/yycc/patch/contains.cpp b/testbench/yycc/patch/contains.cpp new file mode 100644 index 0000000..2bb3fe7 --- /dev/null +++ b/testbench/yycc/patch/contains.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include + +#include + +#define FN_CONTAINS ::yycc::patch::contains::contains + +namespace yycctest::patch::contains { + + TEST(PatchContains, Contains) { + // Set + { + std::set collection{ + UINT32_C(1), + UINT32_C(3), + UINT32_C(5), + }; + EXPECT_TRUE(FN_CONTAINS(collection, 1)); + EXPECT_FALSE(FN_CONTAINS(collection, 2)); + } + // Unordered set + { + std::unordered_set collection{ + UINT32_C(1), + UINT32_C(3), + UINT32_C(5), + }; + EXPECT_TRUE(FN_CONTAINS(collection, 1)); + EXPECT_FALSE(FN_CONTAINS(collection, 2)); + } + // Map + { + std::map collection{ + {UINT32_C(1), UINT32_C(2)}, + {UINT32_C(3), UINT32_C(4)}, + {UINT32_C(5), UINT32_C(6)}, + }; + EXPECT_TRUE(FN_CONTAINS(collection, 1)); + EXPECT_FALSE(FN_CONTAINS(collection, 2)); + } + // Unordered Map + { + std::unordered_map collection{ + {UINT32_C(1), UINT32_C(2)}, + {UINT32_C(3), UINT32_C(4)}, + {UINT32_C(5), UINT32_C(6)}, + }; + EXPECT_TRUE(FN_CONTAINS(collection, 1)); + EXPECT_FALSE(FN_CONTAINS(collection, 2)); + } + } + +} // namespace yycctest::patch::contains diff --git a/testbench/yycc/patch/starts_ends_with.cpp b/testbench/yycc/patch/starts_ends_with.cpp new file mode 100644 index 0000000..efe34ae --- /dev/null +++ b/testbench/yycc/patch/starts_ends_with.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +#define FN_STARTS_WITH ::yycc::patch::starts_ends_with::starts_with +#define FN_ENDS_WITH ::yycc::patch::starts_ends_with::ends_with +using namespace std::literals; + +namespace yycctest::patch::starts_ends_with { + +#define TEST_STARTS_WITH(prefix) \ + { \ + const auto that = prefix##"Hello, C++20!"s; \ + EXPECT_TRUE(FN_STARTS_WITH(that, prefix##"He"sv)); \ + EXPECT_FALSE(FN_STARTS_WITH(that, prefix##"he"sv)); \ + EXPECT_TRUE(FN_STARTS_WITH(that, prefix##'H')); \ + EXPECT_FALSE(FN_STARTS_WITH(that, prefix##'h')); \ + EXPECT_TRUE(FN_STARTS_WITH(that, prefix##"He")); \ + EXPECT_FALSE(FN_STARTS_WITH(that, prefix##"he")); \ + } + + TEST(PatchStartsEndsWith, StartsWith) { + // Ordinary string + TEST_STARTS_WITH(); + // Wide string + TEST_STARTS_WITH(L); + // UTF8 string + TEST_STARTS_WITH(u8); + // UTF-16 string + TEST_STARTS_WITH(u); + // UTF-32 string + TEST_STARTS_WITH(U); + } + +#define TEST_ENDS_WITH(prefix) \ + { \ + const auto that = prefix##"Hello, C++20!"s; \ + EXPECT_TRUE(FN_ENDS_WITH(that, prefix##"C++20!"sv)); \ + EXPECT_FALSE(FN_ENDS_WITH(that, prefix##"c++20!"sv)); \ + EXPECT_TRUE(FN_ENDS_WITH(that, prefix##'!')); \ + EXPECT_FALSE(FN_ENDS_WITH(that, prefix##'?')); \ + EXPECT_TRUE(FN_ENDS_WITH(that, prefix##"C++20!")); \ + EXPECT_FALSE(FN_ENDS_WITH(that, prefix##"c++20!")); \ + } + + TEST(PatchStartsEndsWith, EndsWith) { + // Ordinary string + TEST_ENDS_WITH(); + // Wide string + TEST_ENDS_WITH(L); + // UTF8 string + TEST_ENDS_WITH(u8); + // UTF-16 string + TEST_ENDS_WITH(u); + // UTF-32 string + TEST_ENDS_WITH(U); + } + +} // namespace yycctest::patch::starts_ends_with