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};
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};
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
626 EXPECT_TRUE(set1.is_empty());
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
645 EXPECT_TRUE(set1.is_empty());
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};
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};
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);
974 EXPECT_TRUE(tmp.is_empty());
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);
986 EXPECT_TRUE(tmp.is_empty());
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
1030 EXPECT_TRUE(set2.is_empty());
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
1051 EXPECT_TRUE(set2.is_empty());
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
1073 EXPECT_TRUE(set2.is_empty());
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};
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 // Stubs required by BSTPolicy concept. Only search_or_insert is exercised
1429 // by the exception-safety tests below; the rest are intentionally minimal
1430 // and not functionally correct tree operations.
1431 Node * search(const Key &) noexcept { return root; }
1432
1433 Node * search_or_insert(Node * p)
1434 {
1435 if (enabled)
1436 throw std::runtime_error("search_or_insert failed");
1437
1438 if (root == Node::NullPtr)
1439 return root = p;
1440
1441 return root;
1442 }
1443
1444 Node * insert_dup(Node * p) noexcept
1445 {
1446 if (root == Node::NullPtr)
1447 return root = p;
1448 return p;
1449 }
1450
1451 Node * remove(const Key &) noexcept { return nullptr; }
1452
1453 bool verify() const { return true; }
1454
1455private:
1456
1458 Compare cmp;
1459};
1460
1462
1463} // namespace
1464
1466{
1467 TrackedKey::alive = 0;
1469
1470 {
1471 ThrowingSet set;
1472 const int baseline = TrackedKey::alive;
1473 set.insert(TrackedKey(1));
1474 EXPECT_EQ(set.size(), 1u);
1475 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1476
1478 EXPECT_THROW((void) set.insert(TrackedKey(2)), std::runtime_error);
1479 EXPECT_EQ(set.size(), 1u);
1480 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1481 }
1482
1484 EXPECT_EQ(TrackedKey::alive, 0);
1485}
1486
1488{
1489 TrackedKey::alive = 0;
1491
1492 {
1493 ThrowingSet set;
1494 const int baseline = TrackedKey::alive;
1495 set.insert(TrackedKey(1));
1496 EXPECT_EQ(set.size(), 1u);
1497 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1498
1500 EXPECT_THROW((void) set.search_or_insert(TrackedKey(2)), std::runtime_error);
1501 EXPECT_EQ(set.size(), 1u);
1502 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1503 }
1504
1506 EXPECT_EQ(TrackedKey::alive, 0);
1507}
1508
1510{
1511 TrackedKey::alive = 0;
1513
1514 {
1515 ThrowingSet set;
1516 const int baseline = TrackedKey::alive;
1517 set.insert(TrackedKey(1));
1518 EXPECT_EQ(set.size(), 1u);
1519 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1520
1522 EXPECT_THROW((void) set.contains_or_insert(TrackedKey(2)), std::runtime_error);
1523 EXPECT_EQ(set.size(), 1u);
1524 EXPECT_EQ(TrackedKey::alive, baseline + 1);
1525 }
1526
1528 EXPECT_EQ(TrackedKey::alive, 0);
1529}
1530
1531// ============================================================================
1532// Missing Coverage Tests - Append and Put Methods
1533// ============================================================================
1534
1536{
1537 auto * p1 = this->set.append(10);
1538 ASSERT_NE(p1, nullptr);
1539 EXPECT_EQ(*p1, 10);
1540 EXPECT_EQ(this->set.size(), 1u);
1541
1542 auto * p2 = this->set.append(20);
1543 ASSERT_NE(p2, nullptr);
1544 EXPECT_EQ(*p2, 20);
1545 EXPECT_EQ(this->set.size(), 2u);
1546
1547 // Duplicate should fail
1548 auto * p3 = this->set.append(10);
1549 EXPECT_EQ(p3, nullptr);
1550 EXPECT_EQ(this->set.size(), 2u);
1551}
1552
1554{
1555 auto * p1 = this->set.put(10);
1556 ASSERT_NE(p1, nullptr);
1557 EXPECT_EQ(*p1, 10);
1558 EXPECT_EQ(this->set.size(), 1u);
1559
1560 auto * p2 = this->set.put(20);
1561 ASSERT_NE(p2, nullptr);
1562 EXPECT_EQ(*p2, 20);
1563 EXPECT_EQ(this->set.size(), 2u);
1564
1565 // Duplicate should fail (put is alias for insert)
1566 auto * p3 = this->set.put(10);
1567 EXPECT_EQ(p3, nullptr);
1568 EXPECT_EQ(this->set.size(), 2u);
1569}
1570
1571// ============================================================================
1572// Missing Coverage Tests - Rvalue Insert Variants
1573// ============================================================================
1574
1576{
1577 string s = "test";
1578 // Using int for simplicity - rvalue path tested via move
1579 int val = 42;
1580 auto * p = this->set.insert(std::move(val));
1581 ASSERT_NE(p, nullptr);
1582 EXPECT_EQ(*p, 42);
1583}
1584
1586{
1587 int val = 42;
1588 auto * p = this->set.append(std::move(val));
1589 ASSERT_NE(p, nullptr);
1590 EXPECT_EQ(*p, 42);
1591}
1592
1594{
1595 int val = 42;
1596 auto * p = this->set.put(std::move(val));
1597 ASSERT_NE(p, nullptr);
1598 EXPECT_EQ(*p, 42);
1599}
1600
1602{
1603 int val = 42;
1604 auto * p = this->set.search_or_insert(std::move(val));
1605 ASSERT_NE(p, nullptr);
1606 EXPECT_EQ(*p, 42);
1607 EXPECT_EQ(this->set.size(), 1u);
1608}
1609
1611{
1612 int val = 42;
1613 auto [p, found] = this->set.contains_or_insert(std::move(val));
1614 ASSERT_NE(p, nullptr);
1615 EXPECT_FALSE(found);
1616 EXPECT_EQ(*p, 42);
1617}
1618
1620{
1621 int val1 = 42;
1622 auto * p1 = this->set.insert_dup(std::move(val1));
1623 ASSERT_NE(p1, nullptr);
1624
1625 int val2 = 42;
1626 auto * p2 = this->set.insert_dup(std::move(val2));
1627 ASSERT_NE(p2, nullptr);
1628
1629 EXPECT_EQ(this->set.size(), 2u);
1630}
1631
1632// ============================================================================
1633// Missing Coverage Tests - Tree Metrics
1634// ============================================================================
1635
1637{
1638 EXPECT_EQ(this->set.height(), 0u);
1639
1640 this->set.insert(1);
1641 EXPECT_GE(this->set.height(), 1u);
1642
1643 for (int i = 2; i <= 10; ++i)
1644 this->set.insert(i);
1645
1646 size_t h = this->set.height();
1647 EXPECT_GE(h, 1u);
1648 EXPECT_LE(h, 10u); // Should be balanced, much less than 10
1649}
1650
1652{
1653 // Empty tree
1654 EXPECT_EQ(this->set.internal_path_length(), 0u);
1655
1656 this->set.insert(1);
1657 EXPECT_EQ(this->set.internal_path_length(), 0u); // Root has depth 0
1658
1659 this->set.insert(2);
1660 this->set.insert(3);
1661 EXPECT_GT(this->set.internal_path_length(), 0u);
1662}
1663
1665{
1666 // Empty tree - root may be nullptr or a sentinel node depending on tree type
1667 // Just verify that after insert, the root is valid and different
1668
1669 this->set.insert(42);
1670 auto * node = this->set.get_root_node();
1671 ASSERT_NE(node, nullptr);
1672
1673 // Verify it's a real node with the key
1674 EXPECT_EQ(node->get_key(), 42);
1675}
1676
1677// ============================================================================
1678// Missing Coverage Tests - Access Methods
1679// ============================================================================
1680
1682{
1683 for (int i : {5, 3, 7, 1, 9})
1684 this->set.insert(i);
1685
1686 EXPECT_EQ(this->set.get_first(), 1);
1687 EXPECT_EQ(this->set.get_last(), 9);
1688}
1689
1691{
1692 for (int i : {5, 3, 7, 1, 9})
1693 this->set.insert(i);
1694
1695 // get() is alias for max()
1696 EXPECT_EQ(this->set.get(), 9);
1697}
1698
1700{
1701 this->set.insert(42);
1702
1703 // get_item() returns any element (same as get_root())
1704 EXPECT_NO_THROW(this->set.get_item());
1705}
1706
1708{
1709 this->set.insert(42);
1710
1711 // All three should behave identically
1712 EXPECT_EQ(this->set.exist(42), this->set.has(42));
1713 EXPECT_EQ(this->set.has(42), this->set.contains(42));
1714
1715 EXPECT_EQ(this->set.exist(99), this->set.has(99));
1716 EXPECT_EQ(this->set.has(99), this->set.contains(99));
1717}
1718
1719// ============================================================================
1720// Missing Coverage Tests - Assignment Operators
1721// ============================================================================
1722
1724{
1726
1727 for (int i : {1, 2, 3})
1728 this->set.insert(i);
1729
1730 for (int i : {10, 20})
1731 set2.insert(i);
1732
1733 set2 = this->set;
1734
1735 EXPECT_EQ(set2.size(), 3u);
1736 for (int i : {1, 2, 3})
1737 EXPECT_TRUE(set2.contains(i));
1738 EXPECT_FALSE(set2.contains(10));
1739}
1740
1742{
1743 for (int i : {1, 2, 3})
1744 this->set.insert(i);
1745
1746 this->set = this->set;
1747
1748 EXPECT_EQ(this->set.size(), 3u);
1749 for (int i : {1, 2, 3})
1750 EXPECT_TRUE(this->set.contains(i));
1751}
1752
1754{
1756
1757 for (int i : {1, 2, 3})
1758 this->set.insert(i);
1759
1760 for (int i : {10, 20})
1761 set2.insert(i);
1762
1763 set2 = std::move(this->set);
1764
1765 EXPECT_EQ(set2.size(), 3u);
1766 for (int i : {1, 2, 3})
1767 EXPECT_TRUE(set2.contains(i));
1768
1769 EXPECT_TRUE(this->set.is_empty());
1770}
1771
1773{
1774 for (int i : {1, 2, 3})
1775 this->set.insert(i);
1776
1777 // Self move-assignment should be safe (no-op)
1778#if defined(__clang__)
1779# pragma clang diagnostic push
1780# pragma clang diagnostic ignored "-Wself-move"
1781#elif defined(__GNUC__)
1782# pragma GCC diagnostic push
1783# pragma GCC diagnostic ignored "-Wself-move"
1784#endif
1785 this->set = std::move(this->set);
1786#if defined(__clang__)
1787# pragma clang diagnostic pop
1788#elif defined(__GNUC__)
1789# pragma GCC diagnostic pop
1790#endif
1791
1792 // After self-move, behavior is unspecified but should not crash
1793 // The set should still be in a valid state
1794}
1795
1796// ============================================================================
1797// Missing Coverage Tests - Initializer List Construction
1798// ============================================================================
1799
1801{
1802 DynSetAvlTree<int> set = {5, 3, 7, 1, 9};
1803
1804 EXPECT_EQ(set.size(), 5u);
1805 for (int i : {1, 3, 5, 7, 9})
1806 EXPECT_TRUE(set.contains(i));
1807}
1808
1810{
1811 // Duplicates in initializer list should be ignored
1812 DynSetAvlTree<int> set = {1, 2, 3, 2, 1};
1813
1814 EXPECT_EQ(set.size(), 3u);
1815}
1816
1818{
1819 DynSetAvlTree<int> set = {};
1820
1821 EXPECT_TRUE(set.is_empty());
1822}
1823
1824// ============================================================================
1825// Missing Coverage Tests - Split Operations Edge Cases
1826// ============================================================================
1827
1829{
1831
1832 for (int i : {1, 2, 3, 4, 5})
1833 set.insert(i);
1834
1835 DynSetTreapRk<int> left, right;
1836
1837 // Split on existing key should fail
1838 bool success = set.split_key(3, left, right);
1839
1841 // Original set should be unchanged
1842 EXPECT_EQ(set.size(), 5u);
1843 EXPECT_TRUE(left.is_empty());
1844 EXPECT_TRUE(right.is_empty());
1845}
1846
1848{
1850 DynSetTreapRk<int> left, right;
1851
1852 bool success = set.split_key(42, left, right);
1853
1855 EXPECT_TRUE(left.is_empty());
1856 EXPECT_TRUE(right.is_empty());
1857}
1858
1860{
1862 DynSetTreapRk<int> left, right;
1863
1864 EXPECT_NO_THROW(set.split_key_dup(42, left, right));
1865 EXPECT_TRUE(left.is_empty());
1866 EXPECT_TRUE(right.is_empty());
1867}
1868
1869// ============================================================================
1870// Missing Coverage Tests - Rank Operations on TreapRk
1871// ============================================================================
1872
1874{
1876
1877 for (int i : {5, 3, 7, 1, 9})
1878 set.insert(i);
1879
1880 // access() is alias for select()
1881 EXPECT_EQ(set.access(0), 1);
1882 EXPECT_EQ(set.access(2), 5);
1883 EXPECT_EQ(set.access(4), 9);
1884}
1885
1887{
1889
1890 for (int i : {1, 2, 3})
1891 set.insert(i);
1892
1893 EXPECT_THROW(set.select(3), out_of_range);
1894 EXPECT_THROW(set.select(100), out_of_range);
1895}
1896
1898{
1900
1901 EXPECT_THROW(set.select(0), out_of_range);
1902}
1903
1905{
1907
1908 for (int i : {1, 2, 3})
1909 set.insert(i);
1910
1911 EXPECT_THROW(set.remove_pos(3), out_of_range);
1912 EXPECT_THROW(set.remove_pos(100), out_of_range);
1913}
1914
1916{
1918
1919 auto [pos, ptr] = set.find_position(42);
1920 EXPECT_EQ(pos, 0);
1921 EXPECT_EQ(ptr, nullptr);
1922}
1923
1925{
1927
1928 for (int i : {1, 3, 5, 7, 9})
1929 set.insert(i);
1930
1931 // Position of non-existent key
1932 EXPECT_EQ(set.position(2), -1);
1933 EXPECT_EQ(set.position(4), -1);
1934 EXPECT_EQ(set.position(100), -1);
1935}
1936
1938{
1940
1941 for (int i : {5, 3, 7, 1, 9})
1942 set.insert(i);
1943
1944 const auto & const_set = set;
1945
1946 EXPECT_EQ(const_set.select(0), 1);
1947 EXPECT_EQ(const_set.select(4), 9);
1948}
1949
1950// ============================================================================
1951// Missing Coverage Tests - Iterator Edge Cases
1952// ============================================================================
1953
1955{
1956 typename TypeParam::Iterator it(this->set);
1957
1958 EXPECT_FALSE(it.has_curr());
1959}
1960
1962{
1963 for (int i : {1, 2, 3})
1964 this->set.insert(i);
1965
1966 typename TypeParam::Iterator it(this->set);
1967
1968 // Advance to end
1969 while (it.has_curr())
1970 it.next_ne();
1971
1972 it.reset_first();
1973 EXPECT_TRUE(it.has_curr());
1974 EXPECT_EQ(it.get_curr(), 1);
1975}
1976
1978{
1979 for (int i : {1, 2, 3})
1980 this->set.insert(i);
1981
1982 typename TypeParam::Iterator it(this->set);
1983
1984 it.reset_last();
1985 EXPECT_TRUE(it.has_curr());
1986 EXPECT_EQ(it.get_curr(), 3);
1987}
1988
1989// ============================================================================
1990// Missing Coverage Tests - Traverse Early Exit
1991// ============================================================================
1992
1994{
1995 for (int i : {1, 2, 3, 4, 5})
1996 this->set.insert(i);
1997
1998 int count = 0;
1999 bool completed = this->set.traverse([&count](const int &) {
2000 ++count;
2001 return count < 3; // Stop after 3 elements
2002 });
2003
2005 EXPECT_EQ(count, 3);
2006}
2007
2009{
2010 for (int i : {1, 2, 3, 4, 5})
2011 this->set.insert(i);
2012
2013 const auto & const_set = this->set;
2014
2015 int sum = 0;
2016 bool completed = const_set.traverse([&sum](const int & k) {
2017 sum += k;
2018 return true;
2019 });
2020
2022 EXPECT_EQ(sum, 15);
2023}
2024
2025// ============================================================================
2026// Missing Coverage Tests - operator() for Non-Rank Trees
2027// ============================================================================
2028
2030{
2032
2033 for (int i : {1, 2, 3})
2034 set.insert(i);
2035
2036 // operator() calls select(), which should throw on non-rank trees
2037 EXPECT_THROW(set(0), domain_error);
2038}
2039
2040// ============================================================================
2041// Missing Coverage Tests - Join Edge Cases
2042// ============================================================================
2043
2045{
2048
2049 set1.join(set2, dup);
2050
2051 EXPECT_TRUE(set1.is_empty());
2052 EXPECT_TRUE(set2.is_empty());
2053 EXPECT_TRUE(dup.is_empty());
2054}
2055
2057{
2060
2061 for (int i : {1, 2, 3})
2062 set1.insert(i);
2063
2064 set1.join(set2, dup);
2065
2066 EXPECT_EQ(set1.size(), 3u);
2067 EXPECT_TRUE(set2.is_empty());
2068 EXPECT_TRUE(dup.is_empty());
2069}
2070
2072{
2074
2076
2077 EXPECT_TRUE(set1.is_empty());
2078 EXPECT_TRUE(set2.is_empty());
2079}
2080
2081// ============================================================================
2082// Missing Coverage Tests - Verify on Empty and Single Element
2083// ============================================================================
2084
2089
2091{
2092 this->set.insert(42);
2093 EXPECT_TRUE(this->set.verify());
2094}
2095
2096// ============================================================================
2097// Missing Coverage Tests - String Type with Rvalues
2098// ============================================================================
2099
2101{
2103
2104 string s = "hello";
2105 auto * p = set.insert(std::move(s));
2106
2107 ASSERT_NE(p, nullptr);
2108 EXPECT_EQ(*p, "hello");
2109 EXPECT_EQ(set.size(), 1u);
2110}
2111
2113{
2115
2116 string s1 = "hello";
2117 auto * p1 = set.search_or_insert(std::move(s1));
2118 ASSERT_NE(p1, nullptr);
2119 EXPECT_EQ(*p1, "hello");
2120
2121 string s2 = "hello";
2122 auto * p2 = set.search_or_insert(std::move(s2));
2123 EXPECT_EQ(p1, p2); // Same pointer (found existing)
2124}
2125
2126// ============================================================================
2127// Missing Coverage Tests - All Tree Type Aliases
2128// ============================================================================
2129
2131{
2133 set.insert(1);
2134 set.insert(2);
2135 EXPECT_EQ(set.size(), 2u);
2136 EXPECT_TRUE(set.verify());
2137}
2138
2140{
2142 set.insert(1);
2143 set.insert(2);
2144 EXPECT_EQ(set.size(), 2u);
2145 EXPECT_TRUE(set.verify());
2146}
2147
2149{
2151 set.insert(1);
2152 set.insert(2);
2153 EXPECT_EQ(set.size(), 2u);
2154 EXPECT_TRUE(set.verify());
2155}
2156
2158{
2160 set.insert(1);
2161 set.insert(2);
2162 EXPECT_EQ(set.size(), 2u);
2163 EXPECT_TRUE(set.verify());
2164}
2165
2167{
2168 DynSetTreap<int> set;
2169 set.insert(1);
2170 set.insert(2);
2171 EXPECT_EQ(set.size(), 2u);
2172 EXPECT_TRUE(set.verify());
2173}
2174
2176{
2178 set.insert(1);
2179 set.insert(2);
2180 EXPECT_EQ(set.size(), 2u);
2181 EXPECT_TRUE(set.verify());
2182}
2183
2185{
2187 set.insert(1);
2188 set.insert(2);
2189 EXPECT_EQ(set.size(), 2u);
2190 EXPECT_TRUE(set.verify());
2191}
2192
2193// ============================================================================
2194// Stress and Fuzz Tests
2195// ============================================================================
2196
2198{
2199 const int N = 5000;
2200 for (int k = 0; k < N; ++k)
2201 this->set.insert(k);
2202
2203 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2204 EXPECT_TRUE(this->set.verify());
2205 EXPECT_EQ(this->set.min(), 0);
2206 EXPECT_EQ(this->set.max(), N - 1);
2207}
2208
2210{
2211 const int N = 5000;
2212 for (int k = N - 1; k >= 0; --k)
2213 this->set.insert(k);
2214
2215 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2216 EXPECT_TRUE(this->set.verify());
2217}
2218
2220{
2221 const int N = 3000;
2222
2223 for (int k = 0; k < N; ++k)
2224 this->set.insert(k);
2225
2226 EXPECT_EQ(this->set.size(), static_cast<size_t>(N));
2227
2228 // Remove all
2229 for (int k = 0; k < N; ++k)
2230 this->set.remove(k);
2231
2232 EXPECT_TRUE(this->set.is_empty());
2233}
2234
2236{
2237 std::set<int> oracle;
2238 std::mt19937 gen(12345);
2239 std::uniform_int_distribution<> key_dist(0, 1000);
2240 std::uniform_int_distribution<> op_dist(0, 2);
2241
2242 for (int iter = 0; iter < 5000; ++iter)
2243 {
2244 int key = key_dist(gen);
2245 int op = op_dist(gen);
2246
2247 if (op == 0) // insert
2248 {
2249 if (this->set.insert(key) != nullptr)
2250 oracle.insert(key);
2251 }
2252 else if (op == 1 && !oracle.empty()) // remove
2253 {
2254 auto it = oracle.begin();
2255 std::advance(it, gen() % oracle.size());
2256 int k = *it;
2257
2258 this->set.remove(k);
2259 oracle.erase(k);
2260 }
2261 else // search
2262 {
2263 bool in_set = this->set.contains(key);
2264 bool in_oracle = oracle.count(key) > 0;
2266 }
2267
2268 EXPECT_EQ(this->set.size(), oracle.size());
2269 }
2270
2271 EXPECT_TRUE(this->set.verify());
2272}
2273
2275{
2276 std::set<int> oracle;
2277 std::mt19937 gen(54321);
2278 std::uniform_int_distribution<> key_dist(0, 500);
2279
2280 for (int iter = 0; iter < 3000; ++iter)
2281 {
2282 int key = key_dist(gen);
2283
2284 if (iter % 2 == 0) // insert
2285 {
2286 if (this->set.insert(key) != nullptr)
2287 oracle.insert(key);
2288 }
2289 else if (!oracle.empty()) // remove random existing
2290 {
2291 auto it = oracle.begin();
2292 std::advance(it, gen() % oracle.size());
2293 int k = *it;
2294
2295 this->set.remove(k);
2296 oracle.erase(k);
2297 }
2298
2299 EXPECT_EQ(this->set.size(), oracle.size());
2300 }
2301
2302 EXPECT_TRUE(this->set.verify());
2303}
2304
2306{
2307 // Test each tree type with larger data
2308 auto test_tree = [](auto & tree) {
2309 std::set<int> oracle;
2310 std::mt19937 gen(99999);
2311 std::uniform_int_distribution<> key_dist(0, 10000);
2312 std::uniform_int_distribution<> op_dist(0, 2);
2313
2314 for (int iter = 0; iter < 10000; ++iter)
2315 {
2316 int key = key_dist(gen);
2317 int op = op_dist(gen);
2318
2319 if (op == 0)
2320 {
2321 if (tree.insert(key) != nullptr)
2322 oracle.insert(key);
2323 }
2324 else if (op == 1 && !oracle.empty())
2325 {
2326 auto it = oracle.begin();
2327 std::advance(it, gen() % oracle.size());
2328 int k = *it;
2329 tree.remove(k);
2330 oracle.erase(k);
2331 }
2332 else
2333 {
2334 EXPECT_EQ(tree.contains(key), oracle.count(key) > 0);
2335 }
2336 }
2337
2338 EXPECT_EQ(tree.size(), oracle.size());
2339 EXPECT_TRUE(tree.verify());
2340 };
2341
2342 {
2343 DynSetAvlTree<int> tree;
2344 test_tree(tree);
2345 }
2346 {
2347 DynSetRbTree<int> tree;
2348 test_tree(tree);
2349 }
2350 {
2351 DynSetTreap<int> tree;
2352 test_tree(tree);
2353 }
2354 {
2356 test_tree(tree);
2357 }
2358}
2359
2360int main(int argc, char **argv)
2361{
2362 ::testing::InitGoogleTest(&argc, argv);
2363 return RUN_ALL_TESTS();
2364}
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:142
WeightedDigraph::Node Node
long double h
Definition btreepic.C:154
Node for binary search tree.
static BinNode *const NullPtr
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
Checks if a key exists in the set.
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
#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
DynSetTree & join(DynSetTree &t, DynSetTree &dup)
Union of two binary search trees.
DynSetTree & join_dup(DynSetTree &t)
Union of two binary search trees.
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:136
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
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Definition ahAlgo.H:230
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Definition ahAlgo.H:962
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Definition ahAlgo.H:309
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:737
void test_tree(int n)
int keys[]
static int * k
Dynamic set implementations based on balanced binary search trees.