Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
skiplist_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
33
38#include <gtest/gtest.h>
39#include <ah-random.H>
40#include <climits>
41#include <vector>
42#include <algorithm>
43#include <string>
44#include <tpl_skipList.H>
45
46using namespace Aleph;
47
48// Specialize computeMaxKey for int
49template<>
51{
52 return INT_MAX;
53}
54
55// Specialize computeMaxKey for std::string keys
56template<>
58{
59 return std::string(256, '\xFF'); // Max string
60}
61
62namespace {
63
64// Fixed seed for reproducible tests
65constexpr unsigned long TEST_SEED = 42;
66
67// Helper to allocate a SkipList node with proper size
68template <class Key, class Type>
69typename SkipList<Key, Type>::Node* allocate_node(const Key& key, const Type& data, int level)
70{
71 // Node uses flexible array member, need extra space for forward pointers
72 size_t size = sizeof(typename SkipList<Key, Type>::Node) +
73 sizeof(typename SkipList<Key, Type>::Node*) * level;
74 void* mem = ::operator new(size);
75 return new (mem) typename SkipList<Key, Type>::Node(key, data, level);
76}
77
78template <class Key, class Type>
79void deallocate_node(typename SkipList<Key, Type>::Node* node)
80{
81 if (node)
82 {
83 node->~Node();
84 ::operator delete(node);
85 }
86}
87
88// Test fixture
89class SkipListTest : public ::testing::Test
90{
91protected:
92 using SL = SkipList<int, int>;
93
94 SL* skiplist = nullptr;
95 std::vector<SL::Node*> allocated_nodes;
96
97 void SetUp() override
98 {
99 skiplist = new SL(TEST_SEED, 0.5);
100 }
101
102 void TearDown() override
103 {
104 // Clean up all allocated nodes
105 for (auto* node : allocated_nodes)
106 deallocate_node<int, int>(node);
107 allocated_nodes.clear();
108
109 delete skiplist;
110 }
111
112 SL::Node* create_node(int key, int data = 0)
113 {
114 int level = skiplist->generateRandomLevel();
115 auto* node = allocate_node<int, int>(key, data, level);
116 allocated_nodes.push_back(node);
117 return node;
118 }
119
120 SL::Node* create_node_with_level(int key, int data, int level)
121 {
122 auto* node = allocate_node<int, int>(key, data, level);
123 allocated_nodes.push_back(node);
124 return node;
125 }
126};
127
128// ============================================================================
129// Construction Tests
130// ============================================================================
131
132TEST_F(SkipListTest, ConstructorDefault)
133{
134 EXPECT_TRUE(skiplist->checkSkipList());
135}
136
137TEST_F(SkipListTest, ConstructorWithProbability)
138{
139 SkipList<int, int> sl(TEST_SEED, 0.25);
140 EXPECT_TRUE(sl.checkSkipList());
141}
142
143TEST_F(SkipListTest, ConstructorWithDefaultSeed)
144{
145 SkipList<int, int> sl; // Without a seed: a SplitMix64 of its own
146 EXPECT_TRUE(sl.checkSkipList());
147}
148
149TEST_F(SkipListTest, SetSeed)
150{
151 skiplist->set_seed(123);
152 int level1 = skiplist->generateRandomLevel();
153 skiplist->set_seed(123);
154 int level2 = skiplist->generateRandomLevel();
155 EXPECT_EQ(level1, level2); // Same seed should give same result
156}
157
158TEST_F(SkipListTest, SetSeedSeedsTheGenerator)
159{
160 skiplist->set_seed(123);
161 EXPECT_EQ(skiplist->random_source(),
163}
164
165// ============================================================================
166// Insert Tests
167// ============================================================================
168
169TEST_F(SkipListTest, InsertSingleElement)
170{
171 auto* node = create_node(42, 100);
172 skiplist->insert(node);
173
174 EXPECT_TRUE(skiplist->checkSkipList());
175 EXPECT_EQ(skiplist->get_first(), node);
176}
177
178TEST_F(SkipListTest, InsertMultipleElementsAscending)
179{
180 for (int i = 1; i <= 10; ++i)
181 {
182 auto* node = create_node(i, i * 10);
183 skiplist->insert(node);
184 }
185
186 EXPECT_TRUE(skiplist->checkSkipList());
187
188 // Verify first element
189 auto* first = skiplist->get_first();
190 ASSERT_NE(first, nullptr);
191 EXPECT_EQ(first->get_key(), 1);
192}
193
194TEST_F(SkipListTest, InsertMultipleElementsDescending)
195{
196 for (int i = 10; i >= 1; --i)
197 {
198 auto* node = create_node(i, i * 10);
199 skiplist->insert(node);
200 }
201
202 EXPECT_TRUE(skiplist->checkSkipList());
203
204 // Verify first element is smallest
205 auto* first = skiplist->get_first();
206 ASSERT_NE(first, nullptr);
207 EXPECT_EQ(first->get_key(), 1);
208}
209
210TEST_F(SkipListTest, InsertMultipleElementsRandom)
211{
212 std::vector<int> keys = {5, 2, 8, 1, 9, 3, 7, 4, 6, 10};
213
214 for (int key : keys)
215 {
216 auto* node = create_node(key, key * 10);
217 skiplist->insert(node);
218 }
219
220 EXPECT_TRUE(skiplist->checkSkipList());
221
222 // Verify order by traversing
223 auto* current = skiplist->get_first();
224 int expected = 1;
225 while (current != nullptr && current->get_key() < INT_MAX)
226 {
227 EXPECT_EQ(current->get_key(), expected);
228 ++expected;
229 current = current->get_next();
230 }
231 EXPECT_EQ(expected, 11); // Should have seen 1-10
232}
233
234// ============================================================================
235// Search Tests
236// ============================================================================
237
238TEST_F(SkipListTest, SearchEmptyList)
239{
240 auto* result = skiplist->search(42);
241 EXPECT_EQ(result, nullptr);
242}
243
244TEST_F(SkipListTest, SearchExistingElement)
245{
246 auto* node = create_node(42, 100);
247 skiplist->insert(node);
248
249 auto* found = skiplist->search(42);
250 ASSERT_NE(found, nullptr);
251 EXPECT_EQ(found->get_key(), 42);
252 EXPECT_EQ(found->get_data(), 100);
253}
254
255TEST_F(SkipListTest, SearchNonExistingElement)
256{
257 auto* node = create_node(42, 100);
258 skiplist->insert(node);
259
260 EXPECT_EQ(skiplist->search(41), nullptr);
261 EXPECT_EQ(skiplist->search(43), nullptr);
262 EXPECT_EQ(skiplist->search(0), nullptr);
263 EXPECT_EQ(skiplist->search(100), nullptr);
264}
265
266TEST_F(SkipListTest, SearchMultipleElements)
267{
268 for (int i = 1; i <= 100; ++i)
269 {
270 auto* node = create_node(i * 2, i * 100); // Even keys only
271 skiplist->insert(node);
272 }
273
274 // Search for existing elements
275 for (int i = 1; i <= 100; ++i)
276 {
277 auto* found = skiplist->search(i * 2);
278 ASSERT_NE(found, nullptr) << "Key " << i * 2 << " not found";
279 EXPECT_EQ(found->get_key(), i * 2);
280 EXPECT_EQ(found->get_data(), i * 100);
281 }
282
283 // Search for non-existing elements (odd numbers)
284 for (int i = 1; i <= 100; ++i)
285 {
286 auto* found = skiplist->search(i * 2 - 1);
287 EXPECT_EQ(found, nullptr) << "Key " << i * 2 - 1 << " should not exist";
288 }
289}
290
291// ============================================================================
292// Remove Tests
293// ============================================================================
294
295TEST_F(SkipListTest, RemoveFromEmptyList)
296{
297 auto* result = skiplist->remove(42);
298 EXPECT_EQ(result, nullptr);
299}
300
301TEST_F(SkipListTest, RemoveSingleElement)
302{
303 auto* node = create_node(42, 100);
304 skiplist->insert(node);
305
306 auto* removed = skiplist->remove(42);
307 ASSERT_NE(removed, nullptr);
308 EXPECT_EQ(removed->get_key(), 42);
309 EXPECT_TRUE(skiplist->checkSkipList());
310
311 // Should not find it anymore
312 EXPECT_EQ(skiplist->search(42), nullptr);
313}
314
315TEST_F(SkipListTest, RemoveNonExistingElement)
316{
317 auto* node = create_node(42, 100);
318 skiplist->insert(node);
319
320 EXPECT_EQ(skiplist->remove(41), nullptr);
321 EXPECT_EQ(skiplist->remove(43), nullptr);
322
323 // Original should still exist
324 EXPECT_NE(skiplist->search(42), nullptr);
325}
326
327TEST_F(SkipListTest, RemoveFirstElement)
328{
329 for (int i = 1; i <= 5; ++i)
330 {
331 auto* node = create_node(i, i * 10);
332 skiplist->insert(node);
333 }
334
335 auto* removed = skiplist->remove(1);
336 ASSERT_NE(removed, nullptr);
337 EXPECT_EQ(removed->get_key(), 1);
338 EXPECT_TRUE(skiplist->checkSkipList());
339
340 // New first should be 2
341 auto* first = skiplist->get_first();
342 ASSERT_NE(first, nullptr);
343 EXPECT_EQ(first->get_key(), 2);
344}
345
346TEST_F(SkipListTest, RemoveLastElement)
347{
348 for (int i = 1; i <= 5; ++i)
349 {
350 auto* node = create_node(i, i * 10);
351 skiplist->insert(node);
352 }
353
354 auto* removed = skiplist->remove(5);
355 ASSERT_NE(removed, nullptr);
356 EXPECT_EQ(removed->get_key(), 5);
357 EXPECT_TRUE(skiplist->checkSkipList());
358
359 // 5 should not be found
360 EXPECT_EQ(skiplist->search(5), nullptr);
361}
362
363TEST_F(SkipListTest, RemoveMiddleElement)
364{
365 for (int i = 1; i <= 5; ++i)
366 {
367 auto* node = create_node(i, i * 10);
368 skiplist->insert(node);
369 }
370
371 auto* removed = skiplist->remove(3);
372 ASSERT_NE(removed, nullptr);
373 EXPECT_EQ(removed->get_key(), 3);
374 EXPECT_TRUE(skiplist->checkSkipList());
375
376 // 3 should not be found, but 2 and 4 should
377 EXPECT_EQ(skiplist->search(3), nullptr);
378 EXPECT_NE(skiplist->search(2), nullptr);
379 EXPECT_NE(skiplist->search(4), nullptr);
380}
381
382TEST_F(SkipListTest, RemoveAllElements)
383{
384 for (int i = 1; i <= 10; ++i)
385 {
386 auto* node = create_node(i, i * 10);
387 skiplist->insert(node);
388 }
389
390 // Remove in random order
391 std::vector<int> order = {5, 1, 9, 3, 7, 2, 8, 4, 6, 10};
392 for (int key : order)
393 {
394 auto* removed = skiplist->remove(key);
395 ASSERT_NE(removed, nullptr) << "Failed to remove key " << key;
396 EXPECT_TRUE(skiplist->checkSkipList());
397 }
398}
399
400// remove() lowered the level comparing with nullptr the slot `level` of the
401// header, one past the top one in use, while an empty level ends at the
402// sentinel: the level never went down, and with nodes of the maximum level
403// the read went past the forward pointers of the header (an assert in debug
404// builds)
405TEST_F(SkipListTest, RemovingNodesOfTheMaximumLevelBringsTheLevelToZero)
406{
407 SL list(TEST_SEED, 0.999999); // a level below the maximum is improbable
408 for (int i = 1; i <= 8; ++i)
409 (void) list.insert(create_node_with_level(i, 0, list.generateRandomLevel()));
410 const int max_level = SL::maxLevel;
411 ASSERT_EQ(list.get_level(), max_level);
412
413 for (int i = 1; i <= 8; ++i)
414 ASSERT_NE(list.remove(i), nullptr) << "Failed to remove key " << i;
415 EXPECT_EQ(list.get_level(), 0);
416 EXPECT_FALSE(SL::Iterator(list).has_curr());
417 EXPECT_EQ(list.search(1), nullptr);
418}
419
420TEST_F(SkipListTest, TheLevelIsThatOfTheTallestNodeLeft)
421{
422 const int levels[] = {3, 1, 5, 2, 4};
423 for (int i = 0; i < 5; ++i)
424 (void) skiplist->insert(create_node_with_level(i + 1, 0, levels[i]));
425 ASSERT_EQ(skiplist->get_level(), 5);
426
427 for (const int key : {3, 5, 1, 4, 2}) // levels 5, 4, 3, 2, 1
428 {
429 ASSERT_NE(skiplist->remove(key), nullptr);
430 int tallest = 0;
431 for (SL::Iterator it(*skiplist); it.has_curr(); it.next())
432 tallest = std::max(tallest, it.get_curr()->getLevel());
433 EXPECT_EQ(skiplist->get_level(), tallest) << "after removing " << key;
434 EXPECT_TRUE(skiplist->checkSkipList());
435 }
436 EXPECT_EQ(skiplist->get_level(), 0);
437}
438
439// The sentinel that ends every level has the key of computeMaxKey(): remove()
440// took it for a node with that key, unlinked it and returned it
441TEST_F(SkipListTest, RemovingTheKeyOfTheSentinelFindsNothing)
442{
443 for (int i = 1; i <= 3; ++i)
444 (void) skiplist->insert(create_node(i, i * 10));
445 ASSERT_EQ(skiplist->search(INT_MAX), nullptr);
446
447 EXPECT_EQ(skiplist->remove(INT_MAX), nullptr);
448
449 int count = 0;
450 for (SL::Iterator it(*skiplist); it.has_curr(); it.next())
451 EXPECT_EQ(it.get_curr()->get_key(), ++count);
452 EXPECT_EQ(count, 3);
453 EXPECT_TRUE(skiplist->checkSkipList());
454}
455
456// ============================================================================
457// Level Generation Tests
458// ============================================================================
459
460TEST_F(SkipListTest, GenerateRandomLevelBounds)
461{
462 constexpr int max_level = 32; // Same as SL::maxLevel
463 for (int i = 0; i < 1000; ++i)
464 {
465 int level = skiplist->generateRandomLevel();
466 EXPECT_GE(level, 1);
467 EXPECT_LE(level, max_level);
468 }
469}
470
471TEST_F(SkipListTest, GenerateRandomLevelDistribution)
472{
473 // With p=0.5, about 50% should be level 1, 25% level 2, etc.
474 constexpr int max_level = 32; // Same as SL::maxLevel
475 std::vector<int> counts(max_level + 1, 0);
476 const int trials = 10000;
477
478 for (int i = 0; i < trials; ++i)
479 {
480 int level = skiplist->generateRandomLevel();
481 counts[level]++;
482 }
483
484 // Level 1 should be most common (roughly 50%)
485 EXPECT_GT(counts[1], trials * 0.4); // At least 40%
486 EXPECT_LT(counts[1], trials * 0.6); // At most 60%
487
488 // Level 2 should be about half of level 1
489 double ratio = static_cast<double>(counts[2]) / counts[1];
490 EXPECT_GT(ratio, 0.35);
491 EXPECT_LT(ratio, 0.65);
492}
493
494// ============================================================================
495// Node Tests
496// ============================================================================
497
498TEST_F(SkipListTest, NodeGettersSetters)
499{
500 auto* node = create_node_with_level(42, 100, 5);
501
502 EXPECT_EQ(node->get_key(), 42);
503 EXPECT_EQ(node->get_data(), 100);
504 EXPECT_EQ(node->getLevel(), 5);
505
506 // Modify data
507 node->get_data() = 200;
508 EXPECT_EQ(node->get_data(), 200);
509}
510
511// ============================================================================
512// Integration Tests
513// ============================================================================
514
515TEST_F(SkipListTest, InterleavedInsertRemove)
516{
517 // Insert some elements
518 for (int i = 1; i <= 5; ++i)
519 {
520 auto* node = create_node(i, i);
521 skiplist->insert(node);
522 }
523
524 // Remove even, insert more
525 (void)skiplist->remove(2);
526 (void)skiplist->remove(4);
527
528 auto* node6 = create_node(6, 6);
529 auto* node7 = create_node(7, 7);
530 skiplist->insert(node6);
531 skiplist->insert(node7);
532
533 EXPECT_TRUE(skiplist->checkSkipList());
534
535 // Check expected state
536 EXPECT_NE(skiplist->search(1), nullptr);
537 EXPECT_EQ(skiplist->search(2), nullptr);
538 EXPECT_NE(skiplist->search(3), nullptr);
539 EXPECT_EQ(skiplist->search(4), nullptr);
540 EXPECT_NE(skiplist->search(5), nullptr);
541 EXPECT_NE(skiplist->search(6), nullptr);
542 EXPECT_NE(skiplist->search(7), nullptr);
543}
544
545TEST_F(SkipListTest, LargeScale)
546{
547 const int N = 1000;
548
549 // Insert N elements
550 for (int i = 0; i < N; ++i)
551 {
552 auto* node = create_node(i, i * 10);
553 skiplist->insert(node);
554 }
555
556 EXPECT_TRUE(skiplist->checkSkipList());
557
558 // Search all
559 for (int i = 0; i < N; ++i)
560 {
561 auto* found = skiplist->search(i);
562 ASSERT_NE(found, nullptr) << "Key " << i << " not found";
563 }
564
565 // Remove half (even numbers)
566 for (int i = 0; i < N; i += 2)
567 {
568 auto* removed = skiplist->remove(i);
569 ASSERT_NE(removed, nullptr) << "Failed to remove key " << i;
570 }
571
572 EXPECT_TRUE(skiplist->checkSkipList());
573
574 // Verify remaining
575 for (int i = 0; i < N; ++i)
576 {
577 auto* found = skiplist->search(i);
578 if (i % 2 == 0)
579 EXPECT_EQ(found, nullptr) << "Key " << i << " should have been removed";
580 else
581 EXPECT_NE(found, nullptr) << "Key " << i << " should still exist";
582 }
583}
584
585// ============================================================================
586// String Key Tests
587// ============================================================================
588
589class SkipListStringTest : public ::testing::Test
590{
591protected:
593
594 SL* skiplist = nullptr;
595 std::vector<SL::Node*> allocated_nodes;
596
597 void SetUp() override
598 {
599 skiplist = new SL(TEST_SEED, 0.5);
600 }
601
602 void TearDown() override
603 {
604 for (auto* node : allocated_nodes)
605 deallocate_node<std::string, int>(node);
606 allocated_nodes.clear();
607 delete skiplist;
608 }
609
610 SL::Node* create_node(const std::string& key, int data = 0)
611 {
612 int level = skiplist->generateRandomLevel();
613 auto* node = allocate_node<std::string, int>(key, data, level);
614 allocated_nodes.push_back(node);
615 return node;
616 }
617};
618
619TEST_F(SkipListStringTest, StringKeys)
620{
621 auto* node1 = create_node("apple", 1);
622 auto* node2 = create_node("banana", 2);
623 auto* node3 = create_node("cherry", 3);
624
625 skiplist->insert(node2);
626 skiplist->insert(node1);
627 skiplist->insert(node3);
628
629 EXPECT_TRUE(skiplist->checkSkipList());
630
631 // Should be in alphabetical order
632 auto* first = skiplist->get_first();
633 ASSERT_NE(first, nullptr);
634 EXPECT_EQ(first->get_key(), "apple");
635
636 // Search
637 auto* found = skiplist->search("banana");
638 ASSERT_NE(found, nullptr);
639 EXPECT_EQ(found->get_data(), 2);
640
641 EXPECT_EQ(skiplist->search("date"), nullptr);
642}
643
644// ============================================================================
645// Iterator Tests
646// ============================================================================
647
648TEST_F(SkipListTest, IteratorEmpty)
649{
650 SL::Iterator it(*skiplist);
651 EXPECT_FALSE(it.has_curr());
652 EXPECT_EQ(it.get_curr_ne(), nullptr);
653}
654
655TEST_F(SkipListTest, IteratorSingleElement)
656{
657 auto* node = create_node(42, 100);
658 skiplist->insert(node);
659
660 SL::Iterator it(*skiplist);
661 ASSERT_TRUE(it.has_curr());
662 EXPECT_EQ(it.get_curr(), node);
663 EXPECT_EQ(it.get_key(), 42);
664 EXPECT_EQ(it.get_data(), 100);
665 EXPECT_TRUE(it.is_last());
666
667 it.next();
668 EXPECT_FALSE(it.has_curr());
669}
670
671TEST_F(SkipListTest, IteratorMultipleElements)
672{
673 for (int i = 1; i <= 5; ++i)
674 {
675 auto* node = create_node(i, i * 10);
676 skiplist->insert(node);
677 }
678
679 // Count elements via iterator
680 int count = 0;
681 int expected_key = 1;
682 for (SL::Iterator it(*skiplist); it.has_curr(); it.next())
683 {
684 EXPECT_EQ(it.get_key(), expected_key);
685 EXPECT_EQ(it.get_data(), expected_key * 10);
686 ++expected_key;
687 ++count;
688 }
689 EXPECT_EQ(count, 5);
690}
691
692TEST_F(SkipListTest, IteratorRangeBasedFor)
693{
694 for (int i = 1; i <= 5; ++i)
695 {
696 auto* node = create_node(i, i * 10);
697 skiplist->insert(node);
698 }
699
700 // Use range-based for with begin()/end()
701 int count = 0;
702 int expected_key = 1;
703 for (auto it = skiplist->begin(); it != skiplist->end(); ++it)
704 {
705 EXPECT_EQ((*it)->get_key(), expected_key);
706 ++expected_key;
707 ++count;
708 }
709 EXPECT_EQ(count, 5);
710}
711
712TEST_F(SkipListTest, IteratorReset)
713{
714 for (int i = 1; i <= 3; ++i)
715 {
716 auto* node = create_node(i, i);
717 skiplist->insert(node);
718 }
719
720 SL::Iterator it(*skiplist);
721 EXPECT_EQ(it.get_key(), 1);
722
723 it.next();
724 EXPECT_EQ(it.get_key(), 2);
725
726 it.reset();
727 EXPECT_EQ(it.get_key(), 1);
728}
729
730TEST_F(SkipListTest, IteratorCopy)
731{
732 for (int i = 1; i <= 3; ++i)
733 {
734 auto* node = create_node(i, i);
735 skiplist->insert(node);
736 }
737
738 SL::Iterator it1(*skiplist);
739 it1.next(); // Now on key 2
740
741 SL::Iterator it2 = it1; // Copy
742 EXPECT_EQ(it2.get_key(), 2);
743
744 it1.next(); // it1 on key 3
745 EXPECT_EQ(it1.get_key(), 3);
746 EXPECT_EQ(it2.get_key(), 2); // it2 unchanged
747}
748
749TEST_F(SkipListTest, IteratorEquality)
750{
751 auto* node = create_node(42, 100);
752 skiplist->insert(node);
753
754 SL::Iterator it1(*skiplist);
755 SL::Iterator it2(*skiplist);
756
757 EXPECT_TRUE(it1 == it2);
758 EXPECT_FALSE(it1 != it2);
759
760 it1.next();
761 EXPECT_FALSE(it1 == it2);
762 EXPECT_TRUE(it1 != it2);
763}
764
765TEST_F(SkipListTest, IteratorOperators)
766{
767 for (int i = 1; i <= 3; ++i)
768 {
769 auto* node = create_node(i, i);
770 skiplist->insert(node);
771 }
772
773 SL::Iterator it(*skiplist);
774
775 // Pre-increment
776 EXPECT_EQ(it.get_key(), 1);
777 ++it;
778 EXPECT_EQ(it.get_key(), 2);
779
780 // Post-increment
781 SL::Iterator it2 = it++;
782 EXPECT_EQ(it2.get_key(), 2); // Old value
783 EXPECT_EQ(it.get_key(), 3); // New value
784}
785
786TEST_F(SkipListTest, IteratorThrowsOnOverflow)
787{
788 SL::Iterator it(*skiplist); // Empty list
789
790 EXPECT_THROW(it.get_curr(), std::overflow_error);
791 EXPECT_THROW(it.next(), std::overflow_error);
792
793 // No throw versions should not throw
794 EXPECT_NO_THROW(it.get_curr_ne());
795 EXPECT_NO_THROW(it.next_ne());
796}
797
798TEST_F(SkipListTest, IteratorLargeScale)
799{
800 const int N = 500;
801 for (int i = 0; i < N; ++i)
802 {
803 auto* node = create_node(i, i);
804 skiplist->insert(node);
805 }
806
807 int count = 0;
808 int expected = 0;
809 for (SL::Iterator it(*skiplist); it.has_curr(); it.next())
810 {
811 EXPECT_EQ(it.get_key(), expected);
812 ++expected;
813 ++count;
814 }
815 EXPECT_EQ(count, N);
816}
817
818} // anonymous namespace
819
820int main(int argc, char** argv)
821{
822 ::testing::InitGoogleTest(&argc, argv);
823 return RUN_ALL_TESTS();
824}
TEST_F(AdaptiveHuffmanWithoutMemory, ALostByteLeavesTheDecoderFailed)
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
int main()
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
The source of random numbers of a randomized structure, which draws from one of two generators.
Definition ah-random.H:883
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
const Key & get_key() const noexcept
int getLevel() const noexcept
const Type & get_data() const noexcept
Skip List - a probabilistic ordered data structure.
bool checkSkipList() const noexcept
Verify skip list invariants (for debugging).
Minimal std::expected-style result type for C++20.
#define N
Definition fib.C:294
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
Definition ah-dry.H:233
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
EepicNode * allocate_node(const string &str)
Definition ntreepic.C:555
int keys[]
Skip list: probabilistic sorted linked structure.