ArmNN
 21.08
UtilityTests.cpp File Reference
#include <doctest/doctest.h>
#include <armnn/utility/IgnoreUnused.hpp>
#include <armnn/utility/PolymorphicDowncast.hpp>
#include <armnn/utility/NumericCast.hpp>
#include <armnn/Exceptions.hpp>
#include <limits>

Go to the source code of this file.

Macros

#define ARMNN_POLYMORPHIC_CAST_TESTABLE
 
#define ARMNN_NUMERIC_CAST_TESTABLE
 

Functions

 TEST_SUITE ("UtilityTests")
 

Macro Definition Documentation

◆ ARMNN_NUMERIC_CAST_TESTABLE

#define ARMNN_NUMERIC_CAST_TESTABLE

Definition at line 9 of file UtilityTests.cpp.

◆ ARMNN_POLYMORPHIC_CAST_TESTABLE

#define ARMNN_POLYMORPHIC_CAST_TESTABLE

Definition at line 8 of file UtilityTests.cpp.

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "UtilityTests"  )

Definition at line 20 of file UtilityTests.cpp.

References armnn::IgnoreUnused().

21 {
22 TEST_CASE("PolymorphicDowncast")
23 {
24  using namespace armnn;
25  class Base
26  {
27  public:
28  virtual ~Base(){}
29  float v;
30  };
31 
32  class Child1 : public Base
33  {
34  public:
35  int j;
36  };
37 
38  class Child2 : public Base
39  {
40  public:
41  char b;
42  };
43 
44  Child1 child1;
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);
50 
51  auto ptr2 = dynamic_cast<Child2*>(base1);
52  CHECK(ptr2 == nullptr);
53  CHECK_THROWS_AS(armnn::PolymorphicDowncast<Child2*>(base1), std::bad_cast);
54 
55  armnn::IgnoreUnused(ptr1, ptr2);
56 }
57 
58 
59 TEST_CASE("PolymorphicPointerDowncast_SharedPointer")
60 {
61  using namespace armnn;
62  class Base
63  {
64  public:
65  virtual ~Base(){}
66  float v;
67  };
68 
69  class Child1 : public Base
70  {
71  public:
72  int j;
73  };
74 
75  class Child2 : public Base
76  {
77  public:
78  char b;
79  };
80 
81  std::shared_ptr<Base> base1 = std::make_shared<Child1>();
82 
83  std::shared_ptr<Child1> ptr1 = std::static_pointer_cast<Child1>(base1);
84  CHECK(ptr1);
85  CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
86  CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
87 
88  auto ptr2 = std::dynamic_pointer_cast<Child2>(base1);
89  CHECK(!ptr2);
90  CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
91 
92  armnn::IgnoreUnused(ptr1, ptr2);
93 }
94 
95 
96 TEST_CASE("PolymorphicPointerDowncast_BuildInPointer")
97 {
98  using namespace armnn;
99  class Base
100  {
101  public:
102  virtual ~Base(){}
103  float v;
104  };
105 
106  class Child1 : public Base
107  {
108  public:
109  int j;
110  };
111 
112  class Child2 : public Base
113  {
114  public:
115  char b;
116  };
117 
118  Child1 child1;
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);
124 
125  auto ptr2 = dynamic_cast<Child2*>(base1);
126  CHECK(ptr2 == nullptr);
127  CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
128 
129  armnn::IgnoreUnused(ptr1, ptr2);
130 }
131 
132 
133 TEST_CASE("NumericCast")
134 {
135  using namespace armnn;
136 
137  // To 8 bit
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);
142 
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);
146 
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));
152 
153  // To 16 bit
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);
157 
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);
160 
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));
164 
165  CHECK_NOTHROW(numeric_cast<int16_t>(1U << 8));
166  CHECK_NOTHROW(numeric_cast<int16_t>(1U << 14));
167 
168  // To 32 bit
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));
173 
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));
178 
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));
183 
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();
196 
197  // Float to signed integer
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));
202 
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)));
206 
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);
210 
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);
214 
215  // Signed integer to float
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));
220 
221  CHECK_NOTHROW(numeric_cast<float>(int8_max));
222  CHECK_NOTHROW(numeric_cast<float>(int16_max));
223  CHECK_NOTHROW(numeric_cast<float>(int32_max));
224 
225  CHECK_NOTHROW(numeric_cast<float>(int8_min));
226  CHECK_NOTHROW(numeric_cast<float>(int16_min));
227  CHECK_NOTHROW(numeric_cast<float>(int32_min));
228 
229  // Unsigned integer to float
230  CHECK_NOTHROW(numeric_cast<float>(1U));
231  CHECK(1.0 == numeric_cast<float>(1U));
232 
233  CHECK_NOTHROW(numeric_cast<float>(uint8_max));
234  CHECK_NOTHROW(numeric_cast<float>(uint16_max));
235  CHECK_NOTHROW(numeric_cast<float>(uint32_max));
236 
237  // Float to unsigned integer
238  CHECK_NOTHROW(numeric_cast<uint32_t>(1.43243f));
239  CHECK(1 == numeric_cast<uint32_t>(1.43243f));
240 
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);
243 
244  // Double checks
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));
250 
251  }
252 
253 }
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)