blob: fbc95badd6be9b173a661e0d6f46342f853bb677 [file] [log] [blame]
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_TEST_FRAMEWORK
25#define ARM_COMPUTE_TEST_FRAMEWORK
26
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010027#include "Profiler.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010028#include "TestCase.h"
29#include "TestCaseFactory.h"
30#include "TestResult.h"
31#include "Utils.h"
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010032#include "instruments/Instruments.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010033
34#include <algorithm>
35#include <chrono>
36#include <map>
37#include <memory>
38#include <numeric>
39#include <ostream>
40#include <regex>
41#include <set>
42#include <sstream>
43#include <string>
44#include <tuple>
45#include <vector>
46
47namespace arm_compute
48{
49namespace test
50{
51namespace framework
52{
53/** Main framework class.
54 *
55 * Keeps track of the global state, owns all test cases and collects results.
56 */
57class Framework final
58{
59public:
60 /** Type of a test identifier.
61 *
62 * A test can be identified either via its id or via its name.
63 *
64 * @note The mapping between test id and test name is not guaranteed to be
65 * stable. It is subject to change as new test are added.
66 */
67 using TestId = std::pair<int, std::string>;
68
69 /** Access to the singleton.
70 *
71 * @return Unique instance of the framework class.
72 */
73 static Framework &get();
74
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010075 /** Supported instrument types for benchmarking.
76 *
77 * @return Set of all available instrument types.
78 */
79 std::set<InstrumentType> available_instruments() const;
80
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010081 /** Init the framework.
82 *
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010083 * @param[in] instruments Instrument types that will be used for benchmarking.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010084 * @param[in] num_iterations Number of iterations per test.
85 * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed.
86 * @param[in] id_filter Regular expression to filter tests by id. Only matching tests will be executed.
87 */
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010088 void init(const std::vector<InstrumentType> &instruments, int num_iterations, const std::string &name_filter, const std::string &id_filter);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010089
90 /** Add a new test suite.
91 *
92 * @warning Cannot be used at execution time. It can only be used for
93 * registering test cases.
94 *
95 * @param[in] name Name of the added test suite.
96 *
97 * @return Name of the current test suite.
98 */
99 void push_suite(std::string name);
100
101 /** Remove innermost test suite.
102 *
103 * @warning Cannot be used at execution time. It can only be used for
104 * registering test cases.
105 */
106 void pop_suite();
107
108 /** Add a test case to the framework.
109 *
110 * @param[in] test_name Name of the new test case.
111 */
112 template <typename T>
113 void add_test_case(std::string test_name);
114
115 /** Add a data test case to the framework.
116 *
117 * @param[in] test_name Name of the new test case.
118 * @param[in] description Description of @p data.
119 * @param[in] data Data that will be used as input to the test.
120 */
121 template <typename T, typename D>
122 void add_data_test_case(std::string test_name, std::string description, D &&data);
123
124 /** Tell the framework that execution of a test starts.
125 *
126 * @param[in] test_name Name of the started test case.
127 */
128 void log_test_start(const std::string &test_name);
129
130 /** Tell the framework that a test case is skipped.
131 *
132 * @param[in] test_name Name of the skipped test case.
133 */
134 void log_test_skipped(const std::string &test_name);
135
136 /** Tell the framework that a test case finished.
137 *
138 * @param[in] test_name Name of the finished test case.
139 */
140 void log_test_end(const std::string &test_name);
141
142 /** Tell the framework that the currently running test case failed a non-fatal expectation.
143 *
144 * @param[in] msg Description of the failure.
145 */
146 void log_failed_expectation(const std::string &msg);
147
148 /** Number of iterations per test case.
149 *
150 * @return Number of iterations per test case.
151 */
152 int num_iterations() const;
153
154 /** Set number of iterations per test case.
155 *
156 * @param[in] num_iterations Number of iterations per test case.
157 */
158 void set_num_iterations(int num_iterations);
159
160 /** Should errors be caught or thrown by the framework.
161 *
162 * @return True if errors are thrown.
163 */
164 bool throw_errors() const;
165
166 /** Set whether errors are caught or thrown by the framework.
167 *
168 * @param[in] throw_errors True if errors should be thrown.
169 */
170 void set_throw_errors(bool throw_errors);
171
172 /** Check if a test case would be executed.
173 *
174 * @param[in] id Id of the test case.
175 *
176 * @return True if the test case would be executed.
177 */
178 bool is_enabled(const TestId &id) const;
179
180 /** Run all enabled test cases.
181 *
182 * @return True if all test cases executed successful.
183 */
184 bool run();
185
186 /** Set the result for an executed test case.
187 *
188 * @param[in] test_case_name Name of the executed test case.
189 * @param[in] result Execution result.
190 */
191 void set_test_result(std::string test_case_name, TestResult result);
192
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100193 /** Factory method to obtain a configured profiler.
194 *
195 * The profiler enables all instruments that have been passed to the @ref
196 * init method.
197 *
198 * @return Configured profiler to collect benchmark results.
199 */
200 Profiler get_profiler() const;
201
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100202 /** List of @ref TestId's.
203 *
204 * @return Vector with all test ids.
205 */
206 std::vector<Framework::TestId> test_ids() const;
207
208private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100209 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100210 ~Framework() = default;
211
212 Framework(const Framework &) = delete;
213 Framework &operator=(const Framework &) = delete;
214
215 void run_test(TestCaseFactory &test_factory);
216 std::tuple<int, int, int> count_test_results() const;
217
218 /** Returns the current test suite name.
219 *
220 * @warning Cannot be used at execution time to get the test suite of the
221 * currently executed test case. It can only be used for registering test
222 * cases.
223 *
224 * @return Name of the current test suite.
225 */
226 std::string current_suite_name() const;
227
228 std::vector<std::string> _test_suite_name{};
229 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
230 std::map<std::string, TestResult> _test_results{};
231 std::chrono::seconds _runtime{ 0 };
232 int _num_iterations{ 1 };
233 bool _throw_errors{ false };
234
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100235 using create_function = std::unique_ptr<Instrument>();
236 std::map<InstrumentType, create_function *> _available_instruments{};
237
238 InstrumentType _instruments{ InstrumentType::NONE };
239 std::regex _test_name_filter{ ".*" };
240 std::regex _test_id_filter{ ".*" };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100241};
242
243template <typename T>
244inline void Framework::add_test_case(std::string test_name)
245{
246 _test_factories.emplace_back(support::cpp14::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name)));
247}
248
249template <typename T, typename D>
250inline void Framework::add_data_test_case(std::string test_name, std::string description, D &&data)
251{
252 // WORKAROUND for GCC 4.9
253 // The function should get *it which is tuple but that seems to trigger a
254 // bug in the compiler.
255 auto tmp = std::unique_ptr<DataTestCaseFactory<T, decltype(*std::declval<D>())>>(new DataTestCaseFactory<T, decltype(*std::declval<D>())>(current_suite_name(), std::move(test_name),
256 std::move(description), *data));
257 _test_factories.emplace_back(std::move(tmp));
258}
259} // namespace framework
260} // namespace test
261} // namespace arm_compute
262#endif /* ARM_COMPUTE_TEST_FRAMEWORK */