ArmNN
 21.08
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 
15 #include "TensorHelpers.hpp"
16 
17 #include <doctest/doctest.h>
18 
19 inline void ConfigureLoggingTest()
20 {
21  // Configures logging for both the ARMNN library and this test program.
23 }
24 
25 // The following macros require the caller to have defined FactoryType, with one of the following using statements:
26 //
27 // using FactoryType = armnn::RefWorkloadFactory;
28 // using FactoryType = armnn::ClWorkloadFactory;
29 // using FactoryType = armnn::NeonWorkloadFactory;
30 
31 /// Executes CHECK_MESSAGE on CompareTensors() return value so that the predicate_result message is reported.
32 /// If the test reports itself as not supported then the tensors are not compared.
33 /// Additionally this checks that the supportedness reported by the test matches the name of the test.
34 /// Unsupported tests must be 'tagged' by including "UNSUPPORTED" in their name.
35 /// This is useful because it clarifies that the feature being tested is not actually supported
36 /// (a passed test with the name of a feature would imply that feature was supported).
37 /// If support is added for a feature, the test case will fail because the name incorrectly contains UNSUPPORTED.
38 /// If support is removed for a feature, the test case will fail because the name doesn't contain UNSUPPORTED.
39 template <typename T, std::size_t n>
40 void CompareTestResultIfSupported(const std::string& testName, const LayerTestResult<T, n>& testResult)
41 {
42  bool testNameIndicatesUnsupported = testName.find("UNSUPPORTED") != std::string::npos;
43  CHECK_MESSAGE(testNameIndicatesUnsupported != testResult.m_Supported,
44  "The test name does not match the supportedness it is reporting");
45  if (testResult.m_Supported)
46  {
47  auto result = CompareTensors(testResult.m_ActualData,
48  testResult.m_ExpectedData,
49  testResult.m_ActualShape,
50  testResult.m_ExpectedShape,
51  testResult.m_CompareBoolean);
52  CHECK_MESSAGE(result.m_Result, result.m_Message.str());
53  }
54 }
55 
56 template <typename T, std::size_t n>
57 void CompareTestResultIfSupported(const std::string& testName, const std::vector<LayerTestResult<T, n>>& testResult)
58 {
59  bool testNameIndicatesUnsupported = testName.find("UNSUPPORTED") != std::string::npos;
60  for (unsigned int i = 0; i < testResult.size(); ++i)
61  {
62  CHECK_MESSAGE(testNameIndicatesUnsupported != testResult[i].m_Supported,
63  "The test name does not match the supportedness it is reporting");
64  if (testResult[i].m_Supported)
65  {
66  auto result = CompareTensors(testResult[i].m_ActualData,
67  testResult[i].m_ExpectedData,
68  testResult[i].m_ActualShape,
69  testResult[i].m_ExpectedShape);
70  CHECK_MESSAGE(result.m_Result, result.m_Message.str());
71  }
72  }
73 }
74 
75 template<typename FactoryType, typename TFuncPtr, typename... Args>
76 void RunTestFunction(const char* testName, TFuncPtr testFunction, Args... args)
77 {
78  std::unique_ptr<armnn::IProfiler> profiler = std::make_unique<armnn::IProfiler>();
80 
81  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
82  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
83 
84  auto testResult = (*testFunction)(workloadFactory, memoryManager, args...);
85  CompareTestResultIfSupported(testName, testResult);
86 
88 }
89 
90 
91 template<typename FactoryType, typename TFuncPtr, typename... Args>
92 void RunTestFunctionUsingTensorHandleFactory(const char* testName, TFuncPtr testFunction, Args... args)
93 {
94  std::unique_ptr<armnn::IProfiler> profiler = std::make_unique<armnn::IProfiler>();
96 
97  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
98  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
99 
100  auto tensorHandleFactory = WorkloadFactoryHelper<FactoryType>::GetTensorHandleFactory(memoryManager);
101 
102  auto testResult = (*testFunction)(workloadFactory, memoryManager, tensorHandleFactory, args...);
103  CompareTestResultIfSupported(testName, testResult);
104 
106 }
107 
108 #define ARMNN_SIMPLE_TEST_CASE(TestName, TestFunction) \
109  TEST_CASE(#TestName) \
110  { \
111  TestFunction(); \
112  }
113 
114 #define ARMNN_AUTO_TEST_CASE(TestName, TestFunction, ...) \
115  TEST_CASE(#TestName) \
116  { \
117  RunTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
118  }
119 
120 #define ARMNN_AUTO_TEST_FIXTURE(TestName, Fixture, TestFunction, ...) \
121  TEST_CASE_FIXTURE(Fixture, #TestName) \
122  { \
123  RunTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
124  }
125 
126 #define ARMNN_AUTO_TEST_CASE_WITH_THF(TestName, TestFunction, ...) \
127  TEST_CASE(#TestName) \
128  { \
129  RunTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
130  }
131 
132 #define ARMNN_AUTO_TEST_FIXTURE_WITH_THF(TestName, Fixture, TestFunction, ...) \
133  TEST_CASE_FIXTURE(Fixture, #TestName) \
134  { \
135  RunTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
136  }
137 
138 template<typename FactoryType, typename TFuncPtr, typename... Args>
139 void CompareRefTestFunction(const char* testName, TFuncPtr testFunction, Args... args)
140 {
141  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
142  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
143 
144  armnn::RefWorkloadFactory refWorkloadFactory;
145 
146  auto testResult = (*testFunction)(workloadFactory, memoryManager, refWorkloadFactory, args...);
147  CompareTestResultIfSupported(testName, testResult);
148 }
149 
150 template<typename FactoryType, typename TFuncPtr, typename... Args>
151 void CompareRefTestFunctionUsingTensorHandleFactory(const char* testName, TFuncPtr testFunction, Args... args)
152 {
153  auto memoryManager = WorkloadFactoryHelper<FactoryType>::GetMemoryManager();
154  FactoryType workloadFactory = WorkloadFactoryHelper<FactoryType>::GetFactory(memoryManager);
155 
156  armnn::RefWorkloadFactory refWorkloadFactory;
157  auto tensorHandleFactory = WorkloadFactoryHelper<FactoryType>::GetTensorHandleFactory(memoryManager);
158  auto refTensorHandleFactory =
159  RefWorkloadFactoryHelper::GetTensorHandleFactory(memoryManager);
160 
161  auto testResult = (*testFunction)(
162  workloadFactory, memoryManager, refWorkloadFactory, tensorHandleFactory, refTensorHandleFactory, args...);
163  CompareTestResultIfSupported(testName, testResult);
164 }
165 
166 #define ARMNN_COMPARE_REF_AUTO_TEST_CASE(TestName, TestFunction, ...) \
167  TEST_CASE(#TestName) \
168  { \
169  CompareRefTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
170  }
171 
172 #define ARMNN_COMPARE_REF_AUTO_TEST_CASE_WITH_THF(TestName, TestFunction, ...) \
173  TEST_CASE(#TestName) \
174  { \
175  CompareRefTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
176  }
177 
178 #define ARMNN_COMPARE_REF_FIXTURE_TEST_CASE(TestName, Fixture, TestFunction, ...) \
179  TEST_CASE_FIXTURE(Fixture, #TestName) \
180  { \
181  CompareRefTestFunction<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
182  }
183 
184 #define ARMNN_COMPARE_REF_FIXTURE_TEST_CASE_WITH_THF(TestName, Fixture, TestFunction, ...) \
185  TEST_CASE_FIXTURE(Fixture, #TestName) \
186  { \
187  CompareRefTestFunctionUsingTensorHandleFactory<FactoryType>(#TestName, &TestFunction, ##__VA_ARGS__); \
188  }
armnn::TensorShape m_ExpectedShape
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:526
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
Definition: Utils.cpp:18
void CompareRefTestFunction(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:139
armnn::PredicateResult CompareTensors(const std::vector< T > &actualData, const std::vector< T > &expectedData, const armnn::TensorShape &actualShape, const armnn::TensorShape &expectedShape, bool compareBoolean=false, bool isDynamic=false)
void RunTestFunctionUsingTensorHandleFactory(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:92
void RunTestFunction(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:76
void CompareRefTestFunctionUsingTensorHandleFactory(const char *testName, TFuncPtr testFunction, Args... args)
Definition: UnitTests.hpp:151
void CompareTestResultIfSupported(const std::string &testName, const LayerTestResult< T, n > &testResult)
Executes CHECK_MESSAGE on CompareTensors() return value so that the predicate_result message is repor...
Definition: UnitTests.hpp:40
std::vector< T > m_ExpectedData
void ConfigureLoggingTest()
Definition: UnitTests.hpp:19
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:533
std::vector< T > m_ActualData
armnn::TensorShape m_ActualShape