tSimpleHashTests.cpp 3.9 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) {
H
Hongze Cheng 已提交
34
  SSHashObj *pHashObj = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
C
Cary Xu 已提交
35 36 37 38 39

  assert(pHashObj != nullptr);

  ASSERT_EQ(0, tSimpleHashGetSize(pHashObj));

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

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

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

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

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

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

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

78
TEST(testCase, tSimpleHashTest_binaryKey) {
H
Hongze Cheng 已提交
79
  SSHashObj *pHashObj = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
80 81 82 83 84 85 86 87 88 89 90 91 92

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

H
Hongze Cheng 已提交
93
  int64_t     originDataSum = 0;
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
  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 已提交
140
#pragma GCC diagnostic pop