test: add testbench for stl, windows and iconv encoding
This commit is contained in:
@ -3,6 +3,8 @@ add_executable(YYCCTestbench "")
|
||||
# Setup testbench sources
|
||||
target_sources(YYCCTestbench
|
||||
PRIVATE
|
||||
shared/literals.cpp
|
||||
|
||||
main.cpp
|
||||
yycc/macro/version_cmp.cpp
|
||||
yycc/flag_enum.cpp
|
||||
@ -16,12 +18,15 @@ PRIVATE
|
||||
yycc/num/op.cpp
|
||||
yycc/num/safe_cast.cpp
|
||||
yycc/num/safe_op.cpp
|
||||
yycc/encoding/stl.cpp
|
||||
yycc/encoding/windows.cpp
|
||||
yycc/encoding/iconv.cpp
|
||||
)
|
||||
target_sources(YYCCTestbench
|
||||
PRIVATE
|
||||
FILE_SET HEADERS
|
||||
FILES
|
||||
yycc/encoding/utf_literal.hpp
|
||||
shared/literals.hpp
|
||||
)
|
||||
# Setup headers
|
||||
target_include_directories(YYCCTestbench
|
||||
|
@ -185,126 +185,6 @@ namespace YYCCTestbench {
|
||||
|
||||
}
|
||||
|
||||
static void StringTestbench() {
|
||||
// Test Printf
|
||||
auto test_printf = YYCC::StringHelper::Printf(YYCC_U8("%s == %s"), YYCC_U8("Hello World"), YYCC_U8("Hello, world"));
|
||||
Assert(test_printf == YYCC_U8("Hello World == Hello, world"), YYCC_U8("YYCC::StringHelper::Printf"));
|
||||
|
||||
// Test Replace
|
||||
auto test_replace = YYCC::StringHelper::Replace(YYCC_U8("aabbcc"), YYCC_U8("bb"), YYCC_U8("dd")); // normal case
|
||||
Assert(test_replace == YYCC_U8("aaddcc"), YYCC_U8("YYCC::StringHelper::Replace"));
|
||||
test_replace = YYCC::StringHelper::Replace(YYCC_U8("aabbcc"), YYCC_U8("zz"), YYCC_U8("yy")); // no replace
|
||||
Assert(test_replace == YYCC_U8("aabbcc"), YYCC_U8("YYCC::StringHelper::Replace"));
|
||||
test_replace = YYCC::StringHelper::Replace(YYCC_U8("aabbcc"), YYCC::yycc_u8string_view(), YYCC_U8("zz")); // empty finding
|
||||
Assert(test_replace == YYCC_U8("aabbcc"), YYCC_U8("YYCC::StringHelper::Replace"));
|
||||
test_replace = YYCC::StringHelper::Replace(YYCC_U8("aaaabbaa"), YYCC_U8("aa"), YYCC_U8("")); // no replaced string
|
||||
Assert(test_replace == YYCC_U8("bb"), YYCC_U8("YYCC::StringHelper::Replace"));
|
||||
test_replace = YYCC::StringHelper::Replace(YYCC_U8("aaxcc"), YYCC_U8("x"), YYCC_U8("yx")); // nested replacing
|
||||
Assert(test_replace == YYCC_U8("aayxcc"), YYCC_U8("YYCC::StringHelper::Replace"));
|
||||
test_replace = YYCC::StringHelper::Replace(YYCC::yycc_u8string_view(), YYCC_U8(""), YYCC_U8("xy")); // empty source string
|
||||
Assert(test_replace == YYCC_U8(""), YYCC_U8("YYCC::StringHelper::Replace"));
|
||||
|
||||
// Test Upper / Lower
|
||||
auto test_lower = YYCC::StringHelper::Lower(YYCC_U8("LOWER"));
|
||||
Assert(test_lower == YYCC_U8("lower"), YYCC_U8("YYCC::StringHelper::Lower"));
|
||||
auto test_upper = YYCC::StringHelper::Upper(YYCC_U8("upper"));
|
||||
Assert(test_upper == YYCC_U8("UPPER"), YYCC_U8("YYCC::StringHelper::Upper"));
|
||||
|
||||
// Test Join
|
||||
std::vector<YYCC::yycc_u8string> test_join_container {
|
||||
YYCC_U8(""), YYCC_U8("1"), YYCC_U8("2"), YYCC_U8("")
|
||||
};
|
||||
auto test_join = YYCC::StringHelper::Join(test_join_container.begin(), test_join_container.end(), YYCC_U8(", "));
|
||||
Assert(test_join == YYCC_U8(", 1, 2, "), YYCC_U8("YYCC::StringHelper::Join"));
|
||||
|
||||
// Test Split
|
||||
auto test_split = YYCC::StringHelper::Split(YYCC_U8(", 1, 2, "), YYCC_U8(", ")); // normal
|
||||
Assert(test_split.size() == 4u, YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[0] == YYCC_U8(""), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[1] == YYCC_U8("1"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[2] == YYCC_U8("2"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[3] == YYCC_U8(""), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
test_split = YYCC::StringHelper::Split(YYCC_U8("test"), YYCC_U8("-")); // no matched delimiter
|
||||
Assert(test_split.size() == 1u, YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[0] == YYCC_U8("test"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
test_split = YYCC::StringHelper::Split(YYCC_U8("test"), YYCC::yycc_u8string_view()); // empty delimiter
|
||||
Assert(test_split.size() == 1u, YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[0] == YYCC_U8("test"), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
test_split = YYCC::StringHelper::Split(YYCC::yycc_u8string_view(), YYCC_U8("")); // empty source string
|
||||
Assert(test_split.size() == 1u, YYCC_U8("YYCC::StringHelper::Split"));
|
||||
Assert(test_split[0].empty(), YYCC_U8("YYCC::StringHelper::Split"));
|
||||
|
||||
}
|
||||
|
||||
static void ParserTestbench() {
|
||||
|
||||
// Test success TryParse
|
||||
#define TEST_MACRO(type_t, value, string_value, ...) { \
|
||||
YYCC::yycc_u8string cache_string(YYCC_U8(string_value)); \
|
||||
type_t cache; \
|
||||
Assert(YYCC::ParserHelper::TryParse<type_t>(cache_string, cache, ##__VA_ARGS__) && cache == value, YYCC_U8("YYCC::StringHelper::TryParse<" #type_t ">")); \
|
||||
}
|
||||
|
||||
TEST_MACRO(int8_t, INT8_C(-61), "-61");
|
||||
TEST_MACRO(uint8_t, UINT8_C(200), "200");
|
||||
TEST_MACRO(int16_t, INT16_C(6161), "6161");
|
||||
TEST_MACRO(uint16_t, UINT16_C(32800), "32800");
|
||||
TEST_MACRO(int32_t, INT32_C(61616161), "61616161");
|
||||
TEST_MACRO(uint32_t, UINT32_C(4294967293), "4294967293");
|
||||
TEST_MACRO(int64_t, INT64_C(616161616161), "616161616161");
|
||||
TEST_MACRO(uint64_t, UINT64_C(9223372036854775807), "9223372036854775807");
|
||||
TEST_MACRO(uint32_t, UINT32_C(0xffff), "ffff", 16);
|
||||
TEST_MACRO(float, 1.0f, "1.0");
|
||||
TEST_MACRO(double, 1.0, "1.0");
|
||||
TEST_MACRO(bool, true, "true");
|
||||
|
||||
#undef TEST_MACRO
|
||||
|
||||
// Test failed TryParse
|
||||
#define TEST_MACRO(type_t, string_value, ...) { \
|
||||
YYCC::yycc_u8string cache_string(YYCC_U8(string_value)); \
|
||||
type_t cache; \
|
||||
Assert(!YYCC::ParserHelper::TryParse<type_t>(cache_string, cache, ##__VA_ARGS__), YYCC_U8("YYCC::StringHelper::TryParse<" #type_t ">")); \
|
||||
}
|
||||
|
||||
TEST_MACRO(int8_t, "6161");
|
||||
TEST_MACRO(uint8_t, "32800");
|
||||
TEST_MACRO(int16_t, "61616161");
|
||||
TEST_MACRO(uint16_t, "4294967293");
|
||||
TEST_MACRO(int32_t, "616161616161");
|
||||
TEST_MACRO(uint32_t, "9223372036854775807");
|
||||
TEST_MACRO(int64_t, "616161616161616161616161");
|
||||
TEST_MACRO(uint64_t, "92233720368547758079223372036854775807");
|
||||
TEST_MACRO(float, "1e40");
|
||||
TEST_MACRO(double, "1e114514");
|
||||
TEST_MACRO(bool, "hello, world!");
|
||||
|
||||
#undef TEST_MACRO
|
||||
|
||||
// Test ToString
|
||||
#define TEST_MACRO(type_t, value, string_value, ...) { \
|
||||
type_t cache = value; \
|
||||
YYCC::yycc_u8string ret(YYCC::ParserHelper::ToString<type_t>(cache, ##__VA_ARGS__)); \
|
||||
Assert(ret == YYCC_U8(string_value), YYCC_U8("YYCC::StringHelper::ToString<" #type_t ">")); \
|
||||
}
|
||||
|
||||
TEST_MACRO(int8_t, INT8_C(-61), "-61");
|
||||
TEST_MACRO(uint8_t, UINT8_C(200), "200");
|
||||
TEST_MACRO(int16_t, INT16_C(6161), "6161");
|
||||
TEST_MACRO(uint16_t, UINT16_C(32800), "32800");
|
||||
TEST_MACRO(int32_t, INT32_C(61616161), "61616161");
|
||||
TEST_MACRO(uint32_t, UINT32_C(4294967293), "4294967293");
|
||||
TEST_MACRO(int64_t, INT64_C(616161616161), "616161616161");
|
||||
TEST_MACRO(uint64_t, UINT64_C(9223372036854775807), "9223372036854775807");
|
||||
TEST_MACRO(uint32_t, UINT32_C(0xffff), "ffff", 16);
|
||||
TEST_MACRO(float, 1.0f, "1.0", std::chars_format::fixed, 1);
|
||||
TEST_MACRO(double, 1.0, "1.0", std::chars_format::fixed, 1);
|
||||
TEST_MACRO(bool, true, "true");
|
||||
|
||||
#undef TEST_MACRO
|
||||
|
||||
}
|
||||
|
||||
static void DialogTestbench() {
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
@ -439,62 +319,6 @@ namespace YYCCTestbench {
|
||||
|
||||
}
|
||||
|
||||
enum class TestFlagEnum : uint8_t {
|
||||
Test1 = 0b00000000,
|
||||
Test2 = 0b00000001,
|
||||
Test3 = 0b00000010,
|
||||
Test4 = 0b00000100,
|
||||
Test5 = 0b00001000,
|
||||
Test6 = 0b00010000,
|
||||
Test7 = 0b00100000,
|
||||
Test8 = 0b01000000,
|
||||
Test9 = 0b10000000,
|
||||
Inverted = 0b01111111,
|
||||
Merged = Test3 + Test5,
|
||||
};
|
||||
|
||||
static void EnumHelperTestbench() {
|
||||
TestFlagEnum val;
|
||||
|
||||
Assert(YYCC::EnumHelper::Merge(TestFlagEnum::Test3, TestFlagEnum::Test5) == TestFlagEnum::Merged, YYCC_U8("YYCC::EnumHelper::Merge"));
|
||||
|
||||
Assert(YYCC::EnumHelper::Invert(TestFlagEnum::Test9) == TestFlagEnum::Inverted, YYCC_U8("YYCC::EnumHelper::Invert"));
|
||||
|
||||
val = YYCC::EnumHelper::Merge(TestFlagEnum::Test3, TestFlagEnum::Test5);
|
||||
YYCC::EnumHelper::Mask(val, TestFlagEnum::Test3);
|
||||
Assert(YYCC::EnumHelper::Bool(val), YYCC_U8("YYCC::EnumHelper::Mask"));
|
||||
val = YYCC::EnumHelper::Merge(TestFlagEnum::Test3, TestFlagEnum::Test5);
|
||||
YYCC::EnumHelper::Mask(val, TestFlagEnum::Test4);
|
||||
Assert(!YYCC::EnumHelper::Bool(val), YYCC_U8("YYCC::EnumHelper::Mask"));
|
||||
|
||||
val = TestFlagEnum::Test3;
|
||||
YYCC::EnumHelper::Add(val, TestFlagEnum::Test5);
|
||||
Assert(val == TestFlagEnum::Merged, YYCC_U8("YYCC::EnumHelper::Add"));
|
||||
|
||||
val = TestFlagEnum::Merged;
|
||||
YYCC::EnumHelper::Remove(val, TestFlagEnum::Test5);
|
||||
Assert(val == TestFlagEnum::Test3, YYCC_U8("YYCC::EnumHelper::Remove"));
|
||||
|
||||
val = YYCC::EnumHelper::Merge(TestFlagEnum::Test3, TestFlagEnum::Test5);
|
||||
Assert(YYCC::EnumHelper::Has(val, TestFlagEnum::Test3), YYCC_U8("YYCC::EnumHelper::Has"));
|
||||
Assert(!YYCC::EnumHelper::Has(val, TestFlagEnum::Test4), YYCC_U8("YYCC::EnumHelper::Has"));
|
||||
|
||||
Assert(!YYCC::EnumHelper::Bool(TestFlagEnum::Test1), YYCC_U8("YYCC::EnumHelper::Bool"));
|
||||
Assert(YYCC::EnumHelper::Bool(TestFlagEnum::Test2), YYCC_U8("YYCC::EnumHelper::Bool"));
|
||||
|
||||
}
|
||||
|
||||
static void VersionMacroTestbench() {
|
||||
Assert(YYCC_VERCMP_E(1, 2, 3, 1, 2, 3), YYCC_U8("YYCC_VERCMP_E"));
|
||||
Assert(!YYCC_VERCMP_NE(1, 2, 3, 1, 2, 3), YYCC_U8("YYCC_VERCMP_NE"));
|
||||
Assert(YYCC_VERCMP_G(1, 2, 3, 0, 2, 5), YYCC_U8("YYCC_VERCMP_G"));
|
||||
Assert(YYCC_VERCMP_GE(1, 2, 3, 1, 2, 3), YYCC_U8("YYCC_VERCMP_GE"));
|
||||
Assert(YYCC_VERCMP_NL(1, 2, 3, 1, 2, 3), YYCC_U8("YYCC_VERCMP_NL"));
|
||||
Assert(YYCC_VERCMP_L(0, 2, 5, 1, 2, 3), YYCC_U8("YYCC_VERCMP_L"));
|
||||
Assert(YYCC_VERCMP_LE(1, 2, 3, 1, 2, 3), YYCC_U8("YYCC_VERCMP_LE"));
|
||||
Assert(YYCC_VERCMP_NG(1, 2, 3, 1, 2, 3), YYCC_U8("YYCC_VERCMP_NG"));
|
||||
}
|
||||
|
||||
enum class TestEnum : int8_t {
|
||||
Test1, Test2, Test3
|
||||
};
|
||||
|
203
testbench/shared/literals.cpp
Normal file
203
testbench/shared/literals.cpp
Normal file
@ -0,0 +1,203 @@
|
||||
#include "literals.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace yyccshared::literals {
|
||||
|
||||
#define CONCAT(prefix, strl) prefix##strl
|
||||
#define U8_LITERAL(strl) CONCAT(u8, strl)
|
||||
#define U16_LITERAL(strl) CONCAT(u, strl)
|
||||
#define U32_LITERAL(strl) CONCAT(U, strl)
|
||||
#define WSTR_LITERAL(strl) CONCAT(L, strl)
|
||||
|
||||
#pragma region UtfLiterals Data
|
||||
|
||||
// UNICODE Test Strings
|
||||
// Ref: https://stackoverflow.com/questions/478201/how-to-test-an-application-for-correct-encoding-e-g-utf-8
|
||||
|
||||
#define UNICODE_STR_JAPAN "\u30E6\u30FC\u30B6\u30FC\u5225\u30B5\u30A4\u30C8"
|
||||
#define UNICODE_STR_CHINA "\u7B80\u4F53\u4E2D\u6587"
|
||||
#define UNICODE_STR_KOREA "\uD06C\uB85C\uC2A4 \uD50C\uB7AB\uD3FC\uC73C\uB85C"
|
||||
#define UNICODE_STR_ISRAEL "\u05DE\u05D3\u05D5\u05E8\u05D9\u05DD \u05DE\u05D1\u05D5\u05E7\u05E9\u05D9\u05DD"
|
||||
#define UNICODE_STR_EGYPT "\u0623\u0641\u0636\u0644 \u0627\u0644\u0628\u062D\u0648\u062B"
|
||||
#define UNICODE_STR_GREECE "\u03A3\u1F72 \u03B3\u03BD\u03C9\u03C1\u03AF\u03B6\u03C9 \u1F00\u03C0\u1F78"
|
||||
#define UNICODE_STR_RUSSIA \
|
||||
"\u0414\u0435\u0441\u044F\u0442\u0443\u044E \u041C\u0435\u0436\u0434\u0443\u043D\u0430\u0440\u043E\u0434\u043D\u0443\u044E"
|
||||
#define UNICODE_STR_THAILAND \
|
||||
"\u0E41\u0E1C\u0E48\u0E19\u0E14\u0E34\u0E19\u0E2E\u0E31\u0E48\u0E19\u0E40\u0E2A\u0E37\u0E48\u0E2D\u0E21\u0E42\u0E17\u0E23\u0E21\u0E41\u0E2A\u0E19\u0E2A\u0E31\u0E07\u0E40\u0E27\u0E0A"
|
||||
#define UNICODE_STR_FRANCE "fran\u00E7ais langue \u00E9trang\u00E8re"
|
||||
#define UNICODE_STR_SPAIN "ma\u00F1ana ol\u00E9"
|
||||
#define UNICODE_STR_MATHMATICS "\u222E E\u22C5da = Q, n \u2192 \u221E, \u2211 f(i) = \u220F g(i)"
|
||||
#define UNICODE_STR_EMOJI "\U0001F363 \u2716 \U0001F37A" // sushi x beer mug
|
||||
|
||||
static std::vector<std::u8string> UTFLIT_U8STR_VEC{
|
||||
U8_LITERAL(UNICODE_STR_JAPAN),
|
||||
U8_LITERAL(UNICODE_STR_CHINA),
|
||||
U8_LITERAL(UNICODE_STR_KOREA),
|
||||
U8_LITERAL(UNICODE_STR_ISRAEL),
|
||||
U8_LITERAL(UNICODE_STR_EGYPT),
|
||||
U8_LITERAL(UNICODE_STR_GREECE),
|
||||
U8_LITERAL(UNICODE_STR_RUSSIA),
|
||||
U8_LITERAL(UNICODE_STR_THAILAND),
|
||||
U8_LITERAL(UNICODE_STR_FRANCE),
|
||||
U8_LITERAL(UNICODE_STR_SPAIN),
|
||||
U8_LITERAL(UNICODE_STR_MATHMATICS),
|
||||
U8_LITERAL(UNICODE_STR_EMOJI),
|
||||
};
|
||||
static std::vector<std::wstring> UTFLIT_WSTR_VEC{
|
||||
WSTR_LITERAL(UNICODE_STR_JAPAN),
|
||||
WSTR_LITERAL(UNICODE_STR_CHINA),
|
||||
WSTR_LITERAL(UNICODE_STR_KOREA),
|
||||
WSTR_LITERAL(UNICODE_STR_ISRAEL),
|
||||
WSTR_LITERAL(UNICODE_STR_EGYPT),
|
||||
WSTR_LITERAL(UNICODE_STR_GREECE),
|
||||
WSTR_LITERAL(UNICODE_STR_RUSSIA),
|
||||
WSTR_LITERAL(UNICODE_STR_THAILAND),
|
||||
WSTR_LITERAL(UNICODE_STR_FRANCE),
|
||||
WSTR_LITERAL(UNICODE_STR_SPAIN),
|
||||
WSTR_LITERAL(UNICODE_STR_MATHMATICS),
|
||||
WSTR_LITERAL(UNICODE_STR_EMOJI),
|
||||
};
|
||||
static std::vector<std::u16string> UTFLIT_U16STR_VEC{
|
||||
U16_LITERAL(UNICODE_STR_JAPAN),
|
||||
U16_LITERAL(UNICODE_STR_CHINA),
|
||||
U16_LITERAL(UNICODE_STR_KOREA),
|
||||
U16_LITERAL(UNICODE_STR_ISRAEL),
|
||||
U16_LITERAL(UNICODE_STR_EGYPT),
|
||||
U16_LITERAL(UNICODE_STR_GREECE),
|
||||
U16_LITERAL(UNICODE_STR_RUSSIA),
|
||||
U16_LITERAL(UNICODE_STR_THAILAND),
|
||||
U16_LITERAL(UNICODE_STR_FRANCE),
|
||||
U16_LITERAL(UNICODE_STR_SPAIN),
|
||||
U16_LITERAL(UNICODE_STR_MATHMATICS),
|
||||
U16_LITERAL(UNICODE_STR_EMOJI),
|
||||
};
|
||||
static std::vector<std::u32string> UTFLIT_U32STR_VEC{
|
||||
U32_LITERAL(UNICODE_STR_JAPAN),
|
||||
U32_LITERAL(UNICODE_STR_CHINA),
|
||||
U32_LITERAL(UNICODE_STR_KOREA),
|
||||
U32_LITERAL(UNICODE_STR_ISRAEL),
|
||||
U32_LITERAL(UNICODE_STR_EGYPT),
|
||||
U32_LITERAL(UNICODE_STR_GREECE),
|
||||
U32_LITERAL(UNICODE_STR_RUSSIA),
|
||||
U32_LITERAL(UNICODE_STR_THAILAND),
|
||||
U32_LITERAL(UNICODE_STR_FRANCE),
|
||||
U32_LITERAL(UNICODE_STR_SPAIN),
|
||||
U32_LITERAL(UNICODE_STR_MATHMATICS),
|
||||
U32_LITERAL(UNICODE_STR_EMOJI),
|
||||
};
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region UtfLiterals
|
||||
|
||||
UtfLiterals::UtfLiterals() :
|
||||
u8str_vec(UTFLIT_U8STR_VEC), u16str_vec(UTFLIT_U16STR_VEC), u32str_vec(UTFLIT_U32STR_VEC), wstr_vec(UTFLIT_WSTR_VEC) {
|
||||
// Check whether each vector has same size.
|
||||
bool okey = true;
|
||||
size_t exp_size = this->u8str_vec.size();
|
||||
|
||||
if (this->u16str_vec.size() != exp_size) okey = false;
|
||||
if (this->u32str_vec.size() != exp_size) okey = false;
|
||||
if (this->wstr_vec.size() != exp_size) okey = false;
|
||||
|
||||
if (!okey) throw std::logic_error("utf literal vector have different size");
|
||||
}
|
||||
|
||||
UtfLiterals::~UtfLiterals() {}
|
||||
|
||||
size_t UtfLiterals::get_size() const {
|
||||
// We have checked the size of each vector in ctor.
|
||||
// So we simply return the length of one of them.
|
||||
return this->u8str_vec.size();
|
||||
}
|
||||
|
||||
const std::vector<std::u8string> &UtfLiterals::get_u8str_vec() const {
|
||||
return this->u8str_vec;
|
||||
}
|
||||
|
||||
const std::vector<std::u16string> &UtfLiterals::get_u16str_vec() const {
|
||||
return this->u16str_vec;
|
||||
}
|
||||
|
||||
const std::vector<std::u32string> &UtfLiterals::get_u32str_vec() const {
|
||||
return this->u32str_vec;
|
||||
}
|
||||
|
||||
const std::vector<std::wstring> &UtfLiterals::get_wstr_vec() const {
|
||||
return this->wstr_vec;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region OtherLiteral
|
||||
|
||||
OtherLiteral::OtherLiteral(std::string &&other_str, uint32_t windows_ident, std::string &&iconv_ident, std::u8string &&pycodec_ident) :
|
||||
other_str(std::move(other_str)), windows_ident(windows_ident), iconv_ident(std::move(iconv_ident)),
|
||||
pycodec_ident(std::move(pycodec_ident)) {}
|
||||
|
||||
OtherLiteral::~OtherLiteral() {}
|
||||
|
||||
const std::string &OtherLiteral::get_other_str() const {
|
||||
return this->other_str;
|
||||
}
|
||||
|
||||
uint32_t OtherLiteral::get_windows_ident() const {
|
||||
return this->windows_ident;
|
||||
}
|
||||
|
||||
const std::string &OtherLiteral::get_iconv_ident() const {
|
||||
return this->iconv_ident;
|
||||
}
|
||||
|
||||
const std::u8string &OtherLiteral::get_pycodec_ident() const {
|
||||
return this->pycodec_ident;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region OtherLiterals Data
|
||||
|
||||
std::vector<OtherLiteral> OTHERLIT_OTHERSTR_VEC{{"\xC4\xE3\xBA\xC3\xD6\xD0\xB9\xFA", UINT32_C(936), "GBK", u8"gbk"}};
|
||||
|
||||
#define OTHER_STR_GBK "\u4f60\u597d\u4e2d\u56fd"
|
||||
|
||||
static std::vector<std::u8string> OTHERLIT_U8STR_VEC{U8_LITERAL(OTHER_STR_GBK)};
|
||||
static std::vector<std::wstring> OTHERLIT_WSTR_VEC{WSTR_LITERAL(OTHER_STR_GBK)};
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region OtherLiterals
|
||||
|
||||
OtherLiterals::OtherLiterals() : other_str_vec(OTHERLIT_OTHERSTR_VEC), u8str_vec(OTHERLIT_U8STR_VEC), wstr_vec(OTHERLIT_WSTR_VEC) {
|
||||
// Check whether each vector has same size.
|
||||
bool okey = true;
|
||||
size_t exp_size = this->other_str_vec.size();
|
||||
|
||||
if (this->u8str_vec.size() != exp_size) okey = false;
|
||||
if (this->wstr_vec.size() != exp_size) okey = false;
|
||||
|
||||
if (!okey) throw std::logic_error("utf literal vector have different size");
|
||||
}
|
||||
|
||||
OtherLiterals::~OtherLiterals() {}
|
||||
|
||||
size_t OtherLiterals::get_size() const {
|
||||
// We have checked the size, return size directly.
|
||||
return this->other_str_vec.size();
|
||||
}
|
||||
|
||||
const std::vector<OtherLiteral> &OtherLiterals::get_other_str_vec() const {
|
||||
return this->other_str_vec;
|
||||
}
|
||||
|
||||
const std::vector<std::u8string> &OtherLiterals::get_u8str_vec() const {
|
||||
return this->u8str_vec;
|
||||
}
|
||||
|
||||
const std::vector<std::wstring> &OtherLiterals::get_wstr_vec() const {
|
||||
return this->wstr_vec;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
} // namespace yyccshared::literals
|
64
testbench/shared/literals.hpp
Normal file
64
testbench/shared/literals.hpp
Normal file
@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/macro/class_copy_move.hpp>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace yyccshared::literals {
|
||||
|
||||
class UtfLiterals {
|
||||
public:
|
||||
UtfLiterals();
|
||||
~UtfLiterals();
|
||||
YYCC_DELETE_COPY_MOVE(UtfLiterals)
|
||||
|
||||
size_t get_size() const;
|
||||
const std::vector<std::u8string>& get_u8str_vec() const;
|
||||
const std::vector<std::u16string>& get_u16str_vec() const;
|
||||
const std::vector<std::u32string>& get_u32str_vec() const;
|
||||
const std::vector<std::wstring>& get_wstr_vec() const;
|
||||
|
||||
private:
|
||||
const std::vector<std::u8string>& u8str_vec;
|
||||
const std::vector<std::u16string>& u16str_vec;
|
||||
const std::vector<std::u32string>& u32str_vec;
|
||||
const std::vector<std::wstring>& wstr_vec;
|
||||
};
|
||||
|
||||
class OtherLiteral {
|
||||
public:
|
||||
OtherLiteral(std::string&& other_str, uint32_t windows_ident, std::string&& iconv_ident, std::u8string&& pycodec_ident);
|
||||
~OtherLiteral();
|
||||
YYCC_DEFAULT_COPY_MOVE(OtherLiteral)
|
||||
|
||||
const std::string& get_other_str() const;
|
||||
uint32_t get_windows_ident() const;
|
||||
const std::string& get_iconv_ident() const;
|
||||
const std::u8string& get_pycodec_ident() const;
|
||||
|
||||
private:
|
||||
std::string other_str;
|
||||
uint32_t windows_ident;
|
||||
std::string iconv_ident;
|
||||
std::u8string pycodec_ident;
|
||||
};
|
||||
|
||||
class OtherLiterals {
|
||||
public:
|
||||
OtherLiterals();
|
||||
~OtherLiterals();
|
||||
YYCC_DELETE_COPY_MOVE(OtherLiterals)
|
||||
|
||||
size_t get_size() const;
|
||||
const std::vector<OtherLiteral>& get_other_str_vec() const;
|
||||
const std::vector<std::u8string>& get_u8str_vec() const;
|
||||
const std::vector<std::wstring>& get_wstr_vec() const;
|
||||
|
||||
private:
|
||||
const std::vector<OtherLiteral>& other_str_vec;
|
||||
const std::vector<std::u8string>& u8str_vec;
|
||||
const std::vector<std::wstring>& wstr_vec;
|
||||
};
|
||||
|
||||
} // namespace yyccshared::literals
|
116
testbench/yycc/encoding/iconv.cpp
Normal file
116
testbench/yycc/encoding/iconv.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/encoding/iconv.hpp>
|
||||
#include "../../shared/literals.hpp"
|
||||
|
||||
#define ENC ::yycc::encoding::iconv
|
||||
|
||||
namespace yycctest::encoding::iconv {
|
||||
#if defined(YYCC_FEAT_ICONV)
|
||||
|
||||
static auto UTF_LITERALS = ::yyccshared::literals::UtfLiterals();
|
||||
static auto OTHER_LITERALS = ::yyccshared::literals::OtherLiterals();
|
||||
|
||||
TEST(EncodingIconv, CharToUtf8) {
|
||||
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
|
||||
const auto& u8str_literals = OTHER_LITERALS.get_u8str_vec();
|
||||
|
||||
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
|
||||
const auto& other_str_literal = other_str_literals[i];
|
||||
ENC::CharToUtf8 cv(other_str_literal.get_iconv_ident());
|
||||
|
||||
auto rv = cv.to_utf8(other_str_literal.get_other_str());
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, Utf8ToChar) {
|
||||
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
|
||||
const auto& u8str_literals = OTHER_LITERALS.get_u8str_vec();
|
||||
|
||||
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
|
||||
const auto& other_str_literal = other_str_literals[i];
|
||||
ENC::Utf8ToChar cv(other_str_literal.get_iconv_ident());
|
||||
|
||||
auto rv = cv.to_char(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), other_str_literal.get_other_str());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, Utf8ToWchar) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& wstr_literals = UTF_LITERALS.get_wstr_vec();
|
||||
|
||||
ENC::Utf8ToWchar cv;
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = cv.to_wchar(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), wstr_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, WcharToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& wstr_literals = UTF_LITERALS.get_wstr_vec();
|
||||
|
||||
ENC::WcharToUtf8 cv;
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = cv.to_utf8(wstr_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, Utf8ToUtf16) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
|
||||
|
||||
ENC::Utf8ToUtf16 cv;
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = cv.to_utf16(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u16str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, Utf16ToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
|
||||
|
||||
ENC::Utf16ToUtf8 cv;
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = cv.to_utf8(u16str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, Utf8ToUtf32) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
|
||||
|
||||
ENC::Utf8ToUtf32 cv;
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = cv.to_utf32(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u32str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingIconv, Utf32ToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
|
||||
|
||||
ENC::Utf32ToUtf8 cv;
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = cv.to_utf8(u32str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
}
|
56
testbench/yycc/encoding/stl.cpp
Normal file
56
testbench/yycc/encoding/stl.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/encoding/stl.hpp>
|
||||
#include "../../shared/literals.hpp"
|
||||
|
||||
#define ENC ::yycc::encoding::stl
|
||||
|
||||
namespace yycctest::encoding::stl {
|
||||
|
||||
static auto UTF_LITERALS = ::yyccshared::literals::UtfLiterals();
|
||||
|
||||
TEST(EncodingStl, Utf8ToUtf16) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf16(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u16str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingStl, Utf16ToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf8(u16str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingStl, Utf8ToUtf32) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf32(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u32str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingStl, Utf32ToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf8(u32str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
137
testbench/yycc/encoding/windows.cpp
Normal file
137
testbench/yycc/encoding/windows.cpp
Normal file
@ -0,0 +1,137 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/encoding/windows.hpp>
|
||||
#include "../../shared/literals.hpp"
|
||||
|
||||
#define ENC ::yycc::encoding::windows
|
||||
|
||||
namespace yycctest::encoding::windows {
|
||||
#if defined(YYCC_OS_WINDOWS)
|
||||
|
||||
static auto UTF_LITERALS = ::yyccshared::literals::UtfLiterals();
|
||||
static auto OTHER_LITERALS = ::yyccshared::literals::OtherLiterals();
|
||||
|
||||
TEST(EncodingWindows, CharToWchar) {
|
||||
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
|
||||
const auto& wstr_literals = OTHER_LITERALS.get_wstr_vec();
|
||||
|
||||
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
|
||||
const auto& other_str_literal = other_str_literals[i];
|
||||
|
||||
auto rv = ENC::to_wchar(other_str_literal.get_other_str(), other_str_literal.get_windows_ident());
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), wstr_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, WcharToChar) {
|
||||
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
|
||||
const auto& wstr_literals = OTHER_LITERALS.get_wstr_vec();
|
||||
|
||||
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
|
||||
const auto& other_str_literal = other_str_literals[i];
|
||||
|
||||
auto rv = ENC::to_char(wstr_literals[i], other_str_literal.get_windows_ident());
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), other_str_literal.get_other_str());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, CharToUtf8) {
|
||||
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
|
||||
const auto& u8str_literals = OTHER_LITERALS.get_u8str_vec();
|
||||
|
||||
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
|
||||
const auto& other_str_literal = other_str_literals[i];
|
||||
|
||||
auto rv = ENC::to_utf8(other_str_literal.get_other_str(), other_str_literal.get_windows_ident());
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, Utf8ToChar) {
|
||||
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
|
||||
const auto& u8str_literals = OTHER_LITERALS.get_u8str_vec();
|
||||
|
||||
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
|
||||
const auto& other_str_literal = other_str_literals[i];
|
||||
|
||||
auto rv = ENC::to_char(u8str_literals[i], other_str_literal.get_windows_ident());
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), other_str_literal.get_other_str());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, Utf8ToWchar) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& wstr_literals = UTF_LITERALS.get_wstr_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_wchar(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), wstr_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, WcharToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& wstr_literals = UTF_LITERALS.get_wstr_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf8(wstr_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(YYCC_STL_MSSTL)
|
||||
|
||||
TEST(EncodingWindows, Utf8ToUtf16) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf16(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u16str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, Utf16ToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf8(u16str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, Utf8ToUtf32) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf32(u8str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u32str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EncodingWindows, Utf32ToUtf8) {
|
||||
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
|
||||
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
|
||||
|
||||
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
|
||||
auto rv = ENC::to_utf8(u32str_literals[i]);
|
||||
ASSERT_TRUE(rv.has_value());
|
||||
EXPECT_EQ(rv.value(), u8str_literals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
} // namespace yycctest::encoding::windows
|
Reference in New Issue
Block a user