ArmNN
 20.11
UnitTests.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/Logging.hpp>
8 #include <armnn/Utils.hpp>
11 
14 #include "TensorHelpers.hpp"
15 #include <boost/test/unit_test.hpp>
16 
17 inline void ConfigureLoggingTest()
18 {
19  // Configures logging for both the ARMNN library and this test program.
21 }
22 
23 // The following macros require the caller to have defined FactoryType, with one of the following using statements:
24 //
25 // using FactoryType = armnn::RefWorkloadFactory;
26 // using FactoryType = armnn::ClWorkloadFactory;
27 // using FactoryType = armnn::NeonWorkloadFactory;
28 
29 /// Executes BOOST_TEST on CompareTensors() return value so that the predicate_result message is reported.
30 /// If the test reports itself as not supported then the tensors are not compared.
31 /// Additionally this checks that the supportedness reported by the test matches the name of the test.
32 /// Unsupported tests must be 'tagged' by including "UNSUPPORTED" in their name.
33 /// This is useful because it clarifies that the feature being tested is not actually supported
34 /// (a passed test with the name of a feature would imply that feature was supported).
35 /// If support is added for a feature, the test case will fail because the name incorrectly contains UNSUPPORTED.
36 /// If support is removed for a feature, the test case will fail because the name doesn't contain UNSUPPORTED.
37 template <typename T, std::size_t n>
38 void CompareTestResultIfSupported(const std::string& testName, const LayerTestResult<T, n>& testResult)
39 {
40  bool testNameIndicatesUnsupported = testName.find("UNSUPPORTED") != std::string::npos;
41  BOOST_CHECK_MESSAGE(testNameIndicatesUnsupported != testResult.supported,
42  "The test name does not match the supportedness it is reporting");
43  if (testResult.supported)
44  {
45  BOOST_TEST(CompareTensors(testResult.output, testResult.outputExpected, testResult.compareBoolean));
46  }
47 }
48 
49 template <typename T, std::size_t n>
50 void CompareTestResultIfSupported(const std::string& testName, const std::vector<LayerTestResult<T, n>>& testResult)
51 {
52  bool testNameIndicatesUnsupported = testName.find("UNSUPPORTED") != std::string::npos;
53  for (unsigned int i = 0; i < testResult.size(); ++i)
54  {
55  BOOST_CHECK_MESSAGE(testNameIndicatesUnsupported != testResult[i].supported,
56  "The test name does not match the supportedness it is reporting");
57  if (testResult[i].supported)
58  {
59  BOOST_TEST(CompareTensors(testResult[i].output, testResult[i].outputExpected));
60  }
61  }
62 }
63 
64 template<typename FactoryType, typename TFuncPtr, typename... Args>
65 void RunTestFunction(const char* testName, TFuncPtr testFunction, Args... args)
66 {
67  std::unique_ptr<armnn::Profiler> profiler = std::make_unique<armnn::Profiler>();
69 
70  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
71  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
72 
73  auto testResult = (*testFunction)(workloadFactory, memoryManager, args...);
74  CompareTestResultIfSupported(testName, testResult);
75 
77 }
78 
79 
80 template<typename FactoryType, typename TFuncPtr, typename... Args>
81 void RunTestFunctionUsingTensorHandleFactory(const char* testName, TFuncPtr testFunction, Args... args)
82 {
83  std::unique_ptr<armnn::Profiler> profiler = std::make_unique<armnn::Profiler>();
85 
86  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
87  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
88 
89  auto tensorHandleFactory = WorkloadFactoryHelper<FactoryType>::GetTensorHandleFactory(memoryManager);
90 
91  auto testResult = (*testFunction)(workloadFactory, memoryManager, tensorHandleFactory, args...);
92  CompareTestResultIfSupported(testName, testResult);
93 
95 }
96 
97 #define ARMNN_SIMPLE_TEST_CASE(TestName, TestFunction) \
98  BOOST_AUTO_TEST_CASE(TestName) \
99  { \
100  TestFunction(); \
101  }
102 
103 #define ARMNN_AUTO_TEST_CASE(TestName, TestFunction, ...) \
104  BOOST_AUTO_TEST_CASE(TestName) \
105  { \
106  RunTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
107  }
108 
109 #define ARMNN_AUTO_TEST_CASE_WITH_THF(TestName, TestFunction, ...) \
110  BOOST_AUTO_TEST_CASE(TestName) \
111  { \
112  RunTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
113  }
114 
115 template<typename FactoryType, typename TFuncPtr, typename... Args>
116 void CompareRefTestFunction(const char* testName, TFuncPtr testFunction, Args... args)
117 {
118  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
119  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
120 
121  armnn::RefWorkloadFactory refWorkloadFactory;
122 
123  auto testResult = (*testFunction)(workloadFactory, memoryManager, refWorkloadFactory, args...);
124  CompareTestResultIfSupported(testName, testResult);
125 }
126 
127 template<typename FactoryType, typename TFuncPtr, typename... Args>
128 void CompareRefTestFunctionUsingTensorHandleFactory(const char* testName, TFuncPtr testFunction, Args... args)
129 {
130  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
131  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
132 
133  armnn::RefWorkloadFactory refWorkloadFactory;
134  auto tensorHandleFactory = WorkloadFactoryHelper<FactoryType>::GetTensorHandleFactory(memoryManager);
135  auto refTensorHandleFactory =
136  RefWorkloadFactoryHelper::GetTensorHandleFactory(memoryManager);
137 
138  auto testResult = (*testFunction)(
139  workloadFactory, memoryManager, refWorkloadFactory, tensorHandleFactory, refTensorHandleFactory, args...);
140  CompareTestResultIfSupported(testName, testResult);
141 }
142 
143 #define ARMNN_COMPARE_REF_AUTO_TEST_CASE(TestName, TestFunction, ...) \
144  BOOST_AUTO_TEST_CASE(TestName) \
145  { \
146  CompareRefTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
147  }
148 
149 #define ARMNN_COMPARE_REF_AUTO_TEST_CASE_WITH_THF(TestName, TestFunction, ...) \
150  BOOST_AUTO_TEST_CASE(TestName) \
151  { \
152  CompareRefTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
153  }
154 
155 #define ARMNN_COMPARE_REF_FIXTURE_TEST_CASE(TestName, Fixture, TestFunction, ...) \
156  BOOST_FIXTURE_TEST_CASE(TestName, Fixture) \
157  { \
158  CompareRefTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
159  }
160 
161 #define ARMNN_COMPARE_REF_FIXTURE_TEST_CASE_WITH_THF(TestName, Fixture, TestFunction, ...) \
162  BOOST_FIXTURE_TEST_CASE(TestName, Fixture) \
163  { \
164  CompareRefTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
165  }
void RegisterProfiler(Profiler *profiler)
Definition: Profiling.cpp:493
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:486
boost::test_tools::predicate_result CompareTensors(const boost::multi_array< T, n > &a, const boost::multi_array< T, n > &b, bool compareBoolean=false, bool isDynamic=false)
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
Definition: Utils.cpp:10
ISubgraphViewConverter supported
boost::multi_array< T, n > outputExpected
void CompareRefTestFunction(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:116
void RunTestFunctionUsingTensorHandleFactory(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:81
void RunTestFunction(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:65
void CompareRefTestFunctionUsingTensorHandleFactory(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:128
void CompareTestResultIfSupported(const std::string &testName, const LayerTestResult< T, n > &testResult)
Executes BOOST_TEST on CompareTensors() return value so that the predicate_result message is reported...
Definition: UnitTests.hpp:38
void ConfigureLoggingTest()
Definition: UnitTests.hpp:17
boost::multi_array< T, n > output
ClWorkloadFactory FactoryType