From 5292c22e2c3fc8d8d2a2ef0a1115fe5e9a16ea4c Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 23 Aug 2024 21:55:33 +0100 Subject: [PATCH] Add test for minimal 'interval_map' type interface Add `KeyInterface` (respectively `ValueInterface`). Those classes provide the minimum documented interface for `K` (respectively `V`) in `interval_map`. --- tests/unit/unit_test.cc | 65 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/tests/unit/unit_test.cc b/tests/unit/unit_test.cc index e488c79..288c811 100644 --- a/tests/unit/unit_test.cc +++ b/tests/unit/unit_test.cc @@ -2,6 +2,67 @@ #include -TEST(misc, passing) { - ASSERT_EQ(1, 1); +#include + +template class KeyInterface { +public: + explicit KeyInterface(T val) : underlying_(val) {} + + KeyInterface(KeyInterface const&) = default; + KeyInterface& operator=(KeyInterface const&) = default; + + bool operator<(KeyInterface const& other) const { + return underlying_ < other.underlying_; + } + +private: + T underlying_; +}; + +template struct std::numeric_limits> { + static KeyInterface lowest() { + return KeyInterface(std::numeric_limits::lowest()); + } +}; + +static_assert(std::is_copy_constructible_v>); +static_assert(std::is_copy_assignable_v>); +static_assert( + std::is_same_v, + decltype(std::numeric_limits>::lowest())>); + +template class ValueInterface { +public: + explicit ValueInterface(T val) : underlying_(val) {} + + ValueInterface(ValueInterface const&) = default; + ValueInterface& operator=(ValueInterface const&) = default; + + bool operator==(ValueInterface const& other) const { + return underlying_ == other.underlying_; + } + +private: + T underlying_; +}; + +template struct std::numeric_limits> { + static ValueInterface lowest() { + return ValueInterface(std::numeric_limits::lowest()); + } +}; + +static_assert(std::is_copy_constructible_v>); +static_assert(std::is_copy_assignable_v>); +static_assert(std::is_same_v< + ValueInterface, + decltype(std::numeric_limits>::lowest())>); + +TEST(interval_map, minimal_interface) { + using Key = KeyInterface; + using Value = ValueInterface; + + auto map = amby::interval_map{Value(0)}; + ASSERT_EQ(map[Key(0)], Value(0)); + map.assign(Key(0), Key(1), Value(1)); }