Skip to content

Commit 78b49d8

Browse files
committed
fixed bug in clearr function and added more tests
1 parent 7f4152f commit 78b49d8

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

circular_buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ template<typename T>
269269
inline
270270
void CircularBuffer<T>::clear(){
271271
std::lock_guard<std::mutex> _lck(_mtx);
272-
_head = _tail = _size = _max_size = 0;
272+
_head = _tail = _size = 0;
273273
}
274274

275275
template<typename T>

tests/string_buffer.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "gtest/gtest.h"
66

77
#define TEST_BUFFER_SIZE 100
8+
#define REDUCE_SIZE 100
89

910
class CircularBufferTest : public ::testing::Test{
1011

@@ -72,6 +73,9 @@ TEST_F(CircularBufferTest, ClearTest){
7273
//check size and empty flags after clearing
7374
EXPECT_TRUE(test_buff.empty());
7475
EXPECT_EQ(test_buff.size(), 0);
76+
//fill buffer after clear
77+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
78+
test_buff.push_back("string" + std::to_string(i));
7579

7680
}
7781

@@ -347,6 +351,53 @@ TEST_F(CircularBufferTest, EndIteratorTest){
347351

348352
}
349353

354+
TEST_F(CircularBufferTest, IteratorBasedLoopTest){
355+
//create full buffer
356+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
357+
test_buff.push_back("string" + std::to_string(i));
358+
int i = 0;
359+
for(auto it = test_buff.begin(); it!=test_buff.end(); it++)
360+
EXPECT_EQ(*it, "string" + std::to_string(i++));
361+
//partially fill buffers
362+
test_buff.clear();
363+
for(int i=0; i<TEST_BUFFER_SIZE/2; i++)
364+
test_buff.push_back("string" + std::to_string(i));
365+
//test begin and end on partially full buffer
366+
i = 0;
367+
for(auto it = test_buff.begin(); it!=test_buff.end(); it++)
368+
EXPECT_EQ(*it, "string" + std::to_string(i++));
369+
//test size with increment variable
370+
EXPECT_EQ(i, TEST_BUFFER_SIZE/2);
371+
372+
}
373+
374+
TEST_F(CircularBufferTest, RangeBasedLoopTest){
375+
//create full buffer
376+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
377+
test_buff.push_back("string" + std::to_string(i));
378+
int i = 0;
379+
//copied elements
380+
for(auto buff_elem : test_buff)
381+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
382+
//auto reference
383+
i = 0;
384+
for(auto& buff_elem : test_buff)
385+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
386+
//auto const reference
387+
i = 0;
388+
for(const auto& buff_elem : test_buff)
389+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
390+
//check iterators after modifications
391+
for(int i = 0; i<REDUCE_SIZE; i++)
392+
test_buff.pop_front();
393+
i = 0;
394+
for(const auto& buff_elem : test_buff)
395+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
396+
EXPECT_EQ(i, TEST_BUFFER_SIZE - REDUCE_SIZE);
397+
}
398+
399+
400+
350401

351402

352403

0 commit comments

Comments
 (0)