7 #define ARMNN_POLYMORPHIC_CAST_TESTABLE 8 #define ARMNN_NUMERIC_CAST_TESTABLE 16 #include <doctest/doctest.h> 23 TEST_CASE(
"PolymorphicDowncast")
25 using namespace armnn;
33 class Child1 :
public Base
39 class Child2 :
public Base
46 Base* base1 = &child1;
47 auto ptr1 =
dynamic_cast<Child1*
>(base1);
48 CHECK(ptr1 !=
nullptr);
49 CHECK_NOTHROW(armnn::PolymorphicDowncast<Child1*>(base1));
50 CHECK(armnn::PolymorphicDowncast<Child1*>(base1) == ptr1);
52 auto ptr2 =
dynamic_cast<Child2*
>(base1);
53 CHECK(ptr2 ==
nullptr);
54 CHECK_THROWS_AS(armnn::PolymorphicDowncast<Child2*>(base1), std::bad_cast);
60 TEST_CASE(
"PolymorphicPointerDowncast_SharedPointer")
62 using namespace armnn;
70 class Child1 :
public Base
76 class Child2 :
public Base
82 std::shared_ptr<Base> base1 = std::make_shared<Child1>();
84 std::shared_ptr<Child1> ptr1 = std::static_pointer_cast<Child1>(base1);
86 CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
87 CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
89 auto ptr2 = std::dynamic_pointer_cast<Child2>(base1);
91 CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
97 TEST_CASE(
"PolymorphicPointerDowncast_BuildInPointer")
99 using namespace armnn;
107 class Child1 :
public Base
113 class Child2 :
public Base
120 Base* base1 = &child1;
121 auto ptr1 =
dynamic_cast<Child1*
>(base1);
122 CHECK(ptr1 !=
nullptr);
123 CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
124 CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
126 auto ptr2 =
dynamic_cast<Child2*
>(base1);
127 CHECK(ptr2 ==
nullptr);
128 CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
134 TEST_CASE(
"NumericCast")
136 using namespace armnn;
139 CHECK_THROWS_AS(numeric_cast<unsigned char>(-1), std::bad_cast);
140 CHECK_THROWS_AS(numeric_cast<unsigned char>(1 << 8), std::bad_cast);
141 CHECK_THROWS_AS(numeric_cast<unsigned char>(1L << 16), std::bad_cast);
142 CHECK_THROWS_AS(numeric_cast<unsigned char>(1LL << 32), std::bad_cast);
144 CHECK_THROWS_AS(numeric_cast<signed char>((1L << 8)*-1), std::bad_cast);
145 CHECK_THROWS_AS(numeric_cast<signed char>((1L << 15)*-1), std::bad_cast);
146 CHECK_THROWS_AS(numeric_cast<signed char>((1LL << 31)*-1), std::bad_cast);
148 CHECK_NOTHROW(numeric_cast<unsigned char>(1U));
149 CHECK_NOTHROW(numeric_cast<unsigned char>(1L));
150 CHECK_NOTHROW(numeric_cast<signed char>(-1));
151 CHECK_NOTHROW(numeric_cast<signed char>(-1L));
152 CHECK_NOTHROW(numeric_cast<signed char>((1 << 7)*-1));
155 CHECK_THROWS_AS(numeric_cast<uint16_t>(-1), std::bad_cast);
156 CHECK_THROWS_AS(numeric_cast<uint16_t>(1L << 16), std::bad_cast);
157 CHECK_THROWS_AS(numeric_cast<uint16_t>(1LL << 32), std::bad_cast);
159 CHECK_THROWS_AS(numeric_cast<int16_t>(1L << 15), std::bad_cast);
160 CHECK_THROWS_AS(numeric_cast<int16_t>(1LL << 31), std::bad_cast);
162 CHECK_NOTHROW(numeric_cast<uint16_t>(1L << 8));
163 CHECK_NOTHROW(numeric_cast<int16_t>(1L << 7));
164 CHECK_NOTHROW(numeric_cast<int16_t>((1L << 15)*-1));
166 CHECK_NOTHROW(numeric_cast<int16_t>(1U << 8));
167 CHECK_NOTHROW(numeric_cast<int16_t>(1U << 14));
170 CHECK_NOTHROW(numeric_cast<uint32_t>(1));
171 CHECK_NOTHROW(numeric_cast<uint32_t>(1 << 8));
172 CHECK_NOTHROW(numeric_cast<uint32_t>(1L << 16));
173 CHECK_NOTHROW(numeric_cast<uint32_t>(1LL << 31));
175 CHECK_NOTHROW(numeric_cast<int32_t>(-1));
176 CHECK_NOTHROW(numeric_cast<int32_t>((1L << 8)*-1));
177 CHECK_NOTHROW(numeric_cast<int32_t>((1L << 16)*-1));
178 CHECK_NOTHROW(numeric_cast<int32_t>((1LL << 31)*-1));
180 CHECK_NOTHROW(numeric_cast<int32_t>(1U));
181 CHECK_NOTHROW(numeric_cast<int32_t>(1U << 8));
182 CHECK_NOTHROW(numeric_cast<int32_t>(1U << 16));
183 CHECK_NOTHROW(numeric_cast<int32_t>(1U << 30));
185 float float_max = std::numeric_limits<float>::max();
186 float float_min = std::numeric_limits<float>::lowest();
187 auto int8_max = std::numeric_limits<int8_t>::max();
188 auto int16_max = std::numeric_limits<int16_t>::max();
189 auto int32_max = std::numeric_limits<int32_t>::max();
190 auto int8_min = std::numeric_limits<int8_t>::lowest();
191 auto int16_min = std::numeric_limits<int16_t>::lowest();
192 auto int32_min = std::numeric_limits<int32_t>::lowest();
193 auto uint8_max = std::numeric_limits<uint8_t>::max();
194 auto uint16_max = std::numeric_limits<uint16_t>::max();
195 auto uint32_max = std::numeric_limits<uint32_t>::max();
196 auto double_max = std::numeric_limits<double>::max();
199 CHECK_NOTHROW(numeric_cast<int32_t>(1.324f));
200 CHECK(1 == numeric_cast<int32_t>(1.324f));
201 CHECK_NOTHROW(numeric_cast<int32_t>(-1.0f));
202 CHECK(-1 == numeric_cast<int32_t>(-1.0f));
204 CHECK_NOTHROW(numeric_cast<int8_t>(static_cast<float>(int8_max)));
205 CHECK_NOTHROW(numeric_cast<int16_t>(static_cast<float>(int16_max)));
206 CHECK_NOTHROW(numeric_cast<int32_t>(static_cast<double>(int32_max)));
208 CHECK_THROWS_AS(numeric_cast<int8_t>(float_max), std::bad_cast);
209 CHECK_THROWS_AS(numeric_cast<int16_t>(float_max), std::bad_cast);
210 CHECK_THROWS_AS(numeric_cast<int32_t>(float_max), std::bad_cast);
212 CHECK_THROWS_AS(numeric_cast<int8_t>(float_min), std::bad_cast);
213 CHECK_THROWS_AS(numeric_cast<int16_t>(float_min), std::bad_cast);
214 CHECK_THROWS_AS(numeric_cast<int32_t>(float_min), std::bad_cast);
217 CHECK_NOTHROW(numeric_cast<float>(1));
218 CHECK(1.0 == numeric_cast<float>(1));
219 CHECK_NOTHROW(numeric_cast<float>(-1));
220 CHECK(-1.0 == numeric_cast<float>(-1));
222 CHECK_NOTHROW(numeric_cast<float>(int8_max));
223 CHECK_NOTHROW(numeric_cast<float>(int16_max));
224 CHECK_NOTHROW(numeric_cast<float>(int32_max));
226 CHECK_NOTHROW(numeric_cast<float>(int8_min));
227 CHECK_NOTHROW(numeric_cast<float>(int16_min));
228 CHECK_NOTHROW(numeric_cast<float>(int32_min));
231 CHECK_NOTHROW(numeric_cast<float>(1U));
232 CHECK(1.0 == numeric_cast<float>(1U));
234 CHECK_NOTHROW(numeric_cast<float>(uint8_max));
235 CHECK_NOTHROW(numeric_cast<float>(uint16_max));
236 CHECK_NOTHROW(numeric_cast<float>(uint32_max));
239 CHECK_NOTHROW(numeric_cast<uint32_t>(1.43243f));
240 CHECK(1 == numeric_cast<uint32_t>(1.43243f));
242 CHECK_THROWS_AS(numeric_cast<uint32_t>(-1.1f), std::bad_cast);
243 CHECK_THROWS_AS(numeric_cast<uint32_t>(float_max), std::bad_cast);
246 CHECK_THROWS_AS(numeric_cast<int32_t>(double_max), std::bad_cast);
247 CHECK_THROWS_AS(numeric_cast<int32_t>(double_max), std::bad_cast);
248 CHECK_THROWS_AS(numeric_cast<float>(double_max), std::bad_cast);
249 CHECK_NOTHROW(numeric_cast<double>(int32_max));
250 CHECK_NOTHROW(numeric_cast<long double>(int32_max));
254 TEST_CASE(
"StringToBool")
bool StringToBool(const std::string &s, bool throw_on_error=true)
Converts a string to bool.
TEST_SUITE("UtilityTests")
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)