xref: /aosp_15_r20/external/eigen/test/swap.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2009 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #define EIGEN_NO_STATIC_ASSERT
11*bf2c3715SXin Li #include "main.h"
12*bf2c3715SXin Li 
13*bf2c3715SXin Li template<typename T>
14*bf2c3715SXin Li struct other_matrix_type
15*bf2c3715SXin Li {
16*bf2c3715SXin Li   typedef int type;
17*bf2c3715SXin Li };
18*bf2c3715SXin Li 
19*bf2c3715SXin Li template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
20*bf2c3715SXin Li struct other_matrix_type<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
21*bf2c3715SXin Li {
22*bf2c3715SXin Li   typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
23*bf2c3715SXin Li };
24*bf2c3715SXin Li 
swap(const MatrixType & m)25*bf2c3715SXin Li template<typename MatrixType> void swap(const MatrixType& m)
26*bf2c3715SXin Li {
27*bf2c3715SXin Li   typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
28*bf2c3715SXin Li   typedef typename MatrixType::Scalar Scalar;
29*bf2c3715SXin Li 
30*bf2c3715SXin Li   eigen_assert((!internal::is_same<MatrixType,OtherMatrixType>::value));
31*bf2c3715SXin Li   Index rows = m.rows();
32*bf2c3715SXin Li   Index cols = m.cols();
33*bf2c3715SXin Li 
34*bf2c3715SXin Li   // construct 3 matrix guaranteed to be distinct
35*bf2c3715SXin Li   MatrixType m1 = MatrixType::Random(rows,cols);
36*bf2c3715SXin Li   MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
37*bf2c3715SXin Li   OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
38*bf2c3715SXin Li 
39*bf2c3715SXin Li   MatrixType m1_copy = m1;
40*bf2c3715SXin Li   MatrixType m2_copy = m2;
41*bf2c3715SXin Li   OtherMatrixType m3_copy = m3;
42*bf2c3715SXin Li 
43*bf2c3715SXin Li   // test swapping 2 matrices of same type
44*bf2c3715SXin Li   Scalar *d1=m1.data(), *d2=m2.data();
45*bf2c3715SXin Li   m1.swap(m2);
46*bf2c3715SXin Li   VERIFY_IS_APPROX(m1,m2_copy);
47*bf2c3715SXin Li   VERIFY_IS_APPROX(m2,m1_copy);
48*bf2c3715SXin Li   if(MatrixType::SizeAtCompileTime==Dynamic)
49*bf2c3715SXin Li   {
50*bf2c3715SXin Li     VERIFY(m1.data()==d2);
51*bf2c3715SXin Li     VERIFY(m2.data()==d1);
52*bf2c3715SXin Li   }
53*bf2c3715SXin Li   m1 = m1_copy;
54*bf2c3715SXin Li   m2 = m2_copy;
55*bf2c3715SXin Li 
56*bf2c3715SXin Li   // test swapping 2 matrices of different types
57*bf2c3715SXin Li   m1.swap(m3);
58*bf2c3715SXin Li   VERIFY_IS_APPROX(m1,m3_copy);
59*bf2c3715SXin Li   VERIFY_IS_APPROX(m3,m1_copy);
60*bf2c3715SXin Li   m1 = m1_copy;
61*bf2c3715SXin Li   m3 = m3_copy;
62*bf2c3715SXin Li 
63*bf2c3715SXin Li   // test swapping matrix with expression
64*bf2c3715SXin Li   m1.swap(m2.block(0,0,rows,cols));
65*bf2c3715SXin Li   VERIFY_IS_APPROX(m1,m2_copy);
66*bf2c3715SXin Li   VERIFY_IS_APPROX(m2,m1_copy);
67*bf2c3715SXin Li   m1 = m1_copy;
68*bf2c3715SXin Li   m2 = m2_copy;
69*bf2c3715SXin Li 
70*bf2c3715SXin Li   // test swapping two expressions of different types
71*bf2c3715SXin Li   m1.transpose().swap(m3.transpose());
72*bf2c3715SXin Li   VERIFY_IS_APPROX(m1,m3_copy);
73*bf2c3715SXin Li   VERIFY_IS_APPROX(m3,m1_copy);
74*bf2c3715SXin Li   m1 = m1_copy;
75*bf2c3715SXin Li   m3 = m3_copy;
76*bf2c3715SXin Li 
77*bf2c3715SXin Li   if(m1.rows()>1)
78*bf2c3715SXin Li   {
79*bf2c3715SXin Li     // test assertion on mismatching size -- matrix case
80*bf2c3715SXin Li     VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
81*bf2c3715SXin Li     // test assertion on mismatching size -- xpr case
82*bf2c3715SXin Li     VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
83*bf2c3715SXin Li   }
84*bf2c3715SXin Li }
85*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(swap)86*bf2c3715SXin Li EIGEN_DECLARE_TEST(swap)
87*bf2c3715SXin Li {
88*bf2c3715SXin Li   int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
89*bf2c3715SXin Li   CALL_SUBTEST_1( swap(Matrix3f()) ); // fixed size, no vectorization
90*bf2c3715SXin Li   CALL_SUBTEST_2( swap(Matrix4d()) ); // fixed size, possible vectorization
91*bf2c3715SXin Li   CALL_SUBTEST_3( swap(MatrixXd(s,s)) ); // dyn size, no vectorization
92*bf2c3715SXin Li   CALL_SUBTEST_4( swap(MatrixXf(s,s)) ); // dyn size, possible vectorization
93*bf2c3715SXin Li   TEST_SET_BUT_UNUSED_VARIABLE(s)
94*bf2c3715SXin Li }
95