From 88f5add1fb36726fde2b6d37a584f628723f8cf2 Mon Sep 17 00:00:00 2001 From: Francisco Date: Sun, 21 Mar 2021 13:01:54 +0000 Subject: [PATCH] adt - fix circular map erase function and added extra asserts --- lib/include/srsran/adt/circular_map.h | 28 ++++++++++++++++++++++----- lib/test/adt/circular_map_test.cc | 27 +++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/include/srsran/adt/circular_map.h b/lib/include/srsran/adt/circular_map.h index 04a3b0cc3..7d78b8970 100644 --- a/lib/include/srsran/adt/circular_map.h +++ b/lib/include/srsran/adt/circular_map.h @@ -46,15 +46,32 @@ public: return *this; } - obj_t& operator*() { return ptr->get_obj_(idx); } - obj_t* operator->() { return &ptr->get_obj_(idx); } - const obj_t* operator*() const { return ptr->buffer[idx]; } - const obj_t* operator->() const { return ptr->buffer[idx]; } + obj_t& operator*() + { + assert(idx < ptr->buffer.size() && "Index out-of-bounds"); + return ptr->get_obj_(idx); + } + obj_t* operator->() + { + assert(idx < ptr->buffer.size() && "Index out-of-bounds"); + return &ptr->get_obj_(idx); + } + const obj_t* operator*() const + { + assert(idx < ptr->buffer.size() && "Index out-of-bounds"); + return ptr->buffer[idx]; + } + const obj_t* operator->() const + { + assert(idx < ptr->buffer.size() && "Index out-of-bounds"); + return ptr->buffer[idx]; + } bool operator==(const iterator& other) const { return ptr == other.ptr and idx == other.idx; } bool operator!=(const iterator& other) const { return not(*this == other); } private: + friend class static_circular_map; static_circular_map* ptr = nullptr; size_t idx = 0; }; @@ -78,6 +95,7 @@ public: bool operator!=(const const_iterator& other) const { return not(*this == other); } private: + friend class static_circular_map; const static_circular_map* ptr = nullptr; size_t idx = 0; }; @@ -171,7 +189,7 @@ public: iterator erase(iterator it) { - assert(it->first < N); + assert(it.idx < N); iterator next = it; ++next; it->~obj_t(); diff --git a/lib/test/adt/circular_map_test.cc b/lib/test/adt/circular_map_test.cc index 1db52a581..662f65329 100644 --- a/lib/test/adt/circular_map_test.cc +++ b/lib/test/adt/circular_map_test.cc @@ -61,6 +61,30 @@ int test_id_map() return SRSRAN_SUCCESS; } +int test_id_map_wraparound() +{ + static_circular_map mymap; + + // Fill map + TESTASSERT(mymap.insert(0, "0")); + TESTASSERT(mymap.insert(1, "1")); + TESTASSERT(mymap.insert(2, "2")); + TESTASSERT(mymap.insert(3, "3")); + TESTASSERT(mymap.full()); + + // TEST: Ensure that insertion fails when map is full + TESTASSERT(not mymap.insert(4, "4")); + TESTASSERT(not mymap.erase(4)); + + // TEST: Ensure that insertion works once the element with matching map index is removed + TESTASSERT(mymap.erase(0)); + TESTASSERT(not mymap.full()); + TESTASSERT(mymap.insert(4, "4")); + TESTASSERT(mymap.full()); + + return SRSRAN_SUCCESS; +} + } // namespace srsran int main() @@ -72,5 +96,6 @@ int main() srslog::init(); TESTASSERT(srsran::test_id_map() == SRSRAN_SUCCESS); + TESTASSERT(srsran::test_id_map_wraparound() == SRSRAN_SUCCESS); return SRSRAN_SUCCESS; -} \ No newline at end of file +}