Skip to content

Commit 7966064

Browse files
committed
More tests.
1 parent f61c645 commit 7966064

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

interval_tree.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ namespace lib_interval_tree
177177
value_type low_;
178178
value_type high_;
179179
};
180+
//############################################################################################################
181+
/**
182+
* Creates a safe interval that puts the lower bound left automatically.
183+
*/
184+
template <typename numerical_type, typename interval_kind_ = closed>
185+
interval <numerical_type, interval_kind_> make_safe_interval(numerical_type lhs, numerical_type rhs)
186+
{
187+
return interval <numerical_type, interval_kind_>{std::min(lhs, rhs), std::max(lhs, rhs)};
188+
}
180189
//############################################################################################################
181190
template <typename numerical_type = default_interval_value_type, typename interval_type_ = interval <numerical_type, closed>>
182191
class node
@@ -233,6 +242,18 @@ namespace lib_interval_tree
233242
return !parent_;
234243
}
235244

245+
/**
246+
* Returns the height of the node in the tree. Where height = how many parents does it have.
247+
* The root has no parents and is therefor has height 0.
248+
*/
249+
int height() const
250+
{
251+
int counter{0};
252+
for (auto* p = parent_; p != nullptr; p = p->parent_)
253+
++counter;
254+
return counter;
255+
}
256+
236257
private:
237258
void set_interval(interval_type const& ival)
238259
{

tests/insert_tests.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#include <ctime>
2+
#include <random>
3+
#include <cmath>
4+
15
class InsertTests
26
: public ::testing::Test
37
{
@@ -6,6 +10,8 @@ class InsertTests
610

711
protected:
812
lib_interval_tree::interval_tree <int> tree;
13+
std::default_random_engine gen;
14+
std::uniform_int_distribution <int> dist{-500, 500};
915
};
1016

1117
TEST_F(InsertTests, InsertIntoEmpty1)
@@ -39,3 +45,17 @@ TEST_F(InsertTests, InsertMultipleIntoEmpty)
3945
EXPECT_EQ(*tree.begin(), firstInterval);
4046
EXPECT_EQ(*(++tree.begin()), secondInterval);
4147
}
48+
49+
TEST_F(InsertTests, TreeHeightHealthynessTest)
50+
{
51+
const int amount = 1'000'000;
52+
53+
for (int i = 0; i != amount; ++i)
54+
tree.insert(lib_interval_tree::make_safe_interval(dist(gen), dist(gen)));
55+
56+
auto maxHeight{0};
57+
for (auto i = std::begin(tree); i != std::end(tree); ++i)
58+
maxHeight = std::max(maxHeight, i->height());
59+
60+
EXPECT_LE(maxHeight, 2 * std::log2(amount + 1));
61+
}

tests/interval_tests.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DistanceTests
3434

3535
TEST_F(IntervalTests, FailBadBorders)
3636
{
37-
ASSERT_DEATH(({
37+
EXPECT_DEATH(({
3838
[[maybe_unused]] auto ival = types::interval_type{1 BOOST_PP_COMMA() 0};
3939
}), "low <= high");
4040
}

0 commit comments

Comments
 (0)