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

          Line data    Source code
       1             : // Copyright 2024 Accenture.
       2             : 
       3             : #include "estd/math.h"
       4             : 
       5             : #include <gmock/gmock.h>
       6             : 
       7             : using namespace ::testing;
       8             : 
       9           3 : TEST(Math, abs_default_return_type)
      10             : {
      11           1 :     int32_t number = -1;
      12             :     // [EXAMPLE_MATH_ABS_DEFAULT_RETURN_TYPE_START]
      13             : 
      14           1 :     auto const absMinAuto = ::estd::abs(number);
      15             : 
      16             :     // The absMinAuto holds the absolute value of the number passed.
      17           1 :     EXPECT_EQ(absMinAuto, 1);
      18           2 :     EXPECT_THAT(absMinAuto, A<uint64_t>());
      19             : 
      20             :     // [EXAMPLE_MATH_ABS_DEFAULT_RETURN_TYPE_END]
      21           1 : }
      22             : 
      23             : // This shows the functionality of abs() for different InputType and ReturnType.
      24           3 : TEST(Math, abs_signed_min_to_unsigned)
      25             : {
      26             :     // [EXAMPLE_MATH_ABS_SIGNED_MIN_TO_UNSIGNED_START]
      27             : 
      28             :     // Minimum value of a signed integer of size x can have its absolute value be assigned to an
      29             :     // unsigned/signed integer of the same/greater size than x.
      30             : 
      31             :     // Case1: Unsigned with same size.
      32           1 :     auto absMinInt64_1 = ::estd::abs<uint64_t>(std::numeric_limits<int64_t>::min());
      33             :     // Returning absolute value
      34           1 :     EXPECT_EQ(absMinInt64_1, 9223372036854775808UL);
      35           2 :     EXPECT_THAT(absMinInt64_1, A<uint64_t>());
      36             : 
      37             :     // Case2: Signed with greater size than x.
      38           1 :     auto absMinInt64_2 = ::estd::abs<int64_t>(std::numeric_limits<int32_t>::min());
      39             :     // Returning absolute value
      40           1 :     EXPECT_EQ(absMinInt64_2, 2147483648U);
      41           2 :     EXPECT_THAT(absMinInt64_2, A<int64_t>());
      42             : 
      43             :     // Value of an unsigned integer of size X can have its absolute value be assigned to a
      44             :     // signed/unsigned integer of same/greater size than x.
      45             : 
      46             :     // Case1: Signed with same size.
      47           1 :     auto absMinInt64_3 = ::estd::abs<uint64_t>(9999999999999999999UL);
      48             :     // Returning absolute value
      49           1 :     EXPECT_EQ(absMinInt64_3, 9999999999999999999UL);
      50           2 :     EXPECT_THAT(absMinInt64_3, A<uint64_t>());
      51             : 
      52             :     // Case2: Unsigned with greater size than x.
      53           1 :     uint16_t shortVal = 9999;
      54           1 :     auto absMinInt16  = ::estd::abs<uint16_t>(shortVal);
      55             :     // Returning absolute value
      56           1 :     EXPECT_EQ(absMinInt16, 9999U);
      57           2 :     EXPECT_THAT(absMinInt16, A<uint16_t>());
      58             : 
      59           1 :     auto absMaxUInt64 = ::estd::abs<int64_t>(std::numeric_limits<uint32_t>::max());
      60             :     // Returning absolute value
      61           1 :     EXPECT_EQ(absMaxUInt64, 4294967295);
      62           2 :     EXPECT_THAT(absMaxUInt64, A<int64_t>());
      63             : 
      64             :     // [EXAMPLE_MATH_ABS_SIGNED_MIN_TO_UNSIGNED_END]
      65           1 : }
      66             : 
      67           3 : TEST(Math, abs_signed_floating_to_bigger_size_signed_floating)
      68             : {
      69             :     // [EXAMPLE_ABS_SIGNED_FLOATING_START]
      70             : 
      71             :     // Example to show that a value of a floating point can have its absolute value be assigned to a
      72             :     // floating point of the same size.
      73           1 :     auto absFloat = ::estd::abs<float>(-41.0f / 13.0f);
      74             :     // Returning absolute value
      75           1 :     EXPECT_EQ(absFloat, 41.0f / 13.0f);
      76           2 :     EXPECT_THAT(absFloat, A<float>());
      77             : 
      78           1 :     auto absDouble = ::estd::abs<double>(-41.0 / 13.0);
      79             :     // Returning absolute value
      80           1 :     EXPECT_EQ(absDouble, 41.0 / 13.0);
      81           2 :     EXPECT_THAT(absDouble, A<double>());
      82             : 
      83             :     // [EXAMPLE_ABS_SIGNED_FLOATING_END]
      84           1 : }

Generated by: LCOV version 1.14