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 
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 {
94 
100 
106 
112 
118 
124 
130 }
131 
132 static void test_concat()
133 {
139 
145 
151 
157 }
158 
159 static void test_slice()
160 {
163 
171 
179 
187 
195 
200 }
201 
202 static void test_get()
203 {
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 {
255 }
256 
257 static void test_apply_op()
258 {
262 }
263 
265 {
267 
273 
279 
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 }
constexpr EIGEN_STRONG_INLINE array< T, N > array_reverse(array< T, N > arr)
Definition: CXX11Meta.h:344
static void test_arg_reductions()
Definition: cxx11_meta.cpp:287
static void test_apply_op()
Definition: cxx11_meta.cpp:257
constexpr EIGEN_STRONG_INLINE array< decltype(Op::run(A())), N > array_apply(array< A, N > a)
Definition: CXX11Meta.h:448
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
dummy_inst(int)
Definition: cxx11_meta.cpp:80
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE auto array_sum(const array< T, N > &arr) -> decltype(array_reduce< sum_op, T, N >(arr, static_cast< T >(0)))
Definition: CXX11Meta.h:392
constexpr EIGEN_STRONG_INLINE auto array_apply_and_reduce(array< A, N > a) -> decltype(h_array_apply_and_reduce< Reducer, Op, A, N >(a, typename gen_numeric_list< int, N >::type()))
Definition: CXX11Meta.h:462
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
Point2 a3
Definition: testPose2.cpp:771
static void test_is_same_gf()
Definition: cxx11_meta.cpp:249
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
static void test_array_zip_and_apply()
Definition: cxx11_meta.cpp:310
constexpr EIGEN_STRONG_INLINE auto array_zip_and_reduce(array< A, N > a, array< B, N > b) -> decltype(h_array_zip_and_reduce< Reducer, Op, A, B, N >(a, b, typename gen_numeric_list< int, N >::type()))
Definition: CXX11Meta.h:434
dummy_inst(int, int, int, int, int)
Definition: cxx11_meta.cpp:84
#define VERIFY_IS_APPROX(a, b)
static void test_array_misc()
Definition: cxx11_meta.cpp:327
EIGEN_DECLARE_TEST(cxx11_meta)
Definition: cxx11_meta.cpp:343
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE auto array_reduce(const array< T, N > &arr, T identity) -> decltype(h_array_reduce< Reducer, T, N >::run(arr, identity))
Definition: CXX11Meta.h:384
static void test_get()
Definition: cxx11_meta.cpp:202
Array< int, Dynamic, 1 > v
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:139
constexpr EIGEN_STRONG_INLINE array< decltype(Op::run(A(), B())), N > array_zip(array< A, N > a, array< B, N > b)
Definition: CXX11Meta.h:420
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
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
#define VERIFY(a)
Definition: main.h:380
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
constexprdecltype(reduce< sum_op, Ts... >::run((*((Ts *) 0))...)) EIGEN_STRONG_INLIN arg_sum)(Ts... ts)
Definition: CXX11Meta.h:330
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)
InstType instantiate_by_c_array(ArrType *arr)
Definition: CXX11Meta.h:528
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
EIGEN_DEVICE_FUNC constexprdecltype(reduce< product_op, Ts... >::run((*((Ts *) 0))...)) EIGEN_STRONG_INLIN arg_prod)(Ts... ts)
Definition: CXX11Meta.h:324
constexpr array< t, n > repeat(t v)
Definition: CXX11Meta.h:483


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:06