diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-09-03 12:42:10 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-09-03 12:42:10 +0800 |
commit | efa1266f10e90c0c46f47cc06645422142cb2d9f (patch) | |
tree | 3d8cfefb81ce4645d150c08fc52ad646b6da80e2 /test/base/StringUtilTest.cpp | |
parent | 5e59a8e38c9f8992e6ffd9dbbde11e1f873780e1 (diff) | |
download | cru-efa1266f10e90c0c46f47cc06645422142cb2d9f.tar.gz cru-efa1266f10e90c0c46f47cc06645422142cb2d9f.tar.bz2 cru-efa1266f10e90c0c46f47cc06645422142cb2d9f.zip |
common -> base in test dir.
Diffstat (limited to 'test/base/StringUtilTest.cpp')
-rw-r--r-- | test/base/StringUtilTest.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/test/base/StringUtilTest.cpp b/test/base/StringUtilTest.cpp new file mode 100644 index 00000000..1da6e963 --- /dev/null +++ b/test/base/StringUtilTest.cpp @@ -0,0 +1,118 @@ +#include "cru/base/String.h" +#include "cru/base/StringUtil.h" + +#include <catch2/catch_test_macros.hpp> + +using cru::Index; +using cru::k_invalid_code_point; + +TEST_CASE("StringUtil Utf8NextCodePoint", "[string]") { + using cru::Utf8NextCodePoint; + std::string_view text = "aπ你🤣!"; + Index current = 0; + REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x0061); + REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x03C0); + REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x4F60); + REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x1F923); + REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x0021); + REQUIRE(Utf8NextCodePoint(text.data(), text.size(), current, ¤t) == + k_invalid_code_point); + REQUIRE(current == static_cast<Index>(text.size())); +} + +TEST_CASE("StringUtil Utf16NextCodePoint", "[string]") { + using cru::Utf16NextCodePoint; + std::u16string_view text = u"aπ你🤣!"; + Index current = 0; + REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x0061); + REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x03C0); + REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x4F60); + REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x1F923); + REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) == + 0x0021); + REQUIRE(Utf16NextCodePoint(text.data(), text.size(), current, ¤t) == + k_invalid_code_point); + REQUIRE(current == static_cast<Index>(text.size())); +} + +TEST_CASE("StringUtil Utf16PreviousCodePoint", "[string]") { + using cru::Utf16PreviousCodePoint; + std::u16string_view text = u"aπ你🤣!"; + Index current = text.size(); + REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) == + 0x0021); + REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) == + 0x1F923); + REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) == + 0x4F60); + REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) == + 0x03C0); + REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) == + 0x0061); + REQUIRE(Utf16PreviousCodePoint(text.data(), text.size(), current, ¤t) == + k_invalid_code_point); + REQUIRE(current == 0); +} + +TEST_CASE("StringUtil Utf8CodePointIterator", "[string]") { + using cru::Utf8CodePointIterator; + std::string_view text = "aπ你🤣!"; + std::vector<cru::CodePoint> code_points; + + for (auto cp : Utf8CodePointIterator(text.data(), text.size())) { + code_points.push_back(cp); + } + + std::vector<cru::CodePoint> expected_code_points{0x0061, 0x03C0, 0x4F60, + 0x1F923, 0x0021}; + + REQUIRE(code_points == expected_code_points); +} + +TEST_CASE("StringUtil Utf16CodePointIterator", "[string]") { + using cru::Utf16CodePointIterator; + std::u16string_view text = u"aπ你🤣!"; + std::vector<cru::CodePoint> code_points; + + for (auto cp : Utf16CodePointIterator(text.data(), text.size())) { + code_points.push_back(cp); + } + + std::vector<cru::CodePoint> expected_code_points{0x0061, 0x03C0, 0x4F60, + 0x1F923, 0x0021}; + + REQUIRE(code_points == expected_code_points); +} + +// TEST(WinString, IndexUtf8ToUtf16) { +// using cru::platform::win::IndexUtf8ToUtf16; +// std::string_view utf8_string = "aπ你🤣!"; +// std::wstring_view utf16_string = L"aπ你🤣!"; +// REQUIRE(IndexUtf8ToUtf16(utf8_string, 0, utf16_string), 0); +// REQUIRE(IndexUtf8ToUtf16(utf8_string, 1, utf16_string), 1); +// REQUIRE(IndexUtf8ToUtf16(utf8_string, 3, utf16_string), 2); +// REQUIRE(IndexUtf8ToUtf16(utf8_string, 6, utf16_string), 3); +// REQUIRE(IndexUtf8ToUtf16(utf8_string, 10, utf16_string), 5); +// REQUIRE(IndexUtf8ToUtf16(utf8_string, 11, utf16_string), 6); +// } + +// TEST(WinString, IndexUtf16ToUtf8) { +// using cru::platform::win::IndexUtf16ToUtf8; +// std::string_view utf8_string = "aπ你🤣!"; +// std::wstring_view utf16_string = L"aπ你🤣!"; +// REQUIRE(IndexUtf16ToUtf8(utf16_string, 0, utf8_string), 0); +// REQUIRE(IndexUtf16ToUtf8(utf16_string, 1, utf8_string), 1); +// REQUIRE(IndexUtf16ToUtf8(utf16_string, 2, utf8_string), 3); +// REQUIRE(IndexUtf16ToUtf8(utf16_string, 3, utf8_string), 6); +// REQUIRE(IndexUtf16ToUtf8(utf16_string, 5, utf8_string), 10); +// REQUIRE(IndexUtf16ToUtf8(utf16_string, 6, utf8_string), 11); +// } |