From 73c2b21b94cf5d237e635691e7100ca1d235cbff Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 23 Aug 2024 23:16:39 +0100 Subject: [PATCH] Explicitly test for canonicity This will come in handy later, with more complex test cases. --- src/include/interval-map/interval-map.hh | 6 ++++++ tests/unit/unit_test.cc | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/include/interval-map/interval-map.hh b/src/include/interval-map/interval-map.hh index 8f806c3..4dc2335 100644 --- a/src/include/interval-map/interval-map.hh +++ b/src/include/interval-map/interval-map.hh @@ -2,6 +2,9 @@ #include +// Testing class forward declaration +class IntervalMapTest; + namespace amby { template class interval_map { @@ -21,6 +24,9 @@ public: return std::prev(it)->second; } + // Used in testing + friend class ::IntervalMapTest; + private: V init_; std::map underlying_{}; diff --git a/tests/unit/unit_test.cc b/tests/unit/unit_test.cc index 3cdf79e..d51db5a 100644 --- a/tests/unit/unit_test.cc +++ b/tests/unit/unit_test.cc @@ -69,6 +69,27 @@ protected: void SetUp() override { map = map_type{0}; } + + void TearDown() override { + check_canonicity(); + } + + void check_canonicity() const { + // Consecutive map entries must not contain the same value + for (auto it = map.underlying_.begin(); it != map.underlying_.end(); + ++it) { + const auto next = std::next(it, 1); + if (next == map.underlying_.end()) + break; + EXPECT_NE(it->second, next->second); + } + + // The first entry must not contain the initial value + if (const auto it = map.underlying_.begin(); + it != map.underlying_.end()) { + EXPECT_NE(it->second, map.init_); + } + } }; TEST_F(IntervalMapTest, minimal_interface) {