14#include <gtest/gtest.h>
47 for (
int i = 0; i < 100; ++i)
49 auto * p = this->set.insert(i);
50 ASSERT_NE(p,
nullptr) <<
"Failed to insert " << i;
54 EXPECT_EQ(this->set.size(), 100u);
57 for (
int i = 0; i < 100; ++i)
59 EXPECT_NE(this->set.search(i),
nullptr) <<
"Element " << i <<
" not found";
60 EXPECT_TRUE(this->set.contains(i));
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));
75 for (
int i = 0; i < 100; ++i)
78 EXPECT_EQ(this->set.size(), 100u);
81 for (
int i = 0; i < 100; i += 2)
84 EXPECT_EQ(this->set.size(), 50u);
87 for (
int i = 0; i < 100; ++i)
91 EXPECT_EQ(this->set.search(i),
nullptr) <<
"Element " << i <<
" should be removed";
92 EXPECT_FALSE(this->set.contains(i));
96 EXPECT_NE(this->set.search(i),
nullptr) <<
"Element " << i <<
" should exist";
97 EXPECT_TRUE(this->set.contains(i));
105 for (
int i = 0; i < 50; ++i)
109 TypeParam set_copy(this->set);
111 EXPECT_EQ(set_copy.size(), 50u);
114 for (
int i = 0; i < 50; ++i)
115 EXPECT_TRUE(set_copy.contains(i));
118 this->set.remove(25);
119 EXPECT_FALSE(this->set.contains(25));
120 EXPECT_TRUE(set_copy.contains(25));
126 for (
int i = 0; i < 50; ++i)
130 set_copy = this->set;
132 EXPECT_EQ(set_copy.size(), 50u);
134 for (
int i = 0; i < 50; ++i)
135 EXPECT_TRUE(set_copy.contains(i));
141 for (
int i = 0; i < 50; ++i)
144 TypeParam set_moved(std::move(this->set));
146 EXPECT_EQ(set_moved.size(), 50u);
147 EXPECT_TRUE(this->set.is_empty());
149 for (
int i = 0; i < 50; ++i)
150 EXPECT_TRUE(set_moved.contains(i));
156 for (
int i = 0; i < 50; ++i)
160 set_moved = std::move(this->set);
162 EXPECT_EQ(set_moved.size(), 50u);
163 EXPECT_TRUE(this->set.is_empty());
165 for (
int i = 0; i < 50; ++i)
166 EXPECT_TRUE(set_moved.contains(i));
173 vector<int> values = {50, 25, 75, 10, 30, 60, 90, 5, 15, 27, 35};
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());
183 EXPECT_TRUE(std::is_sorted(collected.begin(), collected.end()));
184 EXPECT_EQ(collected.size(), values.size());
190 for (
int i = 0; i < 50; ++i)
193 EXPECT_FALSE(this->set.is_empty());
194 EXPECT_EQ(this->set.size(), 50u);
198 EXPECT_TRUE(this->set.is_empty());
199 EXPECT_EQ(this->set.size(), 0u);
202 for (
int i = 0; i < 50; ++i)
203 EXPECT_FALSE(this->set.contains(i));
209 for (
int i : {50, 25, 75, 10, 90})
212 EXPECT_EQ(this->set.min(), 10);
213 EXPECT_EQ(this->set.max(), 90);
215 this->set.remove(10);
216 EXPECT_EQ(this->set.min(), 25);
218 this->set.remove(90);
219 EXPECT_EQ(this->set.max(), 75);
225 auto * p1 = this->set.insert(42);
226 ASSERT_NE(p1,
nullptr);
228 EXPECT_EQ(this->set.size(), 1u);
230 auto * p2 = this->set.insert(42);
231 EXPECT_EQ(p2,
nullptr);
232 EXPECT_EQ(this->set.size(), 1u);
238 auto * p1 = this->set.search_or_insert(42);
239 ASSERT_NE(p1,
nullptr);
241 EXPECT_EQ(this->set.size(), 1u);
243 auto * p2 = this->set.search_or_insert(42);
245 EXPECT_EQ(this->set.size(), 1u);
254 for (
int i = 0; i <
N; ++i)
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);
262 for (
int i = 0; i <
N; i += 100)
263 EXPECT_TRUE(this->set.contains(i));
266 for (
int i = 0; i <
N; i += 2)
269 EXPECT_EQ(this->set.size(),
static_cast<size_t>(
N / 2));
272 for (
int i = 0; i <
N; ++i)
275 EXPECT_FALSE(this->set.contains(i));
277 EXPECT_TRUE(this->set.contains(i));
286 for (
int i : {1, 2, 3})
288 for (
int i : {10, 20})
291 this->set.swap(set2);
293 EXPECT_EQ(this->set.size(), 2u);
294 EXPECT_TRUE(this->set.contains(10));
295 EXPECT_TRUE(this->set.contains(20));
297 EXPECT_EQ(set2.size(), 3u);
298 EXPECT_TRUE(set2.contains(1));
299 EXPECT_TRUE(set2.contains(2));
300 EXPECT_TRUE(set2.contains(3));
304TEST(DynSetTreeStringTest, StringKeys)
309 vector<string> words = {
"apple",
"banana",
"cherry",
"date",
"elderberry"};
311 for (
const auto& word : words)
314 EXPECT_EQ(set.size(), 5u);
317 for (
const auto& word : words)
319 EXPECT_NE(set.search(word),
nullptr);
320 EXPECT_TRUE(set.contains(word));
324 vector<string> collected;
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()));
331TEST(DynSetTreeCustomCompare, DescendingOrder)
336 for (
int i : {5, 2, 8, 1, 9})
340 vector<int> collected;
341 for (
auto it = set.get_it(); it.has_curr(); it.next_ne())
342 collected.push_back(it.get_curr());
344 vector<int>
expected = {9, 8, 5, 2, 1};
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.
Number Theoretic Transform over Z / MOD Z.
Minimal std::expected-style result type for C++20.
::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)
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Dynamic set implementations based on balanced binary search trees.