forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringDictionaryTest.cpp
137 lines (118 loc) · 4.36 KB
/
StringDictionaryTest.cpp
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
28
29
30
31
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
71
72
73
74
75
76
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
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "TestHelpers.h"
#include "../StringDictionary/StringDictionary.h"
#include <cstdlib>
#include <limits>
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
extern bool g_cache_string_hash;
TEST(StringDictionary, AddAndGet) {
StringDictionary string_dict(BASE_PATH, false, false, g_cache_string_hash);
auto id1 = string_dict.getOrAdd("foo bar");
auto id2 = string_dict.getOrAdd("foo bar");
ASSERT_EQ(id1, id2);
ASSERT_EQ(0, id1);
auto id3 = string_dict.getOrAdd("baz");
ASSERT_EQ(1, id3);
auto id4 = string_dict.getIdOfString("foo bar");
ASSERT_EQ(id1, id4);
ASSERT_EQ("foo bar", string_dict.getString(id4));
}
TEST(StringDictionary, Recover) {
StringDictionary string_dict(BASE_PATH, false, true, g_cache_string_hash);
auto id1 = string_dict.getOrAdd("baz");
ASSERT_EQ(1, id1);
auto id2 = string_dict.getOrAdd("baz");
ASSERT_EQ(1, id2);
auto id3 = string_dict.getOrAdd("foo bar");
ASSERT_EQ(0, id3);
auto id4 = string_dict.getOrAdd("fizzbuzz");
ASSERT_EQ(2, id4);
ASSERT_EQ("baz", string_dict.getString(id2));
ASSERT_EQ("foo bar", string_dict.getString(id3));
ASSERT_EQ("fizzbuzz", string_dict.getString(id4));
}
TEST(StringDictionary, HandleEmpty) {
StringDictionary string_dict(BASE_PATH, false, false, g_cache_string_hash);
auto id1 = string_dict.getOrAdd("");
auto id2 = string_dict.getOrAdd("");
ASSERT_EQ(id1, id2);
ASSERT_EQ(std::numeric_limits<int32_t>::min(), id1);
}
TEST(StringDictionary, RecoverZero) {
{
StringDictionary string_dict(BASE_PATH, false, false, g_cache_string_hash);
size_t num_strings = string_dict.storageEntryCount();
ASSERT_EQ(static_cast<size_t>(0), num_strings);
}
StringDictionary string_dict(BASE_PATH, false, true, g_cache_string_hash);
size_t num_strings = string_dict.storageEntryCount();
ASSERT_EQ(static_cast<size_t>(0), num_strings);
}
const int g_op_count{250000};
TEST(StringDictionary, ManyAddsAndGets) {
StringDictionary string_dict(BASE_PATH, false, false, g_cache_string_hash);
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
}
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
}
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(std::to_string(i), string_dict.getString(i));
}
}
TEST(StringDictionary, RecoverMany) {
StringDictionary string_dict(BASE_PATH, false, true, g_cache_string_hash);
size_t num_strings = string_dict.storageEntryCount();
ASSERT_EQ(static_cast<size_t>(g_op_count), num_strings);
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
}
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(std::to_string(i), string_dict.getString(i));
}
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
namespace po = boost::program_options;
po::options_description desc("Options");
// these two are here to allow passing correctly google testing parameters
desc.add_options()("gtest_list_tests", "list all test");
desc.add_options()("gtest_filter", "filters tests, use --help for details");
desc.add_options()(
"enable-string-dict-hash-cache",
po::value<bool>(&g_cache_string_hash)
->default_value(g_cache_string_hash)
->implicit_value(true),
"Cache string hash values in the string dictionary server during import.");
logger::LogOptions log_options(argv[0]);
log_options.severity_ = logger::Severity::FATAL;
log_options.set_options(); // update default values
desc.add(log_options.get_options());
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
return err;
}