From 2317285d0fd70b1f794e7773190b9b535183fb26 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 24 Aug 2024 20:59:14 +0100 Subject: [PATCH] Fix edge-case in identify 'end' upper-bound Just my luck, this was found immediately in the CI, right when I uploaded the project thinking I was done. Thankfully the fix is easy and (in hindsight) quite obvious. --- src/include/interval-map/interval-map.hh | 2 +- tests/unit/unit_test.cc | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/include/interval-map/interval-map.hh b/src/include/interval-map/interval-map.hh index ab3fb44..b518f24 100644 --- a/src/include/interval-map/interval-map.hh +++ b/src/include/interval-map/interval-map.hh @@ -35,7 +35,7 @@ public: if (insert_begin) it = underlying_.insert(it, {begin, val}); // Get the proper upper-bound for `end` - it = (it == underlying_.end()) ? it : std::next(it); + it = (it == underlying_.end() || end < it->first) ? it : std::next(it); if (!(at_upper_bound(it) == end_val)) underlying_.insert(it, {end, end_val}); } diff --git a/tests/unit/unit_test.cc b/tests/unit/unit_test.cc index c429d29..53675c3 100644 --- a/tests/unit/unit_test.cc +++ b/tests/unit/unit_test.cc @@ -242,6 +242,12 @@ TEST_F(IntervalMapTest, fuzzing_003) { assign(-110, -10, 4); } +TEST_F(IntervalMapTest, fuzzing_004) { + assign(-20, 120, 1); + assign(50, 110, 0); + assign(-120, 100, 0); +} + TEST_F(IntervalMapTest, randomized_test) { auto const seed = []() { std::random_device r;