From 956fe7c093609d63dcfc61615be96e81dab8d7e6 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Sat, 18 Jun 2022 11:02:50 +1000 Subject: [PATCH] Add tests for IsTrailUtf8CodeUnit --- test/utf8_test.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/utf8_test.cpp b/test/utf8_test.cpp index 5402f62aa..f7ce7e072 100644 --- a/test/utf8_test.cpp +++ b/test/utf8_test.cpp @@ -34,5 +34,33 @@ TEST(AppendUtf8Test, FourByteCodePoint) EXPECT_EQ(s, "あ"); } +TEST(Utf8CodeUnits, ValidCodePoints) +{ + // Working backwards on this loop to avoid triggering signed integer overflow on platforms where char has an + // underlying type of signed char + for (char x = '\x7F'; x >= '\x00' && x <= '\x7F'; x--) { + EXPECT_FALSE(IsTrailUtf8CodeUnit(x)) << "Basic Latin and ASCII Control characters are not trail code units"; + } + + for (char x = '\x80'; x >= '\x80' && x <= '\xBF'; x++) { + EXPECT_TRUE(IsTrailUtf8CodeUnit(x)) << "Bytes in the range 0x80 to 0xBF are potentially valid trail code units"; + } + + for (char x = '\xC2'; x >= '\xC2' && x <= '\xF4'; x++) { + EXPECT_FALSE(IsTrailUtf8CodeUnit(x)) << "Bytes in the range 0xC2 to 0xF4 are never valid trail code units"; + } +} + +TEST(Utf8CodeUnits, InvalidCodePoints) +{ + for (char x = '\xC0'; x >= '\xC0' && x <= '\xC1'; x++) { + EXPECT_FALSE(IsTrailUtf8CodeUnit(x)) << "Bytes in the range 0xC0 to oxC1 are not trail code units"; + } + + for (char x = '\xF5'; x >= '\xF5' && x <= '\xFF'; x++) { + EXPECT_FALSE(IsTrailUtf8CodeUnit(x)) << "Bytes in the range 0xF5 to 0xFF are not trail code units"; + } +} + } // namespace } // namespace devilution