From a01ea0bed2c7e10d11cc0b95f5b76b1b4eaa5199 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 24 Aug 2022 22:34:11 +0800 Subject: [PATCH] more code --- include/util/trbtree.h | 17 +++++++++++++-- source/util/src/trbtree.c | 2 +- source/util/test/CMakeLists.txt | 8 +++++++ source/util/test/trbtreeTest.cpp | 36 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 source/util/test/trbtreeTest.cpp diff --git a/include/util/trbtree.h b/include/util/trbtree.h index f02b746c34..d6a237fb77 100644 --- a/include/util/trbtree.h +++ b/include/util/trbtree.h @@ -13,17 +13,24 @@ * along with this program. If not, see . */ +#ifndef _TD_UTIL_RBTREE_H_ +#define _TD_UTIL_RBTREE_H_ + #include "os.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct SRBTree SRBTree; typedef struct SRBTreeNode SRBTreeNode; typedef struct SRBTreeIter SRBTreeIter; -typedef int32_t (*tRBTreeCmprFn)(void *, void *); +typedef int32_t (*tRBTreeCmprFn)(const void *, const void *); // SRBTree ============================================= #define tRBTreeCreate(compare) \ - (SRBTree) { .cmprFn = (compare), .root = NULL, .minNode = NULL, .maxNode = NULL } + (SRBTree) { .cmprFn = (compare), .rootNode = NULL, .minNode = NULL, .maxNode = NULL } SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew); void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *pNode); @@ -56,3 +63,9 @@ struct SRBTreeIter { SRBTree *pTree; SRBTreeNode *pNode; }; + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_UTIL_RBTREE_H_*/ \ No newline at end of file diff --git a/source/util/src/trbtree.c b/source/util/src/trbtree.c index 887a824151..3c3bad7340 100644 --- a/source/util/src/trbtree.c +++ b/source/util/src/trbtree.c @@ -256,7 +256,7 @@ SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter) { if (pIter->pNode->right) { pIter->pNode = pIter->pNode->right; while (pIter->pNode->left) { - pIter->pNode->left; + pIter->pNode = pIter->pNode->left; } } else { while (true) { diff --git a/source/util/test/CMakeLists.txt b/source/util/test/CMakeLists.txt index d2a503e661..6e42ef7e75 100644 --- a/source/util/test/CMakeLists.txt +++ b/source/util/test/CMakeLists.txt @@ -75,4 +75,12 @@ target_link_libraries(taosbsearchTest os util gtest_main) add_test( NAME taosbsearchTest COMMAND taosbsearchTest +) + +# trbtreeTest +add_executable(rbtreeTest "trbtreeTest.cpp") +target_link_libraries(rbtreeTest os util gtest_main) +add_test( + NAME rbtreeTest + COMMAND rbtreeTest ) \ No newline at end of file diff --git a/source/util/test/trbtreeTest.cpp b/source/util/test/trbtreeTest.cpp new file mode 100644 index 0000000000..4ff990a46d --- /dev/null +++ b/source/util/test/trbtreeTest.cpp @@ -0,0 +1,36 @@ +#include + +#include +#include + +#include "trbtree.h" + +static int32_t tCmprInteger(const void *p1, const void *p2) { + if (*(int *)p1 < *(int *)p2) { + return -1; + } else if (*(int *)p1 > *(int *)p2) { + return 1; + } + return 0; +} + +TEST(trbtreeTest, rbtree_test1) { + SRBTree rt = tRBTreeCreate(tCmprInteger); + int a[] = {1, 3, 4, 2, 7, 5, 8}; + + for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { + SRBTreeNode *pNode = (SRBTreeNode *)taosMemoryMalloc(sizeof(*pNode) + sizeof(int)); + *(int *)pNode->payload = a[i]; + + tRBTreePut(&rt, pNode); + } + + SRBTreeIter rti = tRBTreeIterCreate(&rt); + SRBTreeNode *pNode = tRBTreeIterNext(&rti); + int la = 0; + while (pNode) { + GTEST_ASSERT_GT(*(int *)pNode->payload, la); + la = *(int *)pNode->payload; + pNode = tRBTreeIterNext(&rti); + } +} \ No newline at end of file -- GitLab