From 5775a70a4073f7b6ea58536295abb6540065b5ca Mon Sep 17 00:00:00 2001 From: DahyeonWoo Date: Mon, 12 Sep 2022 03:41:11 +0900 Subject: [PATCH 1/4] Add CharsetAutoSelector (#222) --- euphony/src/main/cpp/CMakeLists.txt | 1 + .../cpp/core/charset/CharsetAutoSelector.cpp | 27 +++++++ .../cpp/core/charset/CharsetAutoSelector.h | 16 ++++ euphony/src/main/cpp/tests/CMakeLists.txt | 1 + .../tests/charset/charsetAutoSelectorTest.cpp | 79 +++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp create mode 100644 euphony/src/main/cpp/core/charset/CharsetAutoSelector.h create mode 100644 euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp diff --git a/euphony/src/main/cpp/CMakeLists.txt b/euphony/src/main/cpp/CMakeLists.txt index d552382..989045a 100644 --- a/euphony/src/main/cpp/CMakeLists.txt +++ b/euphony/src/main/cpp/CMakeLists.txt @@ -31,6 +31,7 @@ set(EUPHONY_SRC core/charset/UTF16Charset.cpp core/charset/UTF32Charset.cpp core/charset/DefaultCharset.cpp + core/charset/CharsetAutoSelector.cpp core/base/Base2.cpp core/base/Base16.cpp core/base/Base32.cpp diff --git a/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp new file mode 100644 index 0000000..dc361d8 --- /dev/null +++ b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp @@ -0,0 +1,27 @@ +#include "CharsetAutoSelector.h" +#include "ASCIICharset.h" +#include "DefaultCharset.h" +#include "UTF8Charset.h" +#include "UTF16Charset.h" +#include "UTF32Charset.h" +#include +#include +#include +#include + +using namespace Euphony; + +HexVector CharsetAutoSelector::select(std::string src) { + + HexVector asciiCharset = ASCIICharset().encode(src); + HexVector defaultCharset = DefaultCharset().encode(src); + HexVector utf8Charset = UTF8Charset().encode(src); + HexVector utf16Charset = UTF16Charset().encode(src); + HexVector utf32Charset = UTF32Charset().encode(src); + + HexVector results[] = {defaultCharset, asciiCharset, utf8Charset, utf16Charset, utf32Charset}; + + std::sort(results, results+5); + + return results[0]; +} \ No newline at end of file diff --git a/euphony/src/main/cpp/core/charset/CharsetAutoSelector.h b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.h new file mode 100644 index 0000000..ee0ddad --- /dev/null +++ b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.h @@ -0,0 +1,16 @@ +#ifndef EUPHONY_CHARSETAUTOSELECTOR_H +#define EUPHONY_CHARSETAUTOSELECTOR_H + +#include "Charset.h" + +namespace Euphony { + + class CharsetAutoSelector : public Charset { + public: + CharsetAutoSelector() = default; + ~CharsetAutoSelector() = default; + HexVector select(std::string src); + }; +} + +#endif //EUPHONY_CHARSETAUTOSELECTOR_H \ No newline at end of file diff --git a/euphony/src/main/cpp/tests/CMakeLists.txt b/euphony/src/main/cpp/tests/CMakeLists.txt index 147a337..cce6b6c 100644 --- a/euphony/src/main/cpp/tests/CMakeLists.txt +++ b/euphony/src/main/cpp/tests/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable( charset/utf8CharsetTest.cpp charset/utf16CharsetTest.cpp charset/utf32CharsetTest.cpp + charset/charsetAutoSelectorTest.cpp base/base2Test.cpp base/base16Test.cpp base/base32Test.cpp diff --git a/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp b/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp new file mode 100644 index 0000000..7b67391 --- /dev/null +++ b/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Euphony; + +typedef std::tuple TestParamType; + +class CharsetAutoSelectorTestFixture : public ::testing::TestWithParam { + +public: + void openCharset() { + EXPECT_EQ(asciiCharset, nullptr); + asciiCharset = new ASCIICharset(); + ASSERT_NE(asciiCharset, nullptr); + + EXPECT_EQ(defaultCharset, nullptr); + defaultCharset = new DefaultCharset(); + ASSERT_NE(defaultCharset, nullptr); + + EXPECT_EQ(utf8Charset, nullptr); + utf8Charset = new UTF8Charset(); + ASSERT_NE(utf8Charset, nullptr); + + EXPECT_EQ(utf16Charset, nullptr); + utf16Charset = new UTF16Charset(); + ASSERT_NE(utf16Charset, nullptr); + + EXPECT_EQ(utf32Charset, nullptr); + utf32Charset = new UTF16Charset(); + ASSERT_NE(utf32Charset, nullptr); + } + + Charset *asciiCharset = nullptr; + Charset *defaultCharset = nullptr; + Charset *utf8Charset = nullptr; + Charset *utf16Charset = nullptr; + Charset *utf32Charset = nullptr; +}; + +TEST_P(CharsetAutoSelectorTestFixture, SelectTest) { + openCharset(); + + std::string source; + std::string expectedResult; + + std::tie(source, expectedResult) = GetParam(); + + HexVector asciiResult = asciiCharset->encode(source); + HexVector defaultResult = defaultCharset->encode(source); + HexVector utf8Result = utf8Charset->encode(source); + HexVector utf16Result = utf16Charset->encode(source); + HexVector utf32Result = utf32Charset->encode(source); + + HexVector results[] = {asciiResult, defaultResult, utf8Result, utf16Result, utf32Result}; + + std::sort(results, results+5); + + HexVector actualResult = results[0]; + + EXPECT_EQ(actualResult.toString(), expectedResult); +} + +INSTANTIATE_TEST_CASE_P( + CharsetAutoSelectorTestSuite, + CharsetAutoSelectorTestFixture, + ::testing::Values( + TestParamType("a", "61"), + TestParamType("b", "62"), + TestParamType("가", "eab080"), + TestParamType("나", "eb8298") +)); \ No newline at end of file From fb3171832b2af859bb017959024338578c763cf8 Mon Sep 17 00:00:00 2001 From: DahyeonWoo Date: Mon, 12 Sep 2022 11:14:48 +0900 Subject: [PATCH 2/4] Fix sort logic (#222) --- euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp | 6 ++++-- .../main/cpp/tests/charset/charsetAutoSelectorTest.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp index dc361d8..11cff22 100644 --- a/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp +++ b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp @@ -19,9 +19,11 @@ HexVector CharsetAutoSelector::select(std::string src) { HexVector utf16Charset = UTF16Charset().encode(src); HexVector utf32Charset = UTF32Charset().encode(src); - HexVector results[] = {defaultCharset, asciiCharset, utf8Charset, utf16Charset, utf32Charset}; + HexVector results[] = {defaultCharset, asciiCharset, utf8Charset, utf16Charset, utf32Charset}; - std::sort(results, results+5); + std::sort(results, results+5, [](HexVector& left, HexVector& right) { + return left.getSize() < right.getSize(); + }); return results[0]; } \ No newline at end of file diff --git a/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp b/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp index 7b67391..3014b02 100644 --- a/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp +++ b/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp @@ -59,10 +59,12 @@ TEST_P(CharsetAutoSelectorTestFixture, SelectTest) { HexVector utf16Result = utf16Charset->encode(source); HexVector utf32Result = utf32Charset->encode(source); - HexVector results[] = {asciiResult, defaultResult, utf8Result, utf16Result, utf32Result}; - - std::sort(results, results+5); + HexVector results[] = {asciiResult, defaultResult, utf8Result, utf16Result, utf32Result}; + std::sort(results, results+5, [](HexVector& left, HexVector& right) { + return left.getSize() < right.getSize(); + }); + HexVector actualResult = results[0]; EXPECT_EQ(actualResult.toString(), expectedResult); From 7e93232a174d49eec2569c6e1a558c73f626c47e Mon Sep 17 00:00:00 2001 From: DahyeonWoo Date: Mon, 12 Sep 2022 14:04:18 +0900 Subject: [PATCH 3/4] Fix variable name (#222) --- .../main/cpp/core/charset/CharsetAutoSelector.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp index 11cff22..c79687f 100644 --- a/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp +++ b/euphony/src/main/cpp/core/charset/CharsetAutoSelector.cpp @@ -13,13 +13,13 @@ using namespace Euphony; HexVector CharsetAutoSelector::select(std::string src) { - HexVector asciiCharset = ASCIICharset().encode(src); - HexVector defaultCharset = DefaultCharset().encode(src); - HexVector utf8Charset = UTF8Charset().encode(src); - HexVector utf16Charset = UTF16Charset().encode(src); - HexVector utf32Charset = UTF32Charset().encode(src); + HexVector asciiResult = ASCIICharset().encode(src); + HexVector defaultResult = DefaultCharset().encode(src); + HexVector utf8Result = UTF8Charset().encode(src); + HexVector utf16Result = UTF16Charset().encode(src); + HexVector utf32Result = UTF32Charset().encode(src); - HexVector results[] = {defaultCharset, asciiCharset, utf8Charset, utf16Charset, utf32Charset}; + HexVector results[] = {asciiResult, defaultResult, utf8Result, utf16Result, utf32Result}; std::sort(results, results+5, [](HexVector& left, HexVector& right) { return left.getSize() < right.getSize(); From 1a907b913226a0a2299b860a44c4ea3bdc53a6f0 Mon Sep 17 00:00:00 2001 From: DahyeonWoo Date: Sat, 17 Sep 2022 16:23:36 +0900 Subject: [PATCH 4/4] Fix object (#222) --- euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp b/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp index 3014b02..c821399 100644 --- a/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp +++ b/euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp @@ -34,7 +34,7 @@ class CharsetAutoSelectorTestFixture : public ::testing::TestWithParam