ArmNN
 20.08
UtilityTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <boost/test/unit_test.hpp>
7 
8 #define ARMNN_POLYMORPHIC_CAST_TESTABLE
9 #define ARMNN_NUMERIC_CAST_TESTABLE
10 
14 
15 #include <armnn/Exceptions.hpp>
16 
17 // Tests of include/Utility files
18 BOOST_AUTO_TEST_SUITE(UtilityTests)
19 
21 {
22  using namespace armnn;
23  class Base
24  {
25  public:
26  virtual ~Base(){}
27  float v;
28  };
29 
30  class Child1 : public Base
31  {
32  public:
33  int j;
34  };
35 
36  class Child2 : public Base
37  {
38  public:
39  char b;
40  };
41 
42  Child1 child1;
43  Base* base1 = &child1;
44  auto ptr1 = dynamic_cast<Child1*>(base1);
45  BOOST_CHECK(ptr1 != nullptr);
46  BOOST_CHECK_NO_THROW(armnn::PolymorphicDowncast<Child1*>(base1));
47  BOOST_CHECK(armnn::PolymorphicDowncast<Child1*>(base1) == ptr1);
48 
49  auto ptr2 = dynamic_cast<Child2*>(base1);
50  BOOST_CHECK(ptr2 == nullptr);
51  BOOST_CHECK_THROW(armnn::PolymorphicDowncast<Child2*>(base1), std::bad_cast);
52 
53  armnn::IgnoreUnused(ptr1, ptr2);
54 }
55 
56 
57 BOOST_AUTO_TEST_CASE(PolymorphicPointerDowncast_SharedPointer)
58 {
59  using namespace armnn;
60  class Base
61  {
62  public:
63  virtual ~Base(){}
64  float v;
65  };
66 
67  class Child1 : public Base
68  {
69  public:
70  int j;
71  };
72 
73  class Child2 : public Base
74  {
75  public:
76  char b;
77  };
78 
79  std::shared_ptr<Base> base1 = std::make_shared<Child1>();
80 
81  std::shared_ptr<Child1> ptr1 = std::static_pointer_cast<Child1>(base1);
82  BOOST_CHECK(ptr1);
83  BOOST_CHECK_NO_THROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
84  BOOST_CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
85 
86  auto ptr2 = std::dynamic_pointer_cast<Child2>(base1);
87  BOOST_CHECK(!ptr2);
88  BOOST_CHECK_THROW(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
89 
90  armnn::IgnoreUnused(ptr1, ptr2);
91 }
92 
93 
94 BOOST_AUTO_TEST_CASE(PolymorphicPointerDowncast_BuildInPointer)
95 {
96  using namespace armnn;
97  class Base
98  {
99  public:
100  virtual ~Base(){}
101  float v;
102  };
103 
104  class Child1 : public Base
105  {
106  public:
107  int j;
108  };
109 
110  class Child2 : public Base
111  {
112  public:
113  char b;
114  };
115 
116  Child1 child1;
117  Base* base1 = &child1;
118  auto ptr1 = dynamic_cast<Child1*>(base1);
119  BOOST_CHECK(ptr1 != nullptr);
120  BOOST_CHECK_NO_THROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
121  BOOST_CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);
122 
123  auto ptr2 = dynamic_cast<Child2*>(base1);
124  BOOST_CHECK(ptr2 == nullptr);
125  BOOST_CHECK_THROW(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);
126 
127  armnn::IgnoreUnused(ptr1, ptr2);
128 }
129 
130 
132 {
133  using namespace armnn;
134 
135  // To 8 bit
136  BOOST_CHECK_THROW(numeric_cast<unsigned char>(-1), std::bad_cast);
137  BOOST_CHECK_THROW(numeric_cast<unsigned char>(1 << 8), std::bad_cast);
138  BOOST_CHECK_THROW(numeric_cast<unsigned char>(1L << 16), std::bad_cast);
139  BOOST_CHECK_THROW(numeric_cast<unsigned char>(1LL << 32), std::bad_cast);
140 
141  BOOST_CHECK_THROW(numeric_cast<signed char>((1L << 8)*-1), std::bad_cast);
142  BOOST_CHECK_THROW(numeric_cast<signed char>((1L << 15)*-1), std::bad_cast);
143  BOOST_CHECK_THROW(numeric_cast<signed char>((1LL << 31)*-1), std::bad_cast);
144 
145  BOOST_CHECK_NO_THROW(numeric_cast<unsigned char>(1U));
146  BOOST_CHECK_NO_THROW(numeric_cast<unsigned char>(1L));
147  BOOST_CHECK_NO_THROW(numeric_cast<signed char>(-1));
148  BOOST_CHECK_NO_THROW(numeric_cast<signed char>(-1L));
149  BOOST_CHECK_NO_THROW(numeric_cast<signed char>((1 << 7)*-1));
150 
151  // To 16 bit
152  BOOST_CHECK_THROW(numeric_cast<uint16_t>(-1), std::bad_cast);
153  BOOST_CHECK_THROW(numeric_cast<uint16_t>(1L << 16), std::bad_cast);
154  BOOST_CHECK_THROW(numeric_cast<uint16_t>(1LL << 32), std::bad_cast);
155 
156  BOOST_CHECK_THROW(numeric_cast<int16_t>(1L << 15), std::bad_cast);
157  BOOST_CHECK_THROW(numeric_cast<int16_t>(1LL << 31), std::bad_cast);
158 
159  BOOST_CHECK_NO_THROW(numeric_cast<uint16_t>(1L << 8));
160  BOOST_CHECK_NO_THROW(numeric_cast<int16_t>(1L << 7));
161  BOOST_CHECK_NO_THROW(numeric_cast<int16_t>((1L << 15)*-1));
162 
163  // To 32 bit
164  BOOST_CHECK_NO_THROW(numeric_cast<uint32_t>(1));
165  BOOST_CHECK_NO_THROW(numeric_cast<uint32_t>(1 << 8));
166  BOOST_CHECK_NO_THROW(numeric_cast<uint32_t>(1L << 16));
167  BOOST_CHECK_NO_THROW(numeric_cast<uint32_t>(1LL << 31));
168 
169  BOOST_CHECK_NO_THROW(numeric_cast<int32_t>(-1));
170  BOOST_CHECK_NO_THROW(numeric_cast<int32_t>((1L << 8)*-1));
171  BOOST_CHECK_NO_THROW(numeric_cast<int32_t>((1L << 16)*-1));
172  BOOST_CHECK_NO_THROW(numeric_cast<int32_t>((1LL << 31)*-1));
173 }
174 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
BOOST_AUTO_TEST_CASE(PolymorphicDowncast)
BOOST_AUTO_TEST_CASE(CheckConvolution2dLayer)
BOOST_AUTO_TEST_SUITE_END()
DestType PolymorphicDowncast(SourceType value)
Polymorphic downcast for build in pointers only.