Skip to content

Commit 8ccf371

Browse files
committed
Addes some very basic test anf found first error in witin test.
1 parent 3a0299c commit 8ccf371

File tree

7 files changed

+258
-19
lines changed

7 files changed

+258
-19
lines changed

interval_tree.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ namespace lib_interval_tree
8989
}
9090

9191
/**
92-
* Returns whether the intervals overlap
92+
* Returns whether the intervals overlap.
93+
* For when both intervals are closed.
9394
*/
9495
bool overlaps(value_type l, value_type h) const
9596
{
@@ -98,6 +99,7 @@ namespace lib_interval_tree
9899

99100
/**
100101
* Returns whether the intervals overlap, excluding border.
102+
* For when at least one interval is open (l&r).
101103
*/
102104
bool overlaps_exclusive(value_type l, value_type h) const
103105
{
@@ -125,7 +127,7 @@ namespace lib_interval_tree
125127
*/
126128
bool within(value_type value) const
127129
{
128-
return interval_kind::within(value);
130+
return interval_kind::within(low_, high_, value);
129131
}
130132

131133
/**

interval_types.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace lib_interval_tree
88
template <typename numerical_type>
99
static inline bool within(numerical_type b, numerical_type e, numerical_type p)
1010
{
11-
return b < p <= e;
11+
return (b < p) && (p <= e);
1212
}
1313
};
1414
// [)
@@ -17,7 +17,7 @@ namespace lib_interval_tree
1717
template <typename numerical_type>
1818
static inline bool within(numerical_type b, numerical_type e, numerical_type p)
1919
{
20-
return b <= p < e;
20+
return (b <= p) && (p < e);
2121
}
2222
};
2323
// []
@@ -26,7 +26,7 @@ namespace lib_interval_tree
2626
template <typename numerical_type>
2727
static inline bool within(numerical_type b, numerical_type e, numerical_type p)
2828
{
29-
return b <= p <= e;
29+
return (b <= p) && (p <= e);
3030
}
3131
};
3232
// ()
@@ -35,7 +35,7 @@ namespace lib_interval_tree
3535
template <typename numerical_type>
3636
static inline bool within(numerical_type b, numerical_type e, numerical_type p)
3737
{
38-
return b < p < e;
38+
return (b < p) && (p < e);
3939
}
4040
};
4141
}

tests/find_tests.hpp

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/insert_tests.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class InsertTests
2+
: public ::testing::Test
3+
{
4+
public:
5+
using types = IntervalTypes <int>;
6+
7+
protected:
8+
lib_interval_tree::interval_tree <int> tree;
9+
};
10+
11+
TEST_F(InsertTests, InsertIntoEmpty1)
12+
{
13+
auto inserted_interval = types::interval_type{0, 16};
14+
15+
tree.insert(inserted_interval);
16+
EXPECT_EQ(*tree.begin(), inserted_interval);
17+
EXPECT_EQ(tree.size(), 1);
18+
}
19+
20+
TEST_F(InsertTests, InsertIntoEmpty2)
21+
{
22+
auto inserted_interval = types::interval_type{-45, 16};
23+
24+
tree.insert(inserted_interval);
25+
EXPECT_EQ(*tree.begin(), inserted_interval);
26+
EXPECT_EQ(tree.size(), 1);
27+
}
28+
29+
TEST_F(InsertTests, InsertMultipleIntoEmpty)
30+
{
31+
tree.insert({0, 16});
32+
tree.insert({5, 13});
33+
34+
EXPECT_EQ(tree.size(), 2);
35+
}

tests/interval_tests.hpp

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#pragma once
2+
3+
#include <boost/preprocessor/comma.hpp>
4+
5+
#include <limits>
6+
7+
class IntervalTests
8+
: public ::testing::Test
9+
{
10+
public:
11+
using types = IntervalTypes <int>;
12+
};
13+
14+
class OverlapTests
15+
: public ::testing::Test
16+
{
17+
public:
18+
using types = IntervalTypes <int>;
19+
};
20+
21+
class ContainmentTests
22+
: public ::testing::Test
23+
{
24+
public:
25+
using types = IntervalTypes <int>;
26+
};
27+
28+
TEST_F(IntervalTests, FailBadBorders)
29+
{
30+
ASSERT_DEATH(({
31+
[[maybe_unused]] auto ival = types::interval_type{1 BOOST_PP_COMMA() 0};
32+
}), "low <= high");
33+
}
34+
35+
TEST_F(IntervalTests, ShallCreateInterval)
36+
{
37+
auto ival = types::interval_type{1, 24};
38+
EXPECT_EQ(ival.low(), 1);
39+
EXPECT_EQ(ival.high(), 24);
40+
}
41+
42+
TEST_F(IntervalTests, ShallCreateInterval2)
43+
{
44+
auto ival = types::interval_type{-23, 24};
45+
EXPECT_EQ(ival.low(), -23);
46+
EXPECT_EQ(ival.high(), 24);
47+
}
48+
49+
TEST_F(IntervalTests, ShallCreateInterval3)
50+
{
51+
auto ival = types::interval_type{-21, -12};
52+
EXPECT_EQ(ival.low(), -21);
53+
EXPECT_EQ(ival.high(), -12);
54+
}
55+
56+
TEST_F(IntervalTests, ShallCreateInterval4)
57+
{
58+
auto ival = types::interval_type{1, 24};
59+
EXPECT_EQ(ival.low(), 1);
60+
EXPECT_EQ(ival.high(), 24);
61+
}
62+
63+
TEST_F(IntervalTests, ShallCreateInterval5)
64+
{
65+
auto ival = types::interval_type{1, 1};
66+
EXPECT_EQ(ival.low(), 1);
67+
EXPECT_EQ(ival.high(), 1);
68+
}
69+
70+
TEST_F(IntervalTests, TestLimits)
71+
{
72+
auto ival = types::interval_type{std::numeric_limits<types::value_type>::min(), std::numeric_limits<types::value_type>::max()};
73+
EXPECT_EQ(ival.low(), std::numeric_limits<types::value_type>::min());
74+
EXPECT_EQ(ival.high(), std::numeric_limits<types::value_type>::max());
75+
}
76+
77+
TEST_F(OverlapTests, ShallOverlapItself)
78+
{
79+
auto base = types::interval_type{0, 5};
80+
EXPECT_EQ(base.overlaps(base), true);
81+
}
82+
83+
TEST_F(OverlapTests, ShallOverlapItself2)
84+
{
85+
auto base = types::interval_type{0, 5};
86+
EXPECT_EQ(base.overlaps({0, 5}), true);
87+
}
88+
89+
TEST_F(OverlapTests, ShallOverlapRight)
90+
{
91+
auto base = types::interval_type{0, 5};
92+
EXPECT_EQ(base.overlaps({3, 16}), true);
93+
}
94+
95+
TEST_F(OverlapTests, ShallOverlapLeft)
96+
{
97+
auto base = types::interval_type{0, 5};
98+
EXPECT_EQ(base.overlaps({-8, 1}), true);
99+
}
100+
101+
TEST_F(OverlapTests, ShallEncompassCompletely)
102+
{
103+
auto base = types::interval_type{0, 5};
104+
EXPECT_EQ(base.overlaps({-99, 16}), true);
105+
}
106+
107+
TEST_F(OverlapTests, ShallBeContainedIn)
108+
{
109+
auto base = types::interval_type{0, 5};
110+
EXPECT_EQ(base.overlaps({3, 4}), true);
111+
}
112+
113+
TEST_F(OverlapTests, ExpectDisjunct)
114+
{
115+
auto base = types::interval_type{0, 5};
116+
EXPECT_EQ(base.overlaps({7, 19}), false);
117+
}
118+
119+
TEST_F(OverlapTests, ShallBarelyOverlapLeft)
120+
{
121+
auto base = types::interval_type{0, 5};
122+
EXPECT_EQ(base.overlaps({-3, 0}), true);
123+
}
124+
125+
TEST_F(OverlapTests, ShallBarelyOverlapRight)
126+
{
127+
auto base = types::interval_type{0, 5};
128+
EXPECT_EQ(base.overlaps({5, 10}), true);
129+
}
130+
131+
TEST_F(OverlapTests, ShallNotOverlapExclusiveLeft)
132+
{
133+
auto base = types::interval_type{0, 5};
134+
EXPECT_EQ(base.overlaps_exclusive({-7, 0}), false);
135+
}
136+
137+
TEST_F(OverlapTests, ShallNotOverlapExclusiveRight)
138+
{
139+
auto base = types::interval_type{0, 5};
140+
EXPECT_EQ(base.overlaps_exclusive({5, 10}), false);
141+
}
142+
143+
TEST_F(OverlapTests, ShallOverlapExclusiveRight)
144+
{
145+
auto base = types::interval_type{0, 5};
146+
EXPECT_EQ(base.overlaps_exclusive({4, 10}), true);
147+
}
148+
149+
TEST_F(OverlapTests, ShallOverlapExclusiveLeft)
150+
{
151+
auto base = types::interval_type{0, 5};
152+
EXPECT_EQ(base.overlaps_exclusive({-4, 2}), true);
153+
}
154+
155+
TEST_F(OverlapTests, ShallOverlapExclusiveEncompass)
156+
{
157+
auto base = types::interval_type{0, 5};
158+
EXPECT_EQ(base.overlaps_exclusive({-6, 10}), true);
159+
}
160+
161+
TEST_F(OverlapTests, ShallOverlapExclusiveContained)
162+
{
163+
auto base = types::interval_type{0, 5};
164+
EXPECT_EQ(base.overlaps_exclusive({1, 4}), true);
165+
}
166+
167+
TEST_F(OverlapTests, ExpectDisjunctExclusive)
168+
{
169+
auto base = types::interval_type{0, 5};
170+
EXPECT_EQ(base.overlaps_exclusive({99, 101}), false);
171+
}
172+
173+
TEST_F(ContainmentTests, ShallSingleBeWithin)
174+
{
175+
auto base = types::interval_type{-86, 35};
176+
EXPECT_EQ(base.within(3), true);
177+
EXPECT_EQ(base.within(-3), true);
178+
EXPECT_EQ(base.within(-86), true);
179+
EXPECT_EQ(base.within(35), true);
180+
}
181+
182+
TEST_F(ContainmentTests, ExpectIntervalWithinOther)
183+
{
184+
auto base = types::interval_type{-100, 100};
185+
EXPECT_EQ(base.within({-23, 10}), true);
186+
EXPECT_EQ(base.within({-100, 100}), true);
187+
EXPECT_EQ(base.within({12, 30}), true);
188+
EXPECT_EQ(base.within({-73, -23}), true);
189+
EXPECT_EQ(base.within({-100, -100}), true);
190+
EXPECT_EQ(base.within({100, 100}), true);
191+
}
192+
193+
TEST_F(ContainmentTests, ExpectIntervalNotWithinOther)
194+
{
195+
auto base = types::interval_type{-100, 100};
196+
EXPECT_EQ(base.within({-101, -100}), false);
197+
EXPECT_EQ(base.within({-100, 101}), false);
198+
EXPECT_EQ(base.within({-200, 0}), false);
199+
EXPECT_EQ(base.within({100, 102}), false);
200+
EXPECT_EQ(base.within({-200, -101}), false);
201+
EXPECT_EQ(base.within({200, 300}), false);
202+
}

tests/tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "gtest/gtest.h"
22
#include "../interval_tree.hpp"
3+
#include "typedefs.hpp"
34

45
// following headers expect to be included after gtest headers and interval_tree
5-
#include "find_tests.hpp"
6+
#include "interval_tests.hpp"
7+
#include "insert_tests.hpp"
68

79
int main(int argc, char** argv)
810
{

tests/typedefs.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
template <typename ContainedT>
4+
struct IntervalTypes
5+
{
6+
using value_type = ContainedT;
7+
using tree_type = lib_interval_tree::interval_tree <ContainedT>;
8+
using iterator_type = typename tree_type::iterator;
9+
using interval_type = typename tree_type::interval_type;
10+
};

0 commit comments

Comments
 (0)