blob: afd881e25be83f997ca7f1a98d838144203b436f [file] [log] [blame]
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01003 *
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_TEST_CASE_FACTORY
25#define ARM_COMPUTE_TEST_TEST_CASE_FACTORY
26
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010027#include "DatasetModes.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010028#include "TestCase.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000029#include "support/MemorySupport.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010030
31#include <memory>
32#include <string>
33
34namespace arm_compute
35{
36namespace test
37{
38namespace framework
39{
40/** Abstract factory class to create test cases. */
41class TestCaseFactory
42{
43public:
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010044 /** Test case status.
45 *
46 * ACTIVE == Test is run and result is validated. Failure on failed validation.
47 * EXPECTED_FAILURE == Test is run and result is validated. Failure on successful validation.
48 * DISABLED == Test is not run.
49 */
50 enum class Status
51 {
52 ACTIVE,
53 EXPECTED_FAILURE,
54 DISABLED
55 };
56
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010057 /** Constructor.
58 *
59 * @param[in] suite_name Name of the test suite to which the test case has been added.
60 * @param[in] name Name of the test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010061 * @param[in] mode Datset mode of the test case.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010062 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010063 * @param[in] description Description of data arguments.
64 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010065 TestCaseFactory(std::string suite_name, std::string name, DatasetMode mode, Status status, std::string description = "");
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010066
67 /** Default destructor. */
68 virtual ~TestCaseFactory() = default;
69
70 /** Name of the test case.
71 *
72 * @return Name of the test case.
73 */
74 std::string name() const;
75
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010076 /** Get the mode for which test case will be enabled.
77 *
78 * @return Dataset mode of the test case.
79 */
80 DatasetMode mode() const;
81
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010082 /** Get the status of the test case.
83 *
84 * @return Status of the test case.
85 */
86 Status status() const;
87
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010088 /** Factory function to create the test case
89 *
90 * @return Unique pointer to a newly created test case.
91 */
92 virtual std::unique_ptr<TestCase> make() const = 0;
93
94private:
95 const std::string _suite_name;
96 const std::string _test_name;
97 const std::string _data_description;
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010098 const DatasetMode _mode{ DatasetMode::ALL };
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010099 const Status _status{ Status::ACTIVE };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100100};
101
102/** Implementation of a test case factory to create non-data test cases. */
103template <typename T>
104class SimpleTestCaseFactory final : public TestCaseFactory
105{
106public:
107 /** Default constructor. */
108 using TestCaseFactory::TestCaseFactory;
109
110 std::unique_ptr<TestCase> make() const override;
111};
112
Alex Gildayc357c472018-03-21 13:54:09 +0000113/** Implementation of a test case factory to create data test cases. */
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100114template <typename T, typename D>
115class DataTestCaseFactory final : public TestCaseFactory
116{
117public:
118 /** Constructor.
119 *
120 * @param[in] suite_name Name of the test suite to which the test case has been added.
121 * @param[in] test_name Name of the test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100122 * @param[in] mode Mode in which the test case is enabled.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100123 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100124 * @param[in] description Description of data arguments.
125 * @param[in] data Input data for the test case.
126 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100127 DataTestCaseFactory(std::string suite_name, std::string test_name, DatasetMode mode, Status status, std::string description, const D &data);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100128
129 std::unique_ptr<TestCase> make() const override;
130
131private:
132 D _data;
133};
134
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100135inline TestCaseFactory::TestCaseFactory(std::string suite_name, std::string test_name, DatasetMode mode, Status status, std::string description)
136 : _suite_name{ std::move(suite_name) }, _test_name{ std::move(test_name) }, _data_description{ std::move(description) }, _mode{ mode }, _status{ status }
137
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100138{
139}
140
141inline std::string TestCaseFactory::name() const
142{
143 std::string name = _suite_name + "/" + _test_name;
144
145 if(!_data_description.empty())
146 {
147 name += "@" + _data_description;
148 }
149
150 return name;
151}
152
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100153inline DatasetMode TestCaseFactory::mode() const
154{
155 return _mode;
156}
157
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100158inline TestCaseFactory::Status TestCaseFactory::status() const
159{
160 return _status;
161}
162
163inline ::std::ostream &operator<<(::std::ostream &stream, TestCaseFactory::Status status)
164{
165 switch(status)
166 {
167 case TestCaseFactory::Status::ACTIVE:
168 stream << "ACTIVE";
169 break;
170 case TestCaseFactory::Status::EXPECTED_FAILURE:
171 stream << "EXPECTED_FAILURE";
172 break;
173 case TestCaseFactory::Status::DISABLED:
174 stream << "DISABLED";
175 break;
176 default:
177 throw std::invalid_argument("Unsupported test case factory status");
178 }
179
180 return stream;
181}
182
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100183template <typename T>
184inline std::unique_ptr<TestCase> SimpleTestCaseFactory<T>::make() const
185{
186 return support::cpp14::make_unique<T>();
187}
188
189template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100190inline DataTestCaseFactory<T, D>::DataTestCaseFactory(std::string suite_name, std::string test_name, DatasetMode mode, Status status, std::string description, const D &data)
191 : TestCaseFactory{ std::move(suite_name), std::move(test_name), mode, status, std::move(description) }, _data{ data }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100192{
193}
194
195template <typename T, typename D>
196inline std::unique_ptr<TestCase> DataTestCaseFactory<T, D>::make() const
197{
198 return support::cpp14::make_unique<T>(_data);
199}
200} // namespace framework
201} // namespace test
202} // namespace arm_compute
203#endif /* ARM_COMPUTE_TEST_TEST_CASE_FACTORY */