-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathtest_private_cavl.cpp
1373 lines (1315 loc) · 45.9 KB
/
test_private_cavl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This software is distributed under the terms of the MIT License.
// Copyright (c) 2016-2020 OpenCyphal Development Team.
// These tests have been adapted from the Cavl test suite that you can find at https://github.com/pavel-kirienko/cavl
#include <_canard_cavl.h>
#include "catch.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdlib>
#include <optional>
#include <numeric>
#include <iostream>
namespace
{
/// These aliases are introduced to keep things nicely aligned in test cases.
constexpr auto Zz = nullptr;
constexpr auto Zzzzz = nullptr;
constexpr auto Zzzzzz = nullptr;
template <typename T>
struct Node final : Cavl
{
explicit Node(const T val) : Cavl{Cavl{}}, value(val) {}
Node(const Cavl& cv, const T val) : Cavl{cv}, value(val) {}
Node() : Cavl{Cavl{}} {}
T value{};
auto checkLinkageUpLeftRightBF(const Cavl* const check_up,
const Cavl* const check_le,
const Cavl* const check_ri,
const std::int8_t check_bf) const -> bool
{
return (up == check_up) && //
(lr[0] == check_le) && (lr[1] == check_ri) && //
(bf == check_bf) && //
((check_up == nullptr) || (check_up->lr[0] == this) || (check_up->lr[1] == this)) && //
((check_le == nullptr) || (check_le->up == this)) && //
((check_ri == nullptr) || (check_ri->up == this));
}
auto min() -> Node* { return reinterpret_cast<Node*>(cavlFindExtremum(this, false)); }
auto max() -> Node* { return reinterpret_cast<Node*>(cavlFindExtremum(this, true)); }
auto operator=(const Cavl& cv) -> Node&
{
static_cast<Cavl&>(*this) = cv;
return *this;
}
};
/// Wrapper over cavlSearch() that supports closures.
template <typename T, typename Predicate, typename Factory>
auto search(Node<T>** const root, const Predicate& predicate, const Factory& factory) -> Node<T>*
{
struct Refs
{
Predicate predicate;
Factory factory;
static auto callPredicate(void* const user_reference, const Cavl* const node) -> std::int8_t
{
const auto ret = static_cast<Refs*>(user_reference)->predicate(reinterpret_cast<const Node<T>&>(*node));
if (ret > 0)
{
return 1;
}
if (ret < 0)
{
return -1;
}
return 0;
}
static auto callFactory(void* const user_reference) -> Cavl*
{
return static_cast<Refs*>(user_reference)->factory();
}
} refs{predicate, factory};
Cavl* const out = cavlSearch(reinterpret_cast<Cavl**>(root), &refs, &Refs::callPredicate, &Refs::callFactory);
return reinterpret_cast<Node<T>*>(out);
}
template <typename T, typename Predicate>
auto search(Node<T>** const root, const Predicate& predicate) -> Node<T>*
{
return search<T, Predicate>(root, predicate, []() { return nullptr; });
}
/// Wrapper over cavlRemove().
template <typename T>
void remove(Node<T>** const root, const Node<T>* const n)
{
cavlRemove(reinterpret_cast<Cavl**>(root), n);
}
template <typename T>
auto getHeight(const Node<T>* const n) -> std::uint8_t // NOLINT recursion
{
return (n != nullptr) ? static_cast<std::uint8_t>(1U + std::max(getHeight(reinterpret_cast<Node<T>*>(n->lr[0])),
getHeight(reinterpret_cast<Node<T>*>(n->lr[1]))))
: 0;
}
template <typename T>
void print(const Node<T>* const nd, const std::uint8_t depth = 0, const char marker = 'T') // NOLINT recursion
{
REQUIRE(10 > getHeight(nd)); // Fail early for malformed cyclic trees, do not overwhelm stdout.
if (nd != nullptr)
{
print<T>(reinterpret_cast<const Node<T>*>(nd->lr[0]), static_cast<std::uint8_t>(depth + 1U), 'L');
for (std::uint16_t i = 1U; i < depth; i++)
{
std::cout << " ";
}
if (marker == 'L')
{
std::cout << " .............";
}
else if (marker == 'R')
{
std::cout << " `````````````";
}
else
{
(void) 0;
}
std::cout << marker << "=" << static_cast<std::int64_t>(nd->value) //
<< " [" << static_cast<std::int16_t>(nd->bf) << "]" << std::endl;
print<T>(reinterpret_cast<const Node<T>*>(nd->lr[1]), static_cast<std::uint8_t>(depth + 1U), 'R');
}
}
template <bool Ascending, typename Node, typename Visitor>
void traverse(Node* const root, const Visitor& visitor) // NOLINT recursion needed for testing
{
if (root != nullptr)
{
traverse<Ascending, Node, Visitor>(reinterpret_cast<Node*>(root->lr[!Ascending]), visitor);
visitor(root);
traverse<Ascending, Node, Visitor>(reinterpret_cast<Node*>(root->lr[Ascending]), visitor);
}
}
template <typename T>
auto checkAscension(const Node<T>* const root) -> std::optional<std::size_t>
{
const Node<T>* prev = nullptr;
bool valid = true;
std::size_t size = 0;
traverse<true, const Node<T>>(root, [&](const Node<T>* const nd) {
if (prev != nullptr)
{
valid = valid && (prev->value < nd->value);
}
prev = nd;
size++;
});
return valid ? std::optional<std::size_t>(size) : std::optional<std::size_t>{};
}
template <typename T>
auto findBrokenAncestry(const Node<T>* const n, const Cavl* const parent = nullptr) // NOLINT recursion
-> const Node<T>*
{
if ((n != nullptr) && (n->up == parent))
{
for (auto* ch : n->lr) // NOLINT array decay due to C API
{
if (const Node<T>* p = findBrokenAncestry(reinterpret_cast<Node<T>*>(ch), n))
{
return p;
}
}
return nullptr;
}
return n;
}
template <typename T>
auto findBrokenBalanceFactor(const Node<T>* const n) -> const Cavl* // NOLINT recursion
{
if (n != nullptr)
{
if (std::abs(n->bf) > 1)
{
return n;
}
const std::int16_t hl = getHeight(reinterpret_cast<Node<T>*>(n->lr[0]));
const std::int16_t hr = getHeight(reinterpret_cast<Node<T>*>(n->lr[1]));
if (n->bf != (hr - hl))
{
return n;
}
for (auto* ch : n->lr) // NOLINT array decay due to C API
{
if (const Cavl* p = findBrokenBalanceFactor(reinterpret_cast<Node<T>*>(ch)))
{
return p;
}
}
}
return nullptr;
}
} // namespace
TEST_CASE("CheckAscension")
{
using N = Node<std::uint8_t>;
N t{2};
N l{1};
N r{3};
N rr{4};
// Correctly arranged tree -- smaller items on the left.
t.lr[0] = &l;
t.lr[1] = &r;
r.lr[1] = &rr;
REQUIRE(4 == checkAscension(&t));
REQUIRE(3 == getHeight(&t));
// Break the arrangement and make sure the breakage is detected.
t.lr[1] = &l;
t.lr[0] = &r;
REQUIRE(4 != checkAscension(&t));
REQUIRE(3 == getHeight(&t));
REQUIRE(&t == findBrokenBalanceFactor(&t)); // All zeros, incorrect.
r.lr[1] = nullptr;
REQUIRE(2 == getHeight(&t));
REQUIRE(nullptr == findBrokenBalanceFactor(&t)); // Balanced now as we removed one node.
}
TEST_CASE("Rotation")
{
using N = Node<std::uint8_t>;
// Original state:
// x.left = a
// x.right = z
// z.left = b
// z.right = c
// After left rotation of X:
// x.left = a
// x.right = b
// z.left = x
// z.right = c
N c{{Zz, {Zz, Zz}, 0}, 3};
N b{{Zz, {Zz, Zz}, 0}, 2};
N a{{Zz, {Zz, Zz}, 0}, 1};
N z{{Zz, {&b, &c}, 0}, 8};
N x{{Zz, {&a, &z}, 1}, 9};
z.up = &x;
c.up = &z;
b.up = &z;
a.up = &x;
std::cout << "Before rotation:\n";
REQUIRE(nullptr == findBrokenAncestry(&x));
print(&x);
std::cout << "After left rotation:\n";
cavlPrivateRotate(&x, false); // z is now the root
REQUIRE(nullptr == findBrokenAncestry(&z));
print(&z);
REQUIRE(&a == x.lr[0]);
REQUIRE(&b == x.lr[1]);
REQUIRE(&x == z.lr[0]);
REQUIRE(&c == z.lr[1]);
std::cout << "After right rotation, back into the original configuration:\n";
cavlPrivateRotate(&z, true); // x is now the root
REQUIRE(nullptr == findBrokenAncestry(&x));
print(&x);
REQUIRE(&a == x.lr[0]);
REQUIRE(&z == x.lr[1]);
REQUIRE(&b == z.lr[0]);
REQUIRE(&c == z.lr[1]);
}
TEST_CASE("BalancingA")
{
using N = Node<std::uint8_t>;
// Double left-right rotation.
// X X Y
// / ` / ` / `
// Z C => Y C => Z X
// / ` / ` / ` / `
// D Y Z G D F G C
// / ` / `
// F G D F
N x{{Zz, {Zz, Zz}, 0}, 1}; // bf = -2
N z{{&x, {Zz, Zz}, 0}, 2}; // bf = +1
N c{{&x, {Zz, Zz}, 0}, 3};
N d{{&z, {Zz, Zz}, 0}, 4};
N y{{&z, {Zz, Zz}, 0}, 5};
N f{{&y, {Zz, Zz}, 0}, 6};
N g{{&y, {Zz, Zz}, 0}, 7};
x.lr[0] = &z;
x.lr[1] = &c;
z.lr[0] = &d;
z.lr[1] = &y;
y.lr[0] = &f;
y.lr[1] = &g;
print(&x);
REQUIRE(nullptr == findBrokenAncestry(&x));
REQUIRE(&x == cavlPrivateAdjustBalance(&x, false)); // bf = -1, same topology
REQUIRE(-1 == x.bf);
REQUIRE(&z == cavlPrivateAdjustBalance(&z, true)); // bf = +1, same topology
REQUIRE(+1 == z.bf);
REQUIRE(&y == cavlPrivateAdjustBalance(&x, false)); // bf = -2, rotation needed
print(&y);
REQUIRE(nullptr == findBrokenBalanceFactor(&y)); // Should be balanced now.
REQUIRE(nullptr == findBrokenAncestry(&y));
REQUIRE(&z == y.lr[0]);
REQUIRE(&x == y.lr[1]);
REQUIRE(&d == z.lr[0]);
REQUIRE(&f == z.lr[1]);
REQUIRE(&g == x.lr[0]);
REQUIRE(&c == x.lr[1]);
REQUIRE(Zz == d.lr[0]);
REQUIRE(Zz == d.lr[1]);
REQUIRE(Zz == f.lr[0]);
REQUIRE(Zz == f.lr[1]);
REQUIRE(Zz == g.lr[0]);
REQUIRE(Zz == g.lr[1]);
REQUIRE(Zz == c.lr[0]);
REQUIRE(Zz == c.lr[1]);
}
TEST_CASE("BalancingB")
{
using N = Node<std::uint8_t>;
// Without F the handling of Z and Y is more complex; Z flips the sign of its balance factor:
// X X Y
// / ` / ` / `
// Z C => Y C => Z X
// / ` / ` / / `
// D Y Z G D G C
// ` /
// G D
N x{};
N z{};
N c{};
N d{};
N y{};
N g{};
x = {{Zz, {&z, &c}, 0}, 1}; // bf = -2
z = {{&x, {&d, &y}, 0}, 2}; // bf = +1
c = {{&x, {Zz, Zz}, 0}, 3};
d = {{&z, {Zz, Zz}, 0}, 4};
y = {{&z, {Zz, &g}, 0}, 5}; // bf = +1
g = {{&y, {Zz, Zz}, 0}, 7};
print(&x);
REQUIRE(nullptr == findBrokenAncestry(&x));
REQUIRE(&x == cavlPrivateAdjustBalance(&x, false)); // bf = -1, same topology
REQUIRE(-1 == x.bf);
REQUIRE(&z == cavlPrivateAdjustBalance(&z, true)); // bf = +1, same topology
REQUIRE(+1 == z.bf);
REQUIRE(&y == cavlPrivateAdjustBalance(&y, true)); // bf = +1, same topology
REQUIRE(+1 == y.bf);
REQUIRE(&y == cavlPrivateAdjustBalance(&x, false)); // bf = -2, rotation needed
print(&y);
REQUIRE(nullptr == findBrokenBalanceFactor(&y)); // Should be balanced now.
REQUIRE(nullptr == findBrokenAncestry(&y));
REQUIRE(&z == y.lr[0]);
REQUIRE(&x == y.lr[1]);
REQUIRE(&d == z.lr[0]);
REQUIRE(Zz == z.lr[1]);
REQUIRE(&g == x.lr[0]);
REQUIRE(&c == x.lr[1]);
REQUIRE(Zz == d.lr[0]);
REQUIRE(Zz == d.lr[1]);
REQUIRE(Zz == g.lr[0]);
REQUIRE(Zz == g.lr[1]);
REQUIRE(Zz == c.lr[0]);
REQUIRE(Zz == c.lr[1]);
}
TEST_CASE("BalancingC")
{
using N = Node<std::uint8_t>;
// Both X and Z are heavy on the same side.
// X Z
// / ` / `
// Z C => D X
// / ` / ` / `
// D Y F G Y C
// / `
// F G
N x{};
N z{};
N c{};
N d{};
N y{};
N f{};
N g{};
x = {{Zz, {&z, &c}, 0}, 1}; // bf = -2
z = {{&x, {&d, &y}, 0}, 2}; // bf = -1
c = {{&x, {Zz, Zz}, 0}, 3};
d = {{&z, {&f, &g}, 0}, 4};
y = {{&z, {Zz, Zz}, 0}, 5};
f = {{&d, {Zz, Zz}, 0}, 6};
g = {{&d, {Zz, Zz}, 0}, 7};
print(&x);
REQUIRE(nullptr == findBrokenAncestry(&x));
REQUIRE(&x == cavlPrivateAdjustBalance(&x, false)); // bf = -1, same topology
REQUIRE(-1 == x.bf);
REQUIRE(&z == cavlPrivateAdjustBalance(&z, false)); // bf = -1, same topology
REQUIRE(-1 == z.bf);
REQUIRE(&z == cavlPrivateAdjustBalance(&x, false));
print(&z);
REQUIRE(nullptr == findBrokenBalanceFactor(&z));
REQUIRE(nullptr == findBrokenAncestry(&z));
REQUIRE(&d == z.lr[0]);
REQUIRE(&x == z.lr[1]);
REQUIRE(&f == d.lr[0]);
REQUIRE(&g == d.lr[1]);
REQUIRE(&y == x.lr[0]);
REQUIRE(&c == x.lr[1]);
REQUIRE(Zz == f.lr[0]);
REQUIRE(Zz == f.lr[1]);
REQUIRE(Zz == g.lr[0]);
REQUIRE(Zz == g.lr[1]);
REQUIRE(Zz == y.lr[0]);
REQUIRE(Zz == y.lr[1]);
REQUIRE(Zz == c.lr[0]);
REQUIRE(Zz == c.lr[1]);
}
TEST_CASE("RetracingOnGrowth")
{
using N = Node<std::uint8_t>;
std::array<N, 100> t{};
for (std::uint8_t i = 0; i < 100; i++)
{
t[i].value = i;
}
// 50 30
// / ` / `
// 30 60? => 20 50
// / ` / / `
// 20 40? 10 40? 60?
// /
// 10
t[50] = {Zzzzzz, {&t[30], &t[60]}, -1};
t[30] = {&t[50], {&t[20], &t[40]}, 00};
t[60] = {&t[50], {Zzzzzz, Zzzzzz}, 00};
t[20] = {&t[30], {&t[10], Zzzzzz}, 00};
t[40] = {&t[30], {Zzzzzz, Zzzzzz}, 00};
t[10] = {&t[20], {Zzzzzz, Zzzzzz}, 00};
print(&t[50]); // The tree is imbalanced because we just added 1 and are about to retrace it.
REQUIRE(nullptr == findBrokenAncestry(&t[50]));
REQUIRE(6 == checkAscension(&t[50]));
REQUIRE(&t[30] == cavlPrivateRetraceOnGrowth(&t[10]));
std::puts("ADD 10:");
print(&t[30]); // This is the new root.
REQUIRE(&t[20] == t[30].lr[0]);
REQUIRE(&t[50] == t[30].lr[1]);
REQUIRE(&t[10] == t[20].lr[0]);
REQUIRE(Zzzzzz == t[20].lr[1]);
REQUIRE(&t[40] == t[50].lr[0]);
REQUIRE(&t[60] == t[50].lr[1]);
REQUIRE(Zzzzzz == t[10].lr[0]);
REQUIRE(Zzzzzz == t[10].lr[1]);
REQUIRE(Zzzzzz == t[40].lr[0]);
REQUIRE(Zzzzzz == t[40].lr[1]);
REQUIRE(Zzzzzz == t[60].lr[0]);
REQUIRE(Zzzzzz == t[60].lr[1]);
REQUIRE(-1 == t[20].bf);
REQUIRE(+0 == t[30].bf);
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
REQUIRE(6 == checkAscension(&t[30]));
// Add a new child under 20 and ensure that retracing stops at 20 because it becomes perfectly balanced:
// 30
// / `
// 20 50
// / ` / `
// 10 21 40 60
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
t[21] = {&t[20], {Zzzzzz, Zzzzzz}, 0};
t[20].lr[1] = &t[21];
REQUIRE(nullptr == cavlPrivateRetraceOnGrowth(&t[21])); // Root not reached, NULL returned.
std::puts("ADD 21:");
print(&t[30]);
REQUIRE(0 == t[20].bf);
REQUIRE(0 == t[30].bf);
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
REQUIRE(7 == checkAscension(&t[30]));
// 30
// / `
// 20 50
// / ` / `
// 10 21 40 60
// `
// 15 <== first we add this, no balancing needed
// `
// 17 <== then we add this, forcing left rotation at 10
//
// After the left rotation of 10, we get:
//
// 30
// / `
// 20 50
// / ` / `
// 15 21 40 60
// / `
// 10 17
//
// When we add one extra item after 17, we force a double rotation (15 left, 20 right). Before the rotation:
//
// 30
// / `
// 20 50
// / ` / `
// 15 21 40 60
// / `
// 10 17
// `
// 18 <== new item causes imbalance
//
// After left rotation of 15:
//
// 30
// / `
// 20 50
// / ` / `
// 17 21 40 60
// / `
// 15 18
// /
// 10
//
// After right rotation of 20, this is the final state:
//
// 30
// / `
// 17 50
// / ` / `
// 15 20 40 60
// / / `
// 10 18 21
std::puts("ADD 15:");
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
REQUIRE(7 == checkAscension(&t[30]));
t[15] = {&t[10], {Zzzzzz, Zzzzzz}, 0};
t[10].lr[1] = &t[15];
REQUIRE(&t[30] == cavlPrivateRetraceOnGrowth(&t[15])); // Same root, its balance becomes -1.
print(&t[30]);
REQUIRE(+1 == t[10].bf);
REQUIRE(-1 == t[20].bf);
REQUIRE(-1 == t[30].bf);
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
REQUIRE(8 == checkAscension(&t[30]));
std::puts("ADD 17:");
t[17] = {&t[15], {Zzzzzz, Zzzzzz}, 0};
t[15].lr[1] = &t[17];
REQUIRE(nullptr == cavlPrivateRetraceOnGrowth(&t[17])); // Same root, same balance, 10 rotated left.
print(&t[30]);
// Check 10
REQUIRE(&t[15] == t[10].up);
REQUIRE(0 == t[10].bf);
REQUIRE(nullptr == t[10].lr[0]);
REQUIRE(nullptr == t[10].lr[1]);
// Check 17
REQUIRE(&t[15] == t[17].up);
REQUIRE(0 == t[17].bf);
REQUIRE(nullptr == t[17].lr[0]);
REQUIRE(nullptr == t[17].lr[1]);
// Check 15
REQUIRE(&t[20] == t[15].up);
REQUIRE(0 == t[15].bf);
REQUIRE(&t[10] == t[15].lr[0]);
REQUIRE(&t[17] == t[15].lr[1]);
// Check 20 -- leaning left
REQUIRE(&t[30] == t[20].up);
REQUIRE(-1 == t[20].bf);
REQUIRE(&t[15] == t[20].lr[0]);
REQUIRE(&t[21] == t[20].lr[1]);
// Check the root -- still leaning left by one.
REQUIRE(nullptr == t[30].up);
REQUIRE(-1 == t[30].bf);
REQUIRE(&t[20] == t[30].lr[0]);
REQUIRE(&t[50] == t[30].lr[1]);
// Check hard invariants.
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
REQUIRE(9 == checkAscension(&t[30]));
std::puts("ADD 18:");
t[18] = {&t[17], {Zzzzzz, Zzzzzz}, 0};
t[17].lr[1] = &t[18];
REQUIRE(nullptr == cavlPrivateRetraceOnGrowth(&t[18])); // Same root, 15 went left, 20 went right.
print(&t[30]);
// Check 17
REQUIRE(&t[30] == t[17].up);
REQUIRE(0 == t[17].bf);
REQUIRE(&t[15] == t[17].lr[0]);
REQUIRE(&t[20] == t[17].lr[1]);
// Check 15
REQUIRE(&t[17] == t[15].up);
REQUIRE(-1 == t[15].bf);
REQUIRE(&t[10] == t[15].lr[0]);
REQUIRE(nullptr == t[15].lr[1]);
// Check 20
REQUIRE(&t[17] == t[20].up);
REQUIRE(0 == t[20].bf);
REQUIRE(&t[18] == t[20].lr[0]);
REQUIRE(&t[21] == t[20].lr[1]);
// Check 10
REQUIRE(&t[15] == t[10].up);
REQUIRE(0 == t[10].bf);
REQUIRE(nullptr == t[10].lr[0]);
REQUIRE(nullptr == t[10].lr[1]);
// Check 18
REQUIRE(&t[20] == t[18].up);
REQUIRE(0 == t[18].bf);
REQUIRE(nullptr == t[18].lr[0]);
REQUIRE(nullptr == t[18].lr[1]);
// Check 21
REQUIRE(&t[20] == t[21].up);
REQUIRE(0 == t[21].bf);
REQUIRE(nullptr == t[21].lr[0]);
REQUIRE(nullptr == t[21].lr[1]);
// Check hard invariants.
REQUIRE(nullptr == findBrokenAncestry(&t[30]));
REQUIRE(nullptr == findBrokenBalanceFactor(&t[30]));
REQUIRE(10 == checkAscension(&t[30]));
}
TEST_CASE("SearchTrivial")
{
using N = Node<std::uint8_t>;
// A
// B C
// D E F G
N a{4};
N b{2};
N c{6};
N d{1};
N e{3};
N f{5};
N g{7};
N q{9};
a = {Zz, {&b, &c}, 0};
b = {&a, {&d, &e}, 0};
c = {&a, {&f, &g}, 0};
d = {&b, {Zz, Zz}, 0};
e = {&b, {Zz, Zz}, 0};
f = {&c, {Zz, Zz}, 0};
g = {&c, {Zz, Zz}, 0};
q = {Zz, {Zz, Zz}, 0};
REQUIRE(nullptr == findBrokenBalanceFactor(&a));
REQUIRE(nullptr == findBrokenAncestry(&a));
REQUIRE(7 == checkAscension(&a));
N* root = &a;
REQUIRE(nullptr == cavlSearch(reinterpret_cast<Cavl**>(&root), nullptr, nullptr, nullptr)); // Bad arguments.
REQUIRE(&a == root);
REQUIRE(nullptr == search(&root, [&](const N& v) { return q.value - v.value; }));
REQUIRE(&a == root);
REQUIRE(&e == search(&root, [&](const N& v) { return e.value - v.value; }));
REQUIRE(&b == search(&root, [&](const N& v) { return b.value - v.value; }));
REQUIRE(&a == root);
print(&a);
REQUIRE(nullptr == cavlFindExtremum(nullptr, true));
REQUIRE(nullptr == cavlFindExtremum(nullptr, false));
REQUIRE(&g == a.max());
REQUIRE(&d == a.min());
REQUIRE(&g == g.max());
REQUIRE(&g == g.min());
REQUIRE(&d == d.max());
REQUIRE(&d == d.min());
}
TEST_CASE("RemovalA")
{
using N = Node<std::uint8_t>;
// 4
// / `
// 2 6
// / ` / `
// 1 3 5 8
// / `
// 7 9
std::array<N, 10> t{};
for (std::uint8_t i = 0; i < 10; i++)
{
t[i].value = i;
}
t[1] = {&t[2], {Zzzzz, Zzzzz}, 00};
t[2] = {&t[4], {&t[1], &t[3]}, 00};
t[3] = {&t[2], {Zzzzz, Zzzzz}, 00};
t[4] = {Zzzzz, {&t[2], &t[6]}, +1};
t[5] = {&t[6], {Zzzzz, Zzzzz}, 00};
t[6] = {&t[4], {&t[5], &t[8]}, +1};
t[7] = {&t[8], {Zzzzz, Zzzzz}, 00};
t[8] = {&t[6], {&t[7], &t[9]}, 00};
t[9] = {&t[8], {Zzzzz, Zzzzz}, 00};
N* root = &t[4];
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(9 == checkAscension(root));
// Remove 9, the easiest case. The rest of the tree remains unchanged.
// 4
// / `
// 2 6
// / ` / `
// 1 3 5 8
// /
// 7
std::puts("REMOVE 9:");
remove(&root, &t[9]);
REQUIRE(&t[4] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(8 == checkAscension(root));
// 1
REQUIRE(&t[2] == t[1].up);
REQUIRE(Zzzzz == t[1].lr[0]);
REQUIRE(Zzzzz == t[1].lr[1]);
REQUIRE(00 == t[1].bf);
// 2
REQUIRE(&t[4] == t[2].up);
REQUIRE(&t[1] == t[2].lr[0]);
REQUIRE(&t[3] == t[2].lr[1]);
REQUIRE(00 == t[2].bf);
// 3
REQUIRE(&t[2] == t[3].up);
REQUIRE(Zzzzz == t[3].lr[0]);
REQUIRE(Zzzzz == t[3].lr[1]);
REQUIRE(00 == t[3].bf);
// 4
REQUIRE(Zzzzz == t[4].up); // Nihil Supernum
REQUIRE(&t[2] == t[4].lr[0]);
REQUIRE(&t[6] == t[4].lr[1]);
REQUIRE(+1 == t[4].bf);
// 5
REQUIRE(&t[6] == t[5].up);
REQUIRE(Zzzzz == t[5].lr[0]);
REQUIRE(Zzzzz == t[5].lr[1]);
REQUIRE(00 == t[5].bf);
// 6
REQUIRE(&t[4] == t[6].up);
REQUIRE(&t[5] == t[6].lr[0]);
REQUIRE(&t[8] == t[6].lr[1]);
REQUIRE(+1 == t[6].bf);
// 7
REQUIRE(&t[8] == t[7].up);
REQUIRE(Zzzzz == t[7].lr[0]);
REQUIRE(Zzzzz == t[7].lr[1]);
REQUIRE(00 == t[7].bf);
// 8
REQUIRE(&t[6] == t[8].up);
REQUIRE(&t[7] == t[8].lr[0]);
REQUIRE(Zzzzz == t[8].lr[1]);
REQUIRE(-1 == t[8].bf);
// Remove 8, 7 takes its place (the one-child case). The rest of the tree remains unchanged.
// 4
// / `
// 2 6
// / ` / `
// 1 3 5 7
std::puts("REMOVE 8:");
remove(&root, &t[8]);
REQUIRE(&t[4] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(7 == checkAscension(root));
// 1
REQUIRE(&t[2] == t[1].up);
REQUIRE(Zzzzz == t[1].lr[0]);
REQUIRE(Zzzzz == t[1].lr[1]);
REQUIRE(00 == t[1].bf);
// 2
REQUIRE(&t[4] == t[2].up);
REQUIRE(&t[1] == t[2].lr[0]);
REQUIRE(&t[3] == t[2].lr[1]);
REQUIRE(00 == t[2].bf);
// 3
REQUIRE(&t[2] == t[3].up);
REQUIRE(Zzzzz == t[3].lr[0]);
REQUIRE(Zzzzz == t[3].lr[1]);
REQUIRE(00 == t[3].bf);
// 4
REQUIRE(Zzzzz == t[4].up); // Nihil Supernum
REQUIRE(&t[2] == t[4].lr[0]);
REQUIRE(&t[6] == t[4].lr[1]);
REQUIRE(00 == t[4].bf);
// 5
REQUIRE(&t[6] == t[5].up);
REQUIRE(Zzzzz == t[5].lr[0]);
REQUIRE(Zzzzz == t[5].lr[1]);
REQUIRE(00 == t[5].bf);
// 6
REQUIRE(&t[4] == t[6].up);
REQUIRE(&t[5] == t[6].lr[0]);
REQUIRE(&t[7] == t[6].lr[1]);
REQUIRE(00 == t[6].bf);
// 7
REQUIRE(&t[6] == t[7].up);
REQUIRE(Zzzzz == t[7].lr[0]);
REQUIRE(Zzzzz == t[7].lr[1]);
REQUIRE(00 == t[7].bf);
// Remove the root node 4, 5 takes its place. The overall structure remains unchanged except that 5 is now the root.
// 5
// / `
// 2 6
// / ` `
// 1 3 7
std::puts("REMOVE 4:");
remove(&root, &t[4]);
REQUIRE(&t[5] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(6 == checkAscension(root));
// 1
REQUIRE(&t[2] == t[1].up);
REQUIRE(Zzzzz == t[1].lr[0]);
REQUIRE(Zzzzz == t[1].lr[1]);
REQUIRE(00 == t[1].bf);
// 2
REQUIRE(&t[5] == t[2].up);
REQUIRE(&t[1] == t[2].lr[0]);
REQUIRE(&t[3] == t[2].lr[1]);
REQUIRE(00 == t[2].bf);
// 3
REQUIRE(&t[2] == t[3].up);
REQUIRE(Zzzzz == t[3].lr[0]);
REQUIRE(Zzzzz == t[3].lr[1]);
REQUIRE(00 == t[3].bf);
// 5
REQUIRE(Zzzzz == t[5].up); // Nihil Supernum
REQUIRE(&t[2] == t[5].lr[0]);
REQUIRE(&t[6] == t[5].lr[1]);
REQUIRE(00 == t[5].bf);
// 6
REQUIRE(&t[5] == t[6].up);
REQUIRE(Zzzzz == t[6].lr[0]);
REQUIRE(&t[7] == t[6].lr[1]);
REQUIRE(+1 == t[6].bf);
// 7
REQUIRE(&t[6] == t[7].up);
REQUIRE(Zzzzz == t[7].lr[0]);
REQUIRE(Zzzzz == t[7].lr[1]);
REQUIRE(00 == t[7].bf);
// Remove the root node 5, 6 takes its place.
// 6
// / `
// 2 7
// / `
// 1 3
std::puts("REMOVE 5:");
remove(&root, &t[5]);
REQUIRE(&t[6] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(5 == checkAscension(root));
// 1
REQUIRE(&t[2] == t[1].up);
REQUIRE(Zzzzz == t[1].lr[0]);
REQUIRE(Zzzzz == t[1].lr[1]);
REQUIRE(00 == t[1].bf);
// 2
REQUIRE(&t[6] == t[2].up);
REQUIRE(&t[1] == t[2].lr[0]);
REQUIRE(&t[3] == t[2].lr[1]);
REQUIRE(00 == t[2].bf);
// 3
REQUIRE(&t[2] == t[3].up);
REQUIRE(Zzzzz == t[3].lr[0]);
REQUIRE(Zzzzz == t[3].lr[1]);
REQUIRE(00 == t[3].bf);
// 6
REQUIRE(Zzzzz == t[6].up); // Nihil Supernum
REQUIRE(&t[2] == t[6].lr[0]);
REQUIRE(&t[7] == t[6].lr[1]);
REQUIRE(-1 == t[6].bf);
// 7
REQUIRE(&t[6] == t[7].up);
REQUIRE(Zzzzz == t[7].lr[0]);
REQUIRE(Zzzzz == t[7].lr[1]);
REQUIRE(00 == t[7].bf);
// Remove the root node 6, 7 takes its place, then right rotation is done to restore balance, 2 is the new root.
// 2
// / `
// 1 7
// /
// 3
std::puts("REMOVE 6:");
remove(&root, &t[6]);
REQUIRE(&t[2] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(4 == checkAscension(root));
// 1
REQUIRE(&t[2] == t[1].up);
REQUIRE(Zzzzz == t[1].lr[0]);
REQUIRE(Zzzzz == t[1].lr[1]);
REQUIRE(00 == t[1].bf);
// 2
REQUIRE(Zzzzz == t[2].up); // Nihil Supernum
REQUIRE(&t[1] == t[2].lr[0]);
REQUIRE(&t[7] == t[2].lr[1]);
REQUIRE(+1 == t[2].bf);
// 3
REQUIRE(&t[7] == t[3].up);
REQUIRE(Zzzzz == t[3].lr[0]);
REQUIRE(Zzzzz == t[3].lr[1]);
REQUIRE(00 == t[3].bf);
// 7
REQUIRE(&t[2] == t[7].up);
REQUIRE(&t[3] == t[7].lr[0]);
REQUIRE(Zzzzz == t[7].lr[1]);
REQUIRE(-1 == t[7].bf);
// Remove 1, then balancing makes 3 the new root node.
// 3
// / `
// 2 7
std::puts("REMOVE 1:");
remove(&root, &t[1]);
REQUIRE(&t[3] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(3 == checkAscension(root));
// 2
REQUIRE(&t[3] == t[2].up);
REQUIRE(Zzzzz == t[2].lr[0]);
REQUIRE(Zzzzz == t[2].lr[1]);
REQUIRE(0 == t[2].bf);
// 3
REQUIRE(Zzzzz == t[3].up); // Nihil Supernum
REQUIRE(&t[2] == t[3].lr[0]);
REQUIRE(&t[7] == t[3].lr[1]);
REQUIRE(00 == t[3].bf);
// 7
REQUIRE(&t[3] == t[7].up);
REQUIRE(Zzzzz == t[7].lr[0]);
REQUIRE(Zzzzz == t[7].lr[1]);
REQUIRE(00 == t[7].bf);
// Remove 7.
// 3
// /
// 2
std::puts("REMOVE 7:");
remove(&root, &t[7]);
REQUIRE(&t[3] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(2 == checkAscension(root));
// 2
REQUIRE(&t[3] == t[2].up);
REQUIRE(Zzzzz == t[2].lr[0]);
REQUIRE(Zzzzz == t[2].lr[1]);
REQUIRE(0 == t[2].bf);
// 3
REQUIRE(Zzzzz == t[3].up); // Nihil Supernum
REQUIRE(&t[2] == t[3].lr[0]);
REQUIRE(Zzzzz == t[3].lr[1]);
REQUIRE(-1 == t[3].bf);
// Remove 3. Only 2 is left, which is now obviously the root.
std::puts("REMOVE 3:");
remove(&root, &t[3]);
REQUIRE(&t[2] == root);
print(root);
REQUIRE(nullptr == findBrokenBalanceFactor(root));
REQUIRE(nullptr == findBrokenAncestry(root));
REQUIRE(1 == checkAscension(root));
// 2
REQUIRE(Zzzzz == t[2].up);
REQUIRE(Zzzzz == t[2].lr[0]);
REQUIRE(Zzzzz == t[2].lr[1]);
REQUIRE(0 == t[2].bf);
// Remove 2. The tree is now empty, make sure the root pointer is updated accordingly.
std::puts("REMOVE 2:");
remove(&root, &t[2]);
REQUIRE(nullptr == root);
}
TEST_CASE("MutationManual")
{