1 /*
2 Copyright (c) 2005-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #ifndef __TBB_test_common_initializer_list_support_H
18 #define __TBB_test_common_initializer_list_support_H
19
20 #include "config.h"
21
22 #include <initializer_list>
23 #include <vector>
24 #include <type_traits>
25
26 namespace initializer_list_support_tests {
27
28 template <typename ContainerType, typename ElementType>
test_ctor(std::initializer_list<ElementType> init,const ContainerType & expected)29 void test_ctor( std::initializer_list<ElementType> init, const ContainerType& expected ) {
30 ContainerType cont(init);
31 REQUIRE_MESSAGE(cont == expected, "Initialization via initializer_list failed");
32 }
33
34 template <typename ContainerType, typename ElementType>
test_assignment_operator(std::initializer_list<ElementType> init,const ContainerType & expected)35 void test_assignment_operator( std::initializer_list<ElementType> init, const ContainerType& expected ) {
36 ContainerType cont;
37 static_assert(std::is_same< decltype(cont = init), ContainerType& >::value == true,
38 "ContainerType::operator=(std::intializer_list) must return ContainerType&");
39
40 cont = init;
41 REQUIRE_MESSAGE(cont == expected, "Assignment from the initializer_list failed");
42 }
43
44 struct SkippedTest {
45 template <typename ContainerType, typename ElementType>
testSkippedTest46 static void test( std::initializer_list<ElementType>, const ContainerType& ) {}
47 }; // struct SkippedTest
48
49 struct TestAssignMethod {
50 template <typename ContainerType, typename ElementType>
testTestAssignMethod51 static void test( std::initializer_list<ElementType> init, const ContainerType& expected) {
52 ContainerType cont;
53 cont.assign(init);
54 REQUIRE_MESSAGE(cont == expected, "assign method with the initializer list argument failed");
55 }
56 }; // struct TestAssign
57
58 struct TestInsertMethod {
59 template <typename ContainerType, typename ElementType>
testTestInsertMethod60 static void test( std::initializer_list<ElementType> init, const ContainerType& expected) {
61 ContainerType cont;
62 cont.insert(init);
63 REQUIRE_MESSAGE(cont == expected, "insert method with the initializer list argument failed");
64 }
65 }; // struct TestInsertMethod
66
67 template <typename ContainerType, typename TestAssign, typename TestSpecial>
test_initializer_list_support(std::initializer_list<typename ContainerType::value_type> init)68 void test_initializer_list_support( std::initializer_list<typename ContainerType::value_type> init ) {
69 using element_type = typename ContainerType::value_type;
70 std::vector<element_type> test_seq(init);
71 ContainerType expected(test_seq.begin(), test_seq.end());
72
73 test_ctor(init, expected);
74 test_assignment_operator(init, expected);
75 TestAssign::test(init, expected);
76 TestSpecial::test(init, expected);
77 }
78
79 template <typename ContainerType, typename TestSpecial = SkippedTest>
test_initializer_list_support(std::initializer_list<typename ContainerType::value_type> init)80 void test_initializer_list_support( std::initializer_list<typename ContainerType::value_type> init ) {
81 test_initializer_list_support<ContainerType, TestAssignMethod, TestSpecial>(init);
82 }
83
84 template <typename ContainerType, typename TestSpecial = SkippedTest>
test_initializer_list_support_without_assign(std::initializer_list<typename ContainerType::value_type> init)85 void test_initializer_list_support_without_assign( std::initializer_list<typename ContainerType::value_type> init ) {
86 test_initializer_list_support<ContainerType, SkippedTest, TestSpecial>(init);
87 }
88
89 } // namespace initializer_list_support_tests
90
91 #endif // __TBB_test_common_initializer_list_support_H
92