Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 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_TEST_CASE_FACTORY |
| 25 | #define ARM_COMPUTE_TEST_TEST_CASE_FACTORY |
| 26 | |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 27 | #include "DatasetModes.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 28 | #include "TestCase.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 29 | |
| 30 | #include <memory> |
| 31 | #include <string> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace framework |
| 38 | { |
| 39 | /** Abstract factory class to create test cases. */ |
| 40 | class TestCaseFactory |
| 41 | { |
| 42 | public: |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 43 | /** Test case status. |
| 44 | * |
| 45 | * ACTIVE == Test is run and result is validated. Failure on failed validation. |
| 46 | * EXPECTED_FAILURE == Test is run and result is validated. Failure on successful validation. |
| 47 | * DISABLED == Test is not run. |
| 48 | */ |
| 49 | enum class Status |
| 50 | { |
| 51 | ACTIVE, |
| 52 | EXPECTED_FAILURE, |
| 53 | DISABLED |
| 54 | }; |
| 55 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 56 | /** Constructor. |
| 57 | * |
| 58 | * @param[in] suite_name Name of the test suite to which the test case has been added. |
| 59 | * @param[in] name Name of the test case. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 60 | * @param[in] mode Datset mode of the test case. |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 61 | * @param[in] status Status of the test case. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 62 | * @param[in] description Description of data arguments. |
| 63 | */ |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 64 | TestCaseFactory(std::string suite_name, std::string name, DatasetMode mode, Status status, std::string description = ""); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 65 | |
| 66 | /** Default destructor. */ |
| 67 | virtual ~TestCaseFactory() = default; |
| 68 | |
| 69 | /** Name of the test case. |
| 70 | * |
| 71 | * @return Name of the test case. |
| 72 | */ |
| 73 | std::string name() const; |
| 74 | |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 75 | /** Get the mode for which test case will be enabled. |
| 76 | * |
| 77 | * @return Dataset mode of the test case. |
| 78 | */ |
| 79 | DatasetMode mode() const; |
| 80 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 81 | /** Get the status of the test case. |
| 82 | * |
| 83 | * @return Status of the test case. |
| 84 | */ |
| 85 | Status status() const; |
| 86 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 87 | /** Factory function to create the test case |
| 88 | * |
| 89 | * @return Unique pointer to a newly created test case. |
| 90 | */ |
| 91 | virtual std::unique_ptr<TestCase> make() const = 0; |
| 92 | |
| 93 | private: |
| 94 | const std::string _suite_name; |
| 95 | const std::string _test_name; |
| 96 | const std::string _data_description; |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 97 | const DatasetMode _mode{ DatasetMode::ALL }; |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 98 | const Status _status{ Status::ACTIVE }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /** Implementation of a test case factory to create non-data test cases. */ |
| 102 | template <typename T> |
| 103 | class SimpleTestCaseFactory final : public TestCaseFactory |
| 104 | { |
| 105 | public: |
| 106 | /** Default constructor. */ |
| 107 | using TestCaseFactory::TestCaseFactory; |
| 108 | |
| 109 | std::unique_ptr<TestCase> make() const override; |
| 110 | }; |
| 111 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 112 | /** Implementation of a test case factory to create data test cases. */ |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 113 | template <typename T, typename D> |
| 114 | class DataTestCaseFactory final : public TestCaseFactory |
| 115 | { |
| 116 | public: |
| 117 | /** Constructor. |
| 118 | * |
| 119 | * @param[in] suite_name Name of the test suite to which the test case has been added. |
| 120 | * @param[in] test_name Name of the test case. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 121 | * @param[in] mode Mode in which the test case is enabled. |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 122 | * @param[in] status Status of the test case. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 123 | * @param[in] description Description of data arguments. |
| 124 | * @param[in] data Input data for the test case. |
| 125 | */ |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 126 | DataTestCaseFactory(std::string suite_name, std::string test_name, DatasetMode mode, Status status, std::string description, const D &data); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 127 | |
| 128 | std::unique_ptr<TestCase> make() const override; |
| 129 | |
| 130 | private: |
| 131 | D _data; |
| 132 | }; |
| 133 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 134 | inline TestCaseFactory::TestCaseFactory(std::string suite_name, std::string test_name, DatasetMode mode, Status status, std::string description) |
| 135 | : _suite_name{ std::move(suite_name) }, _test_name{ std::move(test_name) }, _data_description{ std::move(description) }, _mode{ mode }, _status{ status } |
| 136 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 137 | { |
| 138 | } |
| 139 | |
| 140 | inline std::string TestCaseFactory::name() const |
| 141 | { |
| 142 | std::string name = _suite_name + "/" + _test_name; |
| 143 | |
| 144 | if(!_data_description.empty()) |
| 145 | { |
| 146 | name += "@" + _data_description; |
| 147 | } |
| 148 | |
| 149 | return name; |
| 150 | } |
| 151 | |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 152 | inline DatasetMode TestCaseFactory::mode() const |
| 153 | { |
| 154 | return _mode; |
| 155 | } |
| 156 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 157 | inline TestCaseFactory::Status TestCaseFactory::status() const |
| 158 | { |
| 159 | return _status; |
| 160 | } |
| 161 | |
| 162 | inline ::std::ostream &operator<<(::std::ostream &stream, TestCaseFactory::Status status) |
| 163 | { |
| 164 | switch(status) |
| 165 | { |
| 166 | case TestCaseFactory::Status::ACTIVE: |
| 167 | stream << "ACTIVE"; |
| 168 | break; |
| 169 | case TestCaseFactory::Status::EXPECTED_FAILURE: |
| 170 | stream << "EXPECTED_FAILURE"; |
| 171 | break; |
| 172 | case TestCaseFactory::Status::DISABLED: |
| 173 | stream << "DISABLED"; |
| 174 | break; |
| 175 | default: |
| 176 | throw std::invalid_argument("Unsupported test case factory status"); |
| 177 | } |
| 178 | |
| 179 | return stream; |
| 180 | } |
| 181 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 182 | template <typename T> |
| 183 | inline std::unique_ptr<TestCase> SimpleTestCaseFactory<T>::make() const |
| 184 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 185 | return std::make_unique<T>(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | template <typename T, typename D> |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 189 | inline DataTestCaseFactory<T, D>::DataTestCaseFactory(std::string suite_name, std::string test_name, DatasetMode mode, Status status, std::string description, const D &data) |
| 190 | : TestCaseFactory{ std::move(suite_name), std::move(test_name), mode, status, std::move(description) }, _data{ data } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 191 | { |
| 192 | } |
| 193 | |
| 194 | template <typename T, typename D> |
| 195 | inline std::unique_ptr<TestCase> DataTestCaseFactory<T, D>::make() const |
| 196 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 197 | return std::make_unique<T>(_data); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 198 | } |
| 199 | } // namespace framework |
| 200 | } // namespace test |
| 201 | } // namespace arm_compute |
| 202 | #endif /* ARM_COMPUTE_TEST_TEST_CASE_FACTORY */ |