6 #include <doctest/doctest.h> 8 #define ARMNN_POLYMORPHIC_CAST_TESTABLE 9 #define ARMNN_NUMERIC_CAST_TESTABLE 22 TEST_CASE(
"PolymorphicDowncast")
24 using namespace armnn;
32 class Child1 :
public Base
38 class Child2 :
public Base
45 Base* base1 = &child1;
46 auto ptr1 =
dynamic_cast<Child1*
>(base1);
47 CHECK(ptr1 !=
nullptr);
48 CHECK_NOTHROW(armnn::PolymorphicDowncast<Child1*>(base1));
49 CHECK(armnn::PolymorphicDowncast<Child1*>(base1) == ptr1);
51 auto ptr2 =
dynamic_cast<Child2*
>(base1);
52 CHECK(ptr2 ==
nullptr);
53 CHECK_THROWS_AS(armnn::PolymorphicDowncast<Child2*>(base1), std::bad_cast);
59 TEST_CASE(
"PolymorphicPointerDowncast_SharedPointer")
61 using namespace armnn;
69 class Child1 :
public Base
75 class Child2 :
public Base
81 std::shared_ptr<Base> base1 = std::make_shared<Child1>();
83 std::shared_ptr<Child1> ptr1 = std::static_pointer_cast<Child1>(base1);
85 CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
86 CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
88 auto ptr2 = std::dynamic_pointer_cast<Child2>(base1);
90 CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
96 TEST_CASE(
"PolymorphicPointerDowncast_BuildInPointer")
98 using namespace armnn;
106 class Child1 :
public Base
112 class Child2 :
public Base
119 Base* base1 = &child1;
120 auto ptr1 =
dynamic_cast<Child1*
>(base1);
121 CHECK(ptr1 !=
nullptr);
122 CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
123 CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
125 auto ptr2 =
dynamic_cast<Child2*
>(base1);
126 CHECK(ptr2 ==
nullptr);
127 CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
133 TEST_CASE(
"NumericCast")
135 using namespace armnn;
138 CHECK_THROWS_AS(numeric_cast<unsigned char>(-1), std::bad_cast);
139 CHECK_THROWS_AS(numeric_cast<unsigned char>(1 << 8), std::bad_cast);
140 CHECK_THROWS_AS(numeric_cast<unsigned char>(1L << 16), std::bad_cast);
141 CHECK_THROWS_AS(numeric_cast<unsigned char>(1LL << 32), std::bad_cast);
143 CHECK_THROWS_AS(numeric_cast<signed char>((1L << 8)*-1), std::bad_cast);
144 CHECK_THROWS_AS(numeric_cast<signed char>((1L << 15)*-1), std::bad_cast);
145 CHECK_THROWS_AS(numeric_cast<signed char>((1LL << 31)*-1), std::bad_cast);
147 CHECK_NOTHROW(numeric_cast<unsigned char>(1U));
148 CHECK_NOTHROW(numeric_cast<unsigned char>(1L));
149 CHECK_NOTHROW(numeric_cast<signed char>(-1));
150 CHECK_NOTHROW(numeric_cast<signed char>(-1L));
151 CHECK_NOTHROW(numeric_cast<signed char>((1 << 7)*-1));
154 CHECK_THROWS_AS(numeric_cast<uint16_t>(-1), std::bad_cast);
155 CHECK_THROWS_AS(numeric_cast<uint16_t>(1L << 16), std::bad_cast);
156 CHECK_THROWS_AS(numeric_cast<uint16_t>(1LL << 32), std::bad_cast);
158 CHECK_THROWS_AS(numeric_cast<int16_t>(1L << 15), std::bad_cast);
159 CHECK_THROWS_AS(numeric_cast<int16_t>(1LL << 31), std::bad_cast);
161 CHECK_NOTHROW(numeric_cast<uint16_t>(1L << 8));
162 CHECK_NOTHROW(numeric_cast<int16_t>(1L << 7));
163 CHECK_NOTHROW(numeric_cast<int16_t>((1L << 15)*-1));
165 CHECK_NOTHROW(numeric_cast<int16_t>(1U << 8));
166 CHECK_NOTHROW(numeric_cast<int16_t>(1U << 14));
169 CHECK_NOTHROW(numeric_cast<uint32_t>(1));
170 CHECK_NOTHROW(numeric_cast<uint32_t>(1 << 8));
171 CHECK_NOTHROW(numeric_cast<uint32_t>(1L << 16));
172 CHECK_NOTHROW(numeric_cast<uint32_t>(1LL << 31));
174 CHECK_NOTHROW(numeric_cast<int32_t>(-1));
175 CHECK_NOTHROW(numeric_cast<int32_t>((1L << 8)*-1));
176 CHECK_NOTHROW(numeric_cast<int32_t>((1L << 16)*-1));
177 CHECK_NOTHROW(numeric_cast<int32_t>((1LL << 31)*-1));
179 CHECK_NOTHROW(numeric_cast<int32_t>(1U));
180 CHECK_NOTHROW(numeric_cast<int32_t>(1U << 8));
181 CHECK_NOTHROW(numeric_cast<int32_t>(1U << 16));
182 CHECK_NOTHROW(numeric_cast<int32_t>(1U << 30));
184 float float_max = std::numeric_limits<float>::max();
185 float float_min = std::numeric_limits<float>::lowest();
186 auto int8_max = std::numeric_limits<int8_t>::max();
187 auto int16_max = std::numeric_limits<int16_t>::max();
188 auto int32_max = std::numeric_limits<int32_t>::max();
189 auto int8_min = std::numeric_limits<int8_t>::lowest();
190 auto int16_min = std::numeric_limits<int16_t>::lowest();
191 auto int32_min = std::numeric_limits<int32_t>::lowest();
192 auto uint8_max = std::numeric_limits<uint8_t>::max();
193 auto uint16_max = std::numeric_limits<uint16_t>::max();
194 auto uint32_max = std::numeric_limits<uint32_t>::max();
195 auto double_max = std::numeric_limits<double>::max();
198 CHECK_NOTHROW(numeric_cast<int32_t>(1.324f));
199 CHECK(1 == numeric_cast<int32_t>(1.324f));
200 CHECK_NOTHROW(numeric_cast<int32_t>(-1.0f));
201 CHECK(-1 == numeric_cast<int32_t>(-1.0f));
203 CHECK_NOTHROW(numeric_cast<int8_t>(static_cast<float>(int8_max)));
204 CHECK_NOTHROW(numeric_cast<int16_t>(static_cast<float>(int16_max)));
205 CHECK_NOTHROW(numeric_cast<int32_t>(static_cast<double>(int32_max)));
207 CHECK_THROWS_AS(numeric_cast<int8_t>(float_max), std::bad_cast);
208 CHECK_THROWS_AS(numeric_cast<int16_t>(float_max), std::bad_cast);
209 CHECK_THROWS_AS(numeric_cast<int32_t>(float_max), std::bad_cast);
211 CHECK_THROWS_AS(numeric_cast<int8_t>(float_min), std::bad_cast);
212 CHECK_THROWS_AS(numeric_cast<int16_t>(float_min), std::bad_cast);
213 CHECK_THROWS_AS(numeric_cast<int32_t>(float_min), std::bad_cast);
216 CHECK_NOTHROW(numeric_cast<float>(1));
217 CHECK(1.0 == numeric_cast<float>(1));
218 CHECK_NOTHROW(numeric_cast<float>(-1));
219 CHECK(-1.0 == numeric_cast<float>(-1));
221 CHECK_NOTHROW(numeric_cast<float>(int8_max));
222 CHECK_NOTHROW(numeric_cast<float>(int16_max));
223 CHECK_NOTHROW(numeric_cast<float>(int32_max));
225 CHECK_NOTHROW(numeric_cast<float>(int8_min));
226 CHECK_NOTHROW(numeric_cast<float>(int16_min));
227 CHECK_NOTHROW(numeric_cast<float>(int32_min));
230 CHECK_NOTHROW(numeric_cast<float>(1U));
231 CHECK(1.0 == numeric_cast<float>(1U));
233 CHECK_NOTHROW(numeric_cast<float>(uint8_max));
234 CHECK_NOTHROW(numeric_cast<float>(uint16_max));
235 CHECK_NOTHROW(numeric_cast<float>(uint32_max));
238 CHECK_NOTHROW(numeric_cast<uint32_t>(1.43243f));
239 CHECK(1 == numeric_cast<uint32_t>(1.43243f));
241 CHECK_THROWS_AS(numeric_cast<uint32_t>(-1.1f), std::bad_cast);
242 CHECK_THROWS_AS(numeric_cast<uint32_t>(float_max), std::bad_cast);
245 CHECK_THROWS_AS(numeric_cast<int32_t>(double_max), std::bad_cast);
246 CHECK_THROWS_AS(numeric_cast<int32_t>(double_max), std::bad_cast);
247 CHECK_THROWS_AS(numeric_cast<float>(double_max), std::bad_cast);
248 CHECK_NOTHROW(numeric_cast<double>(int32_max));
249 CHECK_NOTHROW(numeric_cast<long double>(int32_max));
TEST_SUITE("UtilityTests")
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)