blob: 80616c46fce24c5ea32e8636ac91057747b80796 [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Diego Lopez Recas0021d752017-12-18 14:42:56 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +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_DATASET_CONTAINER
25#define ARM_COMPUTE_TEST_DATASET_CONTAINER
26
27#include "Dataset.h"
28#include "support/ToolchainSupport.h"
29
30#include <string>
31#include <tuple>
32#include <type_traits>
33#include <utility>
34#include <vector>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42namespace dataset
43{
44/** Base case. Nothing is a container. */
45template <typename T>
46struct is_container : public std::false_type
47{
48};
49
50/** Vector is considered a container. */
51template <typename V, typename A>
52struct is_container<std::vector<V, A>> : public std::true_type
53{
54};
55
56/** Implementation of a dataset created from a container. */
57template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010058class ContainerDataset : public NamedDataset
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010059{
60private:
61 using container_value_type = typename T::value_type;
62 using container_const_iterator = typename T::const_iterator;
63
64public:
65 /** Construct dataset with given name and values from the container.
66 *
67 * @param[in] name Description of the values.
68 * @param[in] container Values for the dataset.
69 */
70 ContainerDataset(std::string name, T &&container)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010071 : NamedDataset{ std::move(name) }, _container(std::forward<T>(container))
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010072 {
73 }
74
Diego Lopez Recas0021d752017-12-18 14:42:56 +000075 ContainerDataset(const ContainerDataset &) = default;
76 ContainerDataset(ContainerDataset &&) = default;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010077
78 /** Type of the dataset. */
79 using type = std::tuple<container_value_type>;
80
81 /** Iterator for the dataset. */
82 struct iterator
83 {
84 iterator(std::string name, container_const_iterator iterator)
85 : _name{ name }, _iterator{ iterator }
86 {
87 }
88
89 std::string description() const
90 {
91 using support::cpp11::to_string;
92 return _name + "=" + to_string(*_iterator);
93 }
94
95 ContainerDataset::type operator*() const
96 {
97 return std::make_tuple(*_iterator);
98 }
99
100 iterator &operator++()
101 {
102 ++_iterator;
103 return *this;
104 }
105
106 private:
107 std::string _name;
108 container_const_iterator _iterator;
109 };
110
111 /** Iterator pointing at the begin of the dataset.
112 *
113 * @return Iterator for the dataset.
114 */
115 iterator begin() const
116 {
117 return iterator(name(), _container.cbegin());
118 }
119
120 /** Size of the dataset.
121 *
122 * @return Number of values in the dataset.
123 */
124 int size() const
125 {
126 return _container.size();
127 }
128
129private:
130 T _container;
131};
132
133/** Helper function to create a @ref ContainerDataset.
134 *
135 * @param[in] name Name of the dataset.
136 * @param[in] values Container.
137 *
138 * @return A container dataset.
139 */
140template <typename T>
141typename std::enable_if<is_container<T>::value, ContainerDataset<T>>::type make(std::string name, T &&values)
142{
143 return ContainerDataset<T>(std::move(name), std::forward<T>(values));
144}
145} // namespace dataset
146} // namespace framework
147} // namespace test
148} // namespace arm_compute
149#endif /* ARM_COMPUTE_TEST_DATASET_CONTAINER */