2024-05-20 21:41:48 +08:00
|
|
|
#include <YYCCommonplace.hpp>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
namespace Testbench {
|
|
|
|
|
2024-05-22 13:42:43 +08:00
|
|
|
static void Assert(bool condition, const char* description) {
|
|
|
|
if (condition) {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPrintf(stdout, YYCC_TERMCOL_LIGHT_GREEN("OK: %s\n"), description);
|
2024-05-22 13:42:43 +08:00
|
|
|
} else {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPrintf(stdout, YYCC_TERMCOL_LIGHT_RED("Failed: %s\n"), description);
|
2024-05-22 13:42:43 +08:00
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 21:41:48 +08:00
|
|
|
static void TerminalTestbench() {
|
|
|
|
YYCC::TerminalHelper::EnsureTerminalUTF8(stdout);
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPuts("你好世界\n", stdout);
|
2024-05-20 21:41:48 +08:00
|
|
|
YYCC::TerminalHelper::EnsureTerminalColor(stdout);
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPuts(YYCC_TERMCOL_LIGHT_CYAN("Colorful Terminal\n"), stdout);
|
2024-05-20 21:41:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void StringTestbench() {
|
2024-05-30 14:53:46 +08:00
|
|
|
auto test_printf = YYCC::StringHelper::Printf("%s == %s", "Hello World", "你好世界");
|
|
|
|
Assert(test_printf == "Hello World == 你好世界", "YYCC::StringHelper::Printf");
|
2024-05-22 13:42:43 +08:00
|
|
|
|
|
|
|
auto test_lower = YYCC::StringHelper::Lower("LOWER");
|
|
|
|
Assert(test_lower == "lower", "YYCC::StringHelper::Lower");
|
|
|
|
auto test_upper = YYCC::StringHelper::Upper("upper");
|
|
|
|
Assert(test_upper == "UPPER", "YYCC::StringHelper::Upper");
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> test_join_container {
|
|
|
|
"", "1", "2", ""
|
2024-05-21 10:24:05 +08:00
|
|
|
};
|
2024-05-22 13:42:43 +08:00
|
|
|
auto test_join = YYCC::StringHelper::Join(test_join_container, ", ");
|
|
|
|
Assert(test_join == ", 1, 2, ", "YYCC::StringHelper::Join");
|
|
|
|
test_join = YYCC::StringHelper::Join(test_join_container, ", ", true);
|
|
|
|
Assert(test_join == ", 2, 1, ", "YYCC::StringHelper::Join");
|
|
|
|
|
|
|
|
auto test_split = YYCC::StringHelper::Split(", 1, 2, ", ", ");
|
|
|
|
Assert(test_split.size() == 4u, "YYCC::StringHelper::Split");
|
|
|
|
Assert(test_split[0] == "", "YYCC::StringHelper::Split");
|
|
|
|
Assert(test_split[1] == "1", "YYCC::StringHelper::Split");
|
|
|
|
Assert(test_split[2] == "2", "YYCC::StringHelper::Split");
|
|
|
|
Assert(test_split[3] == "", "YYCC::StringHelper::Split");
|
|
|
|
test_split = YYCC::StringHelper::Split("test", "-");
|
|
|
|
Assert(test_split.size() == 1u, "YYCC::StringHelper::Split");
|
|
|
|
Assert(test_split[0] == "test", "YYCC::StringHelper::Split");
|
|
|
|
test_split = YYCC::StringHelper::Split("test", "");
|
|
|
|
Assert(test_split.size() == 1u, "YYCC::StringHelper::Split");
|
|
|
|
Assert(test_split[0] == "test", "YYCC::StringHelper::Split");
|
|
|
|
|
2024-05-20 21:41:48 +08:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:59:41 +08:00
|
|
|
static void ParserTestbench() {
|
|
|
|
|
2024-05-30 14:53:46 +08:00
|
|
|
// Test success TryParse
|
|
|
|
#define TEST_MACRO(type_t, value, string_value) { \
|
|
|
|
std::string cache_string(string_value); \
|
|
|
|
type_t cache; \
|
|
|
|
Assert(YYCC::ParserHelper::TryParse<type_t>(cache_string, cache) && cache == value, "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(bool, true, "true");
|
|
|
|
|
|
|
|
#undef TEST_MACRO
|
|
|
|
|
|
|
|
// Test failed TryParse
|
|
|
|
#define TEST_MACRO(type_t, value, string_value) { \
|
|
|
|
std::string cache_string(string_value); \
|
|
|
|
type_t cache; \
|
|
|
|
Assert(!YYCC::ParserHelper::TryParse<type_t>(cache_string, cache), "YYCC::StringHelper::TryParse<" #type_t ">"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_MACRO(int8_t, INT8_C(-61), "6161");
|
|
|
|
TEST_MACRO(uint8_t, UINT8_C(200), "32800");
|
|
|
|
TEST_MACRO(int16_t, INT16_C(6161), "61616161");
|
|
|
|
TEST_MACRO(uint16_t, UINT16_C(32800), "4294967293");
|
|
|
|
TEST_MACRO(int32_t, INT32_C(61616161), "616161616161");
|
|
|
|
TEST_MACRO(uint32_t, UINT32_C(4294967293), "9223372036854775807");
|
|
|
|
TEST_MACRO(int64_t, INT64_C(616161616161), "616161616161616161616161");
|
|
|
|
TEST_MACRO(uint64_t, UINT64_C(9223372036854775807), "92233720368547758079223372036854775807");
|
|
|
|
TEST_MACRO(bool, true, "hello, world!");
|
|
|
|
|
|
|
|
#undef TEST_MACRO
|
|
|
|
|
|
|
|
// Test ToString
|
2024-05-28 19:59:41 +08:00
|
|
|
#define TEST_MACRO(type_t, value, string_value) { \
|
|
|
|
type_t cache = value; \
|
|
|
|
std::string ret(YYCC::ParserHelper::ToString<type_t>(cache)); \
|
|
|
|
Assert(ret == string_value, "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(bool, true, "true");
|
|
|
|
//{
|
|
|
|
// bool cache = true;
|
|
|
|
// std::string ret(YYCC::ParserHelper::ToString<bool>(cache));
|
|
|
|
// Assert(ret == "true", "YYCC::StringHelper::ToString<bool>");
|
|
|
|
//}
|
|
|
|
|
|
|
|
#undef TEST_MACRO
|
|
|
|
}
|
|
|
|
|
2024-05-22 15:53:46 +08:00
|
|
|
static void DialogTestbench() {
|
2024-05-27 14:27:11 +08:00
|
|
|
std::string ret;
|
|
|
|
std::vector<std::string> rets;
|
2024-05-22 15:53:46 +08:00
|
|
|
|
2024-05-27 14:27:11 +08:00
|
|
|
YYCC::DialogHelper::FileDialog params;
|
|
|
|
auto& filters = params.ConfigreFileTypes();
|
|
|
|
filters.Add("Microsoft Word (*.docx; *.doc)", {"*.docx", "*.doc"});
|
|
|
|
filters.Add("Microsoft Excel (*.xlsx; *.xls)", {"*.xlsx", "*.xls"});
|
|
|
|
filters.Add("Microsoft PowerPoint (*.pptx; *.ppt)", {"*.pptx", "*.ppt"});
|
|
|
|
filters.Add("Text File (*.txt)", {"*.txt"});
|
|
|
|
filters.Add("All Files (*.*)", {"*.*"});
|
|
|
|
params.SetDefaultFileTypeIndex(0u);
|
|
|
|
if (YYCC::DialogHelper::OpenFileDialog(params, ret)) {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPrintf(stdout, "Open File: %s\n", ret.c_str());
|
2024-05-27 14:27:11 +08:00
|
|
|
}
|
|
|
|
if (YYCC::DialogHelper::OpenMultipleFileDialog(params, rets)) {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPuts("Open Multiple Files:\n", stdout);
|
2024-05-27 14:27:11 +08:00
|
|
|
for (const auto& item : rets) {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPrintf(stdout, "\t%s\n", item.c_str());
|
2024-05-27 14:27:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (YYCC::DialogHelper::SaveFileDialog(params, ret)) {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPrintf(stdout, "Save File: %s\n", ret.c_str());
|
2024-05-27 14:27:11 +08:00
|
|
|
}
|
|
|
|
params.Clear();
|
|
|
|
if (YYCC::DialogHelper::OpenFolderDialog(params, ret)) {
|
2024-05-30 14:53:46 +08:00
|
|
|
YYCC::TerminalHelper::FPrintf(stdout, "Open Folder: %s\n", ret.c_str());
|
2024-05-27 14:27:11 +08:00
|
|
|
}
|
2024-05-22 15:53:46 +08:00
|
|
|
}
|
|
|
|
|
2024-05-20 21:41:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** args) {
|
|
|
|
Testbench::TerminalTestbench();
|
|
|
|
Testbench::StringTestbench();
|
2024-05-28 19:59:41 +08:00
|
|
|
Testbench::ParserTestbench();
|
|
|
|
//Testbench::DialogTestbench();
|
2024-05-20 21:41:48 +08:00
|
|
|
}
|