Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class CircularBuffer {
const_iterator end() const;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
iterator rbegin() noexcept;
const_iterator rbegin() const noexcept;
iterator rend() noexcept;
const_iterator rend() const noexcept;


private:
void _increment_bufferstate();
Expand Down Expand Up @@ -153,7 +158,7 @@ class CircularBuffer {

reference operator*(){
if(_reverse)
return (*_ptrToBuffer)[(_ptrToBuffer->size() - _index)];
return (*_ptrToBuffer)[(_ptrToBuffer->size() - _index - 1)];
return (*_ptrToBuffer)[_index];
}

Expand Down Expand Up @@ -484,4 +489,53 @@ typename CircularBuffer<T>::const_iterator CircularBuffer<T>::cend() const noexc
iter._reverse = false;
return iter;
}

template<typename T>
inline
typename CircularBuffer<T>::iterator CircularBuffer<T>::rbegin() noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = true;
return iter;
}

template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::rbegin() const noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = true;
return iter;
}

template<typename T>
inline
typename CircularBuffer<T>::iterator CircularBuffer<T>::rend() noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = true;
return iter;
}

template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::rend() const noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = true;
return iter;
}

#endif /* CIRCULAR_BUFFER_H */
68 changes: 67 additions & 1 deletion tests/member_func_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,38 @@ TEST_F(CircularBufferTest, BeginIteratorTest){
}
}

TEST_F(CircularBufferTest, RbeginIteratorTest){
//create full buffer
for(int i=0; i<TEST_BUFFER_SIZE; i++)
test_buff.push_back("string" + std::to_string(i));
//test first element with iterator
CircularBuffer<std::string>::iterator it = test_buff.rbegin();
//access with rbegin iterator
for(int i=TEST_BUFFER_SIZE-1; i>=0; i--)
EXPECT_EQ(*(it++), "string" + std::to_string(i));

//access with const begin iterator
CircularBuffer<std::string>::const_iterator const_it = test_buff.rbegin();
for(int i=TEST_BUFFER_SIZE - 1; i>=0; i--)
EXPECT_EQ(*(const_it++), "string" + std::to_string(i));
//test out of bounds
try {
*it = "test_string";
FAIL() << "Expected std::out_of_range error";
}
catch(const std::out_of_range& err){
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
}

try {
std::string out_of_bound = *(const_it);
FAIL() << "Expected std::out_of_range error";
}
catch(const std::out_of_range& err){
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
}
}

TEST_F(CircularBufferTest, CbeginIteratorTest){
//create full buffer
for(int i=0; i<TEST_BUFFER_SIZE; i++)
Expand All @@ -334,7 +366,8 @@ TEST_F(CircularBufferTest, CbeginIteratorTest){
catch(const std::out_of_range& err){
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
}
}
}


TEST_F(CircularBufferTest, EndIteratorTest){
//create full buffer
Expand Down Expand Up @@ -369,6 +402,39 @@ TEST_F(CircularBufferTest, EndIteratorTest){
}
}

TEST_F(CircularBufferTest, RendIteratorTest){
//create full buffer
for(int i=0; i<TEST_BUFFER_SIZE; i++)
test_buff.push_back("string" + std::to_string(i));
//test first element with iterator
CircularBuffer<std::string>::iterator it = test_buff.rend() - 1;
//access with rbegin iterator
for(int i=0; i<TEST_BUFFER_SIZE; i++)
EXPECT_EQ(*(it--), "string" + std::to_string(i));

//access with const begin iterator
CircularBuffer<std::string>::const_iterator const_it = test_buff.rend() - 1;
for(int i=0; i<TEST_BUFFER_SIZE; i++)
EXPECT_EQ(*(const_it--), "string" + std::to_string(i));
//test out of bounds
try {
*it = "test_string";
FAIL() << "Expected std::out_of_range error";
}
catch(const std::out_of_range& err){
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
}

try {
std::string out_of_bound = *(const_it);
FAIL() << "Expected std::out_of_range error";
}
catch(const std::out_of_range& err){
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
}
}


TEST_F(CircularBufferTest, CendIteratorTest){
//create full buffer
for(int i=0; i<TEST_BUFFER_SIZE; i++)
Expand Down