Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynset_tree_test.cc
Go to the documentation of this file.
1/*
2 This file is part of Aleph-w
3
4 Test suite for DynSetTree with all tree types:
5 - Binary Tree
6 - AVL Tree
7 - Red-Black Tree
8 - Splay Tree
9 - Treap
10 - Random Tree
11 - Treap with Rank
12*/
13
14#include <gtest/gtest.h>
15#include <tpl_dynSetTree.H>
16#include <vector>
17#include <string>
18
19using namespace std;
20using namespace Aleph;
21
22// Template test fixture that works with any DynSetTree type
23template <typename T>
24class DynSetTreeTest : public ::testing::Test
25{
26protected:
28};
29
30// Define all tree types to test
31using AllTreeTypes = ::testing::Types<
39>;
40
42
43// Test basic insertion and search
45{
46 // Insert 100 elements
47 for (int i = 0; i < 100; ++i)
48 {
49 auto * p = this->set.insert(i);
50 ASSERT_NE(p, nullptr) << "Failed to insert " << i;
51 EXPECT_EQ(*p, i);
52 }
53
54 EXPECT_EQ(this->set.size(), 100u);
55
56 // Test existing elements
57 for (int i = 0; i < 100; ++i)
58 {
59 EXPECT_NE(this->set.search(i), nullptr) << "Element " << i << " not found";
60 EXPECT_TRUE(this->set.contains(i));
61 }
62
63 // Test non-existing elements
64 EXPECT_EQ(this->set.search(-1), nullptr);
65 EXPECT_EQ(this->set.search(100), nullptr);
66 EXPECT_EQ(this->set.search(1000), nullptr);
67 EXPECT_FALSE(this->set.contains(-1));
68 EXPECT_FALSE(this->set.contains(100));
69}
70
71// Test removal of elements
73{
74 // Insert elements
75 for (int i = 0; i < 100; ++i)
76 this->set.insert(i);
77
78 EXPECT_EQ(this->set.size(), 100u);
79
80 // Remove even numbers
81 for (int i = 0; i < 100; i += 2)
82 this->set.remove(i);
83
84 EXPECT_EQ(this->set.size(), 50u);
85
86 // Verify removal
87 for (int i = 0; i < 100; ++i)
88 {
89 if (i % 2 == 0)
90 {
91 EXPECT_EQ(this->set.search(i), nullptr) << "Element " << i << " should be removed";
92 EXPECT_FALSE(this->set.contains(i));
93 }
94 else
95 {
96 EXPECT_NE(this->set.search(i), nullptr) << "Element " << i << " should exist";
97 EXPECT_TRUE(this->set.contains(i));
98 }
99 }
100}
101
102// Test copy constructor
104{
105 for (int i = 0; i < 50; ++i)
106 this->set.insert(i);
107
108 // Copy constructor
109 TypeParam set_copy(this->set);
110
111 EXPECT_EQ(set_copy.size(), 50u);
112
113 // Verify all elements were copied
114 for (int i = 0; i < 50; ++i)
115 EXPECT_TRUE(set_copy.contains(i));
116
117 // Verify independence - modify original
118 this->set.remove(25);
119 EXPECT_FALSE(this->set.contains(25));
120 EXPECT_TRUE(set_copy.contains(25)); // Copy should still have it
121}
122
123// Test assignment operator
125{
126 for (int i = 0; i < 50; ++i)
127 this->set.insert(i);
128
130 set_copy = this->set;
131
132 EXPECT_EQ(set_copy.size(), 50u);
133
134 for (int i = 0; i < 50; ++i)
135 EXPECT_TRUE(set_copy.contains(i));
136}
137
138// Test move constructor
140{
141 for (int i = 0; i < 50; ++i)
142 this->set.insert(i);
143
144 TypeParam set_moved(std::move(this->set));
145
146 EXPECT_EQ(set_moved.size(), 50u);
147 EXPECT_TRUE(this->set.is_empty());
148
149 for (int i = 0; i < 50; ++i)
150 EXPECT_TRUE(set_moved.contains(i));
151}
152
153// Test move assignment
155{
156 for (int i = 0; i < 50; ++i)
157 this->set.insert(i);
158
160 set_moved = std::move(this->set);
161
162 EXPECT_EQ(set_moved.size(), 50u);
163 EXPECT_TRUE(this->set.is_empty());
164
165 for (int i = 0; i < 50; ++i)
166 EXPECT_TRUE(set_moved.contains(i));
167}
168
169// Test iteration
171{
172 // Insert in random order
173 vector<int> values = {50, 25, 75, 10, 30, 60, 90, 5, 15, 27, 35};
174 for (int v : values)
175 this->set.insert(v);
176
177 // Iterate and collect - should be in sorted order
178 vector<int> collected;
179 for (auto it = this->set.get_it(); it.has_curr(); it.next_ne())
180 collected.push_back(it.get_curr());
181
182 // Verify sorted order
183 EXPECT_TRUE(std::is_sorted(collected.begin(), collected.end()));
184 EXPECT_EQ(collected.size(), values.size());
185}
186
187// Test empty operation
189{
190 for (int i = 0; i < 50; ++i)
191 this->set.insert(i);
192
193 EXPECT_FALSE(this->set.is_empty());
194 EXPECT_EQ(this->set.size(), 50u);
195
196 this->set.empty();
197
198 EXPECT_TRUE(this->set.is_empty());
199 EXPECT_EQ(this->set.size(), 0u);
200
201 // Verify all elements were removed
202 for (int i = 0; i < 50; ++i)
203 EXPECT_FALSE(this->set.contains(i));
204}
205
206// Test min and max
208{
209 for (int i : {50, 25, 75, 10, 90})
210 this->set.insert(i);
211
212 EXPECT_EQ(this->set.min(), 10);
213 EXPECT_EQ(this->set.max(), 90);
214
215 this->set.remove(10);
216 EXPECT_EQ(this->set.min(), 25);
217
218 this->set.remove(90);
219 EXPECT_EQ(this->set.max(), 75);
220}
221
222// Test insert duplicate (should return nullptr)
224{
225 auto * p1 = this->set.insert(42);
226 ASSERT_NE(p1, nullptr);
227 EXPECT_EQ(*p1, 42);
228 EXPECT_EQ(this->set.size(), 1u);
229
230 auto * p2 = this->set.insert(42);
231 EXPECT_EQ(p2, nullptr); // Duplicate rejected
232 EXPECT_EQ(this->set.size(), 1u);
233}
234
235// Test search_or_insert
237{
238 auto * p1 = this->set.search_or_insert(42);
239 ASSERT_NE(p1, nullptr);
240 EXPECT_EQ(*p1, 42);
241 EXPECT_EQ(this->set.size(), 1u);
242
243 auto * p2 = this->set.search_or_insert(42);
244 EXPECT_EQ(p1, p2); // Should return existing
245 EXPECT_EQ(this->set.size(), 1u);
246}
247
248// Test with large dataset
250{
251 const int N = 10000;
252
253 // Insert N elements
254 for (int i = 0; i < N; ++i)
255 this->set.insert(i);
256
257 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
258 EXPECT_EQ(this->set.min(), 0);
259 EXPECT_EQ(this->set.max(), N - 1);
260
261 // Verify some elements exist
262 for (int i = 0; i < N; i += 100)
263 EXPECT_TRUE(this->set.contains(i));
264
265 // Remove half the elements
266 for (int i = 0; i < N; i += 2)
267 this->set.remove(i);
268
269 EXPECT_EQ(this->set.size(), static_cast<size_t>(N / 2));
270
271 // Verify removal
272 for (int i = 0; i < N; ++i)
273 {
274 if (i % 2 == 0)
275 EXPECT_FALSE(this->set.contains(i));
276 else
277 EXPECT_TRUE(this->set.contains(i));
278 }
279}
280
281// Test swap operation
283{
285
286 for (int i : {1, 2, 3})
287 this->set.insert(i);
288 for (int i : {10, 20})
289 set2.insert(i);
290
291 this->set.swap(set2);
292
293 EXPECT_EQ(this->set.size(), 2u);
294 EXPECT_TRUE(this->set.contains(10));
295 EXPECT_TRUE(this->set.contains(20));
296
297 EXPECT_EQ(set2.size(), 3u);
298 EXPECT_TRUE(set2.contains(1));
299 EXPECT_TRUE(set2.contains(2));
300 EXPECT_TRUE(set2.contains(3));
301}
302
303// Test with string type (separate test)
305{
307 StringSet set;
308
309 vector<string> words = {"apple", "banana", "cherry", "date", "elderberry"};
310
311 for (const auto& word : words)
312 set.insert(word);
313
314 EXPECT_EQ(set.size(), 5u);
315
316 // Test search
317 for (const auto& word : words)
318 {
319 EXPECT_NE(set.search(word), nullptr);
320 EXPECT_TRUE(set.contains(word));
321 }
322
323 // Test iteration order (should be alphabetical)
325 for (auto it = set.get_it(); it.has_curr(); it.next_ne())
326 collected.push_back(it.get_curr());
327 EXPECT_TRUE(std::is_sorted(collected.begin(), collected.end()));
328}
329
330// Test with custom comparator (separate test)
332{
334 SetType set;
335
336 for (int i : {5, 2, 8, 1, 9})
337 set.insert(i);
338
339 // Verify descending order via iteration
340 vector<int> collected;
341 for (auto it = set.get_it(); it.has_curr(); it.next_ne())
342 collected.push_back(it.get_curr());
343
344 vector<int> expected = {9, 8, 5, 2, 1};
346}
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T remove()
Remove the first item of the list.
Definition htlist.H:1611
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Dynamic set implemented using binary search trees of type BinTree<Key>.
Dynamic set implemented using randomized binary search trees of type Rand_Tree<Key>.
Dynamic set implemented using Red-Black binary search trees of type Rb_Tree<Key> (bottom-up implement...
Dynamic set implemented using splay binary search trees of type Splay_Tree<Key>.
Dynamic set implemented using extended treap binary search trees with rank support of type Treap_Rk<K...
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
::testing::Types< DynSetBinTree< int >, DynSetAvlTree< int >, DynSetRbTree< int >, DynSetSplayTree< int >, DynSetTreap< int >, DynSetRandTree< int >, DynSetTreapRk< int > > AllTreeTypes
TYPED_TEST(DynSetTreeTest, InsertAndSearch)
TYPED_TEST_SUITE(DynSetTreeTest, AllTreeTypes)
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Dynamic set implementations based on balanced binary search trees.