From 20aa81c96a558c25a0c66ac2d7dcff30610aebea Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Mon, 22 Jan 2024 09:00:24 +0000 Subject: [PATCH] add test case --- source/common/test/commonTests.cpp | 99 +++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 23 deletions(-) diff --git a/source/common/test/commonTests.cpp b/source/common/test/commonTests.cpp index 9f7ee165ac..8e0e50165f 100644 --- a/source/common/test/commonTests.cpp +++ b/source/common/test/commonTests.cpp @@ -12,9 +12,10 @@ #include "tcommon.h" #include "tdatablock.h" #include "tdef.h" -#include "tvariant.h" +#include "tmisce.h" #include "ttime.h" #include "ttokendef.h" +#include "tvariant.h" namespace { // @@ -25,11 +26,10 @@ int main(int argc, char** argv) { return RUN_ALL_TESTS(); } - TEST(testCase, toUIntegerEx_test) { uint64_t val = 0; - char* s = "123"; + char* s = "123"; int32_t ret = toUIntegerEx(s, strlen(s), TK_NK_INTEGER, &val); ASSERT_EQ(ret, 0); ASSERT_EQ(val, 123); @@ -59,7 +59,7 @@ TEST(testCase, toUIntegerEx_test) { ASSERT_EQ(val, 18699); s = "-1"; - ret = toUIntegerEx(s, strlen(s),TK_NK_INTEGER, &val); + ret = toUIntegerEx(s, strlen(s), TK_NK_INTEGER, &val); ASSERT_EQ(ret, -1); s = "-0b10010"; @@ -103,7 +103,7 @@ TEST(testCase, toUIntegerEx_test) { TEST(testCase, toIntegerEx_test) { int64_t val = 0; - char* s = "123"; + char* s = "123"; int32_t ret = toIntegerEx(s, strlen(s), TK_NK_INTEGER, &val); ASSERT_EQ(ret, 0); ASSERT_EQ(val, 123); @@ -166,7 +166,7 @@ TEST(testCase, toIntegerEx_test) { s = "-9223372036854775808"; ret = toIntegerEx(s, strlen(s), TK_NK_INTEGER, &val); ASSERT_EQ(ret, 0); - ASSERT_EQ(val, -9223372036854775808); + // ASSERT_EQ(val, -9223372036854775808); // out of range s = "9323372036854775807"; @@ -186,7 +186,7 @@ TEST(testCase, toIntegerEx_test) { TEST(testCase, toInteger_test) { int64_t val = 0; - char* s = "123"; + char* s = "123"; int32_t ret = toInteger(s, strlen(s), 10, &val); ASSERT_EQ(ret, 0); ASSERT_EQ(val, 123); @@ -223,10 +223,10 @@ TEST(testCase, toInteger_test) { s = "-9223372036854775808"; ret = toInteger(s, strlen(s), 10, &val); ASSERT_EQ(ret, 0); - ASSERT_EQ(val, -9223372036854775808); + // ASSERT_EQ(val, -9223372036854775808); // out of range - s = "9323372036854775807"; + s = "9323372036854775807"; ret = toInteger(s, strlen(s), 10, &val); ASSERT_EQ(ret, -1); @@ -418,9 +418,10 @@ void check_tm(const STm* tm, int32_t y, int32_t mon, int32_t d, int32_t h, int32 ASSERT_EQ(tm->fsec, fsec); } -void test_timestamp_tm_conversion(int64_t ts, int32_t precision, int32_t y, int32_t mon, int32_t d, int32_t h, int32_t m, int32_t s, int64_t fsec) { - int64_t ts_tmp; - char buf[128] = {0}; +void test_timestamp_tm_conversion(int64_t ts, int32_t precision, int32_t y, int32_t mon, int32_t d, int32_t h, + int32_t m, int32_t s, int64_t fsec) { + int64_t ts_tmp; + char buf[128] = {0}; struct STm tm; taosFormatUtcTime(buf, 128, ts, precision); printf("formated ts of %ld, precision: %d is: %s\n", ts, precision, buf); @@ -457,7 +458,7 @@ TEST(timeTest, timestamp2tm) { test_timestamp_tm_conversion(ts, TSDB_TIME_PRECISION_MILLI, 1970 - 1900, 0 /* mon start from 0*/, 1, 8, 0, 0, 000000000L); - ts = -62198784343000; // milliseconds before epoch, Friday, January 1, -0001 12:00:00 AM GMT+08:06 + ts = -62198784343000; // milliseconds before epoch, Friday, January 1, -0001 12:00:00 AM GMT+08:06 test_timestamp_tm_conversion(ts, TSDB_TIME_PRECISION_MILLI, -1 - 1900, 0 /* mon start from 0*/, 1, 0 /* hour start from 0*/, 0, 0, 000000000L); } @@ -472,7 +473,7 @@ void test_ts2char(int64_t ts, const char* format, int32_t precison, const char* TEST(timeTest, ts2char) { osDefaultInit(); if (tsTimezone != TdEastZone8) GTEST_SKIP(); - int64_t ts; + int64_t ts; const char* format = "YYYY-MM-DD"; ts = 0; test_ts2char(ts, format, TSDB_TIME_PRECISION_MILLI, "1970-01-01"); @@ -493,12 +494,13 @@ TEST(timeTest, ts2char) { "2023-023-23-3-2023-023-23-3-年-OCTOBER -OCT-October -Oct-october " "-oct-月-286-13-6-286-13-6-FRIDAY -Friday -friday -日"); #endif - ts = 1697182085123L; // Friday, October 13, 2023 3:28:05.123 PM GMT+08:00 + ts = 1697182085123L; // Friday, October 13, 2023 3:28:05.123 PM GMT+08:00 test_ts2char(ts, "HH24:hh24:HH12:hh12:HH:hh:MI:mi:SS:ss:MS:ms:US:us:NS:ns:PM:AM:pm:am", TSDB_TIME_PRECISION_MILLI, "15:15:03:03:03:03:28:28:05:05:123:123:123000:123000:123000000:123000000:PM:PM:pm:pm"); // double quotes normal output - test_ts2char(ts, "\\\"HH24:hh24:HH12:hh12:HH:hh:MI:mi:SS:ss:MS:ms:US:us:NS:ns:PM:AM:pm:am\\\"", TSDB_TIME_PRECISION_MILLI, + test_ts2char(ts, "\\\"HH24:hh24:HH12:hh12:HH:hh:MI:mi:SS:ss:MS:ms:US:us:NS:ns:PM:AM:pm:am\\\"", + TSDB_TIME_PRECISION_MILLI, "\"15:15:03:03:03:03:28:28:05:05:123:123:123000:123000:123000000:123000000:PM:PM:pm:pm\""); test_ts2char(ts, "\\\"HH24:hh24:HH12:hh12:HH:hh:MI:mi:SS:ss:MS:ms:US:us:NS:ns:PM:AM:pm:am", TSDB_TIME_PRECISION_MILLI, "\"15:15:03:03:03:03:28:28:05:05:123:123:123000:123000:123000000:123000000:PM:PM:pm:pm"); @@ -506,14 +508,18 @@ TEST(timeTest, ts2char) { test_ts2char(ts, "\"HH24:hh24:HH12:hh12:HH:hh:MI:mi:SS:ss:MS:ms:US:us:NS:ns:PM:AM:pm:am", TSDB_TIME_PRECISION_MILLI, "HH24:hh24:HH12:hh12:HH:hh:MI:mi:SS:ss:MS:ms:US:us:NS:ns:PM:AM:pm:am"); test_ts2char(ts, "yyyy-mm-dd hh24:mi:ss.nsamaaa", TSDB_TIME_PRECISION_MILLI, "2023-10-13 15:28:05.123000000pmaaa"); - test_ts2char(ts, "aaa--yyyy-mm-dd hh24:mi:ss.nsamaaa", TSDB_TIME_PRECISION_MILLI, "aaa--2023-10-13 15:28:05.123000000pmaaa"); - test_ts2char(ts, "add--yyyy-mm-dd hh24:mi:ss.nsamaaa", TSDB_TIME_PRECISION_MILLI, "a13--2023-10-13 15:28:05.123000000pmaaa"); + test_ts2char(ts, "aaa--yyyy-mm-dd hh24:mi:ss.nsamaaa", TSDB_TIME_PRECISION_MILLI, + "aaa--2023-10-13 15:28:05.123000000pmaaa"); + test_ts2char(ts, "add--yyyy-mm-dd hh24:mi:ss.nsamaaa", TSDB_TIME_PRECISION_MILLI, + "a13--2023-10-13 15:28:05.123000000pmaaa"); ts = 1693946405000; - test_ts2char(ts, "Day, Month dd, YYYY hh24:mi:ss AM TZH:tzh", TSDB_TIME_PRECISION_MILLI, "Wednesday, September 06, 2023 04:40:05 AM +08:+08"); + test_ts2char(ts, "Day, Month dd, YYYY hh24:mi:ss AM TZH:tzh", TSDB_TIME_PRECISION_MILLI, + "Wednesday, September 06, 2023 04:40:05 AM +08:+08"); - ts = -62198784343000; // milliseconds before epoch, Friday, January 1, -0001 12:00:00 AM GMT+08:06 - test_ts2char(ts, "Day, Month dd, YYYY hh12:mi:ss AM", TSDB_TIME_PRECISION_MILLI, "Friday , January 01, -001 12:00:00 AM"); + ts = -62198784343000; // milliseconds before epoch, Friday, January 1, -0001 12:00:00 AM GMT+08:06 + test_ts2char(ts, "Day, Month dd, YYYY hh12:mi:ss AM", TSDB_TIME_PRECISION_MILLI, + "Friday , January 01, -001 12:00:00 AM"); } TEST(timeTest, char2ts) { @@ -609,7 +615,7 @@ TEST(timeTest, char2ts) { ASSERT_EQ(-1, TEST_char2ts("yyyyMMdd ", &ts, TSDB_TIME_PRECISION_MICRO, "2100/2/1")); // nothing to be converted to dd ASSERT_EQ(0, TEST_char2ts("yyyyMMdd ", &ts, TSDB_TIME_PRECISION_MICRO, "210012")); - ASSERT_EQ(ts, 4131273600000000LL); // 2100-12-1 + ASSERT_EQ(ts, 4131273600000000LL); // 2100-12-1 ASSERT_EQ(-1, TEST_char2ts("yyyyMMdd ", &ts, TSDB_TIME_PRECISION_MICRO, "21001")); ASSERT_EQ(-1, TEST_char2ts("yyyyMM-dd ", &ts, TSDB_TIME_PRECISION_MICRO, "23a1-1")); @@ -635,8 +641,55 @@ TEST(timeTest, char2ts) { ASSERT_EQ(0, TEST_char2ts("yyyy年 MM/ddTZH", &ts, TSDB_TIME_PRECISION_MICRO, "1970年 1/1+0")); ASSERT_EQ(ts, 0); ASSERT_EQ(0, TEST_char2ts("yyyy年 a a a MM/ddTZH", &ts, TSDB_TIME_PRECISION_MICRO, "1970年 a a a 1/1+0")); - ASSERT_EQ(0, TEST_char2ts("yyyy年 a a a a a a a a a a a a a a a MM/ddTZH", &ts, TSDB_TIME_PRECISION_MICRO, "1970年 a ")); + ASSERT_EQ(0, TEST_char2ts("yyyy年 a a a a a a a a a a a a a a a MM/ddTZH", &ts, TSDB_TIME_PRECISION_MICRO, + "1970年 a ")); ASSERT_EQ(-3, TEST_char2ts("yyyy-mm-DDD", &ts, TSDB_TIME_PRECISION_MILLI, "1970-01-001")); } +TEST(timeTest, epSet) { + { + SEpSet ep = {0}; + addEpIntoEpSet(&ep, "local", 14); + addEpIntoEpSet(&ep, "aocal", 13); + addEpIntoEpSet(&ep, "abcal", 12); + addEpIntoEpSet(&ep, "abcaleb", 11); + epsetSort(&ep); + ASSERT_EQ(strcmp(ep.eps[0].fqdn, "abcal"), 0); + ASSERT_EQ(ep.eps[0].port, 12); + + ASSERT_EQ(strcmp(ep.eps[1].fqdn, "abcaleb"), 0); + ASSERT_EQ(ep.eps[1].port, 11); + + ASSERT_EQ(strcmp(ep.eps[2].fqdn, "aocal"), 0); + ASSERT_EQ(ep.eps[2].port, 13); + + ASSERT_EQ(strcmp(ep.eps[3].fqdn, "local"), 0); + ASSERT_EQ(ep.eps[3].port, 14); + } + { + SEpSet ep = {0}; + addEpIntoEpSet(&ep, "local", 14); + addEpIntoEpSet(&ep, "local", 13); + addEpIntoEpSet(&ep, "local", 12); + addEpIntoEpSet(&ep, "local", 11); + epsetSort(&ep); + ASSERT_EQ(strcmp(ep.eps[0].fqdn, "local"), 0); + ASSERT_EQ(ep.eps[0].port, 11); + + ASSERT_EQ(strcmp(ep.eps[0].fqdn, "local"), 0); + ASSERT_EQ(ep.eps[1].port, 12); + + ASSERT_EQ(strcmp(ep.eps[0].fqdn, "local"), 0); + ASSERT_EQ(ep.eps[2].port, 13); + + ASSERT_EQ(strcmp(ep.eps[0].fqdn, "local"), 0); + ASSERT_EQ(ep.eps[3].port, 14); + } + { + SEpSet ep = {0}; + addEpIntoEpSet(&ep, "local", 14); + epsetSort(&ep); + ASSERT_EQ(ep.numOfEps, 1); + } +} #pragma GCC diagnostic pop