aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/test/UtilityTests.cpp
blob: b3b6c3bb3d72e7e8818b00a3ce5b0e3bb65e7e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// Copyright © 2020 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <doctest/doctest.h>

#define ARMNN_POLYMORPHIC_CAST_TESTABLE
#define ARMNN_NUMERIC_CAST_TESTABLE

#include <armnn/utility/IgnoreUnused.hpp>
#include <armnn/utility/PolymorphicDowncast.hpp>
#include <armnn/utility/NumericCast.hpp>

#include <armnn/Exceptions.hpp>

#include <limits>

// Tests of include/Utility files
TEST_SUITE("UtilityTests")
{
TEST_CASE("PolymorphicDowncast")
{
    using namespace armnn;
    class Base
    {
    public:
        virtual ~Base(){}
        float v;
    };

    class Child1 : public Base
    {
    public:
        int j;
    };

    class Child2 : public Base
    {
    public:
        char b;
    };

    Child1 child1;
    Base* base1 = &child1;
    auto ptr1 = dynamic_cast<Child1*>(base1);
    CHECK(ptr1 != nullptr);
    CHECK_NOTHROW(armnn::PolymorphicDowncast<Child1*>(base1));
    CHECK(armnn::PolymorphicDowncast<Child1*>(base1) == ptr1);

    auto ptr2 = dynamic_cast<Child2*>(base1);
    CHECK(ptr2 == nullptr);
    CHECK_THROWS_AS(armnn::PolymorphicDowncast<Child2*>(base1), std::bad_cast);

    armnn::IgnoreUnused(ptr1, ptr2);
}


TEST_CASE("PolymorphicPointerDowncast_SharedPointer")
{
    using namespace armnn;
    class Base
    {
    public:
        virtual ~Base(){}
        float v;
    };

    class Child1 : public Base
    {
    public:
        int j;
    };

    class Child2 : public Base
    {
    public:
        char b;
    };

    std::shared_ptr<Base> base1 = std::make_shared<Child1>();

    std::shared_ptr<Child1> ptr1 = std::static_pointer_cast<Child1>(base1);
    CHECK(ptr1);
    CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
    CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);

    auto ptr2 = std::dynamic_pointer_cast<Child2>(base1);
    CHECK(!ptr2);
    CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);

    armnn::IgnoreUnused(ptr1, ptr2);
}


TEST_CASE("PolymorphicPointerDowncast_BuildInPointer")
{
    using namespace armnn;
    class Base
    {
    public:
        virtual ~Base(){}
        float v;
    };

    class Child1 : public Base
    {
    public:
        int j;
    };

    class Child2 : public Base
    {
    public:
        char b;
    };

    Child1 child1;
    Base* base1 = &child1;
    auto ptr1 = dynamic_cast<Child1*>(base1);
    CHECK(ptr1 != nullptr);
    CHECK_NOTHROW(armnn::PolymorphicPointerDowncast<Child1>(base1));
    CHECK(armnn::PolymorphicPointerDowncast<Child1>(base1) == ptr1);

    auto ptr2 = dynamic_cast<Child2*>(base1);
    CHECK(ptr2 == nullptr);
    CHECK_THROWS_AS(armnn::PolymorphicPointerDowncast<Child2>(base1), std::bad_cast);

    armnn::IgnoreUnused(ptr1, ptr2);
}


TEST_CASE("NumericCast")
{
    using namespace armnn;

    // To 8 bit
    CHECK_THROWS_AS(numeric_cast<unsigned char>(-1), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<unsigned char>(1 << 8), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<unsigned char>(1L << 16), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<unsigned char>(1LL << 32), std::bad_cast);

    CHECK_THROWS_AS(numeric_cast<signed char>((1L << 8)*-1), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<signed char>((1L << 15)*-1), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<signed char>((1LL << 31)*-1), std::bad_cast);

    CHECK_NOTHROW(numeric_cast<unsigned char>(1U));
    CHECK_NOTHROW(numeric_cast<unsigned char>(1L));
    CHECK_NOTHROW(numeric_cast<signed char>(-1));
    CHECK_NOTHROW(numeric_cast<signed char>(-1L));
    CHECK_NOTHROW(numeric_cast<signed char>((1 << 7)*-1));

    // To 16 bit
    CHECK_THROWS_AS(numeric_cast<uint16_t>(-1), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<uint16_t>(1L << 16), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<uint16_t>(1LL << 32), std::bad_cast);

    CHECK_THROWS_AS(numeric_cast<int16_t>(1L << 15), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<int16_t>(1LL << 31), std::bad_cast);

    CHECK_NOTHROW(numeric_cast<uint16_t>(1L << 8));
    CHECK_NOTHROW(numeric_cast<int16_t>(1L << 7));
    CHECK_NOTHROW(numeric_cast<int16_t>((1L << 15)*-1));

    CHECK_NOTHROW(numeric_cast<int16_t>(1U << 8));
    CHECK_NOTHROW(numeric_cast<int16_t>(1U << 14));

    // To 32 bit
    CHECK_NOTHROW(numeric_cast<uint32_t>(1));
    CHECK_NOTHROW(numeric_cast<uint32_t>(1 << 8));
    CHECK_NOTHROW(numeric_cast<uint32_t>(1L << 16));
    CHECK_NOTHROW(numeric_cast<uint32_t>(1LL << 31));

    CHECK_NOTHROW(numeric_cast<int32_t>(-1));
    CHECK_NOTHROW(numeric_cast<int32_t>((1L << 8)*-1));
    CHECK_NOTHROW(numeric_cast<int32_t>((1L << 16)*-1));
    CHECK_NOTHROW(numeric_cast<int32_t>((1LL << 31)*-1));

    CHECK_NOTHROW(numeric_cast<int32_t>(1U));
    CHECK_NOTHROW(numeric_cast<int32_t>(1U << 8));
    CHECK_NOTHROW(numeric_cast<int32_t>(1U << 16));
    CHECK_NOTHROW(numeric_cast<int32_t>(1U << 30));

    float float_max = std::numeric_limits<float>::max();
    float float_min = std::numeric_limits<float>::lowest();
    auto int8_max = std::numeric_limits<int8_t>::max();
    auto int16_max = std::numeric_limits<int16_t>::max();
    auto int32_max = std::numeric_limits<int32_t>::max();
    auto int8_min = std::numeric_limits<int8_t>::lowest();
    auto int16_min = std::numeric_limits<int16_t>::lowest();
    auto int32_min = std::numeric_limits<int32_t>::lowest();
    auto uint8_max = std::numeric_limits<uint8_t>::max();
    auto uint16_max = std::numeric_limits<uint16_t>::max();
    auto uint32_max = std::numeric_limits<uint32_t>::max();
    auto double_max = std::numeric_limits<double>::max();

    // Float to signed integer
    CHECK_NOTHROW(numeric_cast<int32_t>(1.324f));
    CHECK(1 == numeric_cast<int32_t>(1.324f));
    CHECK_NOTHROW(numeric_cast<int32_t>(-1.0f));
    CHECK(-1 == numeric_cast<int32_t>(-1.0f));

    CHECK_NOTHROW(numeric_cast<int8_t>(static_cast<float>(int8_max)));
    CHECK_NOTHROW(numeric_cast<int16_t>(static_cast<float>(int16_max)));
    CHECK_NOTHROW(numeric_cast<int32_t>(static_cast<float>(int32_max)));

    CHECK_THROWS_AS(numeric_cast<int8_t>(float_max), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<int16_t>(float_max), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<int32_t>(float_max), std::bad_cast);

    CHECK_THROWS_AS(numeric_cast<int8_t>(float_min), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<int16_t>(float_min), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<int32_t>(float_min), std::bad_cast);

    // Signed integer to float
    CHECK_NOTHROW(numeric_cast<float>(1));
    CHECK(1.0 == numeric_cast<float>(1));
    CHECK_NOTHROW(numeric_cast<float>(-1));
    CHECK(-1.0 == numeric_cast<float>(-1));

    CHECK_NOTHROW(numeric_cast<float>(int8_max));
    CHECK_NOTHROW(numeric_cast<float>(int16_max));
    CHECK_NOTHROW(numeric_cast<float>(int32_max));

    CHECK_NOTHROW(numeric_cast<float>(int8_min));
    CHECK_NOTHROW(numeric_cast<float>(int16_min));
    CHECK_NOTHROW(numeric_cast<float>(int32_min));

    // Unsigned integer to float
    CHECK_NOTHROW(numeric_cast<float>(1U));
    CHECK(1.0 == numeric_cast<float>(1U));

    CHECK_NOTHROW(numeric_cast<float>(uint8_max));
    CHECK_NOTHROW(numeric_cast<float>(uint16_max));
    CHECK_NOTHROW(numeric_cast<float>(uint32_max));

    // Float to unsigned integer
    CHECK_NOTHROW(numeric_cast<uint32_t>(1.43243f));
    CHECK(1 == numeric_cast<uint32_t>(1.43243f));

    CHECK_THROWS_AS(numeric_cast<uint32_t>(-1.1f), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<uint32_t>(float_max), std::bad_cast);

    // Double checks
    CHECK_THROWS_AS(numeric_cast<int32_t>(double_max), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<int32_t>(double_max), std::bad_cast);
    CHECK_THROWS_AS(numeric_cast<float>(double_max), std::bad_cast);
    CHECK_NOTHROW(numeric_cast<double>(int32_max));
    CHECK_NOTHROW(numeric_cast<long double>(int32_max));

    }

}