1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef META_TEST_DATA_HELPERS_H
17 #define META_TEST_DATA_HELPERS_H
18 
19 #include <tuple>
20 #include <type_traits>
21 
22 #include <gtest/gtest.h>
23 
24 #include <base/containers/type_traits.h>
25 #include <base/containers/vector.h>
26 
27 namespace META_NS {
28 
29 template<typename T>
30 struct TType {
31     using Type = T;
32     BASE_NS::vector<std::function<Type()>> values;
33 
34     template<typename... Args, typename = BASE_NS::enable_if_t<std::conjunction_v<std::is_convertible<Args, Type>...>>>
TTypeMETA_NS::TType35     TType(Args... args) : values {  [args] { return static_cast<T>(args); }... }
36     {}
37 
TTypeMETA_NS::TType38     TType(std::initializer_list<std::function<Type()>> values) : values { values } {}
39 };
40 
41 template<typename... T>
42 struct TestTypes {
43     using Types = ::testing::Types<T...>;
44     using Tuple = std::tuple<TType<T>...>;
45 
TestTypesMETA_NS::TestTypes46     TestTypes(TType<T>... v) : values { v... } {}
47 
48     template<typename Type>
GetValueMETA_NS::TestTypes49     auto GetValue(size_t index) const
50     {
51         auto v = std::get<TType<Type>>(values);
52         CORE_ASSERT_MSG(index < v.values.size(), "invalid test values");
53         return v.values[index]();
54     }
55 
56     Tuple values;
57 };
58 
59 }
60 
61 #endif