Skip to content

Commit e184a48

Browse files
committed
fixed the bug with destructor call in push back and also fixed the bug in back function when _head is zero
1 parent 3adab51 commit e184a48

File tree

4 files changed

+151
-15
lines changed

4 files changed

+151
-15
lines changed

circular_buffer.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ typename CircularBuffer<T>::reference CircularBuffer<T>::back() {
258258
std::lock_guard<std::mutex> _lck(_mtx);
259259
if(empty())
260260
throw std::length_error("back function called on empty buffer");
261-
return _buff[_head-1];
261+
//return _buff[_head - 1];
262+
return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1];
262263
}
263264

264265
template<typename T>
@@ -276,14 +277,15 @@ typename CircularBuffer<T>::const_reference CircularBuffer<T>::back() const{
276277
std::lock_guard<std::mutex> _lck(_mtx);
277278
if(empty())
278279
throw std::length_error("back function called on empty buffer");
279-
return _buff[_head - 1];
280+
//return _buff[_head - 1];
281+
return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1];
280282
}
281283

282284
template<typename T>
283285
void CircularBuffer<T>::push_back(const T& data){
284286
std::lock_guard<std::mutex> _lck(_mtx);
285-
if(full())
286-
_buff[_head].~T();
287+
//if(full())
288+
// _buff[_tail].~T();
287289
_buff[_head] = data;
288290
_increment_bufferstate();
289291
}
@@ -295,16 +297,15 @@ void CircularBuffer<T>::_increment_bufferstate(){
295297
_tail = (_tail + 1)%_max_size;
296298
else
297299
++_size;
298-
_head = (_head + 1)%_max_size;
299-
_full = (_head == _tail);
300+
_head = (_head + 1)%_max_size;
300301
}
301302

302303
template<typename T>
303304
inline
304305
void CircularBuffer<T>::pop_front(){
305306
std::lock_guard<std::mutex> _lck(_mtx);
306307
if(empty())
307-
throw std::length_error("pop_back called on empty buffer");
308+
throw std::length_error("pop_front called on empty buffer");
308309
_decrement_bufferstate();
309310
}
310311

samples/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ target_link_libraries(sample_circularbuffer
66
PRIVATE
77
circularbuffer
88
)
9+
10+
add_executable(sample_stringbuffer sample_stringbuffer.cpp)
11+
target_link_libraries(sample_stringbuffer
12+
PRIVATE
13+
circularbuffer
14+
)

samples/sample_stringbuffer.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include "circular_buffer.h"
2+
#include <iostream>
3+
#include <string.h>
4+
#include "gtest/gtest.h"
5+
6+
#include "circular_buffer.h"
7+
#include <iostream>
8+
#include <string.h>
9+
10+
int main(int argc, char *argv[])
11+
{
12+
13+
CircularBuffer<int> test{5};
14+
//std::cout<<"size of data "<<sizeof(data[1].bytes)<<"\n";
15+
std::cout<<"length of buffer "<<test.size()<<"\n";
16+
std::cout<<"size of buffer in bytes "<<test.buffer_size()<<"\n";
17+
std::cout<<"Max capacity of buffer "<<test.capacity()<<"\n";
18+
std::cout<<"Checking is buffer empty function "<<test.empty()<<"\n";
19+
std::cout<<"Pushing back data in bufffer \n";
20+
for(int i = 0; i<100;i++){
21+
test.push_back(i);
22+
std::cout<<"Pushing back i"<<std::endl;
23+
}
24+
25+
CircularBuffer<std::string> test_stringbuf{10};
26+
std::cout<<"Checking is buffer empty function "<<test_stringbuf.empty()<<"\n";
27+
std::cout<<"Checking length of string buffer "<<test_stringbuf.size()<<"\n";
28+
std::cout<<"size of buffer in bytes "<<test_stringbuf.buffer_size()<<"\n";
29+
std::cout<<"Max capacity of buffer "<<test_stringbuf.capacity()<<"\n";
30+
31+
for(int i = 0; i<10;i++){
32+
test_stringbuf.push_back("this is a string" + std::to_string(i));
33+
std::cout<<"Pushing back string "<<i<<std::endl;
34+
}
35+
36+
std::cout<<"Checking is buffer empty function "<<test_stringbuf.empty()<<"\n";
37+
std::cout<<"Checking length of buffer after modification "<<test_stringbuf.size()<<"\n";
38+
std::cout<<"Checking front function "<<test_stringbuf.front()<<"\n";
39+
std::cout<<"Checking back function "<<test_stringbuf.back()<<"\n";
40+
41+
for(int i = 0; i<7;i++){
42+
test_stringbuf.pop_front();
43+
std::cout<<"Poping "<<i<<std::endl;
44+
}
45+
46+
std::cout<<"Checking is buffer empty function "<<test_stringbuf.empty()<<"\n";
47+
std::cout<<"Checking length of buffer after modification "<<test_stringbuf.size()<<"\n";
48+
std::cout<<"Checking front function "<<test_stringbuf.front()<<"\n";
49+
std::cout<<"Checking back function "<<test_stringbuf.back()<<"\n";
50+
/*
51+
//test_stringbuf.pop_front();
52+
std::cout<<"Checking length of buffer after modification "<<test_stringbuf.size()<<"\n";
53+
std::cout<<"Checking front function "<<test_stringbuf.front()<<"\n";
54+
std::cout<<"Checking back function "<<test_stringbuf.back()<<"\n";
55+
56+
/*
57+
std::cout<<"Checking iterator function\n";
58+
auto it = test_stringbuf.begin();
59+
60+
std::cout<<"Checking deference * operator "<<*it<<"\n";
61+
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
62+
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";
63+
64+
65+
std::cout<<"Checking iterator for loop \n";
66+
for(auto it:test_stringbuf)
67+
std::cout<<"Checking for loop function "<<it<<"\n";
68+
69+
std::cout<<"Checking for loop with iterator \n";
70+
for(auto it = test_stringbuf.begin(); it != test_stringbuf.end(); it++)
71+
std::cout<<"Checking for loop function "<<*it<<"\n";
72+
73+
CircularBuffer<test_struct> test_structbuf{5};
74+
std::cout<<"Checking [] operator"<<test_structbuf[0]<<"\n";
75+
76+
auto temp_struct = test_struct();
77+
78+
test_structbuf.push_back(temp_struct);
79+
std::cout<<"Checking [] operator"<<test_structbuf[1]<<"\n";
80+
std::cout<<"Checking at operator"<<test_structbuf.at(0)<<"\n";
81+
82+
test_structbuf.push_back(temp_struct);
83+
std::cout<<"Checking at operator"<<test_structbuf.at(1)<<"\n";
84+
test_structbuf.push_back(temp_struct);
85+
std::cout<<"Checking at operator"<<test_structbuf.at(2)<<"\n";
86+
test_structbuf.push_back(temp_struct);
87+
std::cout<<"Checking at operator"<<test_structbuf.at(3)<<"\n";
88+
test_structbuf.push_back(temp_struct);
89+
std::cout<<"Checking at operator"<<test_structbuf.at(4)<<"\n";
90+
test_structbuf.push_back(temp_struct);
91+
std::cout<<"Checking at operator"<<test_structbuf.at(0)<<"\n";
92+
test_structbuf.push_back(temp_struct);
93+
test_structbuf.push_back(temp_struct);
94+
test_structbuf.push_back(temp_struct);
95+
test_structbuf.push_back(temp_struct);
96+
97+
98+
test_structbuf.pop_front();
99+
100+
CircularBuffer<std::string> test_stringbufcopy{test_stringbuf};
101+
std::cout<<"Checking maxsize of copy buffer"<<test_stringbufcopy.capacity()<<"\n";
102+
std::cout<<"Checking size of copy buffer"<<test_stringbufcopy.size()<<"\n";
103+
std::cout<<"Checking [] in copybuffer"<<test_stringbufcopy[1]<<"\n";
104+
std::cout<<"Checking maxsize buffer"<<test_stringbuf.capacity()<<"\n";
105+
std::cout<<"Checking size buffer"<<test_stringbuf.size()<<"\n";
106+
*/
107+
std::cout<<"end is reached"<<std::endl;
108+
return 0;
109+
}

tests/test_circularbuffer_int.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
#include <string.h>
44
#include "gtest/gtest.h"
55

6+
#define TEST_BUFFER_SIZE 100
67

78
class CircularBufferTest : public ::testing::Test{
89

910
protected:
1011

11-
CircularBuffer<int> test{5};
12+
CircularBuffer<int> test{TEST_BUFFER_SIZE};
1213

1314
};
1415

1516
TEST_F(CircularBufferTest, CapacityTest){
16-
EXPECT_EQ(5, test.capacity());
17+
EXPECT_EQ(TEST_BUFFER_SIZE, test.capacity());
1718
EXPECT_FALSE(test.capacity() == 0);
1819
}
1920

@@ -24,7 +25,14 @@ TEST_F(CircularBufferTest, EmptyTest){
2425
EXPECT_FALSE(test.empty());
2526
test.pop_front();
2627
test.pop_front();
27-
EXPECT_TRUE(test.empty());
28+
EXPECT_TRUE(test.empty());
29+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
30+
test.push_back(10);
31+
EXPECT_FALSE(test.empty());
32+
33+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
34+
test.pop_front();
35+
EXPECT_TRUE(test.empty());
2836
}
2937

3038
TEST_F(CircularBufferTest, PushBackTest){
@@ -41,11 +49,8 @@ TEST_F(CircularBufferTest, PushBackTest){
4149

4250
TEST_F(CircularBufferTest, PopFrontTest){
4351
EXPECT_TRUE(test.empty());
44-
test.push_back(348789);
45-
test.push_back(34824);
46-
test.push_back(234892);
47-
test.push_back(100929);
48-
test.push_back(4878872);
52+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
53+
test.push_back(10);
4954
EXPECT_TRUE(test.full());
5055
while(true){
5156
try{
@@ -69,6 +74,21 @@ TEST_F(CircularBufferTest, SizeTest){
6974
EXPECT_EQ(test.size(), test.capacity());
7075
}
7176

77+
TEST_F(CircularBufferTest, PushAndPopTest){
78+
EXPECT_EQ(0, test.size());
79+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
80+
test.push_back(i);
81+
EXPECT_EQ(TEST_BUFFER_SIZE, test.size());
82+
EXPECT_EQ(test.size(), test.capacity());
83+
for(int i=0; i<TEST_BUFFER_SIZE - 1; i++)
84+
test.pop_front();
85+
EXPECT_EQ(1, test.size());
86+
EXPECT_EQ(TEST_BUFFER_SIZE, test.capacity());
87+
EXPECT_EQ(test.back(), test.front());
88+
EXPECT_EQ(TEST_BUFFER_SIZE - 1, test.front());
89+
90+
}
91+
7292

7393
int main(int argc, char *argv[])
7494
{

0 commit comments

Comments
 (0)