TDengine/source/libs/executor/test/tSimpleHashTests.cpp

149 lines
4.1 KiB
C++
Raw Normal View History

2022-08-11 11:12:24 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <iostream>
#include "taos.h"
#include "thash.h"
#include "tsimplehash.h"
#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"
2022-08-11 11:18:55 +00:00
// int main(int argc, char **argv) {
// testing::InitGoogleTest(&argc, argv);
// return RUN_ALL_TESTS();
// }
2022-08-11 11:12:24 +00:00
TEST(testCase, tSimpleHashTest_intKey) {
2022-10-13 05:41:36 +00:00
SSHashObj *pHashObj = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
2022-08-11 11:12:24 +00:00
TD_ALWAYS_ASSERT(pHashObj != nullptr);
2022-08-11 11:12:24 +00:00
ASSERT_EQ(0, tSimpleHashGetSize(pHashObj));
2022-08-12 13:00:48 +00:00
size_t keyLen = sizeof(int64_t);
size_t dataLen = sizeof(int64_t);
2022-08-11 11:12:24 +00:00
int64_t originKeySum = 0;
for (int64_t i = 1; i <= 100; ++i) {
originKeySum += i;
2024-07-28 07:46:19 +00:00
code = tSimpleHashPut(pHashObj, (const void *)&i, keyLen, (const void *)&i, dataLen);
ASSERT(code == 0);
2022-08-11 11:12:24 +00:00
ASSERT_EQ(i, tSimpleHashGetSize(pHashObj));
}
for (int64_t i = 1; i <= 100; ++i) {
2022-08-12 13:00:48 +00:00
void *data = tSimpleHashGet(pHashObj, (const void *)&i, keyLen);
2022-08-11 11:12:24 +00:00
ASSERT_EQ(i, *(int64_t *)data);
}
void *data = NULL;
int32_t iter = 0;
int64_t keySum = 0;
int64_t dataSum = 0;
size_t kLen = 0;
2022-08-11 11:12:24 +00:00
while ((data = tSimpleHashIterate(pHashObj, data, &iter))) {
void *key = tSimpleHashGetKey(data, &kLen);
ASSERT_EQ(keyLen, kLen);
2022-08-11 11:12:24 +00:00
keySum += *(int64_t *)key;
dataSum += *(int64_t *)data;
}
2022-08-11 11:12:24 +00:00
ASSERT_EQ(keySum, dataSum);
ASSERT_EQ(keySum, originKeySum);
for (int64_t i = 1; i <= 100; ++i) {
2024-07-28 07:46:19 +00:00
code = tSimpleHashRemove(pHashObj, (const void *)&i, keyLen);
ASSERT(code == 0);
2022-08-11 11:12:24 +00:00
ASSERT_EQ(100 - i, tSimpleHashGetSize(pHashObj));
}
2022-08-11 11:18:55 +00:00
tSimpleHashCleanup(pHashObj);
2022-08-11 11:12:24 +00:00
}
TEST(testCase, tSimpleHashTest_binaryKey) {
2022-10-13 05:41:36 +00:00
SSHashObj *pHashObj = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
TD_ALWAYS_ASSERT(pHashObj != nullptr);
ASSERT_EQ(0, tSimpleHashGetSize(pHashObj));
typedef struct {
int64_t suid;
int64_t uid;
} SCombineKey;
size_t keyLen = sizeof(SCombineKey);
size_t dataLen = sizeof(int64_t);
2022-10-13 05:41:36 +00:00
int64_t originDataSum = 0;
SCombineKey combineKey = {0};
for (int64_t i = 1; i <= 100; ++i) {
combineKey.suid = i;
combineKey.uid = i + 1;
2024-07-28 07:46:19 +00:00
code = tSimpleHashPut(pHashObj, (const void *)&combineKey, keyLen, (const void *)&i, dataLen);
ASSERT(code == 0);
originDataSum += i;
ASSERT_EQ(i, tSimpleHashGetSize(pHashObj));
}
for (int64_t i = 1; i <= 100; ++i) {
combineKey.suid = i;
combineKey.uid = i + 1;
void *data = tSimpleHashGet(pHashObj, (const void *)&combineKey, keyLen);
ASSERT_EQ(i, *(int64_t *)data);
}
void *data = NULL;
int32_t iter = 0;
int64_t keySum = 0;
int64_t dataSum = 0;
size_t kLen = 0;
while ((data = tSimpleHashIterate(pHashObj, data, &iter))) {
void *key = tSimpleHashGetKey(data, &kLen);
ASSERT_EQ(keyLen, kLen);
dataSum += *(int64_t *)data;
}
ASSERT_EQ(originDataSum, dataSum);
2024-07-28 07:46:19 +00:00
code = tSimpleHashRemove(pHashObj, (const void *)&combineKey, keyLen);
ASSERT(code == 0);
while ((data = tSimpleHashIterate(pHashObj, data, &iter))) {
void *key = tSimpleHashGetKey(data, &kLen);
ASSERT_EQ(keyLen, kLen);
}
for (int64_t i = 1; i <= 99; ++i) {
combineKey.suid = i;
combineKey.uid = i + 1;
2024-07-28 07:46:19 +00:00
code = tSimpleHashRemove(pHashObj, (const void *)&combineKey, keyLen);
ASSERT(code == 0);
ASSERT_EQ(99 - i, tSimpleHashGetSize(pHashObj));
}
tSimpleHashCleanup(pHashObj);
}
2022-08-11 11:12:24 +00:00
#pragma GCC diagnostic pop