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

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : #include "estd/object_pool.h"
       4             : 
       5             : #include <gmock/gmock.h>
       6             : #include <gtest/gtest.h>
       7             : 
       8             : namespace
       9             : {
      10             : #define MAKE_POOL(T, N, Name)                 \
      11             :     estd::declare::object_pool<T, N> Name##_; \
      12             :     estd::object_pool<T>& Name(Name##_)
      13             : 
      14             : class MyClass
      15             : {
      16             : public:
      17             :     MyClass() = default;
      18             : };
      19             : 
      20           1 : void example_construction()
      21             : {
      22             :     //[EXAMPLE_START construction]
      23             :     // declare the object pool of size 5
      24           1 :     ::estd::declare::object_pool<int, 5> pool{};
      25             :     // construct using allocate and insert value 17 and 23
      26           1 :     auto& i1 = pool.allocate().construct(17);
      27           1 :     pool.allocate().construct(23);
      28             : 
      29             :     // Checking the values using position
      30           1 :     EXPECT_EQ(17, *pool.begin());
      31           1 :     EXPECT_EQ(17, *pool.cbegin());
      32             : 
      33             :     // release the first one
      34           1 :     pool.release(i1);
      35           1 :     EXPECT_EQ(23, *pool.begin());
      36           1 :     EXPECT_EQ(23, *pool.cbegin());
      37             :     //[EXAMPLE_END construction]
      38             : 
      39             :     //[EXAMPLE_START Usage]
      40             :     // declare object pool p with size 10
      41           1 :     MAKE_POOL(MyClass, 10, p);
      42           1 :     ASSERT_EQ(10U, p.size());
      43             : 
      44             :     // create a reference and use acquire
      45           1 :     MyClass& a = p.acquire();
      46             :     // object acquired, so size of p is reduced
      47           1 :     ASSERT_EQ(9U, p.size());
      48             : 
      49           1 :     p.acquire();
      50             : 
      51           2 :     ASSERT_TRUE(p.contains(a));
      52           1 :     ASSERT_EQ(8U, p.size());
      53             : 
      54             :     // release the reference a
      55           1 :     p.release(a);
      56             :     // clears the objects in object pool
      57           1 :     p.clear();
      58             :     //[EXAMPLE_END Usage]
      59             : }
      60             : } // namespace
      61             : 
      62           3 : TEST(ObjectPool, run_examples) { example_construction(); }

Generated by: LCOV version 1.14