From e7fb20c283330e1c935f9ccfe640e732cae36666 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 15 Apr 2018 16:43:28 +0100 Subject: [PATCH] Add tests for rct2 <-> utf8 conversions --- test/tests/Localisation.cpp | 71 ++++++++++++++++++++++++++++ test/tests/StringTest.cpp | 15 ++---- test/tests/helpers/StringHelpers.hpp | 17 +++++++ test/tests/tests.vcxproj | 2 + 4 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 test/tests/Localisation.cpp create mode 100644 test/tests/helpers/StringHelpers.hpp diff --git a/test/tests/Localisation.cpp b/test/tests/Localisation.cpp new file mode 100644 index 0000000000..0239e139c9 --- /dev/null +++ b/test/tests/Localisation.cpp @@ -0,0 +1,71 @@ +#include "helpers/StringHelpers.hpp" +#include "openrct2/localisation/Localisation.h" +#include + +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); +} diff --git a/test/tests/StringTest.cpp b/test/tests/StringTest.cpp index 045278470f..c7f9704e5e 100644 --- a/test/tests/StringTest.cpp +++ b/test/tests/StringTest.cpp @@ -4,6 +4,7 @@ #include #include #include "AssertHelpers.hpp" +#include "helpers/StringHelpers.hpp" using TCase = std::tuple; @@ -11,16 +12,6 @@ class StringTest : public testing::TestWithParam { }; -/////////////////////////////////////////////////////////////////////////////// -// Test Helpers -/////////////////////////////////////////////////////////////////////////////// - -static std::string StringFromBytes(std::initializer_list list) -{ - std::vector 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); } diff --git a/test/tests/helpers/StringHelpers.hpp b/test/tests/helpers/StringHelpers.hpp new file mode 100644 index 0000000000..b0d999ba8e --- /dev/null +++ b/test/tests/helpers/StringHelpers.hpp @@ -0,0 +1,17 @@ +#include +#include +#include + +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; +} diff --git a/test/tests/tests.vcxproj b/test/tests/tests.vcxproj index 9fd921b000..8c23f3c620 100644 --- a/test/tests/tests.vcxproj +++ b/test/tests/tests.vcxproj @@ -52,12 +52,14 @@ + +