adt - fix circular map erase function and added extra asserts

master
Francisco 4 years ago committed by Francisco Paisana
parent 5d676199ab
commit 88f5add1fb

@ -46,15 +46,32 @@ public:
return *this; return *this;
} }
obj_t& operator*() { return ptr->get_obj_(idx); } obj_t& operator*()
obj_t* operator->() { return &ptr->get_obj_(idx); } {
const obj_t* operator*() const { return ptr->buffer[idx]; } assert(idx < ptr->buffer.size() && "Index out-of-bounds");
const obj_t* operator->() const { return ptr->buffer[idx]; } 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 ptr == other.ptr and idx == other.idx; }
bool operator!=(const iterator& other) const { return not(*this == other); } bool operator!=(const iterator& other) const { return not(*this == other); }
private: private:
friend class static_circular_map<K, T, N>;
static_circular_map<K, T, N>* ptr = nullptr; static_circular_map<K, T, N>* ptr = nullptr;
size_t idx = 0; size_t idx = 0;
}; };
@ -78,6 +95,7 @@ public:
bool operator!=(const const_iterator& other) const { return not(*this == other); } bool operator!=(const const_iterator& other) const { return not(*this == other); }
private: private:
friend class static_circular_map<K, T, N>;
const static_circular_map<K, T, N>* ptr = nullptr; const static_circular_map<K, T, N>* ptr = nullptr;
size_t idx = 0; size_t idx = 0;
}; };
@ -171,7 +189,7 @@ public:
iterator erase(iterator it) iterator erase(iterator it)
{ {
assert(it->first < N); assert(it.idx < N);
iterator next = it; iterator next = it;
++next; ++next;
it->~obj_t(); it->~obj_t();

@ -61,6 +61,30 @@ int test_id_map()
return SRSRAN_SUCCESS; return SRSRAN_SUCCESS;
} }
int test_id_map_wraparound()
{
static_circular_map<uint32_t, std::string, 4> 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 } // namespace srsran
int main() int main()
@ -72,5 +96,6 @@ int main()
srslog::init(); srslog::init();
TESTASSERT(srsran::test_id_map() == SRSRAN_SUCCESS); TESTASSERT(srsran::test_id_map() == SRSRAN_SUCCESS);
TESTASSERT(srsran::test_id_map_wraparound() == SRSRAN_SUCCESS);
return SRSRAN_SUCCESS; return SRSRAN_SUCCESS;
} }
Loading…
Cancel
Save