forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringDictionaryTest.cpp
More file actions
93 lines (82 loc) · 2.72 KB
/
StringDictionaryTest.cpp
File metadata and controls
93 lines (82 loc) · 2.72 KB
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
/*
* 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 "../StringDictionary/StringDictionary.h"
#include <limits>
#include <glog/logging.h>
#include <gtest/gtest.h>
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
TEST(StringDictionary, AddAndGet) {
StringDictionary string_dict(BASE_PATH, false);
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);
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);
auto id1 = string_dict.getOrAdd("");
auto id2 = string_dict.getOrAdd("");
ASSERT_EQ(id1, id2);
ASSERT_EQ(std::numeric_limits<int32_t>::min(), id1);
}
const int g_op_count{250000};
TEST(StringDictionary, ManyAddsAndGets) {
StringDictionary string_dict(BASE_PATH, false);
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, true);
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);
auto err = RUN_ALL_TESTS();
return err;
}