From 65271681a2d3676c16557ddc3dbf9ec3284f5060 Mon Sep 17 00:00:00 2001 From: superp00t Date: Mon, 31 Mar 2025 16:32:13 -0400 Subject: [PATCH] feat(test): add unicode unit tests --- test/Unicode.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/Unicode.cpp b/test/Unicode.cpp index 3e61ec3..73d2425 100644 --- a/test/Unicode.cpp +++ b/test/Unicode.cpp @@ -59,3 +59,40 @@ TEST_CASE("SUniSPutUTF8", "[unicode]") { 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(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); + } +}