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

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : #include "estd/stack.h"
       4             : 
       5             : #include "estd/assert.h"
       6             : 
       7             : #include <gtest/gtest.h>
       8             : 
       9           3 : TEST(StackExample, construction)
      10             : {
      11             :     // [EXAMPLE_STACK_CONSTRUCT_START]
      12             :     // Construction of a default estd::declare::stack that stores up to 10 elements
      13           1 :     ::estd::declare::stack<int32_t, 10> a;
      14             :     // Construction of a stack from another stack
      15           1 :     ::estd::declare::stack<int32_t, 10> b(a);
      16             :     // [EXAMPLE_STACK_CONSTRUCT_END]
      17           1 : }
      18             : 
      19           3 : TEST(StackExample, access)
      20             : {
      21             :     // [EXAMPLE_STACK_ACCESS_START]
      22             :     // Declare an empty stack
      23           1 :     ::estd::declare::stack<int32_t, 5> s;
      24           1 :     ::estd::stack<int32_t>& cs = s;
      25           1 :     s.push(1);
      26           1 :     ASSERT_EQ(1, s.top());
      27           1 :     ASSERT_EQ(1, cs.top());
      28           1 :     s.push() = 2;
      29           2 :     ASSERT_EQ(2, s.top());
      30             :     // [EXAMPLE_STACK_ACCESS_END]
      31           1 : }
      32             : 
      33           3 : TEST(StackExample, capacity)
      34             : {
      35             :     // [EXAMPLE_STACK_CAPACITY_START]
      36             :     // Declare an empty stack
      37           1 :     ::estd::declare::stack<int32_t, 10> s;
      38           1 :     s.push(1);
      39           1 :     ASSERT_EQ(1U, s.size());
      40           1 :     s.push() = 2;
      41           1 :     ASSERT_EQ(2U, s.size());
      42           1 :     s.pop();
      43           1 :     ASSERT_EQ(1U, s.size());
      44           1 :     s.pop();
      45           1 :     ASSERT_EQ(0U, s.size());
      46           1 :     ASSERT_EQ(10U, s.max_size());
      47           1 :     ASSERT_TRUE(s.empty());
      48          11 :     for (estd::stack<int32_t>::size_type i = 0; i < s.max_size(); ++i)
      49             :     {
      50          10 :         s.push(0);
      51             :     }
      52           2 :     ASSERT_TRUE(s.full());
      53             :     // [EXAMPLE_STACK_CAPACITY_END]
      54           1 : }
      55             : 
      56           3 : TEST(StackExample, modifiers)
      57             : {
      58             :     // [EXAMPLE_STACK_MODIFIERS_START]
      59             :     // Declare an empty stack
      60           1 :     ::estd::declare::stack<int32_t, 10> s;
      61           1 :     int32_t i = 1;
      62          11 :     while (!s.full())
      63             :     {
      64          10 :         s.emplace().construct(i);
      65          10 :         ASSERT_EQ((size_t)i, s.size());
      66          10 :         ASSERT_EQ(i, s.top());
      67          10 :         ++i;
      68             :     }
      69           1 :     ASSERT_EQ(s.max_size(), s.size());
      70           2 :     ASSERT_TRUE(s.full());
      71             :     // [EXAMPLE_STACK_MODIFIERS_END]
      72           1 : }

Generated by: LCOV version 1.14