Skip to content

Commit 7f4152f

Browse files
committed
added end iterator and begin iterator tests
1 parent de2cd83 commit 7f4152f

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

tests/string_buffer.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,57 @@ TEST_F(CircularBufferTest, BeginIteratorTest){
294294
CircularBuffer<std::string>::const_iterator const_it = test_buff.begin();
295295
for(int i=0; i<TEST_BUFFER_SIZE; i++)
296296
EXPECT_EQ(*(const_it++), "string" + std::to_string(i));
297+
//test out of bounds
298+
try {
299+
*it = "test_string";
300+
FAIL() << "Expected std::out_of_range error";
301+
}
302+
catch(const std::out_of_range& err){
303+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
304+
}
305+
306+
try {
307+
std::string out_of_bound = *(const_it);
308+
FAIL() << "Expected std::out_of_range error";
309+
}
310+
catch(const std::out_of_range& err){
311+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
312+
}
297313
}
298314

315+
299316
TEST_F(CircularBufferTest, EndIteratorTest){
300317
//create full buffer
301318
for(int i=0; i<TEST_BUFFER_SIZE; i++)
302319
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
320+
321+
CircularBuffer<std::string>::iterator it = test_buff.end();
322+
//access with end iterator
307323
for(int i = TEST_BUFFER_SIZE-1; i>=0; i--)
308-
EXPECT_EQ(*(it--), "string" + std::to_string(i));
324+
EXPECT_EQ(*(--it), "string" + std::to_string(i));
309325

310-
//access with const begin iterator
326+
//access with const end iterator
311327
CircularBuffer<std::string>::const_iterator const_it = test_buff.end();
312328
for(int i = TEST_BUFFER_SIZE-1; i>=0; i--)
313329
EXPECT_EQ(*(--const_it), "string" + std::to_string(i));
330+
331+
//test out of bounds
332+
try {
333+
*(--it) = "test_string";
334+
FAIL() << "Expected std::out_of_range error";
335+
}
336+
catch(const std::out_of_range& err){
337+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
338+
}
339+
340+
try {
341+
std::string out_of_bound = *(--const_it);
342+
FAIL() << "Expected std::out_of_range error";
343+
}
344+
catch(const std::out_of_range& err){
345+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
346+
}
347+
314348
}
315349

316350

@@ -324,3 +358,6 @@ TEST_F(CircularBufferTest, EndIteratorTest){
324358

325359

326360

361+
362+
363+

0 commit comments

Comments
 (0)