TDengine/source/common/test/commonTests.cpp

263 lines
7 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
#include <iostream>
2022-01-21 10:19:40 +00:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"
2022-05-11 11:03:59 +00:00
#include "taos.h"
2022-03-04 06:31:21 +00:00
#include "tcommon.h"
#include "tdatablock.h"
#include "tdef.h"
2022-05-11 11:03:59 +00:00
#include "tvariant.h"
namespace {
//
} // namespace
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
TEST(testCase, toInteger_test) {
char* s = "123";
uint32_t type = 0;
int64_t val = 0;
2022-05-11 11:03:59 +00:00
int32_t ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 123);
s = "9223372036854775807";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 9223372036854775807);
s = "9323372036854775807";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 9323372036854775807u);
s = "-9323372036854775807";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, -1);
s = "-1";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, -1);
s = "-9223372036854775807";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, -9223372036854775807);
s = "1000u";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, -1);
s = "0x10";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 16, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 16);
s = "110";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 2, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 6);
s = "110";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 8, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 72);
2022-05-11 11:03:59 +00:00
// 18446744073709551615 UINT64_MAX
s = "18446744073709551615";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, 0);
ASSERT_EQ(val, 18446744073709551615u);
s = "18446744073709551616";
2022-05-11 11:03:59 +00:00
ret = toInteger(s, strlen(s), 10, &val);
ASSERT_EQ(ret, -1);
}
2022-01-21 10:19:40 +00:00
2022-02-04 14:41:54 +00:00
TEST(testCase, Datablock_test) {
SSDataBlock* b = createDataBlock();
2022-02-04 14:41:54 +00:00
SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, 4, 1);
2022-02-04 14:41:54 +00:00
taosArrayPush(b->pDataBlock, &infoData);
blockDataAppendColInfo(b, &infoData);
2022-02-04 14:41:54 +00:00
SColumnInfoData infoData1 = createColumnInfoData(TSDB_DATA_TYPE_BINARY, 40, 2);
blockDataAppendColInfo(b, &infoData1);
blockDataEnsureCapacity(b, 40);
2022-02-04 14:41:54 +00:00
char* str = "the value of: %d";
2022-05-11 11:03:59 +00:00
char buf[128] = {0};
char varbuf[128] = {0};
2022-02-04 14:41:54 +00:00
2022-05-11 11:03:59 +00:00
for (int32_t i = 0; i < 40; ++i) {
SColumnInfoData* p0 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 0);
SColumnInfoData* p1 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 1);
2022-02-04 14:41:54 +00:00
2022-05-11 11:03:59 +00:00
if (i & 0x01) {
2022-02-04 14:41:54 +00:00
int32_t len = sprintf(buf, str, i);
STR_TO_VARSTR(varbuf, buf)
2023-02-20 02:04:08 +00:00
colDataSetVal(p0, i, (const char*)&i, false);
colDataSetVal(p1, i, (const char*)varbuf, false);
2022-02-04 14:41:54 +00:00
memset(varbuf, 0, sizeof(varbuf));
memset(buf, 0, sizeof(buf));
} else {
2023-02-20 02:04:08 +00:00
colDataSetVal(p0, i, (const char*)&i, true);
colDataSetVal(p1, i, (const char*)varbuf, true);
2022-02-04 14:41:54 +00:00
}
b->info.rows++;
}
2022-05-11 11:03:59 +00:00
SColumnInfoData* p0 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 0);
SColumnInfoData* p1 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 1);
for (int32_t i = 0; i < 40; ++i) {
2022-02-04 14:41:54 +00:00
if (i & 0x01) {
ASSERT_EQ(colDataIsNull_f(p0->nullbitmap, i), false);
ASSERT_EQ(colDataIsNull(p1, b->info.rows, i, nullptr), false);
} else {
ASSERT_EQ(colDataIsNull_f(p0->nullbitmap, i), true);
ASSERT_EQ(colDataIsNull(p0, b->info.rows, i, nullptr), true);
ASSERT_EQ(colDataIsNull(p1, b->info.rows, i, nullptr), true);
}
}
2022-05-11 11:03:59 +00:00
printf("binary column length:%d\n", *(int32_t*)p1->pData);
2022-02-04 14:41:54 +00:00
2022-03-09 02:22:53 +00:00
ASSERT_EQ(blockDataGetNumOfCols(b), 2);
ASSERT_EQ(blockDataGetNumOfRows(b), 40);
2022-02-04 14:41:54 +00:00
char* pData = colDataGetData(p1, 3);
2022-02-04 14:41:54 +00:00
printf("the second row of binary:%s, length:%d\n", (char*)varDataVal(pData), varDataLen(pData));
2022-05-11 11:03:59 +00:00
SArray* pOrderInfo = taosArrayInit(3, sizeof(SBlockOrderInfo));
SBlockOrderInfo order = {true, TSDB_ORDER_ASC, 0, NULL};
2022-02-04 14:41:54 +00:00
taosArrayPush(pOrderInfo, &order);
2022-03-28 12:11:02 +00:00
blockDataSort(b, pOrderInfo);
2022-02-08 10:01:21 +00:00
blockDataDestroy(b);
2022-02-04 14:41:54 +00:00
taosArrayDestroy(pOrderInfo);
}
#if 0
TEST(testCase, non_var_dataBlock_split_test) {
2022-03-25 16:29:53 +00:00
SSDataBlock* b = static_cast<SSDataBlock*>(taosMemoryCalloc(1, sizeof(SSDataBlock)));
2022-02-04 14:41:54 +00:00
b->info.numOfCols = 2;
b->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));
SColumnInfoData infoData = {0};
infoData.info.bytes = 4;
infoData.info.type = TSDB_DATA_TYPE_INT;
infoData.info.colId = 1;
int32_t numOfRows = 1000000;
2022-03-25 16:29:53 +00:00
infoData.pData = (char*) taosMemoryCalloc(numOfRows, infoData.info.bytes);
infoData.nullbitmap = (char*) taosMemoryCalloc(1, sizeof(char) * (numOfRows/8));
2022-02-04 14:41:54 +00:00
taosArrayPush(b->pDataBlock, &infoData);
SColumnInfoData infoData1 = {0};
infoData1.info.bytes = 1;
infoData1.info.type = TSDB_DATA_TYPE_TINYINT;
infoData1.info.colId = 2;
2022-03-25 16:29:53 +00:00
infoData1.pData = (char*) taosMemoryCalloc(numOfRows, infoData.info.bytes);
infoData1.nullbitmap = (char*) taosMemoryCalloc(1, sizeof(char) * (numOfRows/8));
2022-02-04 14:41:54 +00:00
taosArrayPush(b->pDataBlock, &infoData1);
for(int32_t i = 0; i < numOfRows; ++i) {
SColumnInfoData* p0 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 0);
SColumnInfoData* p1 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 1);
int8_t v = i;
2023-02-20 02:04:08 +00:00
colDataSetVal(p0, i, (const char*)&i, false);
colDataSetVal(p1, i, (const char*)&v, false);
2022-02-04 14:41:54 +00:00
b->info.rows++;
}
int32_t pageSize = 64 * 1024;
int32_t startIndex= 0;
int32_t stopIndex = 0;
int32_t count = 1;
while(1) {
blockDataSplitRows(b, false, startIndex, &stopIndex, pageSize);
printf("the %d split, from: %d to %d\n", count++, startIndex, stopIndex);
if (stopIndex == numOfRows - 1) {
break;
}
startIndex = stopIndex + 1;
}
}
#endif
TEST(testCase, var_dataBlock_split_test) {
int32_t numOfRows = 1000000;
SSDataBlock* b = createDataBlock();
2022-02-04 14:41:54 +00:00
SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_INT, 4, 1);
blockDataAppendColInfo(b, &infoData);
2022-02-04 14:41:54 +00:00
SColumnInfoData infoData1 = createColumnInfoData(TSDB_DATA_TYPE_BINARY, 40, 2);
blockDataAppendColInfo(b, &infoData1);
2022-02-04 14:41:54 +00:00
blockDataEnsureCapacity(b, numOfRows);
2022-02-04 14:41:54 +00:00
char buf[41] = {0};
char buf1[100] = {0};
2022-05-11 11:03:59 +00:00
for (int32_t i = 0; i < numOfRows; ++i) {
2022-02-04 14:41:54 +00:00
SColumnInfoData* p0 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 0);
SColumnInfoData* p1 = (SColumnInfoData*)taosArrayGet(b->pDataBlock, 1);
int8_t v = i;
2023-02-20 02:04:08 +00:00
colDataSetVal(p0, i, (const char*)&i, false);
2022-02-04 14:41:54 +00:00
sprintf(buf, "the number of row:%d", i);
int32_t len = sprintf(buf1, buf, i);
STR_TO_VARSTR(buf1, buf)
2023-02-20 02:04:08 +00:00
colDataSetVal(p1, i, buf1, false);
2022-02-04 14:41:54 +00:00
b->info.rows++;
memset(buf, 0, sizeof(buf));
memset(buf1, 0, sizeof(buf1));
}
int32_t pageSize = 64 * 1024;
2022-05-11 11:03:59 +00:00
int32_t startIndex = 0;
2022-02-04 14:41:54 +00:00
int32_t stopIndex = 0;
int32_t count = 1;
2022-05-11 11:03:59 +00:00
while (1) {
2022-02-04 14:41:54 +00:00
blockDataSplitRows(b, true, startIndex, &stopIndex, pageSize);
printf("the %d split, from: %d to %d\n", count++, startIndex, stopIndex);
if (stopIndex == numOfRows - 1) {
break;
}
startIndex = stopIndex + 1;
}
}
2022-01-21 10:19:40 +00:00
#pragma GCC diagnostic pop