aboutsummaryrefslogtreecommitdiff
path: root/src/backends/backendsCommon/test/BackendRegistryTests.cpp
blob: ba407d29082a69125654691e3d080b54c370c454 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <armnn/Types.hpp>
#include <armnn/BackendRegistry.hpp>

#include <armnn/backends/IBackendInternal.hpp>
#include <reference/RefBackend.hpp>

#include <doctest/doctest.h>

namespace
{

class SwapRegistryStorage : public armnn::BackendRegistry
{
public:
    SwapRegistryStorage() : armnn::BackendRegistry()
    {
        Swap(armnn::BackendRegistryInstance(),  m_TempStorage);
    }

    ~SwapRegistryStorage()
    {
        Swap(armnn::BackendRegistryInstance(),m_TempStorage);
    }

private:
    FactoryStorage m_TempStorage;
};

}

TEST_SUITE("BackendRegistryTests")
{
TEST_CASE("SwapRegistry")
{
    using namespace armnn;
    auto nFactories = BackendRegistryInstance().Size();
    {
        SwapRegistryStorage helper;
        CHECK(BackendRegistryInstance().Size() == 0);
    }
    CHECK(BackendRegistryInstance().Size() == nFactories);
}

TEST_CASE("TestRegistryHelper")
{
    using namespace armnn;
    SwapRegistryStorage helper;

    bool called = false;

    BackendRegistry::StaticRegistryInitializer factoryHelper(
        BackendRegistryInstance(),
        "HelloWorld",
        [&called]()
        {
            called = true;
            return armnn::IBackendInternalUniquePtr(nullptr);
        }
    );

    // sanity check: the factory has not been called yet
    CHECK(called == false);

    auto factoryFunction = BackendRegistryInstance().GetFactory("HelloWorld");

    // sanity check: the factory still not called
    CHECK(called == false);

    factoryFunction();
    CHECK(called == true);
    BackendRegistryInstance().Deregister("HelloWorld");
}

TEST_CASE("TestDirectCallToRegistry")
{
    using namespace armnn;
    SwapRegistryStorage helper;

    bool called = false;
    BackendRegistryInstance().Register(
        "HelloWorld",
        [&called]()
        {
            called = true;
            return armnn::IBackendInternalUniquePtr(nullptr);
        }
    );

    // sanity check: the factory has not been called yet
    CHECK(called == false);

    auto factoryFunction = BackendRegistryInstance().GetFactory("HelloWorld");

    // sanity check: the factory still not called
    CHECK(called == false);

    factoryFunction();
    CHECK(called == true);
    BackendRegistryInstance().Deregister("HelloWorld");
}

// Test that backends can throw exceptions during their factory function to prevent loading in an unsuitable
// environment. For example Neon Backend loading on armhf device without neon support.
// In reality the dynamic backend is loaded in during the LoadDynamicBackends(options.m_DynamicBackendsPath)
// step of runtime constructor, then the factory function is called to check if supported, in case
// of Neon not being detected the exception is raised and so the backend is not added to the supportedBackends
// list

TEST_CASE("ThrowBackendUnavailableException")
{
    using namespace armnn;

    const BackendId mockBackendId("MockDynamicBackend");

    const std::string exceptionMessage("Mock error message to test unavailable backend");

    // Register the mock backend with a factory function lambda that always throws
    BackendRegistryInstance().Register(mockBackendId,
            [exceptionMessage]()
            {
                throw armnn::BackendUnavailableException(exceptionMessage);
                return IBackendInternalUniquePtr(); // Satisfy return type
            });

    // Get the factory function of the mock backend
    auto factoryFunc = BackendRegistryInstance().GetFactory(mockBackendId);

    try
    {
        // Call the factory function as done during runtime backend registering
        auto backend = factoryFunc();
        FAIL("Expected exception to have been thrown");
    }
    catch (const BackendUnavailableException& e)
    {
        // Caught
        CHECK_EQ(e.what(), exceptionMessage);
    }
}

}