tSimpleHashTests.cpp 2.2 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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"

C
Cary Xu 已提交
28 29 30 31
// int main(int argc, char **argv) {
//   testing::InitGoogleTest(&argc, argv);
//   return RUN_ALL_TESTS();
// }
C
Cary Xu 已提交
32 33 34

TEST(testCase, tSimpleHashTest) {
  SSHashObj *pHashObj =
C
Cary Xu 已提交
35
      tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
C
Cary Xu 已提交
36 37 38 39 40

  assert(pHashObj != nullptr);

  ASSERT_EQ(0, tSimpleHashGetSize(pHashObj));

C
Cary Xu 已提交
41 42 43
  size_t keyLen = sizeof(int64_t);
  size_t dataLen = sizeof(int64_t);

C
Cary Xu 已提交
44 45 46
  int64_t originKeySum = 0;
  for (int64_t i = 1; i <= 100; ++i) {
    originKeySum += i;
C
Cary Xu 已提交
47
    tSimpleHashPut(pHashObj, (const void *)&i, keyLen, (const void *)&i, dataLen);
C
Cary Xu 已提交
48 49 50 51
    ASSERT_EQ(i, tSimpleHashGetSize(pHashObj));
  }

  for (int64_t i = 1; i <= 100; ++i) {
C
Cary Xu 已提交
52
    void *data = tSimpleHashGet(pHashObj, (const void *)&i, keyLen);
C
Cary Xu 已提交
53 54 55 56 57 58 59 60
    ASSERT_EQ(i, *(int64_t *)data);
  }

  void   *data = NULL;
  int32_t iter = 0;
  int64_t keySum = 0;
  int64_t dataSum = 0;
  while ((data = tSimpleHashIterate(pHashObj, data, &iter))) {
C
Cary Xu 已提交
61
    void *key = tSimpleHashGetKey(data, NULL);
C
Cary Xu 已提交
62 63 64 65 66 67 68 69
    keySum += *(int64_t *)key;
    dataSum += *(int64_t *)data;
  }
  
  ASSERT_EQ(keySum, dataSum);
  ASSERT_EQ(keySum, originKeySum);

  for (int64_t i = 1; i <= 100; ++i) {
C
Cary Xu 已提交
70
    tSimpleHashRemove(pHashObj, (const void *)&i, keyLen);
C
Cary Xu 已提交
71 72
    ASSERT_EQ(100 - i, tSimpleHashGetSize(pHashObj));
  }
C
Cary Xu 已提交
73 74

  tSimpleHashCleanup(pHashObj);
C
Cary Xu 已提交
75 76 77
}

#pragma GCC diagnostic pop