Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynMapTree_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <tpl_array.H>
41#include <tpl_dynMapTree.H>
42
43#include <string>
44#include <type_traits>
45#include <utility>
46#include <vector>
47
48using namespace Aleph;
49
50namespace
51{
53{
55 m.insert(1, "one");
56 m.insert(2, "two");
57 m.insert(3, "three");
58 return m;
59}
60
61struct ReverseInt
62{
63 bool operator()(int a, int b) const noexcept { return a > b; }
64};
65} // namespace
66
67TEST(DynMapTree, DefaultConstruction)
68{
70 EXPECT_TRUE(m.is_empty());
71 EXPECT_EQ(m.size(), 0u);
72}
73
74TEST(DynMapTree, ConstructFromKeys)
75{
77 keys.append(3);
78 keys.append(1);
79 keys.append(2);
80
82 EXPECT_EQ(m.size(), 3u);
83 EXPECT_TRUE(m.has(1));
84 EXPECT_TRUE(m.has(2));
85 EXPECT_TRUE(m.has(3));
86
87 // Default-constructed values.
88 EXPECT_EQ(m[1], 0);
89 EXPECT_EQ(m[2], 0);
90 EXPECT_EQ(m[3], 0);
91}
92
93TEST(DynMapTree, InsertReturnsNullptrOnDuplicate)
94{
96 auto * p1 = m.insert(1, 10);
97 ASSERT_NE(p1, nullptr);
98 auto * p2 = m.insert(1, 20);
99 EXPECT_EQ(p2, nullptr);
100 EXPECT_EQ(m.size(), 1u);
101 EXPECT_EQ(m.find(1), 10);
102}
103
104TEST(DynMapTree, AppendAndPutAreAliases)
105{
107 EXPECT_NE(m.append(1, 10), nullptr);
108 EXPECT_EQ(m.put(2, 20)->second, 20);
109 EXPECT_EQ(m.size(), 2u);
110}
111
112TEST(DynMapTree, SearchHasContains)
113{
114 auto m = make_sample();
115
116 auto * p = m.search(2);
117 ASSERT_NE(p, nullptr);
118 EXPECT_EQ(p->second, "two");
119
120 EXPECT_TRUE(m.has(1));
121 EXPECT_TRUE(m.contains(3));
122 EXPECT_FALSE(m.has(99));
123}
124
125TEST(DynMapTree, FindThrowsOnMissing)
126{
128 EXPECT_THROW((void)m.find(1), std::domain_error);
129}
130
131TEST(DynMapTree, OperatorBracketsInsertsDefault)
132{
134 int & v = m[42];
135 EXPECT_EQ(v, 0);
136 v = 7;
137 EXPECT_EQ(m.find(42), 7);
138 EXPECT_EQ(m.size(), 1u);
139}
140
141TEST(DynMapTree, ConstOperatorBracketsThrowsOnMissing)
142{
144 const auto & cm = m;
145 EXPECT_THROW((void)cm[1], std::domain_error);
146}
147
148TEST(DynMapTree, RemoveReturnsData)
149{
150 auto m = make_sample();
151 EXPECT_EQ(m.size(), 3u);
152
153 const auto removed = m.remove(2);
154 EXPECT_EQ(removed, "two");
155 EXPECT_FALSE(m.has(2));
156 EXPECT_EQ(m.size(), 2u);
157}
158
159TEST(DynMapTree, RemoveKeyThrowsOnMissing)
160{
162 EXPECT_THROW(m.remove_key(1), std::domain_error);
163}
164
165TEST(DynMapTree, KeysAndValues)
166{
167 auto m = make_sample();
168
169 auto keys = m.keys();
170 auto values = m.values();
171
172 EXPECT_EQ(keys.size(), 3u);
173 EXPECT_EQ(values.size(), 3u);
174}
175
176TEST(DynMapTree, ValuesPtrAndItemsPtr)
177{
178 auto m = make_sample();
179
180 auto vp = m.values_ptr();
181 auto ip = m.items_ptr();
182
183 EXPECT_EQ(vp.size(), 3u);
184 EXPECT_EQ(ip.size(), 3u);
185
186 // Mutate through a value pointer.
187 *vp.get_first() = "ONE";
188 EXPECT_EQ(m.find(1), "ONE");
189}
190
191TEST(DynMapTree, GetDataAndGetKeyHelpers)
192{
193 auto m = make_sample();
194 auto * p = m.search(3);
195 ASSERT_NE(p, nullptr);
196
197 // get_data via key reference
198 std::string & data = DynMapTree<int, std::string>::get_data(p->first);
199 EXPECT_EQ(data, "three");
200 data = "THREE";
201 EXPECT_EQ(m.find(3), "THREE");
202
203 // get_key via data pointer
204 const int & key = DynMapTree<int, std::string>::get_key(&p->second);
205 EXPECT_EQ(key, 3);
206}
207
208TEST(DynMapTree, CustomComparatorAffectsTraversalOrder)
209{
211 m.insert(1, 1);
212 m.insert(2, 2);
213 m.insert(3, 3);
214
215 // The exact traversal order depends on the tree implementation, but for a
216 // strict reverse comparator, the first key visited should be the largest.
218 ASSERT_TRUE(it.has_curr());
219 EXPECT_EQ(it.get_curr().first, 3);
220}
221
222TEST(DynMapTree, TypeTraits)
223{
224 static_assert(std::is_same_v<DynMapTree<int, int>::Key_Type, int>);
225 static_assert(std::is_same_v<DynMapTree<int, int>::Value_Type, int>);
226}
227
228namespace
229{
230// A tree for the typed tests of the search by the key
231template <template <typename, class> class T>
232struct Tree_Of
233{
234 template <class K, class D>
235 using Map = DynMapTree<K, D, T>;
236};
237
238// The key i, of len characters (len >= 4), distinct for i < 9000: with a
239// prefix, len - 4 equal letters and four digits; without one, the digits
240// first. A std::string of 60 characters keeps them out of the node
241std::string string_key(const int i, const size_t len, const bool prefix)
242{
243 const std::string digits = std::to_string(1000 + i);
244 return prefix ? std::string(len - 4, 'k') + digits : digits + std::string(len - 4, 'x');
245}
246
247template <class Kind>
248class DynMapTreeSearchByKey : public ::testing::Test {};
249
250using Search_Trees =
251 ::testing::Types<Tree_Of<Avl_Tree>, Tree_Of<Avl_Tree_Rk>, Tree_Of<Rb_Tree>, Tree_Of<TdRbTree>,
252 Tree_Of<Treap>, Tree_Of<BinTree>, Tree_Of<Rand_Tree>, Tree_Of<Splay_Tree>>;
253TYPED_TEST_SUITE(DynMapTreeSearchByKey, Search_Trees);
254} // namespace
255
256// The search by a key that is not a scalar finds every pair, at any depth
257// of the tree, and no key between two of them, before them or after them,
258// with short and long string keys, with and without a long common prefix
259TYPED_TEST(DynMapTreeSearchByKey, FindsEveryStringKeyAndNoOther)
260{
261 using Map = typename TypeParam::template Map<std::string, int>;
262 constexpr int N = 300;
263 for (const size_t len : {size_t(7), size_t(16), size_t(60)})
264 for (const bool prefix : {false, true})
265 {
266 SCOPED_TRACE("length " + std::to_string(len) + (prefix ? ", with a prefix" : ""));
267 Map map;
268 for (int i = 0; i < N; ++i) // out of order, so that the tree has any shape
269 ASSERT_NE(map.insert(string_key(i * 37 % N, len, prefix), i * 37 % N), nullptr);
270 Array<std::string> absent = {std::string(), std::string(len + 1, '~')};
271 for (int i = 0; i < N; ++i)
272 {
273 const std::string key = string_key(i, len, prefix);
274 const auto * p = map.search(key);
275 ASSERT_NE(p, nullptr) << key;
276 EXPECT_EQ(p->first, key);
277 EXPECT_EQ(p->second, i);
278 absent.append(key + '!'); // after key and before the next one
279 }
280 for (const auto & key : absent)
281 EXPECT_EQ(map.search(key), nullptr) << key;
282 EXPECT_EQ(map.size(), static_cast<size_t>(N));
283 EXPECT_TRUE(map.verify());
284 }
285}
286
287int main(int argc, char ** argv)
288{
289 ::testing::InitGoogleTest(&argc, argv);
290 return RUN_ALL_TESTS();
291}
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
int main()
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Generic key-value map implemented on top of a binary search tree.
static const Key & get_key(Data *data_ptr) noexcept
typename Base::Iterator Iterator
static Data & get_data(Key &key) noexcept
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
constexpr bool contains(const Key &key) const noexcept
Alias for has().
Definition hashDry.H:769
void remove(const Key &key)
Remove a key from the hash table.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_odhash.H:657
Key * append(const Key &key)
Alias for insert() (copy version).
Definition hashDry.H:733
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Definition hashDry.H:959
DynList< Key > keys() const
Returns a list containing all keys in the table.
Definition hashDry.H:1247
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
constexpr bool has(const Key &key) const noexcept
Checks if a key exists in the table.
Definition hashDry.H:758
Key & find(const Key &key)
Finds a key and returns a reference to it.
Definition hashDry.H:782
#define TEST(name)
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
static void prefix(Node *root, DynList< Node * > &acc)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
int keys[]
Dynamic array container with automatic resizing.
Dynamic key-value map based on balanced binary search trees.
TYPED_TEST(DynMapTreeSearchByKey, FindsEveryStringKeyAndNoOther)