OmniSciDB  04ee39c94c
StringDictionaryTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "../StringDictionary/StringDictionary.h"
18 #include "TestHelpers.h"
19 
20 #include <limits>
21 
22 #include <gtest/gtest.h>
23 
24 #ifndef BASE_PATH
25 #define BASE_PATH "./tmp"
26 #endif
27 
28 TEST(StringDictionary, AddAndGet) {
29  StringDictionary string_dict(BASE_PATH, false, false);
30  auto id1 = string_dict.getOrAdd("foo bar");
31  auto id2 = string_dict.getOrAdd("foo bar");
32  ASSERT_EQ(id1, id2);
33  ASSERT_EQ(0, id1);
34  auto id3 = string_dict.getOrAdd("baz");
35  ASSERT_EQ(1, id3);
36  auto id4 = string_dict.getIdOfString("foo bar");
37  ASSERT_EQ(id1, id4);
38  ASSERT_EQ("foo bar", string_dict.getString(id4));
39 }
40 
42  StringDictionary string_dict(BASE_PATH, false, true);
43  auto id1 = string_dict.getOrAdd("baz");
44  ASSERT_EQ(1, id1);
45  auto id2 = string_dict.getOrAdd("baz");
46  ASSERT_EQ(1, id2);
47  auto id3 = string_dict.getOrAdd("foo bar");
48  ASSERT_EQ(0, id3);
49  auto id4 = string_dict.getOrAdd("fizzbuzz");
50  ASSERT_EQ(2, id4);
51  ASSERT_EQ("baz", string_dict.getString(id2));
52  ASSERT_EQ("foo bar", string_dict.getString(id3));
53  ASSERT_EQ("fizzbuzz", string_dict.getString(id4));
54 }
55 
56 TEST(StringDictionary, HandleEmpty) {
57  StringDictionary string_dict(BASE_PATH, false, false);
58  auto id1 = string_dict.getOrAdd("");
59  auto id2 = string_dict.getOrAdd("");
60  ASSERT_EQ(id1, id2);
61  ASSERT_EQ(std::numeric_limits<int32_t>::min(), id1);
62 }
63 
64 const int g_op_count{250000};
65 
66 TEST(StringDictionary, ManyAddsAndGets) {
67  StringDictionary string_dict(BASE_PATH, false, false);
68  for (int i = 0; i < g_op_count; ++i) {
69  CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
70  }
71  for (int i = 0; i < g_op_count; ++i) {
72  CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
73  }
74  for (int i = 0; i < g_op_count; ++i) {
75  CHECK_EQ(std::to_string(i), string_dict.getString(i));
76  }
77 }
78 
79 TEST(StringDictionary, RecoverMany) {
80  StringDictionary string_dict(BASE_PATH, false, true);
81  for (int i = 0; i < g_op_count; ++i) {
82  CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
83  }
84  for (int i = 0; i < g_op_count; ++i) {
85  CHECK_EQ(std::to_string(i), string_dict.getString(i));
86  }
87 }
88 
89 int main(int argc, char** argv) {
91  testing::InitGoogleTest(&argc, argv);
92 
93  int err{0};
94  try {
95  err = RUN_ALL_TESTS();
96  } catch (const std::exception& e) {
97  LOG(ERROR) << e.what();
98  }
99  return err;
100 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
int32_t getIdOfString(const std::string &str) const
#define LOG(tag)
Definition: Logger.h:182
#define BASE_PATH
std::string getString(int32_t string_id) const
int32_t getOrAdd(const std::string &str) noexcept
std::string to_string(char const *&&v)
TEST(StringDictionary, AddAndGet)
const int g_op_count
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194
int main(int argc, char **argv)