diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/common/StringUtilTest.cpp | 92 |
1 files changed, 66 insertions, 26 deletions
diff --git a/test/common/StringUtilTest.cpp b/test/common/StringUtilTest.cpp index 21a9ea9c..ba5e9321 100644 --- a/test/common/StringUtilTest.cpp +++ b/test/common/StringUtilTest.cpp @@ -4,34 +4,74 @@ using cru::k_invalid_code_point; -// TEST(WinString, Utf8Iterator) { -// using cru::platform::win::Utf8Iterator; -// std::string_view text = "aπ你🤣!"; -// Utf8Iterator i{text}; -// ASSERT_EQ(i.Next(), 0x0061); -// ASSERT_EQ(i.Next(), 0x03C0); -// ASSERT_EQ(i.Next(), 0x4F60); -// ASSERT_EQ(i.Next(), 0x1F923); -// ASSERT_EQ(i.Next(), 0x0021); -// ASSERT_EQ(i.Next(), k_invalid_code_point); -// } +TEST(StringUtil, Utf8NextCodePoint) { + using cru::Utf8NextCodePoint; + std::string_view text = "aπ你🤣!"; + gsl::index current = 0; + ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x0061); + ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x03C0); + ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x4F60); + ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x1F923); + ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), 0x0021); + ASSERT_EQ(Utf8NextCodePoint(text, current, ¤t), k_invalid_code_point); + ASSERT_EQ(current, static_cast<gsl::index>(text.size())); +} + +TEST(StringUtil, Utf16NextCodePoint) { + using cru::Utf16NextCodePoint; + std::u16string_view text = u"aπ你🤣!"; + gsl::index current = 0; + ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x0061); + ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x03C0); + ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x4F60); + ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x1F923); + ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), 0x0021); + ASSERT_EQ(Utf16NextCodePoint(text, current, ¤t), k_invalid_code_point); + ASSERT_EQ(current, static_cast<gsl::index>(text.size())); +} + +TEST(StringUtil, Utf16PreviousCodePoint) { + using cru::Utf16PreviousCodePoint; + std::u16string_view text = u"aπ你🤣!"; + gsl::index current = text.size(); + ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x0021); + ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x1F923); + ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x4F60); + ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x03C0); + ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), 0x0061); + ASSERT_EQ(Utf16PreviousCodePoint(text, current, ¤t), + k_invalid_code_point); + ASSERT_EQ(current, 0u); +} -TEST(WinString, Utf16Iterator) { - using cru::Utf16Iterator; +TEST(StringUtil, Utf8CodePointIterator) { + using cru::Utf8CodePointIterator; + std::string_view text = "aπ你🤣!"; + std::vector<cru::CodePoint> code_points; + + for (auto cp : Utf8CodePointIterator{text}) { + code_points.push_back(cp); + } + + std::vector<cru::CodePoint> expected_code_points{0x0061, 0x03C0, 0x4F60, + 0x1F923, 0x0021}; + + ASSERT_EQ(code_points, expected_code_points); +} + +TEST(StringUtil, Utf16CodePointIterator) { + using cru::Utf16CodePointIterator; std::u16string_view text = u"aπ你🤣!"; - Utf16Iterator i{text}; - ASSERT_EQ(i.Next(), 0x0061); - ASSERT_EQ(i.Next(), 0x03C0); - ASSERT_EQ(i.Next(), 0x4F60); - ASSERT_EQ(i.Next(), 0x1F923); - ASSERT_EQ(i.Next(), 0x0021); - ASSERT_EQ(i.Next(), k_invalid_code_point); - ASSERT_EQ(i.Previous(), 0x0021); - ASSERT_EQ(i.Previous(), 0x1F923); - ASSERT_EQ(i.Previous(), 0x4F60); - ASSERT_EQ(i.Previous(), 0x03C0); - ASSERT_EQ(i.Previous(), 0x0061); - ASSERT_EQ(i.Previous(), k_invalid_code_point); + std::vector<cru::CodePoint> code_points; + + for (auto cp : Utf16CodePointIterator{text}) { + code_points.push_back(cp); + } + + std::vector<cru::CodePoint> expected_code_points{0x0061, 0x03C0, 0x4F60, + 0x1F923, 0x0021}; + + ASSERT_EQ(code_points, expected_code_points); } // TEST(WinString, IndexUtf8ToUtf16) { |