LCOV - code coverage report
Current view: top level - estd/examples - functional.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 26 88.5 %
Date: 2025-02-26 13:32:38 Functions: 10 18 55.6 %

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : #include "estd/functional.h"
       4             : 
       5             : #include "estd/none.h"
       6             : #include "estd/type_traits.h"
       7             : 
       8             : #include <platform/estdint.h>
       9             : 
      10             : #include <gtest/gtest.h>
      11             : 
      12             : // [EXAMPLE_FUNCTION_ZERO_PARAM_START]
      13             : /* Define a zero parameter function */
      14           0 : int32_t foo0() { return 2; }
      15             : 
      16           3 : TEST(Function, TestFreeFunctionZeroParams)
      17             : {
      18           1 :     ::estd::function<int32_t()> f = ::estd::function<int32_t()>::create<&foo0>();
      19           2 :     ASSERT_EQ(2, f());
      20             : }
      21             : 
      22             : // [EXAMPLE_FUNCTION_ZERO_PARAM_END]
      23             : 
      24             : // [EXAMPLE_FUNCTION_FUNCTOR_ZERO_PARAM_START]
      25             : /* Define functor */
      26             : struct Bar0
      27             : {
      28             :     int32_t operator()() const { return 7; }
      29             : };
      30             : 
      31           3 : TEST(Function, TestFunctorZeroParams)
      32             : {
      33           1 :     Bar0 bar;
      34           1 :     ::estd::function<int32_t()> f(::estd::make_function(bar));
      35           2 :     ASSERT_EQ(7, f());
      36             : }
      37             : 
      38             : // [EXAMPLE_FUNCTION_FUNCTOR_ZERO_PARAM_END]
      39             : 
      40           3 : TEST(Function, TestLambdaParameter)
      41             : {
      42             :     // [EXAMPLE_FUNCTION_LAMBDA_PARAM_START]
      43           1 :     auto callme = [](::estd::function<int32_t()> f) { return f(); };
      44           1 :     auto f      = []() { return 88; };
      45           2 :     ASSERT_EQ(88, callme(::estd::make_function(f)));
      46             :     // [EXAMPLE_FUNCTION_LAMBDA_PARAM_END]
      47             : }
      48             : 
      49             : // [EXAMPLE_CLOSURE_THREE_PARAM_START]
      50           0 : int32_t foo3(float, int32_t, int32_t h) { return h; }
      51             : 
      52           3 : TEST(Function, TestFreeFunctionClosureThreeParam)
      53             : {
      54           1 :     ::estd::function<int32_t(float, int32_t, int32_t)> f
      55           1 :         = ::estd::function<int32_t(float, int32_t, int32_t)>::create<&foo3>();
      56           1 :     ::estd::closure<int32_t(float, int32_t, int32_t)> d
      57           1 :         = ::estd::bind_all(f, 2.14f, int32_t(3), int32_t(4));
      58           2 :     ASSERT_EQ(4, d());
      59             : }
      60             : 
      61             : // [EXAMPLE_CLOSURE_THREE_PARAM_END]
      62             : 
      63             : // [EXAMPLE_CLOSURE_BIND1ST_THREE_PARAM_START]
      64           0 : int32_t foobar3(int32_t const, int32_t const, int32_t const h) { return h; }
      65             : 
      66           3 : TEST(Function, TestBind1stThreeParams)
      67             : {
      68           1 :     using fct = ::estd::function<int32_t(int32_t const, int32_t const, int32_t const)>;
      69           1 :     fct f     = fct::create<&foobar3>();
      70           1 :     using bnd = ::estd::binder1st<int32_t(int32_t const, int32_t const, int32_t const)>;
      71           1 :     bnd g     = ::estd::bind1st(f, int32_t(23));
      72           2 :     ASSERT_EQ(4, g(2, 4));
      73             : }
      74             : 
      75             : // [EXAMPLE_CLOSURE_BIND1ST_THREE_PARAM_END]

Generated by: LCOV version 1.14