cxx11_meta.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 #include <array>
14 
17 using Eigen::internal::numeric_list;
18 using Eigen::internal::gen_numeric_list;
19 using Eigen::internal::gen_numeric_list_reversed;
20 using Eigen::internal::gen_numeric_list_swapped_pair;
23 using Eigen::internal::mconcat;
24 using Eigen::internal::take;
25 using Eigen::internal::skip;
26 using Eigen::internal::slice;
28 using Eigen::internal::id_numeric;
29 using Eigen::internal::id_type;
30 using Eigen::internal::is_same_gf;
31 using Eigen::internal::apply_op_from_left;
32 using Eigen::internal::apply_op_from_right;
33 using Eigen::internal::contained_in_list;
34 using Eigen::internal::contained_in_list_gf;
36 using Eigen::internal::arg_sum;
39 using Eigen::internal::array_reverse;
40 using Eigen::internal::array_sum;
42 using Eigen::internal::array_reduce;
43 using Eigen::internal::array_zip;
45 using Eigen::internal::array_apply;
48 using Eigen::internal::instantiate_by_c_array;
49 
50 struct dummy_a {};
51 struct dummy_b {};
52 struct dummy_c {};
53 struct dummy_d {};
54 struct dummy_e {};
55 
56 // dummy operation for testing apply
57 template<typename A, typename B> struct dummy_op;
58 template<> struct dummy_op<dummy_a, dummy_b> { typedef dummy_c type; };
59 template<> struct dummy_op<dummy_b, dummy_a> { typedef dummy_d type; };
60 template<> struct dummy_op<dummy_b, dummy_c> { typedef dummy_a type; };
61 template<> struct dummy_op<dummy_c, dummy_b> { typedef dummy_d type; };
62 template<> struct dummy_op<dummy_c, dummy_a> { typedef dummy_b type; };
63 template<> struct dummy_op<dummy_a, dummy_c> { typedef dummy_d type; };
64 template<> struct dummy_op<dummy_a, dummy_a> { typedef dummy_e type; };
65 template<> struct dummy_op<dummy_b, dummy_b> { typedef dummy_e type; };
66 template<> struct dummy_op<dummy_c, dummy_c> { typedef dummy_e type; };
67 
68 template<typename A, typename B> struct dummy_test { constexpr static bool value = false; constexpr static int global_flags = 0; };
69 template<> struct dummy_test<dummy_a, dummy_a> { constexpr static bool value = true; constexpr static int global_flags = 1; };
70 template<> struct dummy_test<dummy_b, dummy_b> { constexpr static bool value = true; constexpr static int global_flags = 2; };
71 template<> struct dummy_test<dummy_c, dummy_c> { constexpr static bool value = true; constexpr static int global_flags = 4; };
72 
73 struct times2_op { template<typename A> static A run(A v) { return v * 2; } };
74 
75 struct dummy_inst
76 {
77  int c;
78 
79  dummy_inst() : c(0) {}
80  explicit dummy_inst(int) : c(1) {}
81  dummy_inst(int, int) : c(2) {}
82  dummy_inst(int, int, int) : c(3) {}
83  dummy_inst(int, int, int, int) : c(4) {}
84  dummy_inst(int, int, int, int, int) : c(5) {}
85 };
86 
87 static void test_gen_numeric_list()
88 {
89  VERIFY((is_same<typename gen_numeric_list<int, 0>::type, numeric_list<int>>::value));
90  VERIFY((is_same<typename gen_numeric_list<int, 1>::type, numeric_list<int, 0>>::value));
91  VERIFY((is_same<typename gen_numeric_list<int, 2>::type, numeric_list<int, 0, 1>>::value));
92  VERIFY((is_same<typename gen_numeric_list<int, 5>::type, numeric_list<int, 0, 1, 2, 3, 4>>::value));
93  VERIFY((is_same<typename gen_numeric_list<int, 10>::type, numeric_list<int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9>>::value));
94 
95  VERIFY((is_same<typename gen_numeric_list<int, 0, 42>::type, numeric_list<int>>::value));
96  VERIFY((is_same<typename gen_numeric_list<int, 1, 42>::type, numeric_list<int, 42>>::value));
97  VERIFY((is_same<typename gen_numeric_list<int, 2, 42>::type, numeric_list<int, 42, 43>>::value));
98  VERIFY((is_same<typename gen_numeric_list<int, 5, 42>::type, numeric_list<int, 42, 43, 44, 45, 46>>::value));
99  VERIFY((is_same<typename gen_numeric_list<int, 10, 42>::type, numeric_list<int, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51>>::value));
100 
101  VERIFY((is_same<typename gen_numeric_list_reversed<int, 0>::type, numeric_list<int>>::value));
102  VERIFY((is_same<typename gen_numeric_list_reversed<int, 1>::type, numeric_list<int, 0>>::value));
103  VERIFY((is_same<typename gen_numeric_list_reversed<int, 2>::type, numeric_list<int, 1, 0>>::value));
104  VERIFY((is_same<typename gen_numeric_list_reversed<int, 5>::type, numeric_list<int, 4, 3, 2, 1, 0>>::value));
105  VERIFY((is_same<typename gen_numeric_list_reversed<int, 10>::type, numeric_list<int, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0>>::value));
106 
107  VERIFY((is_same<typename gen_numeric_list_reversed<int, 0, 42>::type, numeric_list<int>>::value));
108  VERIFY((is_same<typename gen_numeric_list_reversed<int, 1, 42>::type, numeric_list<int, 42>>::value));
109  VERIFY((is_same<typename gen_numeric_list_reversed<int, 2, 42>::type, numeric_list<int, 43, 42>>::value));
110  VERIFY((is_same<typename gen_numeric_list_reversed<int, 5, 42>::type, numeric_list<int, 46, 45, 44, 43, 42>>::value));
111  VERIFY((is_same<typename gen_numeric_list_reversed<int, 10, 42>::type, numeric_list<int, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42>>::value));
112 
114  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 1, 2, 3>::type, numeric_list<int, 0>>::value));
115  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 2, 2, 3>::type, numeric_list<int, 0, 1>>::value));
116  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 5, 2, 3>::type, numeric_list<int, 0, 1, 3, 2, 4>>::value));
117  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 10, 2, 3>::type, numeric_list<int, 0, 1, 3, 2, 4, 5, 6, 7, 8, 9>>::value));
118 
121  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 2, 44, 45, 42>::type, numeric_list<int, 42, 43>>::value));
122  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 5, 44, 45, 42>::type, numeric_list<int, 42, 43, 45, 44, 46>>::value));
123  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 10, 44, 45, 42>::type, numeric_list<int, 42, 43, 45, 44, 46, 47, 48, 49, 50, 51>>::value));
124 
125  VERIFY((is_same<typename gen_numeric_list_repeated<int, 0, 0>::type, numeric_list<int>>::value));
126  VERIFY((is_same<typename gen_numeric_list_repeated<int, 1, 0>::type, numeric_list<int, 0>>::value));
127  VERIFY((is_same<typename gen_numeric_list_repeated<int, 2, 0>::type, numeric_list<int, 0, 0>>::value));
128  VERIFY((is_same<typename gen_numeric_list_repeated<int, 5, 0>::type, numeric_list<int, 0, 0, 0, 0, 0>>::value));
129  VERIFY((is_same<typename gen_numeric_list_repeated<int, 10, 0>::type, numeric_list<int, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>::value));
130 }
131 
132 static void test_concat()
133 {
139 
140  VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int>>::type, numeric_list<int, 0, 0>>::value));
141  VERIFY((is_same<typename concat<numeric_list<int>, numeric_list<int, 0, 0>>::type, numeric_list<int, 0, 0>>::value));
142  VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int, 0, 0>>::type, numeric_list<int, 0, 0, 0, 0>>::value));
143  VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 0, 1, 2>>::value));
144  VERIFY((is_same<typename concat<numeric_list<int, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
145 
151 
152  VERIFY((is_same<typename mconcat<numeric_list<int, 0>>::type, numeric_list<int, 0>>::value));
153  VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1>>::type, numeric_list<int, 0, 1>>::value));
154  VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1>, numeric_list<int, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
155  VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
156  VERIFY((is_same<typename mconcat<numeric_list<int, 0, 1>, numeric_list<int, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
157 }
158 
159 static void test_slice()
160 {
162  typedef numeric_list<int, 0, 1, 2, 3, 4, 5> il;
163 
171 
172  VERIFY((is_same<typename take<0, il>::type, numeric_list<int>>::value));
173  VERIFY((is_same<typename take<1, il>::type, numeric_list<int, 0>>::value));
174  VERIFY((is_same<typename take<2, il>::type, numeric_list<int, 0, 1>>::value));
175  VERIFY((is_same<typename take<3, il>::type, numeric_list<int, 0, 1, 2>>::value));
176  VERIFY((is_same<typename take<4, il>::type, numeric_list<int, 0, 1, 2, 3>>::value));
177  VERIFY((is_same<typename take<5, il>::type, numeric_list<int, 0, 1, 2, 3, 4>>::value));
178  VERIFY((is_same<typename take<6, il>::type, numeric_list<int, 0, 1, 2, 3, 4, 5>>::value));
179 
187 
188  VERIFY((is_same<typename skip<0, il>::type, numeric_list<int, 0, 1, 2, 3, 4, 5>>::value));
189  VERIFY((is_same<typename skip<1, il>::type, numeric_list<int, 1, 2, 3, 4, 5>>::value));
190  VERIFY((is_same<typename skip<2, il>::type, numeric_list<int, 2, 3, 4, 5>>::value));
191  VERIFY((is_same<typename skip<3, il>::type, numeric_list<int, 3, 4, 5>>::value));
192  VERIFY((is_same<typename skip<4, il>::type, numeric_list<int, 4, 5>>::value));
193  VERIFY((is_same<typename skip<5, il>::type, numeric_list<int, 5>>::value));
194  VERIFY((is_same<typename skip<6, il>::type, numeric_list<int>>::value));
195 
199  VERIFY((is_same<typename slice<1, 3, il>::type, numeric_list<int, 1, 2, 3>>::value));
200 }
201 
202 static void test_get()
203 {
205  typedef numeric_list<int, 4, 8, 15, 16, 23, 42> il;
206 
213 
220 }
221 
223 {
224  (void)a;
225  (void)b;
226  (void)c;
227 }
228 
229 template<int... ii>
230 static void test_id_numeric()
231 {
233 }
234 
235 template<typename... tt>
236 static void test_id_type()
237 {
239 }
240 
241 static void test_id()
242 {
243  // don't call VERIFY here, just assume it works if it compiles
244  // (otherwise it will complain that it can't find the function)
245  test_id_numeric<1, 4, 6>();
246  test_id_type<dummy_a, dummy_b, dummy_c>();
247 }
248 
249 static void test_is_same_gf()
250 {
253  VERIFY_IS_EQUAL((!!is_same_gf<dummy_a, dummy_b>::global_flags), false);
254  VERIFY_IS_EQUAL((!!is_same_gf<dummy_a, dummy_a>::global_flags), false);
255 }
256 
257 static void test_apply_op()
258 {
262 }
263 
265 {
267 
273 
279 
280  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_a, tl>::global_flags), 1);
281  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_b, tl>::global_flags), 2);
282  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_c, tl>::global_flags), 4);
283  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_d, tl>::global_flags), 0);
284  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_e, tl>::global_flags), 0);
285 }
286 
287 static void test_arg_reductions()
288 {
289  VERIFY_IS_EQUAL(arg_sum(1,2,3,4), 10);
290  VERIFY_IS_EQUAL(arg_prod(1,2,3,4), 24);
291  VERIFY_IS_APPROX(arg_sum(0.5, 2, 5), 7.5);
292  VERIFY_IS_APPROX(arg_prod(0.5, 2, 5), 5.0);
293 }
294 
296 {
297  array<int, 6> a{{4, 8, 15, 16, 23, 42}};
298  array<int, 6> b{{42, 23, 16, 15, 8, 4}};
299 
300  // there is no operator<< for std::array, so VERIFY_IS_EQUAL will
301  // not compile
302  VERIFY((array_reverse(a) == b));
303  VERIFY((array_reverse(b) == a));
304  VERIFY_IS_EQUAL((array_sum(a)), 108);
305  VERIFY_IS_EQUAL((array_sum(b)), 108);
306  VERIFY_IS_EQUAL((array_prod(a)), 7418880);
307  VERIFY_IS_EQUAL((array_prod(b)), 7418880);
308 }
309 
311 {
312  array<int, 6> a{{4, 8, 15, 16, 23, 42}};
313  array<int, 6> b{{0, 1, 2, 3, 4, 5}};
314  array<int, 6> c{{4, 9, 17, 19, 27, 47}};
315  array<int, 6> d{{0, 8, 30, 48, 92, 210}};
316  array<int, 6> e{{0, 2, 4, 6, 8, 10}};
317 
318  VERIFY((array_zip<sum_op>(a, b) == c));
319  VERIFY((array_zip<product_op>(a, b) == d));
320  VERIFY((array_apply<times2_op>(b) == e));
321  VERIFY_IS_EQUAL((array_apply_and_reduce<sum_op, times2_op>(a)), 216);
322  VERIFY_IS_EQUAL((array_apply_and_reduce<sum_op, times2_op>(b)), 30);
323  VERIFY_IS_EQUAL((array_zip_and_reduce<product_op, sum_op>(a, b)), 14755932);
324  VERIFY_IS_EQUAL((array_zip_and_reduce<sum_op, product_op>(a, b)), 388);
325 }
326 
327 static void test_array_misc()
328 {
329  array<int, 3> a3{{1, 1, 1}};
330  array<int, 6> a6{{2, 2, 2, 2, 2, 2}};
331  VERIFY((repeat<3, int>(1) == a3));
332  VERIFY((repeat<6, int>(2) == a6));
333 
334  int data[5] = { 0, 1, 2, 3, 4 };
335  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 0>(data).c), 0);
336  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 1>(data).c), 1);
337  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 2>(data).c), 2);
338  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 3>(data).c), 3);
339  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 4>(data).c), 4);
340  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 5>(data).c), 5);
341 }
342 
344 {
357 }
static void test_arg_reductions()
Definition: cxx11_meta.cpp:287
static void test_apply_op()
Definition: cxx11_meta.cpp:257
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
dummy_inst(int)
Definition: cxx11_meta.cpp:80
static void test_id_type()
Definition: cxx11_meta.cpp:236
Scalar * b
Definition: benchVecAdd.cpp:17
static void test_array_reverse_and_reduce()
Definition: cxx11_meta.cpp:295
ArrayXcf v
Definition: Cwise_arg.cpp:1
static void test_is_same_gf()
Definition: cxx11_meta.cpp:249
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
array< t, n > repeat(t v)
static void test_array_zip_and_apply()
Definition: cxx11_meta.cpp:310
dummy_inst(int, int, int, int, int)
Definition: cxx11_meta.cpp:84
Array33i a
#define VERIFY_IS_APPROX(a, b)
void test_cxx11_meta()
Definition: cxx11_meta.cpp:343
static void test_array_misc()
Definition: cxx11_meta.cpp:327
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:331
static void test_get()
Definition: cxx11_meta.cpp:202
bool array_apply_and_reduce(const array< A, N > &a)
static void test_concat()
Definition: cxx11_meta.cpp:132
int data[]
Array< double, 1, 3 > e(1./3., 0.5, 2.)
constexpr descr< 0 > concat()
Definition: descr.h:83
dummy_inst(int, int)
Definition: cxx11_meta.cpp:81
static void test_gen_numeric_list()
Definition: cxx11_meta.cpp:87
static void test_id()
Definition: cxx11_meta.cpp:241
bool array_zip_and_reduce(const array< A, N > &a, const array< B, N > &b)
#define CALL_SUBTEST(FUNC)
Definition: main.h:342
#define VERIFY(a)
Definition: main.h:325
static A run(A v)
Definition: cxx11_meta.cpp:73
static void test_id_helper(dummy_a a, dummy_a b, dummy_a c)
Definition: cxx11_meta.cpp:222
dummy_inst(int, int, int)
Definition: cxx11_meta.cpp:82
dummy_inst(int, int, int, int)
Definition: cxx11_meta.cpp:83
Container::iterator get(Container &c, Position position)
static void test_contained_in_list()
Definition: cxx11_meta.cpp:264
static void test_slice()
Definition: cxx11_meta.cpp:159
static void test_id_numeric()
Definition: cxx11_meta.cpp:230


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:55