ArmNN
 20.02
OptionalTest.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <boost/test/unit_test.hpp>
6 
7 #include <armnn/Optional.hpp>
8 #include <string>
9 
11 
12 namespace
13 {
14 
15 void PassStringRef(armnn::Optional<std::string&> value)
16 {
17  armnn::IgnoreUnused(value);
18 }
19 
20 void PassStringRefWithDefault(armnn::Optional<std::string&> value = armnn::EmptyOptional())
21 {
22  armnn::IgnoreUnused(value);
23 }
24 
25 } // namespace <anonymous>
26 
27 BOOST_AUTO_TEST_SUITE(OptionalTests)
28 
29 BOOST_AUTO_TEST_CASE(SimpleStringTests)
30 {
31  armnn::Optional<std::string> optionalString;
32  BOOST_TEST(static_cast<bool>(optionalString) == false);
33  BOOST_TEST(optionalString.has_value() == false);
34  BOOST_TEST((optionalString == armnn::Optional<std::string>()));
35 
36  optionalString = std::string("Hello World");
37  BOOST_TEST(static_cast<bool>(optionalString) == true);
38  BOOST_TEST(optionalString.has_value() == true);
39  BOOST_TEST(optionalString.value() == "Hello World");
40  BOOST_TEST((optionalString == armnn::Optional<std::string>("Hello World")));
41 
42  armnn::Optional<std::string> otherString;
43  otherString = optionalString;
44  BOOST_TEST(static_cast<bool>(otherString) == true);
45  BOOST_TEST(otherString.value() == "Hello World");
46 
47  optionalString.reset();
48  BOOST_TEST(static_cast<bool>(optionalString) == false);
49  BOOST_TEST(optionalString.has_value() == false);
50 
51  const std::string stringValue("Hello World");
52  armnn::Optional<std::string> optionalString2(stringValue);
53  BOOST_TEST(static_cast<bool>(optionalString2) == true);
54  BOOST_TEST(optionalString2.has_value() == true);
55  BOOST_TEST(optionalString2.value() == "Hello World");
56 
57  armnn::Optional<std::string> optionalString3(std::move(optionalString2));
58  BOOST_TEST(static_cast<bool>(optionalString3) == true);
59  BOOST_TEST(optionalString3.has_value() == true);
60  BOOST_TEST(optionalString3.value() == "Hello World");
61 }
62 
63 BOOST_AUTO_TEST_CASE(StringRefTests)
64 {
66  BOOST_TEST(optionalStringRef.has_value() == false);
67 
68  PassStringRef(optionalStringRef);
69  PassStringRefWithDefault();
70 
71  armnn::Optional<std::string&> optionalStringRef2 = optionalStringRef;
72 
73  std::string helloWorld("Hello World");
74 
75  std::string& helloWorldRef = helloWorld;
76  armnn::Optional<std::string&> optionalHelloRef = helloWorldRef;
77  BOOST_TEST(optionalHelloRef.has_value() == true);
78  BOOST_TEST(optionalHelloRef.value() == "Hello World");
79 
80  armnn::Optional<std::string&> optionalHelloRef2 = helloWorld;
81  BOOST_TEST(optionalHelloRef2.has_value() == true);
82  BOOST_TEST(optionalHelloRef2.value() == "Hello World");
83 
84  armnn::Optional<std::string&> optionalHelloRef3{helloWorldRef};
85  BOOST_TEST(optionalHelloRef3.has_value() == true);
86  BOOST_TEST(optionalHelloRef3.value() == "Hello World");
87 
88  armnn::Optional<std::string&> optionalHelloRef4{helloWorld};
89  BOOST_TEST(optionalHelloRef4.has_value() == true);
90  BOOST_TEST(optionalHelloRef4.value() == "Hello World");
91 
92  // modify through the optional reference
93  optionalHelloRef4.value().assign("Long Other String");
94  BOOST_TEST(helloWorld == "Long Other String");
95  BOOST_TEST(optionalHelloRef.value() == "Long Other String");
96  BOOST_TEST(optionalHelloRef2.value() == "Long Other String");
97  BOOST_TEST(optionalHelloRef3.value() == "Long Other String");
98 }
99 
100 BOOST_AUTO_TEST_CASE(SimpleIntTests)
101 {
102  const int intValue = 123;
103 
104  armnn::Optional<int> optionalInt;
105  BOOST_TEST(static_cast<bool>(optionalInt) == false);
106  BOOST_TEST(optionalInt.has_value() == false);
107  BOOST_TEST((optionalInt == armnn::Optional<int>()));
108 
109  optionalInt = intValue;
110  BOOST_TEST(static_cast<bool>(optionalInt) == true);
111  BOOST_TEST(optionalInt.has_value() == true);
112  BOOST_TEST(optionalInt.value() == intValue);
113  BOOST_TEST((optionalInt == armnn::Optional<int>(intValue)));
114 
115  armnn::Optional<int> otherOptionalInt;
116  otherOptionalInt = optionalInt;
117  BOOST_TEST(static_cast<bool>(otherOptionalInt) == true);
118  BOOST_TEST(otherOptionalInt.value() == intValue);
119 }
120 
121 BOOST_AUTO_TEST_CASE(ObjectConstructedInPlaceTests)
122 {
123  struct SimpleObject
124  {
125  public:
126  SimpleObject(const std::string& name, int value)
127  : m_Name(name)
128  , m_Value(value)
129  {}
130 
131  bool operator ==(const SimpleObject& other)
132  {
133  return m_Name == other.m_Name &&
134  m_Value == other.m_Value;
135  }
136 
137  private:
138  std::string m_Name;
139  int m_Value;
140  };
141 
142  std::string objectName("SimpleObject");
143  int objectValue = 1;
144  SimpleObject referenceObject(objectName, objectValue);
145 
146  // Use MakeOptional
147  armnn::Optional<SimpleObject> optionalObject1 = armnn::MakeOptional<SimpleObject>(objectName, objectValue);
148  BOOST_CHECK(static_cast<bool>(optionalObject1) == true);
149  BOOST_CHECK(optionalObject1.has_value() == true);
150  BOOST_CHECK(optionalObject1.value() == referenceObject);
151 
152  // Call in-place constructor directly
153  armnn::Optional<SimpleObject> optionalObject2(CONSTRUCT_IN_PLACE, objectName, objectValue);
154  BOOST_CHECK(static_cast<bool>(optionalObject1) == true);
155  BOOST_CHECK(optionalObject1.has_value() == true);
156  BOOST_CHECK(optionalObject1.value() == referenceObject);
157 }
158 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_AUTO_TEST_CASE(SimpleStringTests)
#define CONSTRUCT_IN_PLACE
Definition: Optional.hpp:41
void IgnoreUnused(Ts &&...)
BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::WaitingForAck)
bool operator==(const armnn::DataLayout &dataLayout, const DataLayoutIndexed &indexed)
Equality methods.
bool has_value() const noexcept
Definition: Optional.hpp:53
BOOST_AUTO_TEST_SUITE_END()
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32