38#include <gtest/gtest.h>
53class DynSkipListTest :
public ::testing::Test
63 static inline int alive = 0;
64 static inline int copies_left = -1;
67 Counted_Key(
const int v = 0) :
value(v) { ++alive; }
69 Counted_Key(
const Counted_Key & other) :
value(other.
value)
72 throw std::runtime_error(
"the copy of a key");
78 Counted_Key & operator = (
const Counted_Key &) =
default;
80 ~Counted_Key() { --alive; }
82 bool operator < (
const Counted_Key & other)
const {
return value < other.value; }
84 bool operator == (
const Counted_Key & other)
const {
return value == other.value; }
91TEST_F(DynSkipListTest, DefaultConstruction)
93 EXPECT_TRUE(sl.is_empty());
94 EXPECT_EQ(sl.size(), 0);
103 for (
int i = 1; i <= 5; ++i)
104 source.insert(Counted_Key(i));
105 const std::vector<Counted_Key>
keys = {5, 3, 1, 4, 2};
106 const int before = Counted_Key::alive;
109 for (
int k = 0;
k < 100; ++
k)
112 Counted_Key::copies_left =
k;
116 EXPECT_EQ(
copy.size(), source.size());
118 catch (
const std::runtime_error &)
122 Counted_Key::copies_left = -1;
123 EXPECT_EQ(Counted_Key::alive, before) <<
"the copy, at the copy " <<
k;
128 EXPECT_GE(failures, 5u);
130 for (
int k = 0;
k < 5; ++
k)
132 Counted_Key::copies_left =
k;
135 EXPECT_EQ(Counted_Key::alive, before) <<
"the range, at the copy " <<
k;
137 Counted_Key::copies_left =
k;
140 EXPECT_EQ(Counted_Key::alive, before) <<
"the initializer list, at the copy " <<
k;
142 Counted_Key::copies_left = -1;
145TEST_F(DynSkipListTest, InitializerListConstruction)
149 EXPECT_EQ(sl2.size(), 5);
150 EXPECT_TRUE(sl2.has(1));
151 EXPECT_TRUE(sl2.has(2));
152 EXPECT_TRUE(sl2.has(5));
153 EXPECT_TRUE(sl2.has(8));
154 EXPECT_TRUE(sl2.has(9));
157TEST_F(DynSkipListTest, ConstructorWithSeed)
162 EXPECT_EQ(sl2.size(), 2);
165TEST_F(DynSkipListTest, SetSeed)
168 EXPECT_EQ(sl.random_source(),
Random_Source(Random_Source::Engine::Mersenne_Twister, 123));
171TEST_F(DynSkipListTest, TheGeneratorDrawsTheLevels)
176 EXPECT_EQ(sl.random_source(), other.random_source());
177 for (
int i = 0; i < 50; ++i)
182 EXPECT_EQ(sl.random_source(), other.random_source());
183 EXPECT_FALSE(sl.random_source()
184 ==
Random_Source(Random_Source::Engine::Mersenne_Twister, 5));
187TEST_F(DynSkipListTest, CopyConstruction)
195 EXPECT_EQ(sl2.size(), 3);
196 EXPECT_TRUE(sl2.has(1));
197 EXPECT_TRUE(sl2.has(2));
198 EXPECT_TRUE(sl2.has(3));
202 EXPECT_FALSE(sl2.has(4));
205TEST_F(DynSkipListTest, MoveConstruction)
213 EXPECT_EQ(sl2.size(), 3);
214 EXPECT_TRUE(sl2.has(1));
215 EXPECT_TRUE(sl2.has(2));
216 EXPECT_TRUE(sl2.has(3));
219 EXPECT_TRUE(sl.is_empty());
226 bool descending =
false;
229 explicit Order(
const bool d) noexcept : descending(d) {}
230 Order(
const Order &) =
default;
231 Order(Order &&) =
default;
232 Order & operator = (
const Order &) =
delete;
233 Order & operator = (Order &&) =
default;
235 bool operator () (
const int a,
const int b)
const noexcept
237 return descending ? b < a : a < b;
246 src.get_compare() = Order(
true);
247 for (
int i = 0; i < 5; ++i)
251 EXPECT_TRUE(dst.get_compare().descending);
252 EXPECT_TRUE(src.get_compare().descending);
253 EXPECT_TRUE(src.is_empty());
256 ASSERT_EQ(
keys.size(), 5u);
257 for (
size_t i = 0; i <
keys.size(); ++i)
258 EXPECT_EQ(
keys(i), 4 -
static_cast<int>(i));
263 EXPECT_EQ(src.get_first(), 2);
266 other = std::move(dst);
267 EXPECT_EQ(other.size(), 5u);
268 EXPECT_EQ(other.get_first(), 4);
274 Move_Only_Less() =
default;
275 Move_Only_Less(
const Move_Only_Less &) =
delete;
276 Move_Only_Less & operator = (
const Move_Only_Less &) =
delete;
277 Move_Only_Less(Move_Only_Less &&) noexcept = default;
278 Move_Only_Less & operator = (Move_Only_Less &&) noexcept = default;
280 bool operator () (const
int a, const
int b) const noexcept {
return a < b; }
288 for (
const int i : {5, 1, 3})
292 EXPECT_EQ(dst.size(), 3u);
293 EXPECT_TRUE(src.is_empty());
294 EXPECT_EQ(dst.get_first(), 1);
295 EXPECT_NE(dst.search(3),
nullptr);
298 EXPECT_EQ(src.size(), 1u);
299 EXPECT_EQ(src.get_first(), 2);
302 other = std::move(dst);
303 EXPECT_EQ(other.size(), 3u);
310TEST_F(DynSkipListTest, InsertSingle)
312 int * result = sl.insert(42);
314 ASSERT_NE(result,
nullptr);
315 EXPECT_EQ(*result, 42);
316 EXPECT_EQ(sl.size(), 1);
317 EXPECT_TRUE(sl.has(42));
320TEST_F(DynSkipListTest, InsertDuplicate)
323 int * result = sl.insert(42);
325 EXPECT_EQ(result,
nullptr);
326 EXPECT_EQ(sl.size(), 1);
329TEST_F(DynSkipListTest, InsertMultiple)
331 for (
int i = 1; i <= 10; ++i)
334 EXPECT_EQ(sl.size(), 10);
336 for (
int i = 1; i <= 10; ++i)
337 EXPECT_TRUE(sl.has(i));
340TEST_F(DynSkipListTest, InsertDescending)
342 for (
int i = 10; i >= 1; --i)
345 EXPECT_EQ(sl.size(), 10);
346 EXPECT_EQ(sl.min(), 1);
347 EXPECT_EQ(sl.max(), 10);
350TEST_F(DynSkipListTest, InsertRvalue)
352 std::string str =
"hello";
355 sl_str.
insert(std::move(str));
357 EXPECT_EQ(sl_str.
size(), 1);
358 EXPECT_TRUE(sl_str.
has(
"hello"));
365TEST_F(DynSkipListTest, SearchEmpty)
367 EXPECT_EQ(sl.search(42),
nullptr);
370TEST_F(DynSkipListTest, SearchExisting)
374 int * result = sl.search(42);
375 ASSERT_NE(result,
nullptr);
376 EXPECT_EQ(*result, 42);
379TEST_F(DynSkipListTest, SearchNonExisting)
383 EXPECT_EQ(sl.search(41),
nullptr);
384 EXPECT_EQ(sl.search(43),
nullptr);
387TEST_F(DynSkipListTest, HasContainsExist)
391 EXPECT_TRUE(sl.has(42));
392 EXPECT_TRUE(sl.contains(42));
393 EXPECT_TRUE(sl.exist(42));
395 EXPECT_FALSE(sl.has(43));
396 EXPECT_FALSE(sl.contains(43));
397 EXPECT_FALSE(sl.exist(43));
400TEST_F(DynSkipListTest, Find)
404 EXPECT_EQ(sl.find(42), 42);
406 EXPECT_THROW((
void)sl.find(43), std::domain_error);
409TEST_F(DynSkipListTest, AppendOfARepeatedKeyReturnsTheKeyInTheSet)
413 const int & first = sl.append(1);
414 EXPECT_EQ(&sl.append(1), &first);
417 EXPECT_EQ(&sl.append(one), &first);
419 EXPECT_EQ(sl.size(), 1u);
420 EXPECT_EQ(sl.append(2), 2);
421 EXPECT_EQ(sl.size(), 2u);
424TEST_F(DynSkipListTest, FindOnConstantList)
429 const auto & csl = sl;
431 EXPECT_EQ(csl.find(42), 42);
432 EXPECT_THROW((
void) csl.find(43), std::domain_error);
439TEST_F(DynSkipListTest, RemoveExisting)
445 size_t removed = sl.remove(2);
447 EXPECT_EQ(removed, 1);
448 EXPECT_EQ(sl.size(), 2);
449 EXPECT_FALSE(sl.has(2));
450 EXPECT_TRUE(sl.has(1));
451 EXPECT_TRUE(sl.has(3));
454TEST_F(DynSkipListTest, RemoveNonExisting)
459 size_t removed = sl.remove(99);
461 EXPECT_EQ(removed, 0);
462 EXPECT_EQ(sl.size(), 2);
465TEST_F(DynSkipListTest, RemoveFirst)
473 EXPECT_EQ(sl.min(), 2);
476TEST_F(DynSkipListTest, RemoveLast)
484 EXPECT_EQ(sl.max(), 2);
487TEST_F(DynSkipListTest, RemoveAll)
489 for (
int i = 1; i <= 5; ++i)
492 for (
int i = 1; i <= 5; ++i)
495 EXPECT_TRUE(sl.is_empty());
498TEST_F(DynSkipListTest, Del)
502 int deleted = sl.del(42);
504 EXPECT_EQ(deleted, 42);
505 EXPECT_FALSE(sl.has(42));
507 EXPECT_THROW(sl.del(42), std::domain_error);
514TEST_F(DynSkipListTest, MinMax)
522 EXPECT_EQ(sl.min(), 1);
523 EXPECT_EQ(sl.get_first(), 1);
524 EXPECT_EQ(sl.max(), 9);
525 EXPECT_EQ(sl.get_last(), 9);
528TEST_F(DynSkipListTest, MinMaxEmpty)
530 EXPECT_THROW((
void)sl.min(), std::domain_error);
531 EXPECT_THROW((
void)sl.max(), std::domain_error);
538TEST_F(DynSkipListTest, IteratorEmpty)
541 EXPECT_FALSE(it.has_curr());
544TEST_F(DynSkipListTest, IteratorTraverse)
546 for (
int i = 1; i <= 5; ++i)
558TEST_F(DynSkipListTest, IteratorRangeBased)
564 std::vector<int> result;
565 for (
const auto & key : sl)
566 result.push_back(key);
568 EXPECT_EQ(result.size(), 3);
569 EXPECT_EQ(result[0], 1);
570 EXPECT_EQ(result[1], 2);
571 EXPECT_EQ(result[2], 3);
574TEST_F(DynSkipListTest, IteratorOperators)
580 auto it = sl.begin();
590 EXPECT_EQ(it, sl.end());
597TEST_F(DynSkipListTest, CopyAssignment)
607 EXPECT_EQ(sl2.
size(), 2);
608 EXPECT_TRUE(sl2.
has(1));
609 EXPECT_TRUE(sl2.
has(2));
610 EXPECT_FALSE(sl2.
has(99));
613TEST_F(DynSkipListTest, MoveAssignment)
623 EXPECT_EQ(sl2.
size(), 2);
624 EXPECT_TRUE(sl2.
has(1));
625 EXPECT_TRUE(sl.is_empty());
628TEST_F(DynSkipListTest, SelfAssignment)
635 EXPECT_EQ(sl.size(), 2);
642TEST_F(DynSkipListTest, Empty)
650 EXPECT_TRUE(sl.is_empty());
651 EXPECT_EQ(sl.size(), 0);
654TEST_F(DynSkipListTest, Swap)
666 EXPECT_EQ(sl.size(), 3);
667 EXPECT_TRUE(sl.has(10));
669 EXPECT_EQ(sl2.
size(), 2);
670 EXPECT_TRUE(sl2.
has(1));
677TEST_F(DynSkipListTest, ForEach)
684 sl.for_each([&sum](
int x) {
sum += x; });
689TEST_F(DynSkipListTest, All)
695 EXPECT_TRUE(sl.all([](
int x) { return x % 2 == 0; }));
698 EXPECT_FALSE(sl.all([](
int x) { return x % 2 == 0; }));
701TEST_F(DynSkipListTest, Exists)
707 EXPECT_TRUE(sl.exists([](
int x) { return x > 2; }));
708 EXPECT_FALSE(sl.exists([](
int x) { return x > 10; }));
711TEST_F(DynSkipListTest, Traverse)
734TEST_F(DynSkipListTest, LargeScale)
738 for (
int i = 0; i <
N; ++i)
741 EXPECT_EQ(sl.size(),
N);
743 for (
int i = 0; i <
N; ++i)
744 EXPECT_TRUE(sl.has(i));
746 for (
int i = 0; i <
N; i += 2)
749 EXPECT_EQ(sl.size(),
N / 2);
751 for (
int i = 0; i <
N; ++i)
754 EXPECT_FALSE(sl.has(i));
756 EXPECT_TRUE(sl.has(i));
764TEST(DynSkipListStringTest, StringKeys)
772 EXPECT_EQ(sl.
size(), 3);
773 EXPECT_EQ(sl.
min(),
"apple");
774 EXPECT_EQ(sl.
max(),
"cherry");
776 EXPECT_TRUE(sl.
has(
"banana"));
777 EXPECT_FALSE(sl.
has(
"date"));
784TEST_F(DynSkipListTest, SearchOrInsertNew)
788 ASSERT_NE(result,
nullptr);
789 EXPECT_EQ(*result, 42);
790 EXPECT_EQ(sl.
size(), 1);
793TEST_F(DynSkipListTest, SearchOrInsertExisting)
799 ASSERT_NE(result,
nullptr);
800 EXPECT_EQ(*result, 42);
801 EXPECT_EQ(sl.
size(), 1);
808TEST(DynSkipListProbability, MustBeBetweenZeroAndOne)
814 EXPECT_THROW(
DynSkipList<int>(1, std::numeric_limits<double>::quiet_NaN()), std::domain_error);
823 const double not_a_number = std::numeric_limits<double>::quiet_NaN();
834 ::testing::InitGoogleTest(&argc, argv);
835 return RUN_ALL_TESTS();
TEST_F(AdaptiveHuffmanWithoutMemory, ALostByteLeavesTheDecoderFailed)
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
bool operator<(const Time &l, const Time &r)
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
Forward iterator for DynSkipList.
bool has_curr() const noexcept
Dynamic ordered set implemented with a Skip List.
Key * search_or_insert(const Key &key)
Search and insert if not found.
const Key & min() const
Return the minimum element (first in sorted order)
Key * insert(const Key &key)
Insert an element into the set.
size_t size() const noexcept
Return the number of elements in the set.
bool has(const Key &key) const noexcept
Return true if the key exists in the set.
const Key & max() const
Return the maximum element (last in sorted order)
Number Theoretic Transform over Z / MOD Z.
The source of random numbers of a randomized structure, which draws from one of two generators.
Minimal std::expected-style result type for C++20.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
bool completed() const noexcept
Return true if all underlying iterators are finished.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Dynamic array container with automatic resizing.
Dynamic ordered set implemented with a Skip List.