feat(test): add unicode unit tests

This commit is contained in:
phaneron 2025-03-31 16:32:13 -04:00
parent d972d3673e
commit 65271681a2

View file

@ -59,3 +59,40 @@ TEST_CASE("SUniSPutUTF8", "[unicode]") {
REQUIRE(SStrLen(buffer) == 0); REQUIRE(SStrLen(buffer) == 0);
} }
} }
TEST_CASE("SUniConvertUTF8to16", "[unicode]") {
SECTION("convert ASCII to UTF-16") {
auto str = "Software";
uint16_t widechars[] = { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888, 0x9999 };
uint32_t srcchars;
uint32_t dstchars;
auto result = SUniConvertUTF8to16(widechars, 64, reinterpret_cast<const uint8_t*>(str), 128, &dstchars, &srcchars);
REQUIRE(result == 0);
REQUIRE(dstchars == 9);
REQUIRE(srcchars == 8);
REQUIRE(widechars[0] == 0x0053);
REQUIRE(widechars[1] == 0x006f);
REQUIRE(widechars[2] == 0x0066);
REQUIRE(widechars[3] == 0x0074);
REQUIRE(widechars[4] == 0x0077);
REQUIRE(widechars[5] == 0x0061);
REQUIRE(widechars[6] == 0x0072);
REQUIRE(widechars[7] == 0x0065);
REQUIRE(widechars[8] == 0x0000);
}
SECTION("convert UTF-8 汉字 to UTF-16") {
uint16_t widechars[] = { 0x1111, 0x2222, 0x3333 };
uint8_t chars[] = { 0xE6, 0xB1, 0x89, 0xE5, 0xAD, 0x97, 0x00 };
uint32_t srcchars;
uint32_t dstchars;
auto result = SUniConvertUTF8to16(widechars, 3, chars, 7, &dstchars, &srcchars);
REQUIRE(result == 0);
REQUIRE(dstchars == 3);
REQUIRE(srcchars == 6);
REQUIRE(widechars[0] == 0x6C49);
REQUIRE(widechars[1] == 0x5B57);
REQUIRE(widechars[2] == 0x0000);
}
}