Skip to content

Commit 9ab1db7

Browse files
committed
changed size calculation in buffer and added some tests for int type
1 parent c331851 commit 9ab1db7

File tree

3 files changed

+92
-34
lines changed

3 files changed

+92
-34
lines changed

circular_buffer.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef CIRCULAR_BUFFER_H
2+
#define CIRCULAR_BUFFER_H
3+
14
#include <iostream>
25
#include <mutex>
36
#include <memory>
@@ -22,7 +25,6 @@ class CircularBuffer {
2225

2326
public:
2427

25-
2628
explicit CircularBuffer(size_t size)
2729
:_buff{std::unique_ptr<T[]>(new T[size])}, _max_size{size}{}
2830

@@ -45,18 +47,18 @@ class CircularBuffer {
4547
const_iterator begin() const;
4648
iterator end();
4749
const_iterator end() const;
48-
49-
50+
5051
private:
5152
void _increment_bufferstate();
5253
void _decrement_bufferstate();
5354
mutable std::mutex _mtx;
5455
std::unique_ptr<T[]> _buff;
5556
size_type _head = 0;
5657
size_type _tail = 0;
58+
size_type _size = 0;
5759
size_type _max_size = 0;
5860
bool _full = false;
59-
61+
6062
template<bool isConst = false>
6163
class BufferIterator{
6264
public:
@@ -74,11 +76,10 @@ class CircularBuffer {
7476
bool _comparable(const BufferIterator& other){
7577
return (_ptrToBuffer == other._ptrToBuffer)&&(_reverse == other._reverse);
7678
}
77-
79+
7880
BufferIterator()
7981
:_ptrToBuffer{nullptr}, _offset{0}, _index{0}, _reverse{false}{}
8082

81-
8283
BufferIterator(const BufferIterator<false>& it)
8384
:_ptrToBuffer{it._ptrToBuffer},
8485
_offset{it._offset},
@@ -187,13 +188,15 @@ class CircularBuffer {
187188
template<typename T>
188189
inline
189190
bool CircularBuffer<T>::full() const{
190-
return _full;
191+
//return _full;
192+
return _size == _max_size;
191193
}
192194

193195
template<typename T>
194196
inline
195197
bool CircularBuffer<T>::empty() const{
196-
return (!_full &&(_head == _tail));
198+
//return ((!full()) &&(_head == _tail));
199+
return _size == 0;
197200
}
198201

199202
template<typename T>
@@ -206,22 +209,23 @@ template<typename T>
206209
inline
207210
void CircularBuffer<T>::clear(){
208211
std::lock_guard<std::mutex> _lck(_mtx);
209-
_head = _tail = _max_size = 0;
210-
_full = false;
212+
_head = _tail = _size = _max_size = 0;
213+
//_full = false;
211214
}
212215

213216
template<typename T>
214217
inline
215218
typename CircularBuffer<T>::size_type CircularBuffer<T>::size() const{
216219
std::lock_guard<std::mutex> _lck(_mtx);
217-
if(_full)
220+
return _size;
221+
/*(if(_full)
218222
return _max_size;
219223
else{
220224
if(_head >= _tail)
221225
return _head -_tail;
222226
else
223227
return _max_size - _tail +_head;
224-
}
228+
}*/
225229
}
226230

227231
template<typename T>
@@ -270,8 +274,10 @@ void CircularBuffer<T>::push_back(const T& data){
270274
template<typename T>
271275
inline
272276
void CircularBuffer<T>::_increment_bufferstate(){
273-
if(_full)
277+
if(full())
274278
_tail = (_tail + 1)%_max_size;
279+
else
280+
++_size;
275281
_head = (_head + 1)%_max_size;
276282
_full = (_head == _tail);
277283
}
@@ -288,7 +294,8 @@ void CircularBuffer<T>::pop_front(){
288294
template<typename T>
289295
inline
290296
void CircularBuffer<T>::_decrement_bufferstate(){
291-
_full = false;
297+
//_full = false;
298+
--_size;
292299
_tail = (_tail + 1)%_max_size;
293300
}
294301

@@ -343,7 +350,7 @@ typename CircularBuffer<T>::iterator CircularBuffer<T>::end() {
343350
iterator iter;
344351
iter._ptrToBuffer = this;
345352
iter._offset = _tail;
346-
iter._index = size();
353+
iter._index = _size;
347354
iter._reverse = false;
348355
return iter;
349356
}
@@ -355,7 +362,9 @@ typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const{
355362
iterator iter;
356363
iter._ptrToBuffer = this;
357364
iter._offset = _tail;
358-
iter._index = size();
365+
iter._index = _size;
359366
iter._reverse = false;
360367
return iter;
361368
}
369+
370+
#endif /* CIRCULAR_BUFFER_H */
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include <string.h>
44
#include "gtest/gtest.h"
55

6+
#include "circular_buffer.h"
7+
#include <iostream>
8+
#include <string.h>
9+
#include "gtest/gtest.h"
10+
611
struct test_struct{
712
int i;
813
static int count ;
@@ -16,24 +21,9 @@ struct test_struct{
1621
std::cout<<"destructing test_struct\n";
1722
free(bytes);
1823
}
19-
};
2024

21-
TEST(CircularBufferTest, SizeTest){
22-
CircularBuffer<int> test{5};
23-
EXPECT_EQ(5, test.capacity());
24-
}
25+
};
2526

26-
TEST(CircularBufferTest, PushBackTest){
27-
CircularBuffer<int> test{5};
28-
test.push_back(1);
29-
test.push_back(2);
30-
EXPECT_EQ(1, test.front());
31-
EXPECT_EQ(2, test.back());
32-
test.pop_front();
33-
EXPECT_EQ(2, test.front());
34-
EXPECT_EQ(2, test.front());
35-
36-
}
3727

3828
int test_struct::count =0;
3929
int main(int argc, char *argv[])
@@ -89,6 +79,8 @@ int main(int argc, char *argv[])
8979
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
9080
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";
9181

92-
::testing::InitGoogleTest(&argc, argv);
93-
return RUN_ALL_TESTS();
82+
83+
return 0;
9484
}
85+
86+

tests/test_circularbuffer_int.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "circular_buffer.h"
2+
#include <iostream>
3+
#include <string.h>
4+
#include "gtest/gtest.h"
5+
6+
7+
class CircularBufferTest : public ::testing::Test{
8+
9+
protected:
10+
11+
CircularBuffer<int> test{5};
12+
13+
};
14+
15+
TEST_F(CircularBufferTest, CapacityTest){
16+
EXPECT_EQ(5, test.capacity());
17+
}
18+
19+
TEST_F(CircularBufferTest, EmptyTest){
20+
EXPECT_TRUE(test.empty());
21+
test.push_back(10);
22+
test.push_back(5);
23+
EXPECT_FALSE(test.empty());
24+
test.pop_front();
25+
test.pop_front();
26+
EXPECT_TRUE(test.empty());
27+
}
28+
29+
TEST_F(CircularBufferTest, PushBackTest){
30+
test.push_back(1);
31+
test.push_back(2);
32+
EXPECT_EQ(1, test.front());
33+
EXPECT_EQ(2, test.back());
34+
test.pop_front();
35+
EXPECT_EQ(2, test.front());
36+
EXPECT_EQ(2, test.front());
37+
38+
}
39+
40+
41+
TEST_F(CircularBufferTest, SizeTest){
42+
EXPECT_EQ(0, test.size());
43+
test.push_back(1);
44+
test.push_back(2);
45+
EXPECT_EQ(2, test.size());
46+
while(!test.full())
47+
test.push_back(1);
48+
EXPECT_EQ(test.size(), test.capacity());
49+
}
50+
51+
52+
int main(int argc, char *argv[])
53+
{
54+
55+
::testing::InitGoogleTest(&argc, argv);
56+
return RUN_ALL_TESTS();
57+
}

0 commit comments

Comments
 (0)