Aleph-w 3.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_dynMapTree.H>
41
42#include <string>
43#include <type_traits>
44#include <utility>
45#include <vector>
46
47using namespace Aleph;
48
49namespace
50{
52{
54 m.insert(1, "one");
55 m.insert(2, "two");
56 m.insert(3, "three");
57 return m;
58}
59
60struct ReverseInt
61{
62 bool operator()(int a, int b) const noexcept { return a > b; }
63};
64} // namespace
65
72
74{
75 DynList<int> keys;
76 keys.append(3);
77 keys.append(1);
78 keys.append(2);
79
81 EXPECT_EQ(m.size(), 3u);
82 EXPECT_TRUE(m.has(1));
83 EXPECT_TRUE(m.has(2));
84 EXPECT_TRUE(m.has(3));
85
86 // Default-constructed values.
87 EXPECT_EQ(m[1], 0);
88 EXPECT_EQ(m[2], 0);
89 EXPECT_EQ(m[3], 0);
90}
91
93{
95 auto * p1 = m.insert(1, 10);
96 ASSERT_NE(p1, nullptr);
97 auto * p2 = m.insert(1, 20);
98 EXPECT_EQ(p2, nullptr);
99 EXPECT_EQ(m.size(), 1u);
100 EXPECT_EQ(m.find(1), 10);
101}
102
104{
106 EXPECT_NE(m.append(1, 10), nullptr);
107 EXPECT_EQ(m.put(2, 20)->second, 20);
108 EXPECT_EQ(m.size(), 2u);
109}
110
112{
113 auto m = make_sample();
114
115 auto * p = m.search(2);
116 ASSERT_NE(p, nullptr);
117 EXPECT_EQ(p->second, "two");
118
119 EXPECT_TRUE(m.has(1));
120 EXPECT_TRUE(m.contains(3));
121 EXPECT_FALSE(m.has(99));
122}
123
125{
127 EXPECT_THROW((void)m.find(1), std::domain_error);
128}
129
131{
133 int & v = m[42];
134 EXPECT_EQ(v, 0);
135 v = 7;
136 EXPECT_EQ(m.find(42), 7);
137 EXPECT_EQ(m.size(), 1u);
138}
139
141{
143 const auto & cm = m;
144 EXPECT_THROW((void)cm[1], std::domain_error);
145}
146
148{
149 auto m = make_sample();
150 EXPECT_EQ(m.size(), 3u);
151
152 const auto removed = m.remove(2);
153 EXPECT_EQ(removed, "two");
154 EXPECT_FALSE(m.has(2));
155 EXPECT_EQ(m.size(), 2u);
156}
157
163
165{
166 auto m = make_sample();
167
168 auto keys = m.keys();
169 auto values = m.values();
170
171 EXPECT_EQ(keys.size(), 3u);
172 EXPECT_EQ(values.size(), 3u);
173}
174
176{
177 auto m = make_sample();
178
179 auto vp = m.values_ptr();
180 auto ip = m.items_ptr();
181
182 EXPECT_EQ(vp.size(), 3u);
183 EXPECT_EQ(ip.size(), 3u);
184
185 // Mutate through a value pointer.
186 *vp.get_first() = "ONE";
187 EXPECT_EQ(m.find(1), "ONE");
188}
189
191{
192 auto m = make_sample();
193 auto * p = m.search(3);
194 ASSERT_NE(p, nullptr);
195
196 // get_data via key reference
197 std::string & data = DynMapTree<int, std::string>::get_data(p->first);
198 EXPECT_EQ(data, "three");
199 data = "THREE";
200 EXPECT_EQ(m.find(3), "THREE");
201
202 // get_key via data pointer
203 const int & key = DynMapTree<int, std::string>::get_key(&p->second);
204 EXPECT_EQ(key, 3);
205}
206
208{
210 m.insert(1, 1);
211 m.insert(2, 2);
212 m.insert(3, 3);
213
214 // The exact traversal order depends on the tree implementation, but for a
215 // strict reverse comparator, the first key visited should be the largest.
217 ASSERT_TRUE(it.has_curr());
218 EXPECT_EQ(it.get_curr().first, 3);
219}
220
222{
223 static_assert(std::is_same_v<DynMapTree<int, int>::Key_Type, int>);
224 static_assert(std::is_same_v<DynMapTree<int, int>::Value_Type, int>);
225}
226
227int main(int argc, char ** argv)
228{
229 ::testing::InitGoogleTest(&argc, argv);
230 return RUN_ALL_TESTS();
231}
int main()
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
Generic key-value map implemented on top of a binary search tree.
static const Key & get_key(Data *data_ptr) noexcept
void remove_key(const Key &key)
typename Base::Iterator Iterator
Data remove(const Key &key)
Deletes the pair key,data
Pair * put(const Key &key, const Data &data)
Alias for insert().
Pair * append(const Key &key, const Data &data)
DynList< Pair * > items_ptr()
Collect pointers to all stored pairs.
Pair * search(const Key &key) const noexcept
Collect all keys.
bool has(const Key &key) const noexcept
DynList< Key > keys() const
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
bool contains(const Key &key) const noexcept
static Data & get_data(Key &key) noexcept
DynList< Data > values() const
Collect all values.
DynList< Data * > values_ptr()
Collect pointers to all values.
Data & find(const Key &key)
Find the value associated with key.
const size_t & size() const
Returns the cardinality of the set.
bool is_empty() const
returns true if the set is empty
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Dynamic key-value map based on balanced binary search trees.