From 152fa96d26878630e9f5b54569f27f23ef4b15db Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 18 Dec 2016 01:05:25 +0100 Subject: [PATCH] Added test cases for natural_string_sort() and regexp_parse_uint32() functions. --- test/src/GlobalTest.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ test/src/Main.cpp | 26 +++++++++++-------- 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/test/src/GlobalTest.cpp b/test/src/GlobalTest.cpp index 5861299..40390dd 100644 --- a/test/src/GlobalTest.cpp +++ b/test/src/GlobalTest.cpp @@ -331,3 +331,70 @@ TEST_F(GlobalTest, Directory) #undef MAKE_TEST_FILE #undef MAKE_SUB_DIR + +//----------------------------------------------------------------- +// Natural String Sort +//----------------------------------------------------------------- + +TEST_F(GlobalTest, NaturalStrSort) +{ + static const char *const TEST[] = + { + "z0.txt", "z1.txt", "z2.txt", "z3.txt", "z4.txt", "z5.txt", "z6.txt", "z7.txt", "z8.txt", "z9.txt", + "z10.txt", "z11.txt", "z12.txt", "z13.txt", "z14.txt", "z15.txt", "z16.txt", "z17.txt", "z18.txt", "z19.txt", + "z100.txt", "z101.txt", "z102.txt", "z103.txt", "z104.txt", "z105.txt", "z106.txt", "z107.txt", "z108.txt", "z109.txt", + NULL + }; + + QStringList test; + for (size_t i = 0; TEST[i]; i++) + { + test << QLatin1String(TEST[i]); + } + + qsrand(time(NULL)); + for (size_t q = 0; q < 97; q++) + { + for (size_t k = 0; k < 997; k++) + { + const size_t len = size_t(test.count()); + for (size_t i = 0; i < len; i++) + { + test.swap(i, qrand() % len); + } + } + MUtils::natural_string_sort(test, true); + for (size_t i = 0; TEST[i]; i++) + { + ASSERT_QSTR(test[i], TEST[i]); + } + } +} + +//----------------------------------------------------------------- +// RegExp Parser +//----------------------------------------------------------------- + +#define TEST_REGEX_U32(X,Y,Z,...) do \ +{ \ + const QRegExp test(QLatin1String((X))); \ + ASSERT_GE(test.indexIn(QLatin1String((Y))), 0); \ + quint32 result[(Z)]; \ + ASSERT_TRUE(MUtils::regexp_parse_uint32(test, result, (Z))); \ + const quint32 expected[] = { __VA_ARGS__ }; \ + for(size_t i = 0; i < (Z); i++) \ + { \ + ASSERT_EQ(result[i], expected[i]); \ + } \ +} \ +while(0) + +TEST_F(GlobalTest, ParseRegExp) +{ + TEST_REGEX_U32("(\\d+)", "42", 1, 42); + TEST_REGEX_U32("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)", "4 8 15 16 23 42", 6, 4, 8, 15, 16, 23, 42); + TEST_REGEX_U32("x264\\s+(\\d+)\\.(\\d+)\\.(\\d+)\\s+\\w+", "x264 0.148.2744 b97ae06", 3, 0, 148, 2744); + TEST_REGEX_U32("HEVC\\s+encoder\\s+version\\s+(\\d+)\\.(\\d+)\\+(\\d+)-\\w+", "HEVC encoder version 2.1+70-78e1e1354a25", 3, 2, 1, 70); +} + +#undef TEST_REGEX_U32 diff --git a/test/src/Main.cpp b/test/src/Main.cpp index fcc92de..a2025b0 100644 --- a/test/src/Main.cpp +++ b/test/src/Main.cpp @@ -55,25 +55,23 @@ static void initialize_mutils_log_file(const int argc, const wchar_t *const *con } _snprintf_s(gtestOutputPath, _MAX_PATH + 16, _TRUNCATE, "xml:%S.xml", basePath); - if ((!gtestOutputPath[0]) || strchr(gtestOutputPath, '?')) + if (gtestOutputPath[0] && (!strchr(gtestOutputPath, '?'))) { - strcpy_s(gtestOutputPath, _MAX_PATH + 16, "xml:MUtilsTest.xml"); + ::testing::GTEST_FLAG(output) = std::string(gtestOutputPath); } - ::testing::GTEST_FLAG(output) = std::string(gtestOutputPath); } -static void get_time_stamp(char *const buffer, const size_t buff_size) +static bool get_time_stamp(char *const buffer, const size_t buff_size) { const time_t time_stamp = time(NULL); struct tm tm_info; - if(!localtime_s(&tm_info, &time_stamp)) - { - strftime(buffer, buff_size, "%Y-%m-%d %H:%M:%S", &tm_info); - } - else + if(localtime_s(&tm_info, &time_stamp)) { buffer[0] = L'\0'; + return false; } + const size_t ret = strftime(buffer, buff_size, "%Y-%m-%d %H:%M:%S", &tm_info); + return (ret > 0) && (ret < buff_size); } static void qt_message_handler(QtMsgType type, const char *const msg) @@ -87,8 +85,14 @@ static void qt_message_handler(QtMsgType type, const char *const msg) if (g_logFile && (!ferror(g_logFile))) { char time_buffer[32]; - get_time_stamp(time_buffer, 32); - fprintf(g_logFile, "[%s] %s\n", time_buffer, msg); + if (get_time_stamp(time_buffer, 32)) + { + fprintf(g_logFile, "[%s] %s\n", time_buffer, msg); + } + else + { + fprintf(g_logFile, "%s\n", msg); + } } } -- 2.11.4.GIT