From 74f371fcd3ec126d132fdf69b4f504e6157a6147 Mon Sep 17 00:00:00 2001 From: Lane Kolbly Date: Wed, 9 Mar 2022 14:54:31 -0600 Subject: host: Create meta_range_t::as_monotonic In order to perform certain operations (start/stop/step), meta_range_t objects must be "monotonic", meaning that the subranges composing it are sorted and non-overlapping. This commit creates a method which takes a non-monotonic meta_range_t containing no non-continuous subranges and converts it into a monotonic meta_range_t. --- host/tests/ranges_test.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'host/tests') diff --git a/host/tests/ranges_test.cpp b/host/tests/ranges_test.cpp index 223560289..126b4c4e9 100644 --- a/host/tests/ranges_test.cpp +++ b/host/tests/ranges_test.cpp @@ -90,3 +90,23 @@ BOOST_AUTO_TEST_CASE(test_ranges_compare) BOOST_CHECK(range == n_range); BOOST_CHECK(range != d_range); } + +BOOST_AUTO_TEST_CASE(test_meta_range_monotonize) +{ + // This meta_range is both not sorted and contains overlapping (and + // fully contained) sub-ranges. + meta_range_t mr; + mr.push_back(range_t(3.0, 4.0)); + mr.push_back(range_t(1.0, 2.0)); + mr.push_back(range_t(2.1, 3.1)); + mr.push_back(range_t(1.4, 1.6)); + + meta_range_t monotonic_mr = mr.as_monotonic(); + BOOST_CHECK(monotonic_mr.start() == 1.0); + BOOST_CHECK(monotonic_mr.stop() == 4.0); + + BOOST_CHECK_CLOSE(monotonic_mr.at(0).start(), 1.0, tolerance); + BOOST_CHECK_CLOSE(monotonic_mr.at(0).stop(), 2.0, tolerance); + BOOST_CHECK_CLOSE(monotonic_mr.at(1).start(), 2.1, tolerance); + BOOST_CHECK_CLOSE(monotonic_mr.at(1).stop(), 4.0, tolerance); +} -- cgit v1.2.3