blob: c3bbe37b7547771a10c40d0e7d0bd96c1539c21b [file] [log] [blame]
Antonio Sanchez26e5beb2021-08-26 13:05:23 -07001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2021 The Eigen Team
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 <Eigen/Core>
13#include <Eigen/src/Core/arch/GPU/Tuple.h>
14
15using namespace Eigen::internal;
16using Eigen::internal::tuple_impl::tuple;
17
18void basic_tuple_test() {
19 // Construction.
20 tuple<> tuple0 {};
21 tuple<int> tuple1 {1};
22 tuple<int, float> tuple2 {3, 5.0f};
23 tuple<int, float, double> tuple3 {7, 11.0f, 13.0};
24 // Default construction.
25 tuple<> tuple0default;
26 EIGEN_UNUSED_VARIABLE(tuple0default)
27 tuple<int> tuple1default;
28 EIGEN_UNUSED_VARIABLE(tuple1default)
29 tuple<int, float> tuple2default;
30 EIGEN_UNUSED_VARIABLE(tuple2default)
31 tuple<int, float, double> tuple3default;
32 EIGEN_UNUSED_VARIABLE(tuple3default)
33
34 // Assignment.
35 tuple<> tuple0b = tuple0;
36 EIGEN_UNUSED_VARIABLE(tuple0b)
37 decltype(tuple1) tuple1b = tuple1;
38 EIGEN_UNUSED_VARIABLE(tuple1b)
39 decltype(tuple2) tuple2b = tuple2;
40 EIGEN_UNUSED_VARIABLE(tuple2b)
41 decltype(tuple3) tuple3b = tuple3;
42 EIGEN_UNUSED_VARIABLE(tuple3b)
43
44 // get.
45 VERIFY_IS_EQUAL(tuple_impl::get<0>(tuple3), 7);
46 VERIFY_IS_EQUAL(tuple_impl::get<1>(tuple3), 11.0f);
47 VERIFY_IS_EQUAL(tuple_impl::get<2>(tuple3), 13.0);
48
49 // tuple_impl::tuple_size.
Antonio Sanchezf49217e2021-09-17 19:40:22 -070050 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple0)>::value, size_t(0));
51 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple1)>::value, size_t(1));
52 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple2)>::value, size_t(2));
53 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple3)>::value, size_t(3));
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070054
55 // tuple_impl::tuple_cat.
56 auto tuple2cat3 = tuple_impl::tuple_cat(tuple2, tuple3);
Antonio Sanchezf49217e2021-09-17 19:40:22 -070057 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple2cat3)>::value, size_t(5));
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070058 VERIFY_IS_EQUAL(tuple_impl::get<1>(tuple2cat3), 5.0f);
59 VERIFY_IS_EQUAL(tuple_impl::get<3>(tuple2cat3), 11.0f);
60 auto tuple3cat0 = tuple_impl::tuple_cat(tuple3, tuple0);
Antonio Sanchezf49217e2021-09-17 19:40:22 -070061 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple3cat0)>::value, size_t(3));
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070062 auto singlecat = tuple_impl::tuple_cat(tuple3);
Antonio Sanchezf49217e2021-09-17 19:40:22 -070063 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(singlecat)>::value, size_t(3));
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070064 auto emptycat = tuple_impl::tuple_cat();
Antonio Sanchezf49217e2021-09-17 19:40:22 -070065 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(emptycat)>::value, size_t(0));
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070066 auto tuple0cat1cat2cat3 = tuple_impl::tuple_cat(tuple0, tuple1, tuple2, tuple3);
Antonio Sanchezf49217e2021-09-17 19:40:22 -070067 VERIFY_IS_EQUAL(tuple_impl::tuple_size<decltype(tuple0cat1cat2cat3)>::value, size_t(6));
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070068
69 // make_tuple.
70 // The tuple types should uses values for the second and fourth parameters.
71 double tmp = 20;
72 auto tuple_make = tuple_impl::make_tuple(int(10), tmp, float(20.0f), tuple0);
73 VERIFY( (std::is_same<decltype(tuple_make), tuple<int, double, float, tuple<> > >::value) );
74 VERIFY_IS_EQUAL(tuple_impl::get<1>(tuple_make), tmp);
75
76 // forward_as_tuple.
77 // The tuple types should uses references for the second and fourth parameters.
78 auto tuple_forward = tuple_impl::forward_as_tuple(int(10), tmp, float(20.0f), tuple0);
79 VERIFY( (std::is_same<decltype(tuple_forward), tuple<int, double&, float, tuple<>& > >::value) );
80 VERIFY_IS_EQUAL(tuple_impl::get<1>(tuple_forward), tmp);
81
82 // tie.
83 auto tuple_tie = tuple_impl::tie(tuple0, tuple1, tuple2, tuple3);
84 VERIFY( (std::is_same<decltype(tuple_tie),
85 tuple<decltype(tuple0)&,
86 decltype(tuple1)&,
87 decltype(tuple2)&,
88 decltype(tuple3)&> >::value) );
Antonio Sanchezf49217e2021-09-17 19:40:22 -070089 VERIFY_IS_EQUAL( (tuple_impl::get<1>(tuple_impl::get<2>(tuple_tie))), 5.0f );
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070090 // Modify value and ensure tuple2 is updated.
Antonio Sanchezf49217e2021-09-17 19:40:22 -070091 tuple_impl::get<1>(tuple_impl::get<2>(tuple_tie)) = 10.0f;
92 VERIFY_IS_EQUAL( (tuple_impl::get<1>(tuple2)), 10.0f );
Antonio Sanchez26e5beb2021-08-26 13:05:23 -070093
94 // Assignment.
95 int x = -1;
96 float y = -1;
97 double z = -1;
98 tuple_impl::tie(x, y, z) = tuple3;
99 VERIFY_IS_EQUAL(x, tuple_impl::get<0>(tuple3));
100 VERIFY_IS_EQUAL(y, tuple_impl::get<1>(tuple3));
101 VERIFY_IS_EQUAL(z, tuple_impl::get<2>(tuple3));
Antonio Sanchezfd5f48e2021-09-29 20:34:03 -0700102 tuple<int, float, double> tuple3c(-2, -2.0f, -2.0);
Antonio Sanchez26e5beb2021-08-26 13:05:23 -0700103 tuple3c = std::move(tuple3b);
104 VERIFY_IS_EQUAL(tuple_impl::get<0>(tuple3c), tuple_impl::get<0>(tuple3));
105 VERIFY_IS_EQUAL(tuple_impl::get<1>(tuple3c), tuple_impl::get<1>(tuple3));
106 VERIFY_IS_EQUAL(tuple_impl::get<2>(tuple3c), tuple_impl::get<2>(tuple3));
107}
108
109void eigen_tuple_test() {
110 tuple<Eigen::Matrix3d, Eigen::MatrixXd> tuple;
111 tuple_impl::get<0>(tuple).setRandom();
112 tuple_impl::get<1>(tuple).setRandom(10, 10);
113
114 auto tuple_tie = tuple_impl::tie(tuple_impl::get<0>(tuple), tuple_impl::get<1>(tuple));
115 tuple_impl::get<1>(tuple_tie).setIdentity();
116 VERIFY(tuple_impl::get<1>(tuple).isIdentity());
117}
118
119EIGEN_DECLARE_TEST(tuple)
120{
121 CALL_SUBTEST(basic_tuple_test());
122 CALL_SUBTEST(eigen_tuple_test());
123}