ArmNN
 21.02
BackendRegistryTests.cpp File Reference
#include <armnn/Types.hpp>
#include <armnn/BackendRegistry.hpp>
#include <armnn/backends/IBackendInternal.hpp>
#include <reference/RefBackend.hpp>
#include <boost/test/unit_test.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (SwapRegistry)
 
 BOOST_AUTO_TEST_CASE (TestRegistryHelper)
 
 BOOST_AUTO_TEST_CASE (TestDirectCallToRegistry)
 
 BOOST_AUTO_TEST_CASE (ThrowBackendUnavailableException)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( SwapRegistry  )

Definition at line 38 of file BackendRegistryTests.cpp.

References armnn::BackendRegistryInstance(), and BackendRegistry::Size().

39 {
40  using namespace armnn;
41  auto nFactories = BackendRegistryInstance().Size();
42  {
43  SwapRegistryStorage helper;
44  BOOST_TEST(BackendRegistryInstance().Size() == 0);
45  }
46  BOOST_TEST(BackendRegistryInstance().Size() == nFactories);
47 }
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( TestRegistryHelper  )

Definition at line 49 of file BackendRegistryTests.cpp.

References armnn::BackendRegistryInstance(), BackendRegistry::Deregister(), and BackendRegistry::GetFactory().

50 {
51  using namespace armnn;
52  SwapRegistryStorage helper;
53 
54  bool called = false;
55 
58  "HelloWorld",
59  [&called]()
60  {
61  called = true;
62  return armnn::IBackendInternalUniquePtr(nullptr);
63  }
64  );
65 
66  // sanity check: the factory has not been called yet
67  BOOST_TEST(called == false);
68 
69  auto factoryFunction = BackendRegistryInstance().GetFactory("HelloWorld");
70 
71  // sanity check: the factory still not called
72  BOOST_TEST(called == false);
73 
74  factoryFunction();
75  BOOST_TEST(called == true);
76  BackendRegistryInstance().Deregister("HelloWorld");
77 }
FactoryFunction GetFactory(const BackendId &id) const
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
void Deregister(const BackendId &id)
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( TestDirectCallToRegistry  )

Definition at line 79 of file BackendRegistryTests.cpp.

References armnn::BackendRegistryInstance(), BackendRegistry::Deregister(), BackendRegistry::GetFactory(), and BackendRegistry::Register().

80 {
81  using namespace armnn;
82  SwapRegistryStorage helper;
83 
84  bool called = false;
86  "HelloWorld",
87  [&called]()
88  {
89  called = true;
90  return armnn::IBackendInternalUniquePtr(nullptr);
91  }
92  );
93 
94  // sanity check: the factory has not been called yet
95  BOOST_TEST(called == false);
96 
97  auto factoryFunction = BackendRegistryInstance().GetFactory("HelloWorld");
98 
99  // sanity check: the factory still not called
100  BOOST_TEST(called == false);
101 
102  factoryFunction();
103  BOOST_TEST(called == true);
104  BackendRegistryInstance().Deregister("HelloWorld");
105 }
FactoryFunction GetFactory(const BackendId &id) const
void Register(const BackendId &id, FactoryFunction factory)
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
void Deregister(const BackendId &id)
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( ThrowBackendUnavailableException  )

Definition at line 114 of file BackendRegistryTests.cpp.

References ARMNN_LOG, armnn::BackendRegistryInstance(), BOOST_AUTO_TEST_SUITE_END(), BackendRegistry::GetFactory(), armnn::info, BackendRegistry::Register(), and Exception::what().

115 {
116  using namespace armnn;
117 
118  const BackendId mockBackendId("MockDynamicBackend");
119 
120  const std::string exceptionMessage("Neon support not found on device, could not register CpuAcc Backend.\n");
121 
122  // Register the mock backend with a factory function lambda equivalent to NeonRegisterInitializer
123  BackendRegistryInstance().Register(mockBackendId,
124  [exceptionMessage]()
125  {
126  if (false)
127  {
129  }
130  ARMNN_LOG(info) << "Neon support not found on device, could not register CpuAcc Backend.";
131  throw armnn::BackendUnavailableException(exceptionMessage);
132  });
133 
134  // Get the factory function of the mock backend
135  auto factoryFunc = BackendRegistryInstance().GetFactory(mockBackendId);
136 
137  try
138  {
139  // Call the factory function as done during runtime backend registering
140  auto backend = factoryFunc();
141  }
142  catch (const BackendUnavailableException& e)
143  {
144  // Caught
145  BOOST_CHECK_EQUAL(e.what(), exceptionMessage);
146  BOOST_TEST_MESSAGE("ThrowBackendUnavailableExceptionImpl: BackendUnavailableException caught.");
147  }
148 }
FactoryFunction GetFactory(const BackendId &id) const
void Register(const BackendId &id, FactoryFunction factory)
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IBackendInternal > IBackendInternalUniquePtr
Class for non-fatal exceptions raised while initialising a backend.
Definition: Exceptions.hpp:68