Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include "estd/none.h" 4 : 5 : #include <estd/functional.h> 6 : #include <estd/optional.h> 7 : 8 : #include <gtest/gtest.h> 9 : 10 : // [EXAMPLE_NONE_INITIALIZATION_TO_OBJECT_STRUCT_START] 11 : // Creating a structure Foo. 12 : struct Foo 13 : { 14 : // Static method that returns a constant integer value of 5 15 : static int32_t bar0() { return 5; } 16 : 17 : // Static method that takes a float value and returns the integer value of the float. 18 : static int32_t bar1(float f) { return f; } 19 : }; 20 : 21 : // [EXAMPLE_NONE_INITIALIZATION_TO_OBJECT_STRUCT_END] 22 : 23 3 : TEST(NoneExample, Usage_of_none_in_function) 24 : { 25 : // [EXAMPLE_NONE_INITIALIZATION_TO_OBJECT_START] 26 : // Declare a function object of type. 27 1 : ::estd::function<int32_t()> f0; // estd::function<int32_t()> named f0. 28 1 : ::estd::function<int32_t(float)> f1; // estd::function<int32_t(float)> named f1. 29 : 30 : // Check if f0 and f1 has been set to a target. 31 1 : EXPECT_FALSE(f0.has_value()); // Expecting it to be false initially. 32 1 : EXPECT_FALSE(f1.has_value()); 33 : 34 : // Set f0 to a target function Foo::bar0 and f1 to a target function Foo::bar1. 35 1 : f0 = ::estd::function<int32_t()>::create<&Foo::bar0>(); 36 1 : f1 = ::estd::function<int32_t(float)>::create<&Foo::bar1>(); 37 : 38 : // Check if f0 and f1 has been set to a target. 39 1 : EXPECT_TRUE(f0.has_value()); // Expecting it to be true now. 40 1 : EXPECT_TRUE(f1.has_value()); 41 : 42 : // Assign estd::none to f0 and f1 . 43 1 : f0 = ::estd::none; // Making it an empty or null function object. 44 1 : f1 = ::estd::none; 45 : 46 : // Check if f0 and f1 has been set to a target. 47 1 : EXPECT_FALSE(f0.has_value()); // Expecting it to be false after assignment to estd::none. 48 1 : EXPECT_FALSE(f1.has_value()); 49 : 50 : // [EXAMPLE_NONE_INITIALIZATION_TO_OBJECT_END] 51 1 : } 52 : 53 3 : TEST(NoneExample, conversion_operator_of_none) 54 : { 55 : // [EXAMPLE_NONE_CONVERSION_OPERATOR_OF_NONE_START] 56 1 : estd::none_t noneInstance; 57 : 58 : // Test the conversion to int 59 1 : int resultInt = static_cast<int>(noneInstance); 60 : 61 : // Check if resultInt is equal to 0 62 1 : EXPECT_EQ(resultInt, 0); // Assuming the default-constructed int value is 0 63 : 64 : // Test the conversion to double 65 1 : double resultDouble = static_cast<double>(noneInstance); 66 : 67 : // Check if resultDouble is equal to 0.0. 68 1 : EXPECT_EQ(resultDouble, 0.0); // Assuming the default-constructed double value is 0.0 69 : // [EXAMPLE_NONE_CONVERSION_OPERATOR_OF_NONE_END] 70 1 : }