1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Hauke Heibel <hauke.heibel@gmail.com>
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
14 using internal::UIntPtr;
15
16 #if EIGEN_HAS_RVALUE_REFERENCES
17 template <typename MatrixType>
rvalue_copyassign(const MatrixType & m)18 void rvalue_copyassign(const MatrixType& m)
19 {
20
21 typedef typename internal::traits<MatrixType>::Scalar Scalar;
22
23 // create a temporary which we are about to destroy by moving
24 MatrixType tmp = m;
25 UIntPtr src_address = reinterpret_cast<UIntPtr>(tmp.data());
26
27 // move the temporary to n
28 MatrixType n = std::move(tmp);
29 UIntPtr dst_address = reinterpret_cast<UIntPtr>(n.data());
30
31 if (MatrixType::RowsAtCompileTime==Dynamic|| MatrixType::ColsAtCompileTime==Dynamic)
32 {
33 // verify that we actually moved the guts
34 VERIFY_IS_EQUAL(src_address, dst_address);
35 }
36
37 // verify that the content did not change
38 Scalar abs_diff = (m-n).array().abs().sum();
39 VERIFY_IS_EQUAL(abs_diff, Scalar(0));
40 }
41 #else
42 template <typename MatrixType>
rvalue_copyassign(const MatrixType &)43 void rvalue_copyassign(const MatrixType&) {}
44 #endif
45
test_rvalue_types()46 void test_rvalue_types()
47 {
48 CALL_SUBTEST_1(rvalue_copyassign( MatrixXf::Random(50,50).eval() ));
49 CALL_SUBTEST_1(rvalue_copyassign( ArrayXXf::Random(50,50).eval() ));
50
51 CALL_SUBTEST_1(rvalue_copyassign( Matrix<float,1,Dynamic>::Random(50).eval() ));
52 CALL_SUBTEST_1(rvalue_copyassign( Array<float,1,Dynamic>::Random(50).eval() ));
53
54 CALL_SUBTEST_1(rvalue_copyassign( Matrix<float,Dynamic,1>::Random(50).eval() ));
55 CALL_SUBTEST_1(rvalue_copyassign( Array<float,Dynamic,1>::Random(50).eval() ));
56
57 CALL_SUBTEST_2(rvalue_copyassign( Array<float,2,1>::Random().eval() ));
58 CALL_SUBTEST_2(rvalue_copyassign( Array<float,3,1>::Random().eval() ));
59 CALL_SUBTEST_2(rvalue_copyassign( Array<float,4,1>::Random().eval() ));
60
61 CALL_SUBTEST_2(rvalue_copyassign( Array<float,2,2>::Random().eval() ));
62 CALL_SUBTEST_2(rvalue_copyassign( Array<float,3,3>::Random().eval() ));
63 CALL_SUBTEST_2(rvalue_copyassign( Array<float,4,4>::Random().eval() ));
64 }
65