blob: 09e9d198d6c2d740ba01cfdf39742c0ad262a9ea [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_TEST_CASE_FACTORY
25#define ARM_COMPUTE_TEST_TEST_CASE_FACTORY
26
27#include "TestCase.h"
28#include "support/ToolchainSupport.h"
29
30#include <memory>
31#include <string>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace framework
38{
39/** Abstract factory class to create test cases. */
40class TestCaseFactory
41{
42public:
43 /** Constructor.
44 *
45 * @param[in] suite_name Name of the test suite to which the test case has been added.
46 * @param[in] name Name of the test case.
47 * @param[in] description Description of data arguments.
48 */
49 TestCaseFactory(std::string suite_name, std::string name, std::string description = "");
50
51 /** Default destructor. */
52 virtual ~TestCaseFactory() = default;
53
54 /** Name of the test case.
55 *
56 * @return Name of the test case.
57 */
58 std::string name() const;
59
60 /** Factory function to create the test case
61 *
62 * @return Unique pointer to a newly created test case.
63 */
64 virtual std::unique_ptr<TestCase> make() const = 0;
65
66private:
67 const std::string _suite_name;
68 const std::string _test_name;
69 const std::string _data_description;
70};
71
72/** Implementation of a test case factory to create non-data test cases. */
73template <typename T>
74class SimpleTestCaseFactory final : public TestCaseFactory
75{
76public:
77 /** Default constructor. */
78 using TestCaseFactory::TestCaseFactory;
79
80 std::unique_ptr<TestCase> make() const override;
81};
82
83template <typename T, typename D>
84class DataTestCaseFactory final : public TestCaseFactory
85{
86public:
87 /** Constructor.
88 *
89 * @param[in] suite_name Name of the test suite to which the test case has been added.
90 * @param[in] test_name Name of the test case.
91 * @param[in] description Description of data arguments.
92 * @param[in] data Input data for the test case.
93 */
94 DataTestCaseFactory(std::string suite_name, std::string test_name, std::string description, const D &data);
95
96 std::unique_ptr<TestCase> make() const override;
97
98private:
99 D _data;
100};
101
102inline TestCaseFactory::TestCaseFactory(std::string suite_name, std::string test_name, std::string description)
103 : _suite_name{ std::move(suite_name) }, _test_name{ std::move(test_name) }, _data_description{ std::move(description) }
104{
105}
106
107inline std::string TestCaseFactory::name() const
108{
109 std::string name = _suite_name + "/" + _test_name;
110
111 if(!_data_description.empty())
112 {
113 name += "@" + _data_description;
114 }
115
116 return name;
117}
118
119template <typename T>
120inline std::unique_ptr<TestCase> SimpleTestCaseFactory<T>::make() const
121{
122 return support::cpp14::make_unique<T>();
123}
124
125template <typename T, typename D>
126inline DataTestCaseFactory<T, D>::DataTestCaseFactory(std::string suite_name, std::string test_name, std::string description, const D &data)
127 : TestCaseFactory{ std::move(suite_name), std::move(test_name), std::move(description) }, _data{ data }
128{
129}
130
131template <typename T, typename D>
132inline std::unique_ptr<TestCase> DataTestCaseFactory<T, D>::make() const
133{
134 return support::cpp14::make_unique<T>(_data);
135}
136} // namespace framework
137} // namespace test
138} // namespace arm_compute
139#endif /* ARM_COMPUTE_TEST_TEST_CASE_FACTORY */