Analysis Software
Documentation for sPHENIX simulation 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 <tuple>
20 #include <type_traits>
21 
22 #include <boost/hana.hpp>
23 #include <boost/hana/core/to.hpp>
24 #include <boost/hana/equal.hpp>
25 #include <boost/hana/ext/std/tuple.hpp>
26 #include <boost/hana/integral_constant.hpp>
27 #include <boost/hana/set.hpp>
28 #include <boost/hana/transform.hpp>
29 #include <boost/hana/tuple.hpp>
30 #include <boost/hana/type.hpp>
31 #include <boost/hana/union.hpp>
32 
33 namespace hana = boost::hana;
34 namespace Acts {
35 namespace detail {
36 template <bool ascending, bool strict, typename T, T... values>
37 struct are_sorted;
38 template <typename T, T MIN, T MAX, T... values>
39 struct are_within;
40 template <typename T, size_t index, T... values>
41 struct at_index;
42 } // namespace detail
43 
44 namespace Test {
45 
46 BOOST_AUTO_TEST_CASE(all_of_test) {
47  using detail::all_of_v;
48 
49  static_assert(not all_of_v<true, true, false>,
50  "all_of_v<true, true, false> failed");
51  static_assert(not all_of_v<false, true, true, false>,
52  "all_of_v<false, true, true, false> failed");
53  static_assert(all_of_v<true, true, true>,
54  "all_of_v<true, true, true> failed");
55  static_assert(all_of_v<true>, "all_of_v<true> failed");
56  static_assert(not all_of_v<false>, "all_of_v<false> failed");
57  static_assert(all_of_v<>, "all_of_v<> failed");
58 }
59 
60 BOOST_AUTO_TEST_CASE(hana_set_union_test) {
61  // using first = typename bm::set<float, int, char, bool>::type;
62  constexpr auto first = hana::make_set(hana::type_c<float>, hana::type_c<int>,
63  hana::type_c<char>, hana::type_c<bool>);
64  // using second = typename bm::vector<long, int>::type;
65  constexpr auto second = hana::make_set(hana::type_c<long>, hana::type_c<int>);
66  constexpr auto found = hana::union_(first, second);
67  // using found = typename detail::boost_set_merger_t<first, second>;
68  // using expected = typename bm::set<float, int, char, bool, long>::type;
69  constexpr auto expected =
70  hana::make_set(hana::type_c<float>, hana::type_c<int>, hana::type_c<char>,
71  hana::type_c<bool>, hana::type_c<long>);
72 
73  static_assert(found == expected, "union of hana::sets failed");
74 }
75 
76 BOOST_AUTO_TEST_CASE(hana_set_to_tuple_test) {
77  constexpr auto a_set = hana::make_set(hana::type_c<float>, hana::type_c<int>,
78  hana::type_c<char>, hana::type_c<bool>);
79  constexpr auto h_tuple =
80  hana::make_tuple(hana::type_c<float>, hana::type_c<int>,
81  hana::type_c<char>, hana::type_c<bool>);
82 
83  static_assert(hana::to<hana::tuple_tag>(a_set) == h_tuple, "not equal");
84 
85  // using std_tuple = decltype(hana::unpack(a_set,
86  // hana::template_<std::tuple>))::type; using expected = std::tuple<float,
87  // int, char>; static_assert(std::is_same<std_tuple, expected>::value,
88  // "using
89  // boost::mpl::set for variadic templates failed");
90 }
91 
92 template <typename... args>
94  using tuple = std::tuple<args...>;
95 };
96 
97 BOOST_AUTO_TEST_CASE(unpack_boost_set_as_template_test) {
98  constexpr auto hana_set = hana::make_set(
99  hana::type_c<float>, hana::type_c<int>, hana::type_c<char>);
100  using found =
101  decltype(hana::unpack(hana_set, hana::template_<variadic_struct>))::type;
102 
103  using expected = variadic_struct<float, int, char>;
104 
106  "using boost::mpl::set for variadic templates failed");
107 
108  static_assert(
109  std::is_same<expected::tuple, std::tuple<float, int, char>>::value,
110  "not equal");
111 }
112 
113 namespace {
114 struct traits1 {
115  using result_type = int;
116  using action_type = char;
117 };
118 
119 template <bool>
120 struct traits2;
121 
122 template <>
123 struct traits2<false> {
124  using result_type = bool;
125  using action_type = float;
126 };
127 
128 template <>
129 struct traits2<true> {
130  using action_type = float;
131 };
132 } // namespace
133 
134 template <typename... Args>
135 struct tuple_helper {
136  using tuple = std::tuple<Args...>;
137 };
138 
139 BOOST_AUTO_TEST_CASE(type_collector_test) {
140  // test some predicates
141  static_assert(detail::has_result_type_v<traits1>, "Did not find result type");
143  "Did not find result type");
144  static_assert(not detail::has_result_type_v<traits2<true>>,
145  "Did find result type");
146 
147  static_assert(detail::has_action_type_v<traits1>, "Did not find action type");
149  "Did not find action type");
151  "Did not find action type");
152 
153  constexpr auto found_results =
156  constexpr auto expected_results =
157  hana::make_set(hana::type_c<int>, hana::type_c<bool>);
158  static_assert(found_results == expected_results,
159  "Didn't find expected results");
160 
161  // check unpack
162  using found_results_tuple = decltype(hana::unpack(
163  found_results, hana::template_<tuple_helper>))::type::tuple;
164  using expected_results_tuple = std::tuple<int, bool>;
165  static_assert(
167  "Unpacked results tuple not correct");
168 
169  constexpr auto found_actions =
171  traits2<true>, traits2<false>>;
172  constexpr auto expected_actions =
173  hana::make_set(hana::type_c<char>, hana::type_c<float>);
174  static_assert(found_actions == expected_actions,
175  "Didn't find expected actions");
176 
177  // check unpack
178  using found_actions_tuple = decltype(hana::unpack(
179  found_actions, hana::template_<tuple_helper>))::type::tuple;
180  using expected_actions_tuple = std::tuple<char, float>;
181  static_assert(
183  "Unpacked actions tuple not correct");
184 }
185 
186 BOOST_AUTO_TEST_CASE(has_duplicates_test) {
188  static_assert(has_duplicates_v<int, float, char, int>,
189  "has_duplicates_v failed");
190  static_assert(has_duplicates_v<int, int, char, float>,
191  "has_duplicates_v failed");
192  static_assert(has_duplicates_v<int, char, float, float>,
193  "has_duplicates_v failed");
194  static_assert(has_duplicates_v<int, char, char, float>,
195  "has_duplicates_v failed");
196  static_assert(not has_duplicates_v<int, bool, char, float>,
197  "has_duplicates_v failed");
198 }
199 
200 BOOST_AUTO_TEST_CASE(any_of_test) {
201  using detail::any_of_v;
202 
203  static_assert(any_of_v<true, true, false>,
204  "any_of_v<true, true, false> failed");
205  static_assert(any_of_v<false, true, true, false>,
206  "any_of_v<false, true, true, false> failed");
207  static_assert(any_of_v<true, true, true>,
208  "any_of_v<true, true, true> failed");
209  static_assert(not any_of_v<false, false>, "any_of_v<false, false> failed");
210  static_assert(any_of_v<true>, "any_of_v<true> failed");
211  static_assert(not any_of_v<false>, "any_of_v<false> failed");
212  static_assert(not any_of_v<>, "any_of_v<> failed");
213 }
214 
235 BOOST_AUTO_TEST_CASE(are_sorted_helper_tests) {
236  using detail::are_sorted;
237  // strictly ascending
241  // weakly ascending
245  // strictly descending
249  // weakly descending
253 }
254 
271 BOOST_AUTO_TEST_CASE(are_within_helper_tests) {
272  using detail::are_within;
279  BOOST_CHECK((not are_within<int, 0, 10, 1, 10>::value));
281 }
282 
286 BOOST_AUTO_TEST_CASE(at_index_helper_tests) {
287  using detail::at_index;
288  BOOST_CHECK((at_index<int, 0, 10, 1, 3, 7, 2>::value == 10));
289  BOOST_CHECK((at_index<int, 1, 10, 1, 3, 7, 2>::value == 1));
290  BOOST_CHECK((at_index<int, 2, 10, 1, 3, 7, 2>::value == 3));
291  BOOST_CHECK((at_index<int, 3, 10, 1, 3, 7, 2>::value == 7));
292  BOOST_CHECK((at_index<int, 4, 10, 1, 3, 7, 2>::value == 2));
293 }
294 } // namespace Test
295 
296 } // namespace Acts