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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

TEST(testCase, tSimpleHashTest) {
  SSHashObj *pHashObj =
      tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), sizeof(int64_t), sizeof(int64_t));

  assert(pHashObj != nullptr);

  ASSERT_EQ(0, tSimpleHashGetSize(pHashObj));

  int64_t originKeySum = 0;
  for (int64_t i = 1; i <= 100; ++i) {
    originKeySum += i;
    tSimpleHashPut(pHashObj, (const void *)&i, (const void *)&i);
    ASSERT_EQ(i, tSimpleHashGetSize(pHashObj));
  }

  for (int64_t i = 1; i <= 100; ++i) {
    void *data = tSimpleHashGet(pHashObj, (const void *)&i);
    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))) {
    void *key = tSimpleHashGetKey(pHashObj, data, NULL);
    keySum += *(int64_t *)key;
    dataSum += *(int64_t *)data;
  }
  
  ASSERT_EQ(keySum, dataSum);
  ASSERT_EQ(keySum, originKeySum);

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

  tSimpleHashCleanup(pHashObj);
C
Cary Xu 已提交
73 74 75
}

#pragma GCC diagnostic pop