Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynsettree.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 <algorithm>
39#include <random>
40#include <set>
41#include <stdexcept>
42#include <vector>
43#include <string>
44#include <gtest/gtest.h>
45
46#include <tpl_dynSetTree.H>
47
48using namespace std;
49using namespace Aleph;
50
51// ============================================================================
52// Typed Tests - Run same tests against ALL tree types
53// ============================================================================
54
55template <typename T>
56class DynSetTreeTypedTest : public ::testing::Test
57{
58protected:
60};
61
62using AllTreeTypes = ::testing::Types<
70>;
71
73
75{
76 EXPECT_TRUE(this->set.is_empty());
77 EXPECT_EQ(this->set.size(), 0u);
78 EXPECT_THROW(this->set.min(), domain_error);
79 EXPECT_THROW(this->set.max(), domain_error);
80 EXPECT_THROW(this->set.get_root(), domain_error);
81 EXPECT_FALSE(this->set.contains(42));
82 EXPECT_EQ(this->set.search(42), nullptr);
83}
84
86{
87 for (int i : {5, 3, 7, 1, 9, 2, 8})
88 {
89 auto * p = this->set.insert(i);
90 ASSERT_NE(p, nullptr);
91 EXPECT_EQ(*p, i);
92 }
93
94 EXPECT_EQ(this->set.size(), 7u);
95 EXPECT_EQ(this->set.min(), 1);
96 EXPECT_EQ(this->set.max(), 9);
97
98 for (int i : {1, 2, 3, 5, 7, 8, 9})
99 EXPECT_TRUE(this->set.contains(i));
100
101 EXPECT_FALSE(this->set.contains(4));
102 EXPECT_FALSE(this->set.contains(6));
103}
104
106{
107 auto * p1 = this->set.insert(42);
108 ASSERT_NE(p1, nullptr);
109 EXPECT_EQ(this->set.size(), 1u);
110
111 auto * p2 = this->set.insert(42);
112 EXPECT_EQ(p2, nullptr);
113 EXPECT_EQ(this->set.size(), 1u);
114}
115
117{
118 for (int i : {5, 3, 7, 1, 9})
119 this->set.insert(i);
120
121 EXPECT_EQ(this->set.size(), 5u);
122
123 this->set.remove(3);
124 EXPECT_EQ(this->set.size(), 4u);
125 EXPECT_FALSE(this->set.contains(3));
126 EXPECT_TRUE(this->set.contains(5));
127}
128
130{
131 auto * p1 = this->set.search_or_insert(42);
132 ASSERT_NE(p1, nullptr);
133 EXPECT_EQ(*p1, 42);
134 EXPECT_EQ(this->set.size(), 1u);
135
136 auto * p2 = this->set.search_or_insert(42);
137 EXPECT_EQ(p1, p2);
138 EXPECT_EQ(this->set.size(), 1u);
139}
140
142{
143 auto [p1, found1] = this->set.contains_or_insert(42);
144 ASSERT_NE(p1, nullptr);
146 EXPECT_EQ(this->set.size(), 1u);
147
148 auto [p2, found2] = this->set.contains_or_insert(42);
150 EXPECT_EQ(this->set.size(), 1u);
151}
152
154{
155 this->set.insert(42);
156
157 const auto & key = this->set.find(42);
158 EXPECT_EQ(key, 42);
159
160 EXPECT_THROW(this->set.find(99), domain_error);
161
162 int removed = this->set.del(42);
163 EXPECT_EQ(removed, 42);
164 EXPECT_TRUE(this->set.is_empty());
165
166 EXPECT_THROW(this->set.del(42), domain_error);
167}
168
170{
171 for (int i : {1, 2, 3, 4, 5})
172 this->set.insert(i);
173
174 TypeParam copy(this->set);
175
176 EXPECT_EQ(copy.size(), 5u);
177 for (int i : {1, 2, 3, 4, 5})
178 EXPECT_TRUE(copy.contains(i));
179
180 // Verify independence
181 this->set.remove(3);
182 EXPECT_FALSE(this->set.contains(3));
183 EXPECT_TRUE(copy.contains(3));
184}
185
187{
188 for (int i : {1, 2, 3, 4, 5})
189 this->set.insert(i);
190
191 TypeParam moved(std::move(this->set));
192
193 EXPECT_EQ(moved.size(), 5u);
194 for (int i : {1, 2, 3, 4, 5})
195 EXPECT_TRUE(moved.contains(i));
196
197 EXPECT_TRUE(this->set.is_empty());
198}
199
201{
203
204 for (int i : {1, 2, 3})
205 this->set.insert(i);
206 for (int i : {10, 20})
207 set2.insert(i);
208
209 this->set.swap(set2);
210
211 EXPECT_EQ(this->set.size(), 2u);
212 EXPECT_TRUE(this->set.contains(10));
213 EXPECT_EQ(set2.size(), 3u);
214 EXPECT_TRUE(set2.contains(1));
215}
216
218{
219 for (int i = 0; i < 50; ++i)
220 this->set.insert(i);
221
222 EXPECT_EQ(this->set.size(), 50u);
223
224 this->set.empty();
225
226 EXPECT_TRUE(this->set.is_empty());
227 EXPECT_EQ(this->set.size(), 0u);
228}
229
231{
232 for (int i : {5, 3, 7, 1, 9})
233 this->set.insert(i);
234
235 vector<int> keys;
236 for (typename TypeParam::Iterator it(this->set); it.has_curr(); it.next_ne())
237 keys.push_back(it.get_curr());
238
239 vector<int> expected = {1, 3, 5, 7, 9};
240 EXPECT_EQ(keys, expected);
241}
242
244{
245 for (int i : {5, 3, 7, 1, 9})
246 this->set.insert(i);
247
248 vector<int> keys;
249 this->set.for_each_inorder([&keys](const int & k) { keys.push_back(k); });
250
251 vector<int> expected = {1, 3, 5, 7, 9};
252 EXPECT_EQ(keys, expected);
253}
254
256{
257 for (int i : {1, 2, 3, 4, 5})
258 this->set.insert(i);
259
260 int sum = 0;
261 bool completed = this->set.traverse([&sum](const int & k) {
262 sum += k;
263 return true;
264 });
265
267 EXPECT_EQ(sum, 15);
268}
269
271{
272 for (int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
273 this->set.insert(i);
274
275 EXPECT_TRUE(this->set.verify());
276}
277
279{
280 // Insert 500 elements
281 for (int i = 0; i < 500; ++i)
282 this->set.insert(i);
283
284 EXPECT_EQ(this->set.size(), 500u);
285 EXPECT_EQ(this->set.min(), 0);
286 EXPECT_EQ(this->set.max(), 499);
287
288 // Remove half
289 for (int i = 0; i < 500; i += 2)
290 this->set.remove(i);
291
292 EXPECT_EQ(this->set.size(), 250u);
293
294 // Verify correct ones remain
295 for (int i = 1; i < 500; i += 2)
296 EXPECT_TRUE(this->set.contains(i));
297}
298
299// ============================================================================
300// Basic Operations Tests (applicable to all tree types)
301// ============================================================================
302
303#if 0
304
306{
308
309 EXPECT_TRUE(set.is_empty());
310 EXPECT_EQ(set.size(), 0u);
311 EXPECT_THROW(set.min(), domain_error);
312 EXPECT_THROW(set.max(), domain_error);
313 EXPECT_THROW(set.get_root(), domain_error);
314 EXPECT_THROW(set.get_item(), domain_error);
315 EXPECT_FALSE(set.contains(42));
316 EXPECT_EQ(set.search(42), nullptr);
317}
318
320{
322
323 auto * p = set.insert(42);
324 ASSERT_NE(p, nullptr);
325 EXPECT_EQ(*p, 42);
326 EXPECT_FALSE(set.is_empty());
327 EXPECT_EQ(set.size(), 1u);
328 EXPECT_TRUE(set.contains(42));
329 EXPECT_EQ(set.min(), 42);
330 EXPECT_EQ(set.max(), 42);
331 EXPECT_EQ(set.get_root(), 42);
332}
333
335{
337
338 for (int i : {5, 3, 7, 1, 9, 2, 8})
339 {
340 auto * p = set.insert(i);
341 ASSERT_NE(p, nullptr);
342 EXPECT_EQ(*p, i);
343 }
344
345 EXPECT_EQ(set.size(), 7u);
346 EXPECT_EQ(set.min(), 1);
347 EXPECT_EQ(set.max(), 9);
348
349 for (int i : {1, 2, 3, 5, 7, 8, 9})
350 EXPECT_TRUE(set.contains(i));
351
352 EXPECT_FALSE(set.contains(4));
353 EXPECT_FALSE(set.contains(6));
354}
355
357{
359
360 auto * p1 = set.insert(42);
361 ASSERT_NE(p1, nullptr);
362 EXPECT_EQ(*p1, 42);
363 EXPECT_EQ(set.size(), 1u);
364
365 auto * p2 = set.insert(42);
366 EXPECT_EQ(p2, nullptr); // Duplicate rejected
367 EXPECT_EQ(set.size(), 1u); // Size unchanged
368}
369
371{
373
374 auto * p1 = set.insert_dup(42);
375 ASSERT_NE(p1, nullptr);
376 EXPECT_EQ(*p1, 42);
377
378 auto * p2 = set.insert_dup(42);
379 ASSERT_NE(p2, nullptr);
380 EXPECT_EQ(*p2, 42);
381
382 EXPECT_EQ(set.size(), 2u);
383}
384
386{
388
389 set.insert(10);
390 set.insert(20);
391 set.insert(30);
392
393 auto * p = set.search(20);
394 ASSERT_NE(p, nullptr);
395 EXPECT_EQ(*p, 20);
396}
397
399{
401
402 set.insert(10);
403 set.insert(30);
404
405 EXPECT_EQ(set.search(20), nullptr);
406}
407
409{
411
412 // First call: inserts
413 auto * p1 = set.search_or_insert(42);
414 ASSERT_NE(p1, nullptr);
415 EXPECT_EQ(*p1, 42);
416 EXPECT_EQ(set.size(), 1u);
417
418 // Second call: returns existing
419 auto * p2 = set.search_or_insert(42);
420 ASSERT_NE(p2, nullptr);
421 EXPECT_EQ(*p2, 42);
422 EXPECT_EQ(set.size(), 1u); // Size unchanged
423 EXPECT_EQ(p1, p2); // Same pointer
424}
425
427{
429
430 // First call: inserts, returns false
431 auto [p1, found1] = set.contains_or_insert(42);
432 ASSERT_NE(p1, nullptr);
433 EXPECT_EQ(*p1, 42);
435 EXPECT_EQ(set.size(), 1u);
436
437 // Second call: finds, returns true
438 auto [p2, found2] = set.contains_or_insert(42);
439 ASSERT_NE(p2, nullptr);
440 EXPECT_EQ(*p2, 42);
442 EXPECT_EQ(set.size(), 1u);
443}
444
446{
448
449 set.insert(42);
450
451 const auto & key = set.find(42);
452 EXPECT_EQ(key, 42);
453}
454
456{
458
459 set.insert(10);
460
461 EXPECT_THROW(set.find(42), domain_error);
462}
463
464// ============================================================================
465// Remove Tests
466// ============================================================================
467
469{
471
472 set.insert(10);
473 set.insert(20);
474 set.insert(30);
475
476 size_t new_size = set.remove(20);
477 EXPECT_EQ(new_size, 2u);
478 EXPECT_EQ(set.size(), 2u);
479 EXPECT_FALSE(set.contains(20));
480 EXPECT_TRUE(set.contains(10));
481 EXPECT_TRUE(set.contains(30));
482}
483
485{
487
488 set.insert(10);
489 set.insert(30);
490
491 size_t new_size = set.remove(20);
492 EXPECT_EQ(new_size, 2u);
493 EXPECT_EQ(set.size(), 2u);
494}
495
497{
499
500 size_t new_size = set.remove(42);
501 EXPECT_EQ(new_size, 0u);
502 EXPECT_TRUE(set.is_empty());
503}
504
506{
508
509 set.insert(42);
510
511 int removed = set.del(42);
512 EXPECT_EQ(removed, 42);
513 EXPECT_FALSE(set.contains(42));
514 EXPECT_EQ(set.size(), 0u);
515}
516
518{
520
521 set.insert(10);
522
523 EXPECT_THROW(set.del(42), domain_error);
524}
525
527{
529
530 for (int i = 0; i < 10; ++i)
531 set.insert(i);
532
533 EXPECT_EQ(set.size(), 10u);
534
535 for (int i = 0; i < 10; ++i)
536 set.remove(i);
537
538 EXPECT_TRUE(set.is_empty());
539 EXPECT_EQ(set.size(), 0u);
540}
541
543{
545
546 for (int i = 0; i < 100; ++i)
547 set.insert(i);
548
549 EXPECT_EQ(set.size(), 100u);
550
551 set.empty();
552
553 EXPECT_TRUE(set.is_empty());
554 EXPECT_EQ(set.size(), 0u);
555}
556
557// ============================================================================
558// Copy/Move Semantics Tests
559// ============================================================================
560
562{
564
565 for (int i : {1, 2, 3, 4, 5})
566 set1.insert(i);
567
569
570 EXPECT_EQ(set2.size(), 5u);
571 for (int i : {1, 2, 3, 4, 5})
572 EXPECT_TRUE(set2.contains(i));
573
574 // Verify independence
575 set1.remove(3);
576 EXPECT_FALSE(set1.contains(3));
577 EXPECT_TRUE(set2.contains(3));
578}
579
581{
583
584 for (int i : {1, 2, 3})
585 set1.insert(i);
586
587 for (int i : {10, 20})
588 set2.insert(i);
589
590 set2 = set1;
591
592 EXPECT_EQ(set2.size(), 3u);
593 for (int i : {1, 2, 3})
594 EXPECT_TRUE(set2.contains(i));
595 EXPECT_FALSE(set2.contains(10));
596 EXPECT_FALSE(set2.contains(20));
597}
598
600{
602
603 for (int i : {1, 2, 3})
604 set.insert(i);
605
606 set = set;
607
608 EXPECT_EQ(set.size(), 3u);
609 for (int i : {1, 2, 3})
610 EXPECT_TRUE(set.contains(i));
611}
612
614{
616
617 for (int i : {1, 2, 3, 4, 5})
618 set1.insert(i);
619
620 DynSetAvlTree<int> set2(std::move(set1));
621
622 EXPECT_EQ(set2.size(), 5u);
623 for (int i : {1, 2, 3, 4, 5})
624 EXPECT_TRUE(set2.contains(i));
625
627}
628
630{
632
633 for (int i : {1, 2, 3})
634 set1.insert(i);
635
636 for (int i : {10, 20})
637 set2.insert(i);
638
639 set2 = std::move(set1);
640
641 EXPECT_EQ(set2.size(), 3u);
642 for (int i : {1, 2, 3})
643 EXPECT_TRUE(set2.contains(i));
644
646}
647
649{
651
652 for (int i : {1, 2, 3})
653 set1.insert(i);
654
655 for (int i : {10, 20})
656 set2.insert(i);
657
658 set1.swap(set2);
659
660 EXPECT_EQ(set1.size(), 2u);
661 EXPECT_TRUE(set1.contains(10));
662 EXPECT_TRUE(set1.contains(20));
663
664 EXPECT_EQ(set2.size(), 3u);
665 EXPECT_TRUE(set2.contains(1));
666 EXPECT_TRUE(set2.contains(2));
667 EXPECT_TRUE(set2.contains(3));
668}
669
670// ============================================================================
671// Iterator Tests
672// ============================================================================
673
675{
677
679 EXPECT_FALSE(it.has_curr());
680}
681
683{
685
686 for (int i : {5, 3, 7, 1, 9})
687 set.insert(i);
688
689 vector<int> keys;
690 for (DynSetAvlTree<int>::Iterator it(set); it.has_curr(); it.next_ne())
691 keys.push_back(it.get_curr());
692
693 vector<int> expected = {1, 3, 5, 7, 9};
694 EXPECT_EQ(keys, expected);
695}
696
698{
700
701 for (int i : {1, 2, 3})
702 set.insert(i);
703
705
706 ASSERT_TRUE(it.has_curr());
707 EXPECT_EQ(it.get_curr(), 1);
708
709 it.next_ne();
710 EXPECT_EQ(it.get_curr(), 2);
711
712 it.reset_first();
713 EXPECT_EQ(it.get_curr(), 1);
714}
715
716// ============================================================================
717// Traversal Tests (for_each_*)
718// ============================================================================
719
721{
723
724 for (int i : {5, 3, 7, 1, 9})
725 set.insert(i);
726
727 vector<int> keys;
728 set.for_each_inorder([&keys](const int & k) { keys.push_back(k); });
729
730 vector<int> expected = {1, 3, 5, 7, 9};
731 EXPECT_EQ(keys, expected);
732}
733
735{
737
738 for (int i : {5, 3, 7, 1, 9})
739 set.insert(i);
740
741 vector<int> keys;
742 set.for_each_preorder([&keys](const int & k) { keys.push_back(k); });
743
744 // Just verify we got all keys (order depends on tree structure)
745 EXPECT_EQ(keys.size(), 5u);
746 for (int i : {1, 3, 5, 7, 9})
747 EXPECT_TRUE(find(keys.begin(), keys.end(), i) != keys.end());
748}
749
751{
753
754 for (int i : {5, 3, 7, 1, 9})
755 set.insert(i);
756
757 vector<int> keys;
758 set.for_each_postorder([&keys](const int & k) { keys.push_back(k); });
759
760 // Just verify we got all keys
761 EXPECT_EQ(keys.size(), 5u);
762 for (int i : {1, 3, 5, 7, 9})
763 EXPECT_TRUE(find(keys.begin(), keys.end(), i) != keys.end());
764}
765
767{
769
770 for (int i : {1, 2, 3, 4, 5})
771 set.insert(i);
772
773 int sum = 0;
774 bool completed = set.traverse([&sum](const int & k) {
775 sum += k;
776 return true;
777 });
778
780 EXPECT_EQ(sum, 15);
781}
782
784{
786
787 for (int i : {1, 2, 3, 4, 5})
788 set.insert(i);
789
790 int count = 0;
791 bool completed = set.traverse([&count](const int &) {
792 ++count;
793 return count < 3; // Stop after 3 elements
794 });
795
797 EXPECT_EQ(count, 3);
798}
799
800#endif
801
802// ============================================================================
803// Range Operations Tests (only for trees with rank support)
804// ============================================================================
805
807{
809
810 for (int i : {5, 3, 7, 1, 9, 2, 8})
811 set.insert(i);
812
813 // Sorted order: 1, 2, 3, 5, 7, 8, 9
814 EXPECT_EQ(set.select(0), 1);
815 EXPECT_EQ(set.select(1), 2);
816 EXPECT_EQ(set.select(2), 3);
817 EXPECT_EQ(set.select(3), 5);
818 EXPECT_EQ(set.select(6), 9);
819}
820
822{
824
825 for (int i : {5, 3, 7, 1, 9})
826 set.insert(i);
827
828 // Sorted order: 1, 3, 5, 7, 9
829 EXPECT_EQ(set.position(1), 0);
830 EXPECT_EQ(set.position(3), 1);
831 EXPECT_EQ(set.position(5), 2);
832 EXPECT_EQ(set.position(7), 3);
833 EXPECT_EQ(set.position(9), 4);
834 EXPECT_EQ(set.position(99), -1); // Not found
835}
836
838{
840
841 for (int i : {5, 3, 7, 1, 9})
842 set.insert(i);
843
844 auto [pos, key_ptr] = set.find_position(5);
845 EXPECT_EQ(pos, 2);
846 ASSERT_NE(key_ptr, nullptr);
847 EXPECT_EQ(*key_ptr, 5);
848
849 auto [pos2, key_ptr2] = set.find_position(6); // Not found
850 EXPECT_GE(pos2, 2); // Position where it would be
851 ASSERT_NE(key_ptr2, nullptr);
852}
853
855{
857
858 for (int i : {5, 3, 7, 1, 9})
859 set.insert(i);
860
861 // Remove element at position 2 (which is 5 in sorted order 1,3,5,7,9)
862 int removed = set.remove_pos(2);
863 EXPECT_EQ(removed, 5);
864 EXPECT_EQ(set.size(), 4u);
865 EXPECT_FALSE(set.contains(5));
866}
867
869{
871
872 for (int i : {5, 3, 7, 1, 9})
873 set.insert(i);
874
875 // Sorted order: 1, 3, 5, 7, 9
876 EXPECT_EQ(set(0), 1);
877 EXPECT_EQ(set(2), 5);
878 EXPECT_EQ(set(4), 9);
879}
880
881// ============================================================================
882// Set Operations Tests (join, split)
883// ============================================================================
884
886{
888
889 // Insert even numbers only, so we can split on an odd number not in the tree
890 for (int i : {2, 4, 6, 8, 10, 12, 14, 16, 18})
891 set.insert(i);
892
893 DynSetTreapRk<int> left, right;
894
895 // Split on 11, which is not in the tree
896 bool success = set.split_key(11, left, right);
897
899 EXPECT_TRUE(set.is_empty());
900
901 // left should have {2, 4, 6, 8, 10} - keys < 11
902 EXPECT_EQ(left.size(), 5u);
903 for (int i : {2, 4, 6, 8, 10})
904 EXPECT_TRUE(left.contains(i));
905
906 // right should have {12, 14, 16, 18} - keys > 11
907 EXPECT_EQ(right.size(), 4u);
908 for (int i : {12, 14, 16, 18})
909 EXPECT_TRUE(right.contains(i));
910
911 // 11 is not in either (it was never inserted)
912 EXPECT_FALSE(left.contains(11));
913 EXPECT_FALSE(right.contains(11));
914}
915
917{
919
920 for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
921 set.insert(i);
922
923 DynSetTreapRk<int> left, right;
924
925 set.split_key_dup(5, left, right);
926
927 EXPECT_TRUE(set.is_empty());
928
929 // left should have {1, 2, 3, 4, 5} - keys <= 5
930 EXPECT_EQ(left.size(), 5u);
931 EXPECT_TRUE(left.contains(5));
932
933 // right should have {6, 7, 8, 9} - keys > 5
934 EXPECT_EQ(right.size(), 4u);
935 EXPECT_FALSE(right.contains(5));
936}
937
939{
941
942 for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9})
943 set.insert(i);
944
945 DynSetTreapRk<int> left, right;
946
947 set.split_pos(4, left, right);
948
949 EXPECT_TRUE(set.is_empty());
950
951 // left should have first 5 elements: {1, 2, 3, 4, 5}
952 EXPECT_EQ(left.size(), 5u);
953 for (int i : {1, 2, 3, 4, 5})
954 EXPECT_TRUE(left.contains(i));
955
956 // right should have rest: {6, 7, 8, 9}
957 EXPECT_EQ(right.size(), 4u);
958 for (int i : {6, 7, 8, 9})
959 EXPECT_TRUE(right.contains(i));
960}
961
963{
965
966 for (int i : {1, 2, 3, 4, 5})
967 set.insert(i);
968
969 // pos == 0 => left has {1}, right has {2,3,4,5}
970 {
972 DynSetTreapRk<int> left, right;
973 tmp.split_pos(0, left, right);
975 EXPECT_EQ(left.size(), 1u);
976 EXPECT_EQ(right.size(), 4u);
977 EXPECT_TRUE(left.contains(1));
978 EXPECT_TRUE(right.contains(5));
979 }
980
981 // pos == size-1 => left has all, right empty
982 {
984 DynSetTreapRk<int> left, right;
985 tmp.split_pos(tmp.size() - 1, left, right);
987 EXPECT_EQ(left.size(), 5u);
988 EXPECT_TRUE(right.is_empty());
989 }
990
991 // out of range
992 {
994 DynSetTreapRk<int> left, right;
995 EXPECT_THROW(tmp.split_pos(tmp.size(), left, right), out_of_range);
996 EXPECT_THROW(tmp.split_pos(tmp.size() + 10, left, right), out_of_range);
997 }
998}
999
1001{
1003 for (int i : {1, 2, 3})
1004 set.insert(i);
1005
1006 EXPECT_THROW(set.select(3), out_of_range);
1007 EXPECT_THROW(set.remove_pos(3), out_of_range);
1008
1009 // empty
1010 DynSetTreapRk<int> empty;
1011 EXPECT_THROW(empty.select(0), out_of_range);
1012 EXPECT_THROW(empty.remove_pos(0), out_of_range);
1013 DynSetTreapRk<int> left, right;
1014 EXPECT_THROW(empty.split_pos(0, left, right), out_of_range);
1015}
1016
1018{
1020
1021 for (int i : {1, 3, 5})
1022 set1.insert(i);
1023
1024 for (int i : {2, 4, 6})
1025 set2.insert(i);
1026
1028 set1.join(set2, dup);
1029
1031 EXPECT_EQ(set1.size(), 6u);
1032 for (int i : {1, 2, 3, 4, 5, 6})
1033 EXPECT_TRUE(set1.contains(i));
1034
1035 EXPECT_TRUE(dup.is_empty());
1036}
1037
1039{
1041
1042 for (int i : {1, 2, 3, 4, 5})
1043 set1.insert(i);
1044
1045 for (int i : {3, 4, 5, 6, 7})
1046 set2.insert(i);
1047
1049 set1.join(set2, dup);
1050
1052 EXPECT_EQ(set1.size(), 7u);
1053 for (int i : {1, 2, 3, 4, 5, 6, 7})
1054 EXPECT_TRUE(set1.contains(i));
1055
1056 EXPECT_EQ(dup.size(), 3u);
1057 for (int i : {3, 4, 5})
1058 EXPECT_TRUE(dup.contains(i));
1059}
1060
1062{
1064
1065 for (int i : {1, 2, 3})
1066 set1.insert(i);
1067
1068 for (int i : {3, 4, 5})
1069 set2.insert(i);
1070
1071 set1.join_dup(set2);
1072
1074 EXPECT_EQ(set1.size(), 6u); // Duplicates allowed (3 appears twice)
1075}
1076
1077// =========================================================================
1078// insert_dup() Tests
1079// =========================================================================
1080
1081template <typename T>
1083{
1084 T set;
1085 set.insert_dup(42);
1086 set.insert_dup(42);
1087 set.insert_dup(7);
1088
1089 EXPECT_EQ(set.size(), 3u);
1090
1091 size_t count_42 = 0;
1092 for (typename T::Iterator it(set); it.has_curr(); it.next_ne())
1093 if (it.get_curr() == 42)
1094 ++count_42;
1095
1096 EXPECT_EQ(count_42, 2u);
1097}
1098
1109
1110// =========================================================================
1111// Range APIs on non-rank trees must throw domain_error
1112// =========================================================================
1113
1114template <typename T>
1116{
1117 T set;
1118
1119 // On empty: must still report "unsupported" rather than pretending it works.
1120 EXPECT_THROW(set.position(1), domain_error);
1121 EXPECT_THROW(set.find_position(1), domain_error);
1122 EXPECT_THROW(set.select(0), domain_error);
1123 EXPECT_THROW(set.remove_pos(0), domain_error);
1124 T left, right;
1125 EXPECT_THROW(set.split_pos(0, left, right), domain_error);
1126}
1127
1136
1137// ============================================================================
1138// Different Tree Types Tests
1139// ============================================================================
1140
1141#if 0
1142
1144{
1146
1147 for (int i : {5, 3, 7, 1, 9})
1148 set.insert(i);
1149
1150 EXPECT_EQ(set.size(), 5u);
1151 EXPECT_TRUE(set.contains(3));
1152 EXPECT_FALSE(set.contains(4));
1153}
1154
1156{
1158
1159 for (int i : {5, 3, 7, 1, 9})
1160 set.insert(i);
1161
1162 EXPECT_EQ(set.size(), 5u);
1163 EXPECT_TRUE(set.contains(3));
1164}
1165
1167{
1169
1170 for (int i : {5, 3, 7, 1, 9})
1171 set.insert(i);
1172
1173 EXPECT_EQ(set.size(), 5u);
1174 EXPECT_TRUE(set.contains(3));
1175}
1176
1178{
1179 DynSetTreap<int> set;
1180
1181 for (int i : {5, 3, 7, 1, 9})
1182 set.insert(i);
1183
1184 EXPECT_EQ(set.size(), 5u);
1185 EXPECT_TRUE(set.contains(3));
1186}
1187
1188#endif
1189
1190// ============================================================================
1191// Custom Comparator Tests
1192// ============================================================================
1193
1195{
1197
1198 for (int i : {5, 3, 7, 1, 9})
1199 set.insert(i);
1200
1201 EXPECT_EQ(set.min(), 9); // Max in normal order
1202 EXPECT_EQ(set.max(), 1); // Min in normal order
1203
1204 vector<int> keys;
1205 for (DynSetTree<int, Avl_Tree, std::greater<int>>::Iterator it(set); it.has_curr(); it.next_ne())
1206 keys.push_back(it.get_curr());
1207
1208 vector<int> expected = {9, 7, 5, 3, 1};
1209 EXPECT_EQ(keys, expected);
1210}
1211
1212// ============================================================================
1213// Operator[] Tests
1214// ============================================================================
1215
1217{
1218 const DynSetAvlTree<int> set;
1219
1220 EXPECT_THROW(set[42], domain_error);
1221}
1222
1224{
1226
1227 EXPECT_FALSE(set.contains(42));
1228
1229 const int & ref = set[42];
1230 EXPECT_EQ(ref, 42);
1231 EXPECT_TRUE(set.contains(42));
1232 EXPECT_EQ(set.size(), 1u);
1233}
1234
1235// ============================================================================
1236// Verify Tests
1237// ============================================================================
1238
1240{
1242
1243 for (int i : {5, 3, 7, 1, 9, 2, 8, 4, 6})
1244 set.insert(i);
1245
1246 EXPECT_TRUE(set.verify());
1247}
1248
1249// ============================================================================
1250// Stress Tests
1251// ============================================================================
1252
1254{
1256
1257 // Insert 1000 elements
1258 for (int i = 0; i < 1000; ++i)
1259 set.insert(i);
1260
1261 EXPECT_EQ(set.size(), 1000u);
1262 EXPECT_EQ(set.min(), 0);
1263 EXPECT_EQ(set.max(), 999);
1264
1265 // Verify all present
1266 for (int i = 0; i < 1000; ++i)
1267 EXPECT_TRUE(set.contains(i));
1268
1269 // Remove half
1270 for (int i = 0; i < 1000; i += 2)
1271 set.remove(i);
1272
1273 EXPECT_EQ(set.size(), 500u);
1274
1275 // Verify correct ones remain
1276 for (int i = 0; i < 1000; ++i)
1277 {
1278 if (i % 2 == 0)
1279 EXPECT_FALSE(set.contains(i));
1280 else
1281 EXPECT_TRUE(set.contains(i));
1282 }
1283}
1284
1286{
1288 vector<int> inserted;
1289
1290 // Random insertions
1291 for (int i = 0; i < 200; ++i)
1292 {
1293 if (int key = rand() % 500; set.insert(key) != nullptr)
1294 inserted.push_back(key);
1295 }
1296
1297 // Verify all inserted keys are present
1298 for (int key : inserted)
1299 EXPECT_TRUE(set.contains(key));
1300
1301 EXPECT_EQ(set.size(), inserted.size());
1302
1303 // Random removals
1304 for (size_t i = 0; i < inserted.size() / 2; ++i)
1305 {
1306 int idx = rand() % inserted.size();
1307 set.remove(inserted[idx]);
1308 inserted.erase(inserted.begin() + idx);
1309 }
1310
1311 EXPECT_EQ(set.size(), inserted.size());
1312 for (int key : inserted)
1313 EXPECT_TRUE(set.contains(key));
1314}
1315
1316// ============================================================================
1317// String Key Tests
1318// ============================================================================
1319
1321{
1323
1324 set.insert("apple");
1325 set.insert("banana");
1326 set.insert("cherry");
1327
1328 EXPECT_EQ(set.size(), 3u);
1329 EXPECT_TRUE(set.contains("apple"));
1330 EXPECT_TRUE(set.contains("banana"));
1331 EXPECT_FALSE(set.contains("date"));
1332
1333 EXPECT_EQ(set.min(), "apple");
1334 EXPECT_EQ(set.max(), "cherry");
1335}
1336
1337// ============================================================================
1338// Edge Cases
1339// ============================================================================
1340
1342{
1344
1345 set.insert(42);
1346
1347 EXPECT_EQ(set.min(), 42);
1348 EXPECT_EQ(set.max(), 42);
1349 EXPECT_EQ(set.get_root(), 42);
1350 EXPECT_EQ(set.size(), 1u);
1351
1352 set.remove(42);
1353 EXPECT_TRUE(set.is_empty());
1354}
1355
1357{
1359
1360 for (int cycle = 0; cycle < 10; ++cycle)
1361 {
1362 for (int i = 0; i < 50; ++i)
1363 set.insert(i);
1364
1365 EXPECT_EQ(set.size(), 50u);
1366
1367 for (int i = 0; i < 50; ++i)
1368 set.remove(i);
1369
1370 EXPECT_TRUE(set.is_empty());
1371 }
1372}
1373
1375{
1377
1378 for (int i : {1, 2, 3, 4, 5})
1379 set.insert(i);
1380
1381 EXPECT_EQ(set.get_first(), 1);
1382 EXPECT_EQ(set.get_last(), 5);
1383 EXPECT_EQ(set.get(), 5);
1385}
1386
1387namespace {
1388
1389struct TrackedKey
1390{
1391 int v;
1392 inline static int alive = 0;
1393
1394 TrackedKey() : v(0) { ++alive; }
1395 explicit TrackedKey(int vv) : v(vv) { ++alive; }
1396 TrackedKey(const TrackedKey & other) : v(other.v) { ++alive; }
1397 TrackedKey(TrackedKey && other) noexcept : v(other.v) { ++alive; }
1398 ~TrackedKey() { --alive; }
1399};
1400
1401inline bool operator < (const TrackedKey & a, const TrackedKey & b)
1402{
1403 return a.v < b.v;
1404}
1405
1406template <typename Key, class Compare>
1407class ThrowingSearchOrInsertTree
1408{
1409public:
1410
1411 using Node = BinNode<Key>;
1412
1413 inline static bool enabled = false;
1414
1415 ThrowingSearchOrInsertTree(Compare c = Compare()) : cmp(c) { /* empty */ }
1416
1417 Compare get_compare() const { return cmp; }
1418
1419 Node *& getRoot() noexcept { return root; }
1420 Node * getRoot() const noexcept { return root; }
1421
1422 void swap(ThrowingSearchOrInsertTree & other) noexcept
1423 {
1424 std::swap(root, other.root);
1425 std::swap(cmp, other.cmp);
1426 }
1427
1428 Node * search_or_insert(Node * p)
1429 {
1430 if (enabled)
1431 throw std::runtime_error("search_or_insert failed");
1432
1433 if (root == Node::NullPtr)
1434 return root = p;
1435
1436 return root;
1437 }
1438
1439 bool verify() const { return true; }
1440
1441private:
1442
1444 Compare cmp;
1445};
1446
1448
1449} // namespace
1450
1452{
1453 TrackedKey::alive = 0;
1455
1456 {
1457 ThrowingSet set;
1458 const int baseline = TrackedKey::alive;
1459 set.insert(TrackedKey(1));
1460 EXPECT_EQ(set.size(), 1u);
1461 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1462
1464 EXPECT_THROW((void) set.insert(TrackedKey(2)), std::runtime_error);
1465 EXPECT_EQ(set.size(), 1u);
1466 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1467 }
1468
1470 EXPECT_EQ(TrackedKey::alive, 0);
1471}
1472
1474{
1475 TrackedKey::alive = 0;
1477
1478 {
1479 ThrowingSet set;
1480 const int baseline = TrackedKey::alive;
1481 set.insert(TrackedKey(1));
1482 EXPECT_EQ(set.size(), 1u);
1483 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1484
1486 EXPECT_THROW((void) set.search_or_insert(TrackedKey(2)), std::runtime_error);
1487 EXPECT_EQ(set.size(), 1u);
1488 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1489 }
1490
1492 EXPECT_EQ(TrackedKey::alive, 0);
1493}
1494
1496{
1497 TrackedKey::alive = 0;
1499
1500 {
1501 ThrowingSet set;
1502 const int baseline = TrackedKey::alive;
1503 set.insert(TrackedKey(1));
1504 EXPECT_EQ(set.size(), 1u);
1505 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1506
1508 EXPECT_THROW((void) set.contains_or_insert(TrackedKey(2)), std::runtime_error);
1509 EXPECT_EQ(set.size(), 1u);
1510 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1511 }
1512
1514 EXPECT_EQ(TrackedKey::alive, 0);
1515}
1516
1517// ============================================================================
1518// Missing Coverage Tests - Append and Put Methods
1519// ============================================================================
1520
1522{
1523 auto * p1 = this->set.append(10);
1524 ASSERT_NE(p1, nullptr);
1525 EXPECT_EQ(*p1, 10);
1526 EXPECT_EQ(this->set.size(), 1u);
1527
1528 auto * p2 = this->set.append(20);
1529 ASSERT_NE(p2, nullptr);
1530 EXPECT_EQ(*p2, 20);
1531 EXPECT_EQ(this->set.size(), 2u);
1532
1533 // Duplicate should fail
1534 auto * p3 = this->set.append(10);
1535 EXPECT_EQ(p3, nullptr);
1536 EXPECT_EQ(this->set.size(), 2u);
1537}
1538
1540{
1541 auto * p1 = this->set.put(10);
1542 ASSERT_NE(p1, nullptr);
1543 EXPECT_EQ(*p1, 10);
1544 EXPECT_EQ(this->set.size(), 1u);
1545
1546 auto * p2 = this->set.put(20);
1547 ASSERT_NE(p2, nullptr);
1548 EXPECT_EQ(*p2, 20);
1549 EXPECT_EQ(this->set.size(), 2u);
1550
1551 // Duplicate should fail (put is alias for insert)
1552 auto * p3 = this->set.put(10);
1553 EXPECT_EQ(p3, nullptr);
1554 EXPECT_EQ(this->set.size(), 2u);
1555}
1556
1557// ============================================================================
1558// Missing Coverage Tests - Rvalue Insert Variants
1559// ============================================================================
1560
1562{
1563 string s = "test";
1564 // Using int for simplicity - rvalue path tested via move
1565 int val = 42;
1566 auto * p = this->set.insert(std::move(val));
1567 ASSERT_NE(p, nullptr);
1568 EXPECT_EQ(*p, 42);
1569}
1570
1572{
1573 int val = 42;
1574 auto * p = this->set.append(std::move(val));
1575 ASSERT_NE(p, nullptr);
1576 EXPECT_EQ(*p, 42);
1577}
1578
1580{
1581 int val = 42;
1582 auto * p = this->set.put(std::move(val));
1583 ASSERT_NE(p, nullptr);
1584 EXPECT_EQ(*p, 42);
1585}
1586
1588{
1589 int val = 42;
1590 auto * p = this->set.search_or_insert(std::move(val));
1591 ASSERT_NE(p, nullptr);
1592 EXPECT_EQ(*p, 42);
1593 EXPECT_EQ(this->set.size(), 1u);
1594}
1595
1597{
1598 int val = 42;
1599 auto [p, found] = this->set.contains_or_insert(std::move(val));
1600 ASSERT_NE(p, nullptr);
1602 EXPECT_EQ(*p, 42);
1603}
1604
1606{
1607 int val1 = 42;
1608 auto * p1 = this->set.insert_dup(std::move(val1));
1609 ASSERT_NE(p1, nullptr);
1610
1611 int val2 = 42;
1612 auto * p2 = this->set.insert_dup(std::move(val2));
1613 ASSERT_NE(p2, nullptr);
1614
1615 EXPECT_EQ(this->set.size(), 2u);
1616}
1617
1618// ============================================================================
1619// Missing Coverage Tests - Tree Metrics
1620// ============================================================================
1621
1623{
1624 EXPECT_EQ(this->set.height(), 0u);
1625
1626 this->set.insert(1);
1627 EXPECT_GE(this->set.height(), 1u);
1628
1629 for (int i = 2; i <= 10; ++i)
1630 this->set.insert(i);
1631
1632 size_t h = this->set.height();
1633 EXPECT_GE(h, 1u);
1634 EXPECT_LE(h, 10u); // Should be balanced, much less than 10
1635}
1636
1638{
1639 // Empty tree
1640 EXPECT_EQ(this->set.internal_path_length(), 0u);
1641
1642 this->set.insert(1);
1643 EXPECT_EQ(this->set.internal_path_length(), 0u); // Root has depth 0
1644
1645 this->set.insert(2);
1646 this->set.insert(3);
1647 EXPECT_GT(this->set.internal_path_length(), 0u);
1648}
1649
1651{
1652 // Empty tree - root may be nullptr or a sentinel node depending on tree type
1653 // Just verify that after insert, the root is valid and different
1654
1655 this->set.insert(42);
1656 auto * node = this->set.get_root_node();
1657 ASSERT_NE(node, nullptr);
1658
1659 // Verify it's a real node with the key
1660 EXPECT_EQ(node->get_key(), 42);
1661}
1662
1663// ============================================================================
1664// Missing Coverage Tests - Access Methods
1665// ============================================================================
1666
1668{
1669 for (int i : {5, 3, 7, 1, 9})
1670 this->set.insert(i);
1671
1672 EXPECT_EQ(this->set.get_first(), 1);
1673 EXPECT_EQ(this->set.get_last(), 9);
1674}
1675
1677{
1678 for (int i : {5, 3, 7, 1, 9})
1679 this->set.insert(i);
1680
1681 // get() is alias for max()
1682 EXPECT_EQ(this->set.get(), 9);
1683}
1684
1686{
1687 this->set.insert(42);
1688
1689 // get_item() returns any element (same as get_root())
1690 EXPECT_NO_THROW(this->set.get_item());
1691}
1692
1694{
1695 this->set.insert(42);
1696
1697 // All three should behave identically
1698 EXPECT_EQ(this->set.exist(42), this->set.has(42));
1699 EXPECT_EQ(this->set.has(42), this->set.contains(42));
1700
1701 EXPECT_EQ(this->set.exist(99), this->set.has(99));
1702 EXPECT_EQ(this->set.has(99), this->set.contains(99));
1703}
1704
1705// ============================================================================
1706// Missing Coverage Tests - Assignment Operators
1707// ============================================================================
1708
1710{
1712
1713 for (int i : {1, 2, 3})
1714 this->set.insert(i);
1715
1716 for (int i : {10, 20})
1717 set2.insert(i);
1718
1719 set2 = this->set;
1720
1721 EXPECT_EQ(set2.size(), 3u);
1722 for (int i : {1, 2, 3})
1723 EXPECT_TRUE(set2.contains(i));
1724 EXPECT_FALSE(set2.contains(10));
1725}
1726
1728{
1729 for (int i : {1, 2, 3})
1730 this->set.insert(i);
1731
1732 this->set = this->set;
1733
1734 EXPECT_EQ(this->set.size(), 3u);
1735 for (int i : {1, 2, 3})
1736 EXPECT_TRUE(this->set.contains(i));
1737}
1738
1740{
1742
1743 for (int i : {1, 2, 3})
1744 this->set.insert(i);
1745
1746 for (int i : {10, 20})
1747 set2.insert(i);
1748
1749 set2 = std::move(this->set);
1750
1751 EXPECT_EQ(set2.size(), 3u);
1752 for (int i : {1, 2, 3})
1753 EXPECT_TRUE(set2.contains(i));
1754
1755 EXPECT_TRUE(this->set.is_empty());
1756}
1757
1759{
1760 for (int i : {1, 2, 3})
1761 this->set.insert(i);
1762
1763 // Self move-assignment should be safe (no-op)
1764#if defined(__clang__)
1765# pragma clang diagnostic push
1766# pragma clang diagnostic ignored "-Wself-move"
1767#elif defined(__GNUC__)
1768# pragma GCC diagnostic push
1769# pragma GCC diagnostic ignored "-Wself-move"
1770#endif
1771 this->set = std::move(this->set);
1772#if defined(__clang__)
1773# pragma clang diagnostic pop
1774#elif defined(__GNUC__)
1775# pragma GCC diagnostic pop
1776#endif
1777
1778 // After self-move, behavior is unspecified but should not crash
1779 // The set should still be in a valid state
1780}
1781
1782// ============================================================================
1783// Missing Coverage Tests - Initializer List Construction
1784// ============================================================================
1785
1787{
1788 DynSetAvlTree<int> set = {5, 3, 7, 1, 9};
1789
1790 EXPECT_EQ(set.size(), 5u);
1791 for (int i : {1, 3, 5, 7, 9})
1792 EXPECT_TRUE(set.contains(i));
1793}
1794
1796{
1797 // Duplicates in initializer list should be ignored
1798 DynSetAvlTree<int> set = {1, 2, 3, 2, 1};
1799
1800 EXPECT_EQ(set.size(), 3u);
1801}
1802
1804{
1805 DynSetAvlTree<int> set = {};
1806
1807 EXPECT_TRUE(set.is_empty());
1808}
1809
1810// ============================================================================
1811// Missing Coverage Tests - Split Operations Edge Cases
1812// ============================================================================
1813
1815{
1817
1818 for (int i : {1, 2, 3, 4, 5})
1819 set.insert(i);
1820
1821 DynSetTreapRk<int> left, right;
1822
1823 // Split on existing key should fail
1824 bool success = set.split_key(3, left, right);
1825
1827 // Original set should be unchanged
1828 EXPECT_EQ(set.size(), 5u);
1829 EXPECT_TRUE(left.is_empty());
1830 EXPECT_TRUE(right.is_empty());
1831}
1832
1834{
1836 DynSetTreapRk<int> left, right;
1837
1838 bool success = set.split_key(42, left, right);
1839
1841 EXPECT_TRUE(left.is_empty());
1842 EXPECT_TRUE(right.is_empty());
1843}
1844
1846{
1848 DynSetTreapRk<int> left, right;
1849
1850 EXPECT_NO_THROW(set.split_key_dup(42, left, right));
1851 EXPECT_TRUE(left.is_empty());
1852 EXPECT_TRUE(right.is_empty());
1853}
1854
1855// ============================================================================
1856// Missing Coverage Tests - Rank Operations on TreapRk
1857// ============================================================================
1858
1860{
1862
1863 for (int i : {5, 3, 7, 1, 9})
1864 set.insert(i);
1865
1866 // access() is alias for select()
1867 EXPECT_EQ(set.access(0), 1);
1868 EXPECT_EQ(set.access(2), 5);
1869 EXPECT_EQ(set.access(4), 9);
1870}
1871
1873{
1875
1876 for (int i : {1, 2, 3})
1877 set.insert(i);
1878
1879 EXPECT_THROW(set.select(3), out_of_range);
1880 EXPECT_THROW(set.select(100), out_of_range);
1881}
1882
1884{
1886
1887 EXPECT_THROW(set.select(0), out_of_range);
1888}
1889
1891{
1893
1894 for (int i : {1, 2, 3})
1895 set.insert(i);
1896
1897 EXPECT_THROW(set.remove_pos(3), out_of_range);
1898 EXPECT_THROW(set.remove_pos(100), out_of_range);
1899}
1900
1902{
1904
1905 auto [pos, ptr] = set.find_position(42);
1906 EXPECT_EQ(pos, 0);
1907 EXPECT_EQ(ptr, nullptr);
1908}
1909
1911{
1913
1914 for (int i : {1, 3, 5, 7, 9})
1915 set.insert(i);
1916
1917 // Position of non-existent key
1918 EXPECT_EQ(set.position(2), -1);
1919 EXPECT_EQ(set.position(4), -1);
1920 EXPECT_EQ(set.position(100), -1);
1921}
1922
1924{
1926
1927 for (int i : {5, 3, 7, 1, 9})
1928 set.insert(i);
1929
1930 const auto & const_set = set;
1931
1932 EXPECT_EQ(const_set.select(0), 1);
1933 EXPECT_EQ(const_set.select(4), 9);
1934}
1935
1936// ============================================================================
1937// Missing Coverage Tests - Iterator Edge Cases
1938// ============================================================================
1939
1941{
1942 typename TypeParam::Iterator it(this->set);
1943
1944 EXPECT_FALSE(it.has_curr());
1945}
1946
1948{
1949 for (int i : {1, 2, 3})
1950 this->set.insert(i);
1951
1952 typename TypeParam::Iterator it(this->set);
1953
1954 // Advance to end
1955 while (it.has_curr())
1956 it.next_ne();
1957
1958 it.reset_first();
1959 EXPECT_TRUE(it.has_curr());
1960 EXPECT_EQ(it.get_curr(), 1);
1961}
1962
1964{
1965 for (int i : {1, 2, 3})
1966 this->set.insert(i);
1967
1968 typename TypeParam::Iterator it(this->set);
1969
1970 it.reset_last();
1971 EXPECT_TRUE(it.has_curr());
1972 EXPECT_EQ(it.get_curr(), 3);
1973}
1974
1975// ============================================================================
1976// Missing Coverage Tests - Traverse Early Exit
1977// ============================================================================
1978
1980{
1981 for (int i : {1, 2, 3, 4, 5})
1982 this->set.insert(i);
1983
1984 int count = 0;
1985 bool completed = this->set.traverse([&count](const int &) {
1986 ++count;
1987 return count < 3; // Stop after 3 elements
1988 });
1989
1991 EXPECT_EQ(count, 3);
1992}
1993
1995{
1996 for (int i : {1, 2, 3, 4, 5})
1997 this->set.insert(i);
1998
1999 const auto & const_set = this->set;
2000
2001 int sum = 0;
2002 bool completed = const_set.traverse([&sum](const int & k) {
2003 sum += k;
2004 return true;
2005 });
2006
2008 EXPECT_EQ(sum, 15);
2009}
2010
2011// ============================================================================
2012// Missing Coverage Tests - operator() for Non-Rank Trees
2013// ============================================================================
2014
2016{
2018
2019 for (int i : {1, 2, 3})
2020 set.insert(i);
2021
2022 // operator() calls select(), which should throw on non-rank trees
2023 EXPECT_THROW(set(0), domain_error);
2024}
2025
2026// ============================================================================
2027// Missing Coverage Tests - Join Edge Cases
2028// ============================================================================
2029
2031{
2034
2035 set1.join(set2, dup);
2036
2039 EXPECT_TRUE(dup.is_empty());
2040}
2041
2043{
2046
2047 for (int i : {1, 2, 3})
2048 set1.insert(i);
2049
2050 set1.join(set2, dup);
2051
2052 EXPECT_EQ(set1.size(), 3u);
2054 EXPECT_TRUE(dup.is_empty());
2055}
2056
2058{
2060
2061 set1.join_dup(set2);
2062
2065}
2066
2067// ============================================================================
2068// Missing Coverage Tests - Verify on Empty and Single Element
2069// ============================================================================
2070
2075
2077{
2078 this->set.insert(42);
2079 EXPECT_TRUE(this->set.verify());
2080}
2081
2082// ============================================================================
2083// Missing Coverage Tests - String Type with Rvalues
2084// ============================================================================
2085
2087{
2089
2090 string s = "hello";
2091 auto * p = set.insert(std::move(s));
2092
2093 ASSERT_NE(p, nullptr);
2094 EXPECT_EQ(*p, "hello");
2095 EXPECT_EQ(set.size(), 1u);
2096}
2097
2099{
2101
2102 string s1 = "hello";
2103 auto * p1 = set.search_or_insert(std::move(s1));
2104 ASSERT_NE(p1, nullptr);
2105 EXPECT_EQ(*p1, "hello");
2106
2107 string s2 = "hello";
2108 auto * p2 = set.search_or_insert(std::move(s2));
2109 EXPECT_EQ(p1, p2); // Same pointer (found existing)
2110}
2111
2112// ============================================================================
2113// Missing Coverage Tests - All Tree Type Aliases
2114// ============================================================================
2115
2117{
2119 set.insert(1);
2120 set.insert(2);
2121 EXPECT_EQ(set.size(), 2u);
2122 EXPECT_TRUE(set.verify());
2123}
2124
2126{
2128 set.insert(1);
2129 set.insert(2);
2130 EXPECT_EQ(set.size(), 2u);
2131 EXPECT_TRUE(set.verify());
2132}
2133
2135{
2137 set.insert(1);
2138 set.insert(2);
2139 EXPECT_EQ(set.size(), 2u);
2140 EXPECT_TRUE(set.verify());
2141}
2142
2144{
2146 set.insert(1);
2147 set.insert(2);
2148 EXPECT_EQ(set.size(), 2u);
2149 EXPECT_TRUE(set.verify());
2150}
2151
2153{
2154 DynSetTreap<int> set;
2155 set.insert(1);
2156 set.insert(2);
2157 EXPECT_EQ(set.size(), 2u);
2158 EXPECT_TRUE(set.verify());
2159}
2160
2162{
2164 set.insert(1);
2165 set.insert(2);
2166 EXPECT_EQ(set.size(), 2u);
2167 EXPECT_TRUE(set.verify());
2168}
2169
2171{
2173 set.insert(1);
2174 set.insert(2);
2175 EXPECT_EQ(set.size(), 2u);
2176 EXPECT_TRUE(set.verify());
2177}
2178
2179// ============================================================================
2180// Stress and Fuzz Tests
2181// ============================================================================
2182
2184{
2185 const int N = 5000;
2186 for (int k = 0; k < N; ++k)
2187 this->set.insert(k);
2188
2189 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2190 EXPECT_TRUE(this->set.verify());
2191 EXPECT_EQ(this->set.min(), 0);
2192 EXPECT_EQ(this->set.max(), N - 1);
2193}
2194
2196{
2197 const int N = 5000;
2198 for (int k = N - 1; k >= 0; --k)
2199 this->set.insert(k);
2200
2201 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2202 EXPECT_TRUE(this->set.verify());
2203}
2204
2206{
2207 const int N = 3000;
2208
2209 for (int k = 0; k < N; ++k)
2210 this->set.insert(k);
2211
2212 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2213
2214 // Remove all
2215 for (int k = 0; k < N; ++k)
2216 this->set.remove(k);
2217
2218 EXPECT_TRUE(this->set.is_empty());
2219}
2220
2222{
2223 std::set<int> oracle;
2224 std::mt19937 gen(12345);
2225 std::uniform_int_distribution<> key_dist(0, 1000);
2226 std::uniform_int_distribution<> op_dist(0, 2);
2227
2228 for (int iter = 0; iter < 5000; ++iter)
2229 {
2230 int key = key_dist(gen);
2231 int op = op_dist(gen);
2232
2233 if (op == 0) // insert
2234 {
2235 if (this->set.insert(key) != nullptr)
2236 oracle.insert(key);
2237 }
2238 else if (op == 1 && !oracle.empty()) // remove
2239 {
2240 auto it = oracle.begin();
2241 std::advance(it, gen() % oracle.size());
2242 int k = *it;
2243
2244 this->set.remove(k);
2245 oracle.erase(k);
2246 }
2247 else // search
2248 {
2249 bool in_set = this->set.contains(key);
2250 bool in_oracle = oracle.count(key) > 0;
2252 }
2253
2254 EXPECT_EQ(this->set.size(), oracle.size());
2255 }
2256
2257 EXPECT_TRUE(this->set.verify());
2258}
2259
2261{
2262 std::set<int> oracle;
2263 std::mt19937 gen(54321);
2264 std::uniform_int_distribution<> key_dist(0, 500);
2265
2266 for (int iter = 0; iter < 3000; ++iter)
2267 {
2268 int key = key_dist(gen);
2269
2270 if (iter % 2 == 0) // insert
2271 {
2272 if (this->set.insert(key) != nullptr)
2273 oracle.insert(key);
2274 }
2275 else if (!oracle.empty()) // remove random existing
2276 {
2277 auto it = oracle.begin();
2278 std::advance(it, gen() % oracle.size());
2279 int k = *it;
2280
2281 this->set.remove(k);
2282 oracle.erase(k);
2283 }
2284
2285 EXPECT_EQ(this->set.size(), oracle.size());
2286 }
2287
2288 EXPECT_TRUE(this->set.verify());
2289}
2290
2292{
2293 // Test each tree type with larger data
2294 auto test_tree = [](auto & tree) {
2295 std::set<int> oracle;
2296 std::mt19937 gen(99999);
2297 std::uniform_int_distribution<> key_dist(0, 10000);
2298 std::uniform_int_distribution<> op_dist(0, 2);
2299
2300 for (int iter = 0; iter < 10000; ++iter)
2301 {
2302 int key = key_dist(gen);
2303 int op = op_dist(gen);
2304
2305 if (op == 0)
2306 {
2307 if (tree.insert(key) != nullptr)
2308 oracle.insert(key);
2309 }
2310 else if (op == 1 && !oracle.empty())
2311 {
2312 auto it = oracle.begin();
2313 std::advance(it, gen() % oracle.size());
2314 int k = *it;
2315 tree.remove(k);
2316 oracle.erase(k);
2317 }
2318 else
2319 {
2320 EXPECT_EQ(tree.contains(key), oracle.count(key) > 0);
2321 }
2322 }
2323
2324 EXPECT_EQ(tree.size(), oracle.size());
2325 EXPECT_TRUE(tree.verify());
2326 };
2327
2328 {
2329 DynSetAvlTree<int> tree;
2330 test_tree(tree);
2331 }
2332 {
2333 DynSetRbTree<int> tree;
2334 test_tree(tree);
2335 }
2336 {
2337 DynSetTreap<int> tree;
2338 test_tree(tree);
2339 }
2340 {
2342 test_tree(tree);
2343 }
2344}
2345
2346int main(int argc, char **argv)
2347{
2348 ::testing::InitGoogleTest(&argc, argv);
2349 return RUN_ALL_TESTS();
2350}
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:142
int main()
WeightedDigraph::Node Node
long double h
Definition btreepic.C:154
Node for binary search tree.
static BinNode *const NullPtr
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
void empty() noexcept
empty the list
Definition htlist.H:1689
DynList & swap(DynList &l) noexcept
Swap this with l.
Definition htlist.H:1448
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 & access(size_t i)
const Key & get_first() const
size_t height() const
Calculates and returns the height of the binary search tree.
long position(const Key &key) const
Returns the infix (ordered) position of the key.
Key * append(const Key &key)
const Key & get_last() const
bool split_key(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key.
const Key & get_item() const
Returns any element of the set.
const size_t & size() const
Returns the cardinality of the set.
Key remove_pos(const size_t i)
Removes a key from the dynamic set.
Key del(const Key &key)
Deletes key and returns a full copy of stored key.
std::pair< long, Key * > find_position(const Key &key) const
Returns the infix (ordinate) position of the key key or whatever It would be your position of belongi...
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool exist(const Key &key) const
Returns true if key belongs to the dynamic set.
void split_key_dup(const Key &key, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on a key that may be present in the tree.
bool has(const Key &key) const
void for_each_postorder(Key_Op &key_op) const
Performs a postfix traversal over all keys in the set and invokes operation Op.
Key * put(const Key &key)
Key & find(const Key &key) const
Returns a modifiable reference to an element within the set.
size_t internal_path_length() const
Calculates and returns the length of the internal path of the tree search binary.
size_t remove(const Key &key)
Removes a key from the dynamic set.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
bool contains(const Key &key) const
const Key & get() const
Synonym of max.
const Key & get_root() const
Key * insert_dup(const Key &key)
void split_pos(const size_t pos, DynSetTree &l, DynSetTree &r)
Partitions the binary search tree based on an infix position.
Key * search(const Key &key) const
Find an element in the set.
Key & select(size_t i)
Returns the ith node in infix position.
void empty()
remove all elements from the set
bool traverse(Operation &op)
Traverse all the set of pairs and for each key executes the operation op.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
void for_each_preorder(Key_Op &key_op) const
Performs a prefix traversal over all keys in the set and invokes operation Op.
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
void for_each_inorder(Key_Op &key_op) const
Performs an infix traversal over all keys in the set and invokes operation Op.
std::pair< Key *, bool > contains_or_insert(const Key &key)
bool is_empty() const
returns true if the set is empty
Node * get_root_node() const
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
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
static void range_methods_throw_domain_error_test()
static void insert_dup_traversal_test()
TYPED_TEST(DynSetTreeTypedTest, EmptySetProperties)
Definition dynsettree.cc:74
TYPED_TEST_SUITE(DynSetTreeTypedTest, AllTreeTypes)
#define N
Definition fib.C:294
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
void verify()
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool completed() const noexcept
Return true if all underlying iterators are finished.
Definition ah-zip.H:140
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:121
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Definition ahAlgo.H:230
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:704
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:95
Dynamic set implementations based on balanced binary search trees.