LCOV - code coverage report
Current view: top level - estd/examples - optional.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 49 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/uncopyable.h"
       4             : 
       5             : #include <estd/optional.h>
       6             : #include <platform/estdint.h>
       7             : 
       8             : #include <gtest/gtest.h>
       9             : 
      10           3 : TEST(OptionalExample, Creation)
      11             : {
      12             :     // [EXAMPLE_OPTIONAL_CREATION_START]
      13             :     // Creating empty optional instance of the class optional final.
      14           1 :     ::estd::optional<int32_t> a;
      15             :     // Returns false, since 'a' is empty.
      16           1 :     EXPECT_FALSE(a.has_value());
      17             : 
      18             :     // Creating an optional instance with value being passed to it.
      19           1 :     ::estd::optional<int32_t> b = 11;
      20           2 :     ::estd::optional<int32_t> c(5);
      21             :     // Creating and assigning an optional instance from another optional instance.
      22           1 :     ::estd::optional<int32_t> d(b);
      23             : 
      24           1 :     EXPECT_TRUE(b.has_value());
      25           1 :     EXPECT_TRUE(c.has_value());
      26           1 :     EXPECT_TRUE(d.has_value());
      27             : 
      28             :     // Creating constant optional instance of the class optional final.
      29           1 :     ::estd::optional<int32_t> const e(10);
      30             : 
      31             :     // Creating empty optional instance of the class optional<T&>
      32           1 :     ::estd::optional<int32_t&> ir;
      33             :     // Returns false, since 'ir' is empty.
      34           1 :     EXPECT_FALSE(ir.has_value());
      35             :     // Creating constant empty optional instance of the class optional<T&>
      36           1 :     ::estd::optional<int32_t&> const irr;
      37             : 
      38             :     // Creating an optional instance of the class optional<T&> with reference being passed to it.
      39           1 :     int32_t i = 0;
      40           1 :     ::estd::optional<int32_t&> ir2(i);
      41           1 :     ASSERT_TRUE(ir2.has_value());
      42             :     // [EXAMPLE_OPTIONAL_CREATION_END]
      43           1 : }
      44             : 
      45           3 : TEST(OptionalExample, Dereferencing)
      46             : {
      47             :     // [EXAMPLE_OPTIONAL_DEREFERENCING_START]
      48             :     // Defining a class 'SomeClass' with parameterized constructor
      49           1 :     class SomeClass
      50             :     {
      51             :     public:
      52             :         int i;
      53             : 
      54           1 :         SomeClass(int i) : i(i) {}
      55             :     };
      56             : 
      57           1 :     ::estd::optional<int32_t> a(5);
      58           1 :     ASSERT_TRUE(a.has_value());
      59             :     // dereferencing the value using operator*().
      60           1 :     EXPECT_EQ(5, *a);
      61             :     // Changing the value of 'a'.
      62           1 :     a = 10;
      63           1 :     EXPECT_EQ(10, *a);
      64           1 :     ::estd::optional<SomeClass> optionalObject = SomeClass(42);
      65             :     // dereferencing the value using operator->().
      66           1 :     EXPECT_EQ(optionalObject->i, 42);
      67             : 
      68           1 :     int32_t b = 0;
      69           1 :     ::estd::optional<int32_t&> ir(b);
      70           1 :     ASSERT_TRUE(ir.has_value());
      71             :     // dereferencing the value using operator*()
      72           1 :     EXPECT_EQ(b, *ir);
      73             :     // dereferencing the value using operator->().
      74           2 :     EXPECT_EQ(&b, ir.operator->());
      75             :     // [EXAMPLE_OPTIONAL_DEREFERENCING_END]
      76           1 : }
      77             : 
      78           3 : TEST(OptionalExample, Can_be_defaulted_to_a_value)
      79             : {
      80             :     // [EXAMPLE_OPTIONAL_VALUE_OR_START]
      81           1 :     ::estd::optional<int32_t> i;
      82             : 
      83             :     // i.value_or(5) returns 5, since 'i' is empty.
      84           1 :     EXPECT_EQ(5, i.value_or(5));
      85             : 
      86           1 :     i = 6;
      87             :     // i.value_or(5) returns 6, since 'i' contains value 6.
      88           3 :     EXPECT_EQ(6, i.value_or(5));
      89             :     // [EXAMPLE_OPTIONAL_VALUE_OR_END]
      90           1 : }
      91             : 
      92           3 : TEST(OptionalExample, Comparison_opearators)
      93             : {
      94             :     // [EXAMPLE_OPTIONAL_COMPARISION_OPERATORS_START]
      95           1 :     ::estd::optional<int32_t> i0 = 7;
      96           1 :     ::estd::optional<int32_t> i1 = 5;
      97             : 
      98           1 :     EXPECT_TRUE(i0 == i0);
      99           1 :     EXPECT_TRUE(i0 != i1);
     100             : 
     101           1 :     i0.reset();
     102           1 :     i1.reset();
     103           1 :     EXPECT_TRUE(i0 == i1);
     104           2 :     EXPECT_FALSE(i0 != i1);
     105             :     // [EXAMPLE_OPTIONAL_COMPARISION_OPERATORS_END]
     106           1 : }

Generated by: LCOV version 1.14