mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-06 06:32:56 +01:00
Add tests for rct2 <-> utf8 conversions
This commit is contained in:
committed by
Aaron van Geffen
parent
947a0625c8
commit
e7fb20c283
71
test/tests/Localisation.cpp
Normal file
71
test/tests/Localisation.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "helpers/StringHelpers.hpp"
|
||||
#include "openrct2/localisation/Localisation.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class Localisation : public testing::Test
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tests for rct2_to_utf8
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_F(Localisation, RCT2_to_UTF8_UK)
|
||||
{
|
||||
auto input = "The quick brown fox";
|
||||
auto expected = u8"The quick brown fox";
|
||||
auto actual = rct2_to_utf8(input, RCT2_LANGUAGE_ID_ENGLISH_UK);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST_F(Localisation, RCT2_to_UTF8_JP)
|
||||
{
|
||||
auto input = StringFromHex("83748340835883678375838983458393837483488362834e8358");
|
||||
auto expected = u8"ファストブラウンフォックス";
|
||||
auto actual = rct2_to_utf8(input, RCT2_LANGUAGE_ID_JAPANESE);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST_F(Localisation, RCT2_to_UTF8_ZH_TW)
|
||||
{
|
||||
auto input = StringFromHex("a7d6b374aabab4c4a6e2aab0af57");
|
||||
auto expected = u8"快速的棕色狐狸";
|
||||
auto actual = rct2_to_utf8(input, RCT2_LANGUAGE_ID_CHINESE_TRADITIONAL);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST_F(Localisation, RCT2_to_UTF8_PL)
|
||||
{
|
||||
auto input = StringFromHex("47F372736b6120446ff76b692054e6637a6f7779");
|
||||
auto expected = u8"Górska Dołki Tęczowy";
|
||||
auto actual = rct2_to_utf8(input, RCT2_LANGUAGE_ID_ENGLISH_UK);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tests for utf8_to_rct2
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_F(Localisation, UTF8_to_RCT2_Basic)
|
||||
{
|
||||
auto input = u8"à l'époque était";
|
||||
auto expected = StringFromHex("e0206c27e9706f71756520e974616974");
|
||||
auto actual = utf8_to_rct2(input);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST_F(Localisation, UTF8_to_RCT2_ChineseTraditional)
|
||||
{
|
||||
auto input = u8"$: 快速的棕色狐狸";
|
||||
auto expected = StringFromHex("243a20ff5febff901fff7684ff68d5ff8272ff72d0ff72f8");
|
||||
auto actual = utf8_to_rct2(input);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
TEST_F(Localisation, UTF8_to_RCT2_PL)
|
||||
{
|
||||
auto input = u8"Górska Dołki Tęczowy";
|
||||
auto expected = StringFromHex("47F372736b6120446ff76b692054e6637a6f7779");
|
||||
auto actual = utf8_to_rct2(input);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <openrct2/core/String.hpp>
|
||||
#include "AssertHelpers.hpp"
|
||||
#include "helpers/StringHelpers.hpp"
|
||||
|
||||
using TCase = std::tuple<std::string, std::string, std::string>;
|
||||
|
||||
@@ -11,16 +12,6 @@ class StringTest : public testing::TestWithParam<TCase>
|
||||
{
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Test Helpers
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static std::string StringFromBytes(std::initializer_list<uint8> list)
|
||||
{
|
||||
std::vector<uint8> data(list);
|
||||
return std::string(data.begin(), data.end());
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tests for String::Trim
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -92,7 +83,7 @@ TEST_F(StringTest, Split_ByEmpty)
|
||||
|
||||
TEST_F(StringTest, Convert_950_to_UTF8)
|
||||
{
|
||||
auto input = StringFromBytes({ 0xA7, 0xD6, 0xB3, 0x74, 0xAA, 0xBA, 0xB4, 0xC4, 0xA6, 0xE2, 0xAA, 0xB0, 0xAF, 0x57 });
|
||||
auto input = StringFromHex("a7d6b374aabab4c4a6e2aab0af57");
|
||||
auto expected = u8"快速的棕色狐狸";
|
||||
auto actual = String::Convert(input, CODE_PAGE::CP_950, CODE_PAGE::CP_UTF8);
|
||||
ASSERT_EQ(expected, actual);
|
||||
@@ -101,7 +92,7 @@ TEST_F(StringTest, Convert_950_to_UTF8)
|
||||
TEST_F(StringTest, Convert_UTF8_to_932)
|
||||
{
|
||||
auto input = u8"ファストブラウンフォックス";
|
||||
auto expected = StringFromBytes({ 0x83, 0x74, 0x83, 0x40, 0x83, 0x58, 0x83, 0x67, 0x83, 0x75, 0x83, 0x89, 0x83, 0x45, 0x83, 0x93, 0x83, 0x74, 0x83, 0x48, 0x83, 0x62, 0x83, 0x4E, 0x83, 0x58 });
|
||||
auto expected = StringFromHex("83748340835883678375838983458393837483488362834e8358");
|
||||
auto actual = String::Convert(input, CODE_PAGE::CP_UTF8, CODE_PAGE::CP_932);
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
17
test/tests/helpers/StringHelpers.hpp
Normal file
17
test/tests/helpers/StringHelpers.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
inline std::string StringFromHex(const std::string_view& input)
|
||||
{
|
||||
assert((input.size() & 1) == 0);
|
||||
|
||||
std::string result;
|
||||
result.reserve(input.size() / 2);
|
||||
for (size_t i = 0; i < input.size(); i += 2)
|
||||
{
|
||||
auto val = std::stoi(std::string(input.substr(i, 2)), 0, 16);
|
||||
result.push_back(val);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -52,12 +52,14 @@
|
||||
<!-- Files -->
|
||||
<ItemGroup>
|
||||
<ClInclude Include="AssertHelpers.hpp" />
|
||||
<ClInclude Include="helpers\StringHelpers.hpp" />
|
||||
<ClInclude Include="TestData.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="LanguagePackTest.cpp" />
|
||||
<ClCompile Include="IniReaderTest.cpp" />
|
||||
<ClCompile Include="IniWriterTest.cpp" />
|
||||
<ClCompile Include="Localisation.cpp" />
|
||||
<ClCompile Include="MultiLaunch.cpp" />
|
||||
<ClCompile Include="RideRatings.cpp" />
|
||||
<ClCompile Include="sawyercoding_test.cpp" />
|
||||
|
||||
Reference in New Issue
Block a user