LCOV - code coverage report
Current view: top level - estd/examples - ring.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 33 100.0 %
Date: 2025-01-20 13:53:09 Functions: 4 6 66.7 %

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : #include <estd/ring.h>
       4             : 
       5             : #include <gtest/gtest.h>
       6             : 
       7           3 : TEST(RingExample, construction_and_operation)
       8             : {
       9             :     // [EXAMPLE_RING_CONSTRUCTION_AND_OPERATION_START]
      10             :     // The array of size 1000 is created.
      11           1 :     uint8_t mem[1000];
      12             :     // The slice object is referencing to array named mem.
      13           1 :     ::estd::slice<uint8_t> s(mem);
      14             :     // ring of size 5 is created from slice object.
      15           1 :     ::estd::ring<uint16_t>& r = *::estd::ring<uint16_t>::make(5, s);
      16             : 
      17             :     // Checks whether the ring r is empty.
      18           1 :     ASSERT_TRUE(r.empty());
      19             : 
      20             :     // Checking the size of the ring.
      21           1 :     EXPECT_EQ(5U, r.length());
      22             : 
      23             :     // Pushing elements at the back of the ring using push_back() function.
      24           1 :     r.push_back(10);
      25           1 :     r.push_back(20);
      26           1 :     r.push_back(30);
      27             : 
      28             :     // Checks the number of used index of the ring.
      29           1 :     EXPECT_EQ(3, r.used());
      30             : 
      31             :     // Element at particular index is accessed using at() function.
      32           1 :     EXPECT_EQ(10, r.at(0));
      33           1 :     EXPECT_EQ(20, r.at(1));
      34           1 :     EXPECT_EQ(30, r.at(2));
      35             : 
      36             :     // The oldest or the front element can be accessed using front() function.
      37           1 :     EXPECT_EQ(10, r.front());
      38             :     // Popping front element from the ring.
      39           1 :     r.pop_front();
      40             :     // The front element will be 20 after pop_front().
      41           1 :     ASSERT_EQ(20, r.front());
      42           1 :     r.pop_front();
      43           1 :     r.pop_front();
      44             :     // 'r.empty()' returns true, since there are no elements in the ring.
      45           2 :     EXPECT_TRUE(r.empty());
      46             :     // [EXAMPLE_RING_CONSTRUCTION_AND_OPERATION_END]
      47             : }
      48             : 
      49           3 : TEST(RingExample, push_back)
      50             : {
      51             :     // [EXAMPLE_RING_PUSH_BACK_START]
      52             :     // The array of size 100 is created.
      53           1 :     uint8_t mem[100];
      54             :     // The slice object is referencing to array named mem.
      55           1 :     ::estd::slice<uint8_t> s(mem);
      56             :     // ring of size 3 is created from slice object.
      57           1 :     ::estd::ring<uint16_t>& r = *::estd::ring<uint16_t>::make(3, s);
      58             : 
      59             :     // Pushing elements at the back of the ring using push_back() function.
      60           1 :     r.push_back(10);
      61           1 :     r.push_back(20);
      62           1 :     r.push_back(30);
      63           1 :     EXPECT_EQ(10, r.at(0));
      64             :     // Checks if the ring is full. It returns true, since ring is at maximum capacity.
      65           1 :     EXPECT_TRUE(r.full());
      66             : 
      67             :     // Pushing element to overwrite the ring.
      68           1 :     r.push_back(40);
      69             : 
      70             :     // Overwritten elements.
      71           1 :     EXPECT_EQ(20, r.at(0));
      72           1 :     EXPECT_EQ(30, r.at(1));
      73           1 :     EXPECT_EQ(40, r.at(2));
      74             :     // [EXAMPLE_RING_PUSH_BACK_END]
      75           1 : }

Generated by: LCOV version 1.14