aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/test/UtilityTests.cpp
blob: d5779c1a76a13d0127dea7e736c992027dd420d9 (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
//
// Copyright © 2020 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <boost/test/unit_test.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/polymorphic_cast.hpp>

#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>

// Tests of include/Utility files
BOOST_AUTO_TEST_SUITE(UtilityTests)

BOOST_AUTO_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);
    BOOST_CHECK(ptr1 != nullptr);
    BOOST_CHECK_NO_THROW(armnn::PolymorphicDowncast<Child1*>(base1));
    BOOST_CHECK(armnn::PolymorphicDowncast<Child1*>(base1) == ptr1);

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

    armnn::IgnoreUnused(ptr1, ptr2);
}


BOOST_AUTO_TEST_CASE(NumericCast)
{
    using namespace armnn;

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

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

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

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

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

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

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

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

BOOST_AUTO_TEST_SUITE_END()