Skip to content

Commit 931392d

Browse files
committed
renamed tests and added tests with stl algorithms
1 parent ae705c7 commit 931392d

File tree

4 files changed

+99
-52
lines changed

4 files changed

+99
-52
lines changed

tests/CMakeLists.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
3333
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
3434
EXCLUDE_FROM_ALL)
3535

36-
add_executable(int_buffer int_buffer.cpp)
37-
target_link_libraries(int_buffer
36+
add_executable(simple_int_test simple_int_test.cpp)
37+
target_link_libraries(simple_int_test
3838
circularbuffer
3939
gtest_main
4040
)
41-
add_test(NAME test_int_buffer COMMAND int_buffer)
41+
add_test(NAME simple_int_test COMMAND simple_int_test)
4242

43-
add_executable(string_buffer string_buffer.cpp)
44-
target_link_libraries(string_buffer
43+
add_executable(member_func_test member_func_test.cpp)
44+
target_link_libraries(member_func_test
4545
circularbuffer
4646
gtest_main
4747
)
48-
add_test(NAME test_string_buffer COMMAND string_buffer)
49-
48+
add_test(NAME member_func_test COMMAND member_func_test)
5049

50+
add_executable(iterator_test iterator_test.cpp)
51+
target_link_libraries(iterator_test
52+
circularbuffer
53+
gtest_main
54+
)
55+
add_test(NAME iterator_test COMMAND iterator_test)

tests/iterator_test.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "circular_buffer.h"
2+
#include <algorithm>
3+
#include <iostream>
4+
#include <string.h>
5+
#include <utility>
6+
#include "gtest/gtest.h"
7+
8+
#define TEST_BUFFER_SIZE 100
9+
#define REDUCE_SIZE 100
10+
11+
class CircularBufferTest : public ::testing::Test{
12+
13+
protected:
14+
15+
CircularBuffer<std::string> test_buff{TEST_BUFFER_SIZE};
16+
17+
};
18+
19+
TEST_F(CircularBufferTest, IteratorBasedLoopTest){
20+
//create full buffer
21+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
22+
test_buff.push_back("string" + std::to_string(i));
23+
int i = 0;
24+
for(auto it = test_buff.begin(); it!=test_buff.end(); it++)
25+
EXPECT_EQ(*it, "string" + std::to_string(i++));
26+
//partially fill buffers
27+
test_buff.clear();
28+
for(int i=0; i<TEST_BUFFER_SIZE/2; i++)
29+
test_buff.push_back("string" + std::to_string(i));
30+
//test begin and end on partially full buffer
31+
i = 0;
32+
for(auto it = test_buff.begin(); it!=test_buff.end(); it++)
33+
EXPECT_EQ(*it, "string" + std::to_string(i++));
34+
//test size with increment variable
35+
EXPECT_EQ(i, TEST_BUFFER_SIZE/2);
36+
37+
38+
}
39+
40+
TEST_F(CircularBufferTest, RangeBasedLoopTest){
41+
//create full buffer
42+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
43+
test_buff.push_back("string" + std::to_string(i));
44+
int i = 0;
45+
//copied elements
46+
for(auto buff_elem : test_buff)
47+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
48+
//auto reference
49+
i = 0;
50+
for(auto& buff_elem : test_buff)
51+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
52+
//auto const reference
53+
i = 0;
54+
for(const auto& buff_elem : test_buff)
55+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
56+
//check iterators after modifications
57+
for(int i = 0; i<REDUCE_SIZE; i++)
58+
test_buff.pop_front();
59+
i = 0;
60+
for(const auto& buff_elem : test_buff)
61+
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
62+
EXPECT_EQ(i, TEST_BUFFER_SIZE - REDUCE_SIZE);
63+
}
64+
65+
TEST_F(CircularBufferTest, FindTest){
66+
//create full buffer
67+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
68+
test_buff.push_back("string" + std::to_string(i));
69+
auto result = std::find(test_buff.cbegin(), test_buff.cend(), "string50");
70+
EXPECT_EQ(*result, "string50");
71+
result = std::find(test_buff.cbegin(), test_buff.cend(), "string100");
72+
EXPECT_EQ(result, test_buff.cend());
73+
}
74+
75+
TEST_F(CircularBufferTest, ForEachTest){
76+
//create full buffer
77+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
78+
test_buff.push_back("string" + std::to_string(i));
79+
std::for_each(test_buff.begin(), test_buff.end(), [](std::string& elem){ elem = elem + "modified";});
80+
int i=0;
81+
for(const auto& elem: test_buff)
82+
EXPECT_EQ(elem, "string" + std::to_string(i++) + "modified");
83+
}
84+
85+
86+

tests/string_buffer.cpp renamed to tests/member_func_test.cpp

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ TEST_F(CircularBufferTest, AtFunctionTest){
242242
}
243243

244244
TEST_F(CircularBufferTest, AccessOperatorTest){
245-
// push back & at access for individual elements
245+
// push back & [] access for individual elements
246246
test_buff.push_back("string1");
247247
test_buff.push_back("string2");
248248
EXPECT_EQ(test_buff.size(), 2);
@@ -389,51 +389,7 @@ TEST_F(CircularBufferTest, CendIteratorTest){
389389
}
390390

391391

392-
TEST_F(CircularBufferTest, IteratorBasedLoopTest){
393-
//create full buffer
394-
for(int i=0; i<TEST_BUFFER_SIZE; i++)
395-
test_buff.push_back("string" + std::to_string(i));
396-
int i = 0;
397-
for(auto it = test_buff.begin(); it!=test_buff.end(); it++)
398-
EXPECT_EQ(*it, "string" + std::to_string(i++));
399-
//partially fill buffers
400-
test_buff.clear();
401-
for(int i=0; i<TEST_BUFFER_SIZE/2; i++)
402-
test_buff.push_back("string" + std::to_string(i));
403-
//test begin and end on partially full buffer
404-
i = 0;
405-
for(auto it = test_buff.begin(); it!=test_buff.end(); it++)
406-
EXPECT_EQ(*it, "string" + std::to_string(i++));
407-
//test size with increment variable
408-
EXPECT_EQ(i, TEST_BUFFER_SIZE/2);
409-
410-
411-
}
412392

413-
TEST_F(CircularBufferTest, RangeBasedLoopTest){
414-
//create full buffer
415-
for(int i=0; i<TEST_BUFFER_SIZE; i++)
416-
test_buff.push_back("string" + std::to_string(i));
417-
int i = 0;
418-
//copied elements
419-
for(auto buff_elem : test_buff)
420-
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
421-
//auto reference
422-
i = 0;
423-
for(auto& buff_elem : test_buff)
424-
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
425-
//auto const reference
426-
i = 0;
427-
for(const auto& buff_elem : test_buff)
428-
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
429-
//check iterators after modifications
430-
for(int i = 0; i<REDUCE_SIZE; i++)
431-
test_buff.pop_front();
432-
i = 0;
433-
for(const auto& buff_elem : test_buff)
434-
EXPECT_EQ(buff_elem, "string" + std::to_string(i++));
435-
EXPECT_EQ(i, TEST_BUFFER_SIZE - REDUCE_SIZE);
436-
}
437393

438394

439395

File renamed without changes.

0 commit comments

Comments
 (0)