• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <armnn/Types.hpp>
7 #include <armnn/BackendRegistry.hpp>
8 
9 #include <armnn/backends/IBackendInternal.hpp>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 namespace
14 {
15 
16 class SwapRegistryStorage : public armnn::BackendRegistry
17 {
18 public:
SwapRegistryStorage()19     SwapRegistryStorage() : armnn::BackendRegistry()
20     {
21         Swap(armnn::BackendRegistryInstance(),  m_TempStorage);
22     }
23 
~SwapRegistryStorage()24     ~SwapRegistryStorage()
25     {
26         Swap(armnn::BackendRegistryInstance(),m_TempStorage);
27     }
28 
29 private:
30     FactoryStorage m_TempStorage;
31 };
32 
33 }
34 
35 BOOST_AUTO_TEST_SUITE(BackendRegistryTests)
36 
BOOST_AUTO_TEST_CASE(SwapRegistry)37 BOOST_AUTO_TEST_CASE(SwapRegistry)
38 {
39     using namespace armnn;
40     auto nFactories = BackendRegistryInstance().Size();
41     {
42         SwapRegistryStorage helper;
43         BOOST_TEST(BackendRegistryInstance().Size() == 0);
44     }
45     BOOST_TEST(BackendRegistryInstance().Size() == nFactories);
46 }
47 
BOOST_AUTO_TEST_CASE(TestRegistryHelper)48 BOOST_AUTO_TEST_CASE(TestRegistryHelper)
49 {
50     using namespace armnn;
51     SwapRegistryStorage helper;
52 
53     bool called = false;
54 
55     BackendRegistry::StaticRegistryInitializer factoryHelper(
56         BackendRegistryInstance(),
57         "HelloWorld",
58         [&called]()
59         {
60             called = true;
61             return armnn::IBackendInternalUniquePtr(nullptr);
62         }
63     );
64 
65     // sanity check: the factory has not been called yet
66     BOOST_TEST(called == false);
67 
68     auto factoryFunction = BackendRegistryInstance().GetFactory("HelloWorld");
69 
70     // sanity check: the factory still not called
71     BOOST_TEST(called == false);
72 
73     factoryFunction();
74     BOOST_TEST(called == true);
75     BackendRegistryInstance().Deregister("HelloWorld");
76 }
77 
BOOST_AUTO_TEST_CASE(TestDirectCallToRegistry)78 BOOST_AUTO_TEST_CASE(TestDirectCallToRegistry)
79 {
80     using namespace armnn;
81     SwapRegistryStorage helper;
82 
83     bool called = false;
84     BackendRegistryInstance().Register(
85         "HelloWorld",
86         [&called]()
87         {
88             called = true;
89             return armnn::IBackendInternalUniquePtr(nullptr);
90         }
91     );
92 
93     // sanity check: the factory has not been called yet
94     BOOST_TEST(called == false);
95 
96     auto factoryFunction = BackendRegistryInstance().GetFactory("HelloWorld");
97 
98     // sanity check: the factory still not called
99     BOOST_TEST(called == false);
100 
101     factoryFunction();
102     BOOST_TEST(called == true);
103     BackendRegistryInstance().Deregister("HelloWorld");
104 }
105 
106 BOOST_AUTO_TEST_SUITE_END()
107