forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_dbm_tree_impl_test.cc
158 lines (148 loc) · 6.01 KB
/
tkrzw_dbm_tree_impl_test.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*************************************************************************************************
* Tests for tkrzw_dbm_tree_impl.h
*
* Copyright 2020 Google LLC
* 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
* https://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 "tkrzw_sys_config.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "tkrzw_dbm.h"
#include "tkrzw_dbm_tree_impl.h"
#include "tkrzw_file.h"
#include "tkrzw_file_mmap.h"
#include "tkrzw_file_pos.h"
#include "tkrzw_file_std.h"
#include "tkrzw_file_util.h"
#include "tkrzw_lib_common.h"
#include "tkrzw_str_util.h"
using namespace testing;
// Main routine
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
TEST(DBMTreeImplTest, TreeRecord) {
tkrzw::TreeRecord* rec = tkrzw::CreateTreeRecord("key", "value");
EXPECT_EQ("key", rec->GetKey());
EXPECT_EQ("value", rec->GetValue());
EXPECT_EQ(10, rec->GetSerializedSize());
tkrzw::TreeRecord* mod_rec = tkrzw::ModifyTreeRecord(rec, "VALUE");
ASSERT_EQ(rec, mod_rec);
EXPECT_EQ("key", mod_rec->GetKey());
EXPECT_EQ("VALUE", mod_rec->GetValue());
EXPECT_EQ(10, mod_rec->GetSerializedSize());
const std::string value(1000, 'v');
rec = tkrzw::ModifyTreeRecord(rec, value);
EXPECT_EQ("key", rec->GetKey());
EXPECT_EQ(value, rec->GetValue());
EXPECT_EQ(1006, rec->GetSerializedSize());
tkrzw::TreeRecord* rec2 = tkrzw::CreateTreeRecord("j", "xyz");
EXPECT_FALSE(tkrzw::TreeRecordComparator(tkrzw::LexicalKeyComparator)(rec, rec2));
EXPECT_TRUE(tkrzw::TreeRecordComparator(tkrzw::LexicalKeyComparator)(rec2, rec));
EXPECT_FALSE(tkrzw::TreeRecordComparator(tkrzw::LexicalKeyComparator)(rec, rec));
EXPECT_FALSE(tkrzw::TreeRecordComparator(tkrzw::LexicalKeyComparator)(rec2, rec2));
tkrzw::FreeTreeRecord(rec2);
tkrzw::FreeTreeRecord(rec);
}
TEST(DBMTreeImplTest, TreeRecordSearch) {
std::vector<tkrzw::TreeRecord*> records;
for (int32_t i = 0; i < 10; i++) {
const std::string key = tkrzw::SPrintF("%03d", i);
const std::string value = tkrzw::SPrintF("%08d", i);
records.emplace_back(tkrzw::CreateTreeRecord(key, value));
}
tkrzw::TreeRecordComparator comp(tkrzw::LexicalKeyComparator);
{
tkrzw::TreeRecordOnStack stack("");
tkrzw::TreeRecord* record = stack.record;
auto it = std::lower_bound(records.begin(), records.end(), record, comp);
ASSERT_NE(records.end(), it);
EXPECT_EQ("000", (*it)->GetKey());
EXPECT_EQ("00000000", (*it)->GetValue());
}
for (int32_t i = 0; i < 10; i++) {
const std::string key = tkrzw::SPrintF("%03d", i);
const std::string value = tkrzw::SPrintF("%08d", i);
tkrzw::TreeRecordOnStack stack(key);
tkrzw::TreeRecord* record = stack.record;
auto it = std::lower_bound(records.begin(), records.end(), record, comp);
ASSERT_NE(records.end(), it);
EXPECT_EQ(key, (*it)->GetKey());
EXPECT_EQ(value, (*it)->GetValue());
}
{
std::string key(1024, '9');
tkrzw::TreeRecordOnStack stack(key);
tkrzw::TreeRecord* record = stack.record;
auto it = std::lower_bound(records.begin(), records.end(), record, comp);
EXPECT_EQ(records.end(), it);
}
tkrzw::FreeTreeRecords(&records);
}
TEST(DBMTreeImplTest, TreeLink) {
tkrzw::TreeLink* link = tkrzw::CreateTreeLink("key", 1);
EXPECT_EQ("key", link->GetKey());
EXPECT_EQ(1, link->child);
tkrzw::TreeLink* link2 = tkrzw::CreateTreeLink("j", 2);
EXPECT_FALSE(tkrzw::TreeLinkComparator(tkrzw::LexicalKeyComparator)(link, link2));
EXPECT_TRUE(tkrzw::TreeLinkComparator(tkrzw::LexicalKeyComparator)(link2, link));
EXPECT_FALSE(tkrzw::TreeLinkComparator(tkrzw::LexicalKeyComparator)(link, link));
EXPECT_FALSE(tkrzw::TreeLinkComparator(tkrzw::LexicalKeyComparator)(link2, link2));
EXPECT_EQ(10, link->GetSerializedSize(6));
EXPECT_EQ(8, link2->GetSerializedSize(6));
tkrzw::FreeTreeLink(link2);
tkrzw::FreeTreeLink(link);
}
TEST(DBMTreeImplTest, TreeLinkSearch) {
std::vector<tkrzw::TreeLink*> links;
for (int32_t i = 0; i < 10; i++) {
const std::string key = tkrzw::SPrintF("%03d", i);
links.emplace_back(tkrzw::CreateTreeLink(key, i));
}
tkrzw::TreeLinkComparator comp(tkrzw::LexicalKeyComparator);
{
tkrzw::TreeLinkOnStack stack("");
tkrzw::TreeLink* link = stack.link;
auto it = std::lower_bound(links.begin(), links.end(), link, comp);
ASSERT_NE(links.end(), it);
EXPECT_EQ("000", (*it)->GetKey());
EXPECT_EQ(0, (*it)->child);
EXPECT_EQ(10, (*it)->GetSerializedSize(6));
}
for (int32_t i = 0; i < 10; i++) {
const std::string key = tkrzw::SPrintF("%03d", i);
tkrzw::TreeLinkOnStack stack(key);
tkrzw::TreeLink* link = stack.link;
auto it = std::lower_bound(links.begin(), links.end(), link, comp);
ASSERT_NE(links.end(), it);
EXPECT_EQ(key, (*it)->GetKey());
EXPECT_EQ(i, (*it)->child);
EXPECT_EQ(10, (*it)->GetSerializedSize(6));
}
{
std::string key(1024, '9');
tkrzw::TreeLinkOnStack stack(key);
tkrzw::TreeLink* link = stack.link;
auto it = std::lower_bound(links.begin(), links.end(), link, comp);
EXPECT_EQ(links.end(), it);
}
links.emplace_back(tkrzw::CreateTreeLink(std::string(1000, 'z'), 999));
{
tkrzw::TreeLinkOnStack stack("z");
tkrzw::TreeLink* link = stack.link;
auto it = std::lower_bound(links.begin(), links.end(), link, comp);
ASSERT_NE(links.end(), it);
EXPECT_EQ(std::string(1000, 'z'), (*it)->GetKey());
EXPECT_EQ(999, (*it)->child);
EXPECT_EQ(1008, (*it)->GetSerializedSize(6));
}
tkrzw::FreeTreeLinks(&links);
}
// END OF FILE