UnitTests: Add some StringUtil character encoding conversion tests.

This commit is contained in:
Jordan Woyak 2025-10-15 18:44:51 -05:00
parent da7c813edf
commit 0ebc54a0fa

View File

@ -2,10 +2,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "Common/StringUtil.h"
#include "Common/Swap.h"
TEST(StringUtil, StringPopBackIf)
{
@ -256,3 +258,26 @@ TEST(StringUtil, CaseInsensitiveContains_OverlappingMatches)
EXPECT_TRUE(Common::CaseInsensitiveContains("aaaaaa", "aa"));
EXPECT_TRUE(Common::CaseInsensitiveContains("ababababa", "bABa"));
}
TEST(StringUtil, CharacterEncodingConversion)
{
// wstring
EXPECT_EQ(WStringToUTF8(L"hello 🐬"), "hello 🐬");
// UTF-16
EXPECT_EQ(UTF16ToUTF8(u"hello 🐬"), "hello 🐬");
EXPECT_EQ(UTF8ToUTF16("hello 🐬"), u"hello 🐬");
// UTF-16BE
char16_t utf16be_str[] = u"hello 🐬";
for (auto& c : utf16be_str)
c = Common::swap16(c);
EXPECT_EQ(UTF16BEToUTF8(utf16be_str, 99), "hello 🐬");
// Shift JIS
EXPECT_EQ(SHIFTJISToUTF8("\x83\x43\x83\x8b\x83\x4a"), "イルカ");
EXPECT_EQ(UTF8ToSHIFTJIS("イルカ"), "\x83\x43\x83\x8b\x83\x4a");
// CP1252
EXPECT_EQ(CP1252ToUTF8("hello \xa5"), "hello ¥");
}