diff --git a/concordia/interval.cpp b/concordia/interval.cpp index d23e395..ffee629 100644 --- a/concordia/interval.cpp +++ b/concordia/interval.cpp @@ -15,6 +15,10 @@ bool Interval::intersects(Interval & interval) { interval.getEnd() - 1 < _start); } +bool Interval::contains(Interval & interval) { + return (_start <= interval.getStart() && _end >= interval.getEnd()); +} + SUFFIX_MARKER_TYPE Interval::getLength() { return _end - _start; } diff --git a/concordia/interval.hpp b/concordia/interval.hpp index e77fb40..24e7e08 100644 --- a/concordia/interval.hpp +++ b/concordia/interval.hpp @@ -32,6 +32,12 @@ public: */ bool intersects(Interval & interval); + /*! Checks if this interval contains another. + \param interval another interval + \returns true if this interval contains the other + */ + bool contains(Interval & interval); + /*! Getter for interval length. \returns end - start */ diff --git a/concordia/t/test_interval.cpp b/concordia/t/test_interval.cpp index dd9f7b6..79f61de 100644 --- a/concordia/t/test_interval.cpp +++ b/concordia/t/test_interval.cpp @@ -68,4 +68,37 @@ BOOST_AUTO_TEST_CASE( IntervalIntersects8 ) BOOST_CHECK(!interval2.intersects(interval1)); } +BOOST_AUTO_TEST_CASE( IntervalContains1 ) +{ + Interval interval1(2,9); + Interval interval2(3,7); + BOOST_CHECK(interval1.contains(interval2)); + BOOST_CHECK(!interval2.contains(interval1)); +} + +BOOST_AUTO_TEST_CASE( IntervalContains2 ) +{ + Interval interval1(3,9); + Interval interval2(3,7); + BOOST_CHECK(interval1.contains(interval2)); + BOOST_CHECK(!interval2.contains(interval1)); +} + +BOOST_AUTO_TEST_CASE( IntervalContains3 ) +{ + Interval interval1(4,9); + Interval interval2(3,7); + BOOST_CHECK(!interval1.contains(interval2)); + BOOST_CHECK(!interval2.contains(interval1)); +} + +BOOST_AUTO_TEST_CASE( IntervalContains4 ) +{ + Interval interval1(2,9); + Interval interval2(3,17); + BOOST_CHECK(!interval1.contains(interval2)); + BOOST_CHECK(!interval2.contains(interval1)); +} + + BOOST_AUTO_TEST_SUITE_END()