|
| 1 | +#include "circular_buffer.h" |
| 2 | +#include <iostream> |
| 3 | +#include <string.h> |
| 4 | +#include "circular_buffer.h" |
| 5 | +#include <iostream> |
| 6 | +#include <string.h> |
| 7 | +#include <utility> |
| 8 | + |
| 9 | + |
| 10 | +int main(int argc, char *argv[]) |
| 11 | +{ |
| 12 | + |
| 13 | + CircularBuffer<std::string> string_buff{10}; |
| 14 | + //checking buffer empty function |
| 15 | + std::cout<<"Calling buffer empty function "<<string_buff.empty()<<"\n"; |
| 16 | + |
| 17 | + //Pushing data back into buffer |
| 18 | + std::cout<<"Pushing back string data in bufffer \n"; |
| 19 | + for(int i = 0; i<10;i++){ |
| 20 | + string_buff.push_back(" sample string " + std::to_string(i)); |
| 21 | + std::cout<<"Pushing back string "<<i<<std::endl; |
| 22 | + } |
| 23 | + |
| 24 | + //Length of buffer |
| 25 | + std::cout<<"Checking length of buffer "<<string_buff.size()<<"\n"; |
| 26 | + //Using Front Function, gives the first element |
| 27 | + std::cout<<"Checking front function "<<string_buff.front()<<"\n"; |
| 28 | + //Using Back Function, gives last element |
| 29 | + std::cout<<"Checking back function "<<string_buff.back()<<"\n"; |
| 30 | + //Front and Back can be used for assignment |
| 31 | + string_buff.front() = "modified front string"; |
| 32 | + string_buff.back() = "modified back string"; |
| 33 | + |
| 34 | + //Pop Function removes element as per FIFO |
| 35 | + std::cout<<"Checking pop_front function\n"; |
| 36 | + string_buff.pop_front(); |
| 37 | + std::cout<<"pop front executed new size: "<<string_buff.size() |
| 38 | + <<" new front:" <<string_buff.front()<<"\n"; |
| 39 | + |
| 40 | + std::cout<<"Checking iterator function\n"; |
| 41 | + auto it = string_buff.begin(); |
| 42 | + |
| 43 | + std::cout<<"Checking deference * operator "<<*it<<"\n"; |
| 44 | + std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n"; |
| 45 | + std::cout<<"Checking deference -- operator "<<*(--it)<<"\n"; |
| 46 | + |
| 47 | + // Range based for loop can be used like other STL containers |
| 48 | + std::cout<<"Checking iterator for loop \n"; |
| 49 | + for(auto& it: string_buff) |
| 50 | + std::cout<<"Checking range based for loop function "<<it<<"\n"; |
| 51 | + |
| 52 | + // for loop with iterators can be used like other STL stile containers |
| 53 | + std::cout<<"Checking for loop with iterator \n"; |
| 54 | + for(auto it = string_buff.begin(); it != string_buff.end(); it++) |
| 55 | + std::cout<<"Checking for loop function "<<*it<<"\n"; |
| 56 | + |
| 57 | + // push_back using move semantics |
| 58 | + std::string string_to_move{"This string is moved"}; |
| 59 | + string_buff.push_back(std::move(string_to_move)); |
| 60 | + std::cout<<"string moved to buffer new size: "<<string_buff.size() |
| 61 | + <<" new back:" <<string_buff.back()<<"\n"; |
| 62 | + |
| 63 | + //creating buffer from another buffer |
| 64 | + CircularBuffer<std::string> string_buff_copy1{string_buff}; |
| 65 | + std::cout<<"Buffer Copy Created, copied buffer size "<<string_buff_copy1.size()<<"\n"; |
| 66 | + |
| 67 | + //copying buffer |
| 68 | + CircularBuffer<std::string> string_buff_copy2{10}; |
| 69 | + string_buff_copy2 = string_buff; |
| 70 | + std::cout<<"Buffer Copy assigned , copied buffer size "<<string_buff_copy2.size()<<"\n"; |
| 71 | + |
| 72 | + //move constructing |
| 73 | + CircularBuffer<std::string> string_buff_move1{std::move(string_buff)}; |
| 74 | + std::cout<<"Buffer created with move constructor, moved buffer size "<<string_buff_move1.size()<<"\n"; |
| 75 | + |
| 76 | + //move assignment |
| 77 | + CircularBuffer<std::string> string_buff_move2{10}; |
| 78 | + string_buff_move2 = std::move(string_buff_move1); |
| 79 | + std::cout<<"Buffer moved with assignment operator, moved buffer size "<<string_buff_move2.size()<<"\n"; |
| 80 | + |
| 81 | +} |
0 commit comments