tSimpleHashTests.cpp 4.0 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
TEST(testCase, tSimpleHashTest_intKey) {
C
Cary Xu 已提交
34
  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
    ASSERT_EQ(i, *(int64_t *)data);
  }

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

C
Cary Xu 已提交
68 69 70 71
  ASSERT_EQ(keySum, dataSum);
  ASSERT_EQ(keySum, originKeySum);

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

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

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

TEST(testCase, tSimpleHashTest_binaryKey) {
  SSHashObj *pHashObj =
      tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));

  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);

  int64_t originDataSum = 0;
  SCombineKey combineKey = {0};
  for (int64_t i = 1; i <= 100; ++i) {
    combineKey.suid = i;
    combineKey.uid = i + 1;
    tSimpleHashPut(pHashObj, (const void *)&combineKey, keyLen, (const void *)&i, dataLen);
    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);

  tSimpleHashRemove(pHashObj, (const void *)&combineKey, keyLen);

  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;
    tSimpleHashRemove(pHashObj, (const void *)&combineKey, keyLen);
    ASSERT_EQ(99 - i, tSimpleHashGetSize(pHashObj));
  }

  tSimpleHashCleanup(pHashObj);
}


C
Cary Xu 已提交
144
#pragma GCC diagnostic pop