|
| 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 | + |
0 commit comments