EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MPLTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MPLTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
18 
19 #include <type_traits>
20 
21 #include <boost/hana.hpp>
22 #include <boost/hana/core/to.hpp>
23 #include <boost/hana/equal.hpp>
24 #include <boost/hana/ext/std/tuple.hpp>
25 #include <boost/hana/integral_constant.hpp>
26 #include <boost/hana/set.hpp>
27 #include <boost/hana/transform.hpp>
28 #include <boost/hana/tuple.hpp>
29 #include <boost/hana/type.hpp>
30 #include <boost/hana/union.hpp>
31 
32 namespace hana = boost::hana;
33 namespace Acts {
34 
35 namespace Test {
36 
37 BOOST_AUTO_TEST_CASE(all_of_test) {
38  using detail::all_of_v;
39 
40  static_assert(not all_of_v<true, true, false>,
41  "all_of_v<true, true, false> failed");
42  static_assert(not all_of_v<false, true, true, false>,
43  "all_of_v<false, true, true, false> failed");
44  static_assert(all_of_v<true, true, true>,
45  "all_of_v<true, true, true> failed");
46  static_assert(all_of_v<true>, "all_of_v<true> failed");
47  static_assert(not all_of_v<false>, "all_of_v<false> failed");
48  static_assert(all_of_v<>, "all_of_v<> failed");
49 }
50 
51 BOOST_AUTO_TEST_CASE(hana_set_union_test) {
52  // using first = typename bm::set<float, int, char, bool>::type;
53  constexpr auto first = hana::make_set(hana::type_c<float>, hana::type_c<int>,
54  hana::type_c<char>, hana::type_c<bool>);
55  // using second = typename bm::vector<long, int>::type;
56  constexpr auto second = hana::make_set(hana::type_c<long>, hana::type_c<int>);
57  constexpr auto found = hana::union_(first, second);
58  // using found = typename detail::boost_set_merger_t<first, second>;
59  // using expected = typename bm::set<float, int, char, bool, long>::type;
60  constexpr auto expected =
61  hana::make_set(hana::type_c<float>, hana::type_c<int>, hana::type_c<char>,
62  hana::type_c<bool>, hana::type_c<long>);
63 
64  static_assert(found == expected, "union of hana::sets failed");
65 }
66 
67 BOOST_AUTO_TEST_CASE(hana_set_to_tuple_test) {
68  constexpr auto a_set = hana::make_set(hana::type_c<float>, hana::type_c<int>,
69  hana::type_c<char>, hana::type_c<bool>);
70  constexpr auto h_tuple =
71  hana::make_tuple(hana::type_c<float>, hana::type_c<int>,
72  hana::type_c<char>, hana::type_c<bool>);
73 
74  static_assert(hana::to<hana::tuple_tag>(a_set) == h_tuple, "not equal");
75 
76  // using std_tuple = decltype(hana::unpack(a_set,
77  // hana::template_<std::tuple>))::type; using expected = std::tuple<float,
78  // int, char>; static_assert(std::is_same<std_tuple, expected>::value,
79  // "using
80  // boost::mpl::set for variadic templates failed");
81 }
82 
83 template <typename... args>
85  using tuple = std::tuple<args...>;
86 };
87 
88 BOOST_AUTO_TEST_CASE(unpack_boost_set_as_template_test) {
89  constexpr auto hana_set = hana::make_set(
90  hana::type_c<float>, hana::type_c<int>, hana::type_c<char>);
91  using found =
92  decltype(hana::unpack(hana_set, hana::template_<variadic_struct>))::type;
93 
94  using expected = variadic_struct<float, int, char>;
95 
97  "using boost::mpl::set for variadic templates failed");
98 
99  static_assert(
100  std::is_same<expected::tuple, std::tuple<float, int, char>>::value,
101  "not equal");
102 }
103 
104 namespace {
105 struct traits1 {
106  using result_type = int;
107  using action_type = char;
108 };
109 
110 template <bool>
111 struct traits2;
112 
113 template <>
114 struct traits2<false> {
115  using result_type = bool;
116  using action_type = float;
117 };
118 
119 template <>
120 struct traits2<true> {
121  using action_type = float;
122 };
123 } // namespace
124 
125 template <typename... Args>
126 struct tuple_helper {
127  using tuple = std::tuple<Args...>;
128 };
129 
130 BOOST_AUTO_TEST_CASE(type_collector_test) {
131  // test some predicates
132  static_assert(detail::has_result_type_v<traits1>, "Did not find result type");
134  "Did not find result type");
135  static_assert(not detail::has_result_type_v<traits2<true>>,
136  "Did find result type");
137 
138  static_assert(detail::has_action_type_v<traits1>, "Did not find action type");
140  "Did not find action type");
142  "Did not find action type");
143 
144  constexpr auto found_results =
147  constexpr auto expected_results =
148  hana::make_set(hana::type_c<int>, hana::type_c<bool>);
149  static_assert(found_results == expected_results,
150  "Didn't find expected results");
151 
152  // check unpack
153  using found_results_tuple = decltype(
154  hana::unpack(found_results, hana::template_<tuple_helper>))::type::tuple;
155  using expected_results_tuple = std::tuple<int, bool>;
156  static_assert(
158  "Unpacked results tuple not correct");
159 
160  constexpr auto found_actions =
162  traits2<true>, traits2<false>>;
163  constexpr auto expected_actions =
164  hana::make_set(hana::type_c<char>, hana::type_c<float>);
165  static_assert(found_actions == expected_actions,
166  "Didn't find expected actions");
167 
168  // check unpack
169  using found_actions_tuple = decltype(
170  hana::unpack(found_actions, hana::template_<tuple_helper>))::type::tuple;
171  using expected_actions_tuple = std::tuple<char, float>;
172  static_assert(
174  "Unpacked actions tuple not correct");
175 }
176 
177 BOOST_AUTO_TEST_CASE(has_duplicates_test) {
179  static_assert(has_duplicates_v<int, float, char, int>,
180  "has_duplicates_v failed");
181  static_assert(has_duplicates_v<int, int, char, float>,
182  "has_duplicates_v failed");
183  static_assert(has_duplicates_v<int, char, float, float>,
184  "has_duplicates_v failed");
185  static_assert(has_duplicates_v<int, char, char, float>,
186  "has_duplicates_v failed");
187  static_assert(not has_duplicates_v<int, bool, char, float>,
188  "has_duplicates_v failed");
189 }
190 
191 BOOST_AUTO_TEST_CASE(any_of_test) {
192  using detail::any_of_v;
193 
194  static_assert(any_of_v<true, true, false>,
195  "any_of_v<true, true, false> failed");
196  static_assert(any_of_v<false, true, true, false>,
197  "any_of_v<false, true, true, false> failed");
198  static_assert(any_of_v<true, true, true>,
199  "any_of_v<true, true, true> failed");
200  static_assert(not any_of_v<false, false>, "any_of_v<false, false> failed");
201  static_assert(any_of_v<true>, "any_of_v<true> failed");
202  static_assert(not any_of_v<false>, "any_of_v<false> failed");
203  static_assert(not any_of_v<>, "any_of_v<> failed");
204 }
205 
226 BOOST_AUTO_TEST_CASE(are_sorted_helper_tests) {
227  using detail::are_sorted;
228  // strictly ascending
232  // weakly ascending
236  // strictly descending
240  // weakly descending
244 }
245 
262 BOOST_AUTO_TEST_CASE(are_within_helper_tests) {
263  using detail::are_within;
270  BOOST_CHECK((not are_within<int, 0, 10, 1, 10>::value));
272 }
273 
277 BOOST_AUTO_TEST_CASE(at_index_helper_tests) {
278  using detail::at_index;
279  BOOST_CHECK((at_index<int, 0, 10, 1, 3, 7, 2>::value == 10));
280  BOOST_CHECK((at_index<int, 1, 10, 1, 3, 7, 2>::value == 1));
281  BOOST_CHECK((at_index<int, 2, 10, 1, 3, 7, 2>::value == 3));
282  BOOST_CHECK((at_index<int, 3, 10, 1, 3, 7, 2>::value == 7));
283  BOOST_CHECK((at_index<int, 4, 10, 1, 3, 7, 2>::value == 2));
284 }
285 } // namespace Test
286 
287 } // namespace Acts