Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include <estd/tiny_ring.h> 4 : 5 : #include <gtest/gtest.h> 6 : 7 3 : TEST(TinyRingExample, construction_and_operation) 8 : { 9 : // [EXAMPLE_TINY_RING_CONSTRUCTION_AND_OPERATION_START] 10 : // tiny_ring of size 3 is created. 11 1 : ::estd::tiny_ring<uint16_t, 3> r; 12 : // 'r.empty()' returns true, since there are no elements in the tiny_ring. 13 1 : ASSERT_TRUE(r.empty()); 14 : 15 : // Pushing elements at the back of the tiny_ring using push_back() function. 16 1 : r.push_back(1); 17 1 : r.push_back(2); 18 1 : r.push_back(3); 19 : 20 : // 'r.full()' returns true, since tiny_ring is at maximum capacity. 21 1 : ASSERT_TRUE(r.full()); 22 : 23 : // Get a copy of the oldest element in the tiny_ring. 24 1 : ASSERT_EQ(1, r.front()); 25 : 26 : // Popping front element from the tiny_ring. 27 1 : r.pop_front(); 28 : // The front element will be 2 after 'pop_front()'. 29 2 : ASSERT_EQ(2, r.front()); 30 : // [EXAMPLE_TINY_RING_CONSTRUCTION_AND_OPERATION_END] 31 : } 32 : 33 3 : TEST(TinyRingExample, push_back) 34 : { 35 : // [EXAMPLE_TINY_RING_PUSH_BACK_START] 36 : // tiny_ring of size 3 is created. 37 1 : ::estd::tiny_ring<uint16_t, 3> r; 38 : 39 : // Pushing elements at the back of the tiny_ring using push_back() function. 40 1 : r.push_back(1); 41 1 : r.push_back(2); 42 1 : r.push_back(3); 43 : 44 : // 'r.full()' returns true, since tiny_ring is at maximum capacity. 45 1 : ASSERT_TRUE(r.full()); 46 : // The front element is 1. 47 1 : EXPECT_EQ(1, r.front()); 48 : // Pushing element to overwrite the tiny_ring. 49 1 : r.push_back(4); 50 : // The front element will be 2 since 1 is overwritten. 51 2 : EXPECT_EQ(2, r.front()); 52 : // [EXAMPLE_TINY_RING_PUSH_BACK_END] 53 : } 54 : 55 3 : TEST(TinyRingExample, data) 56 : { 57 : // [EXAMPLE_TINY_RING_DATA_FUNCTION_START] 58 1 : ::estd::tiny_ring<uint16_t, 5> r; 59 : // r.data().size() returns 5, since the size of the tiny_ring is 5. 60 1 : ASSERT_EQ(5U, r.data().size()); 61 1 : r.push_back(1); 62 1 : r.push_back(2); 63 : // r.data()[0] Returns 1, since 1 is at the index 0. 64 1 : ASSERT_EQ(1, r.data()[0]); 65 : // r.data()[1] Returns 2, since 2 is at the index 1. 66 2 : ASSERT_EQ(2, r.data()[1]); 67 : // [EXAMPLE_TINY_RING_DATA_FUNCTION_END] 68 : }