Adds clear method and test for CircularBuffer (#176)
* added clear method and test * removed some duplicate code from the constructor and call new clear method * changes based on PR comments * fixed build issue
This commit is contained in:
Родитель
b58980d5eb
Коммит
7f5327c5d9
|
@ -408,3 +408,49 @@ TEST_CASE("CircularBuffer - range with offset") {
|
|||
// when n and offset are not out of bound
|
||||
CHECK(++(++start_iter3) == end_iter3);
|
||||
}
|
||||
|
||||
TEST_CASE("CircularBuffer - clear") {
|
||||
NS::Featurizers::Components::CircularBuffer<std::int16_t> circ_buf(5);
|
||||
|
||||
// Add some dummy data
|
||||
circ_buf.push(1);
|
||||
circ_buf.push(2);
|
||||
CHECK(circ_buf.size() == 2);
|
||||
|
||||
// Make sure after clear the size is correct
|
||||
circ_buf.clear();
|
||||
CHECK(circ_buf.size() == 0);
|
||||
|
||||
// Make sure start/end iters are correct after clear
|
||||
auto start_iter = circ_buf.begin();
|
||||
auto end_iter = circ_buf.end();
|
||||
CHECK(start_iter == end_iter);
|
||||
CHECK(circ_buf.capacity() == 5);
|
||||
|
||||
// Add one item and make sure start offset is correct.
|
||||
circ_buf.push(1);
|
||||
auto start_iter2 = circ_buf.begin();
|
||||
CHECK(*start_iter2 == 1);
|
||||
|
||||
// Add till the size is full and make sure capacity is still the same.
|
||||
circ_buf.push(2);
|
||||
circ_buf.push(3);
|
||||
circ_buf.push(4);
|
||||
circ_buf.push(5);
|
||||
circ_buf.push(6);
|
||||
CHECK(circ_buf.size() == 5);
|
||||
|
||||
// Make sure size is correct after final clear.
|
||||
circ_buf.clear();
|
||||
CHECK(circ_buf.size() == 0);
|
||||
|
||||
// Make sure start/end iters are correct after clear
|
||||
auto start_iter3 = circ_buf.begin();
|
||||
auto end_iter3 = circ_buf.end();
|
||||
CHECK(start_iter3 == end_iter3);
|
||||
|
||||
// One more time add one item and make sure the start offset is correct.
|
||||
circ_buf.push(1);
|
||||
auto start_iter4 = circ_buf.begin();
|
||||
CHECK(*start_iter4 == 1);
|
||||
}
|
||||
|
|
|
@ -135,6 +135,8 @@ namespace Components {
|
|||
// ----------------------------------------------------------------------
|
||||
CircularBuffer(size_t max_size);
|
||||
|
||||
void clear();
|
||||
|
||||
size_t size() const;
|
||||
|
||||
size_t capacity() const;
|
||||
|
@ -291,11 +293,23 @@ bool CircularIterator<T>::at_end() const {
|
|||
// ----------------------------------------------------------------------
|
||||
|
||||
template <class T>
|
||||
CircularBuffer<T>::CircularBuffer(size_t max_size) : _max_size(max_size), _start_offset(0) {
|
||||
CircularBuffer<T>::CircularBuffer(size_t max_size) : _max_size(max_size) {
|
||||
if (_max_size == 0) {
|
||||
throw std::invalid_argument("Max size cannot be zero");
|
||||
}
|
||||
|
||||
clear();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void CircularBuffer<T>::clear() {
|
||||
_data.clear();
|
||||
|
||||
// Make sure that the start offset is back to 0;
|
||||
_start_offset = 0;
|
||||
|
||||
// The c++ reference says "the vector capacity is not guaranteed to change due to calling this function."
|
||||
// Since the capacity may change, calling reserve again just to make sure we still have the correct size.
|
||||
_data.reserve(_max_size);
|
||||
}
|
||||
|
||||
|
@ -306,7 +320,7 @@ size_t CircularBuffer<T>::size() const {
|
|||
|
||||
template <class T>
|
||||
size_t CircularBuffer<T>::capacity() const {
|
||||
return _max_size;
|
||||
return _data.capacity();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
|
|
Загрузка…
Ссылка в новой задаче