|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <functional> |
| 4 | +#include <list> |
| 5 | +#include <cmath> |
| 6 | + |
| 7 | +/** |
| 8 | + * Warning this function is very expensive. |
| 9 | + */ |
| 10 | +template <typename TreeT> |
| 11 | +void testRedBlackPropertyViolation(TreeT const& tree) |
| 12 | +{ |
| 13 | + using namespace lib_interval_tree; |
| 14 | + |
| 15 | + // root is always black. |
| 16 | + EXPECT_EQ(tree.root().color(), rb_color::black); |
| 17 | + |
| 18 | + // check that all nodes have red or black coloring. (seems obvious, but is not on bug) |
| 19 | + for (auto i = std::begin(tree); i != std::end(tree); ++i) |
| 20 | + { |
| 21 | + EXPECT_EQ(true, i.color() == rb_color::black || i.color() == rb_color::red); |
| 22 | + } |
| 23 | + |
| 24 | + // check for (red children = black) property: |
| 25 | + for (auto i = std::begin(tree); i != std::end(tree); ++i) |
| 26 | + { |
| 27 | + auto nodeColor = i.color(); |
| 28 | + if (nodeColor == rb_color::red) |
| 29 | + { |
| 30 | + if (i.left() != std::end(tree)) |
| 31 | + { |
| 32 | + EXPECT_EQ(i.left().color(), rb_color::black); |
| 33 | + } |
| 34 | + if (i.right() != std::end(tree)) |
| 35 | + { |
| 36 | + EXPECT_EQ(i.right().color(), rb_color::black); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + auto leafCollector = [&](typename TreeT::const_iterator root) |
| 42 | + { |
| 43 | + std::list <typename TreeT::const_iterator> leaves{}; |
| 44 | + std::function <void(typename std::list <typename TreeT::const_iterator>::iterator)> recursiveLeafFinder; |
| 45 | + recursiveLeafFinder = [&](typename std::list <typename TreeT::const_iterator>::iterator self) |
| 46 | + { |
| 47 | + if (self->left() != std::end(tree)) |
| 48 | + { |
| 49 | + recursiveLeafFinder(leaves.insert(self, self->left())); |
| 50 | + } |
| 51 | + if (self->right() != std::end(tree)) |
| 52 | + { |
| 53 | + *self = self->right(); |
| 54 | + recursiveLeafFinder(self); |
| 55 | + } |
| 56 | + }; |
| 57 | + leaves.push_back(root); |
| 58 | + recursiveLeafFinder(leaves.begin()); |
| 59 | + return leaves; |
| 60 | + }; |
| 61 | + |
| 62 | + // Test that for every node, on the path to its leaves, has the same number of black nodes. |
| 63 | + for (auto i = std::cbegin(tree); i != std::cend(tree); ++i) |
| 64 | + { |
| 65 | + auto leaves = leafCollector(i); |
| 66 | + int comparisonCounter{0}; |
| 67 | + for (auto const& leaf : leaves) |
| 68 | + { |
| 69 | + auto p = leaf; |
| 70 | + int counter{0}; |
| 71 | + do |
| 72 | + { |
| 73 | + if (p.color() == rb_color::black) |
| 74 | + ++counter; |
| 75 | + p = p.parent(); |
| 76 | + } while (p != i && p != std::end(tree)); |
| 77 | + if (comparisonCounter == 0) |
| 78 | + comparisonCounter = counter; |
| 79 | + else |
| 80 | + { |
| 81 | + EXPECT_EQ(comparisonCounter, counter); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +template <typename TreeT> |
| 88 | +void testMaxProperty(TreeT const& tree) |
| 89 | +{ |
| 90 | + for (auto i = std::begin(tree); i != std::end(tree); ++i) |
| 91 | + { |
| 92 | + if (i->left()) |
| 93 | + { |
| 94 | + EXPECT_LE(i->left()->max(), i->max()); |
| 95 | + } |
| 96 | + if (i->right()) |
| 97 | + { |
| 98 | + EXPECT_LE(i->right()->max(), i->max()); |
| 99 | + } |
| 100 | + EXPECT_GE(i->max(), i->interval().high()); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +template <typename TreeT> |
| 105 | +void testTreeHeightHealth(TreeT const& tree) |
| 106 | +{ |
| 107 | + int treeSize = tree.size(); |
| 108 | + |
| 109 | + auto maxHeight{0}; |
| 110 | + for (auto i = std::begin(tree); i != std::end(tree); ++i) |
| 111 | + maxHeight = std::max(maxHeight, i->height()); |
| 112 | + |
| 113 | + EXPECT_LE(maxHeight, 2 * std::log2(treeSize + 1)); |
| 114 | +} |
0 commit comments