Skip to content

Commit de2cd83

Browse files
committed
added some iterator tests and modified the access control of typedefs for iter and const_iter
1 parent 267e9b1 commit de2cd83

File tree

2 files changed

+131
-11
lines changed

2 files changed

+131
-11
lines changed

circular_buffer.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ class CircularBuffer {
2020
typedef const T& const_reference;
2121
typedef size_t size_type;
2222
typedef ptrdiff_t difference_type;
23-
template <bool isConst>
24-
struct BufferIterator;
25-
typedef BufferIterator<false> iterator;
26-
typedef BufferIterator<true> const_iterator;
23+
template <bool isConst> struct BufferIterator;
24+
2725

2826
public:
29-
3027
explicit CircularBuffer(size_t size)
3128
:_buff{std::unique_ptr<T[]>(new value_type[size])}, _max_size{size}{}
3229

@@ -104,6 +101,9 @@ class CircularBuffer {
104101
const_reference at(size_type index) const;
105102
reference at(size_type index);
106103

104+
typedef BufferIterator<false> iterator;
105+
typedef BufferIterator<true> const_iterator;
106+
107107
iterator begin();
108108
const_iterator begin() const;
109109
iterator end();
@@ -191,6 +191,7 @@ class CircularBuffer {
191191
rhsiter._index += n;
192192
return rhsiter;
193193
}
194+
194195

195196
BufferIterator& operator+=(difference_type n){
196197
_index += n;
@@ -363,8 +364,8 @@ template<typename T>
363364
inline
364365
typename CircularBuffer<T>::reference CircularBuffer<T>::operator[](size_t index) {
365366
std::lock_guard<std::mutex> _lck(_mtx);
366-
if((index<0)||(index>=_max_size))
367-
throw std::out_of_range("Index is out of Range of buffer capacity");
367+
if((index<0)||(index>=_size))
368+
throw std::out_of_range("Index is out of Range of buffer size");
368369
index += _tail;
369370
index %= _max_size;
370371
return _buff[index];
@@ -374,8 +375,8 @@ template<typename T>
374375
inline
375376
typename CircularBuffer<T>::const_reference CircularBuffer<T>::operator[](size_t index) const {
376377
std::lock_guard<std::mutex> _lck(_mtx);
377-
if((index<0)||(index>=_max_size))
378-
throw std::out_of_range("Index is out of Range of buffer capacity");
378+
if((index<0)||(index>=_size))
379+
throw std::out_of_range("Index is out of Range of buffer size");
379380
index += _tail;
380381
index %= _max_size;
381382
return _buff[index];
@@ -419,7 +420,7 @@ template<typename T>
419420
inline
420421
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::begin() const{
421422
std::lock_guard<std::mutex> _lck(_mtx);
422-
iterator iter;
423+
const_iterator iter;
423424
iter._ptrToBuffer = this;
424425
iter._offset = _tail;
425426
iter._index = 0;
@@ -443,7 +444,7 @@ template<typename T>
443444
inline
444445
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const{
445446
std::lock_guard<std::mutex> _lck(_mtx);
446-
iterator iter;
447+
const_iterator iter;
447448
iter._ptrToBuffer = this;
448449
iter._offset = _tail;
449450
iter._index = _size;

tests/string_buffer.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,125 @@ TEST_F(CircularBufferTest, BackTest){
196196

197197
}
198198

199+
TEST_F(CircularBufferTest, AtFunctionTest){
200+
// push back & at access for individual elements
201+
test_buff.push_back("string1");
202+
test_buff.push_back("string2");
203+
EXPECT_EQ(test_buff.size(), 2);
204+
EXPECT_EQ(test_buff.at(0), "string1");
205+
EXPECT_EQ(test_buff.at(1), "string2");
206+
test_buff.at(0) = "string2";
207+
test_buff.at(1) = "string1";
208+
EXPECT_EQ(test_buff.at(0), "string2");
209+
EXPECT_EQ(test_buff.at(1), "string1");
210+
//create full buffer
211+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
212+
test_buff.push_back("string" + std::to_string(i));
213+
//test at function for each element
214+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
215+
EXPECT_EQ(test_buff.at(i), "string" + std::to_string(i));
216+
//test assignment with at function for each element
217+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
218+
test_buff.at(i) = "test_string" ;
219+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
220+
EXPECT_EQ(test_buff.at(i),"test_string");
221+
//test out of bounds
222+
try {
223+
test_buff.at(TEST_BUFFER_SIZE) = "test_string";
224+
FAIL() << "Expected std::out_of_range error";
225+
}
226+
catch(const std::out_of_range& err){
227+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
228+
}
229+
//test out of size
230+
test_buff.pop_front();
231+
try {
232+
test_buff.at(TEST_BUFFER_SIZE - 1) = "test_string";
233+
FAIL() << "Expected std::out_of_range error";
234+
}
235+
catch(const std::out_of_range& err){
236+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
237+
}
238+
}
239+
240+
TEST_F(CircularBufferTest, AccessOperatorTest){
241+
// push back & at access for individual elements
242+
test_buff.push_back("string1");
243+
test_buff.push_back("string2");
244+
EXPECT_EQ(test_buff.size(), 2);
245+
EXPECT_EQ(test_buff[0], "string1");
246+
EXPECT_EQ(test_buff[1], "string2");
247+
test_buff[0] = "string2";
248+
test_buff[1] = "string1";
249+
EXPECT_EQ(test_buff[0], "string2");
250+
EXPECT_EQ(test_buff[1], "string1");
251+
//create full buffer
252+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
253+
test_buff.push_back("string" + std::to_string(i));
254+
//test [] operator for each element
255+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
256+
EXPECT_EQ(test_buff[i], "string" + std::to_string(i));
257+
//test assignment with [] operator for each element
258+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
259+
test_buff[i] = "test_string" ;
260+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
261+
EXPECT_EQ(test_buff[i],"test_string");
262+
//test out of bounds
263+
try {
264+
test_buff[TEST_BUFFER_SIZE] = "test_string";
265+
FAIL() << "Expected std::out_of_range error";
266+
}
267+
catch(const std::out_of_range& err){
268+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
269+
}
270+
//test out of size
271+
test_buff.pop_front();
272+
try {
273+
test_buff[TEST_BUFFER_SIZE - 1] = "test_string";
274+
FAIL() << "Expected std::out_of_range error";
275+
}
276+
catch(const std::out_of_range& err){
277+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
278+
}
279+
}
280+
281+
282+
TEST_F(CircularBufferTest, BeginIteratorTest){
283+
//create full buffer
284+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
285+
test_buff.push_back("string" + std::to_string(i));
286+
//test first element with iterator
287+
CircularBuffer<std::string>::iterator it = test_buff.begin();
288+
EXPECT_EQ(*it, "string0" );
289+
//access with begin iterator
290+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
291+
EXPECT_EQ(*(it++), "string" + std::to_string(i));
292+
293+
//access with const begin iterator
294+
CircularBuffer<std::string>::const_iterator const_it = test_buff.begin();
295+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
296+
EXPECT_EQ(*(const_it++), "string" + std::to_string(i));
297+
}
298+
299+
TEST_F(CircularBufferTest, EndIteratorTest){
300+
//create full buffer
301+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
302+
test_buff.push_back("string" + std::to_string(i));
303+
//test last element with iterator
304+
CircularBuffer<std::string>::iterator it = test_buff.end();
305+
EXPECT_EQ(*(--it), "string99" );
306+
//access with begin iterator
307+
for(int i = TEST_BUFFER_SIZE-1; i>=0; i--)
308+
EXPECT_EQ(*(it--), "string" + std::to_string(i));
309+
310+
//access with const begin iterator
311+
CircularBuffer<std::string>::const_iterator const_it = test_buff.end();
312+
for(int i = TEST_BUFFER_SIZE-1; i>=0; i--)
313+
EXPECT_EQ(*(--const_it), "string" + std::to_string(i));
314+
}
315+
316+
317+
199318

200319

201320

0 commit comments

Comments
 (0)