nodesTest.cpp 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>

18 19 20 21 22
#include "querynodes.h"

using namespace std;

static EDealRes rewriterTest(SNode** pNode, void* pContext) {
X
Xiaoyu Wang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
  EDealRes* pRes = (EDealRes*)pContext;
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
    SOperatorNode* pOp = (SOperatorNode*)(*pNode);
    if (QUERY_NODE_VALUE != nodeType(pOp->pLeft) || QUERY_NODE_VALUE != nodeType(pOp->pRight)) {
      *pRes = DEAL_RES_ERROR;
    }
    SValueNode* pVal = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
    string tmp = to_string(stoi(((SValueNode*)(pOp->pLeft))->literal) + stoi(((SValueNode*)(pOp->pRight))->literal));
    pVal->literal = strdup(tmp.c_str());
    nodesDestroyNode(*pNode);
    *pNode = (SNode*)pVal;
  }
  return DEAL_RES_CONTINUE;
36 37
}

38
TEST(NodesTest, traverseTest) {
X
Xiaoyu Wang 已提交
39 40 41 42 43 44 45 46 47 48
  SNode*         pRoot = (SNode*)nodesMakeNode(QUERY_NODE_OPERATOR);
  SOperatorNode* pOp = (SOperatorNode*)pRoot;
  SOperatorNode* pLeft = (SOperatorNode*)nodesMakeNode(QUERY_NODE_OPERATOR);
  pLeft->pLeft = (SNode*)nodesMakeNode(QUERY_NODE_VALUE);
  ((SValueNode*)(pLeft->pLeft))->literal = strdup("10");
  pLeft->pRight = (SNode*)nodesMakeNode(QUERY_NODE_VALUE);
  ((SValueNode*)(pLeft->pRight))->literal = strdup("5");
  pOp->pLeft = (SNode*)pLeft;
  pOp->pRight = (SNode*)nodesMakeNode(QUERY_NODE_VALUE);
  ((SValueNode*)(pOp->pRight))->literal = strdup("3");
49

X
Xiaoyu Wang 已提交
50 51 52 53 54 55
  EXPECT_EQ(nodeType(pRoot), QUERY_NODE_OPERATOR);
  EDealRes res = DEAL_RES_CONTINUE;
  nodesRewriteExprPostOrder(&pRoot, rewriterTest, &res);
  EXPECT_EQ(res, DEAL_RES_CONTINUE);
  EXPECT_EQ(nodeType(pRoot), QUERY_NODE_VALUE);
  EXPECT_EQ(string(((SValueNode*)pRoot)->literal), "18");
56 57 58
}

int main(int argc, char* argv[]) {
X
Xiaoyu Wang 已提交
59 60
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
61
}