Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / libs / eigen / test / commainitializer.cpp
blob9844adbd2286435697cd74c5035d87aadd3e4a05
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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/.
10 #include "main.h"
13 template<int M1, int M2, int N1, int N2>
14 void test_blocks()
16 Matrix<int, M1+M2, N1+N2> m_fixed;
17 MatrixXi m_dynamic(M1+M2, N1+N2);
19 Matrix<int, M1, N1> mat11; mat11.setRandom();
20 Matrix<int, M1, N2> mat12; mat12.setRandom();
21 Matrix<int, M2, N1> mat21; mat21.setRandom();
22 Matrix<int, M2, N2> mat22; mat22.setRandom();
24 MatrixXi matx11 = mat11, matx12 = mat12, matx21 = mat21, matx22 = mat22;
27 VERIFY_IS_EQUAL((m_fixed << mat11, mat12, mat21, matx22).finished(), (m_dynamic << mat11, matx12, mat21, matx22).finished());
28 VERIFY_IS_EQUAL((m_fixed.template topLeftCorner<M1,N1>()), mat11);
29 VERIFY_IS_EQUAL((m_fixed.template topRightCorner<M1,N2>()), mat12);
30 VERIFY_IS_EQUAL((m_fixed.template bottomLeftCorner<M2,N1>()), mat21);
31 VERIFY_IS_EQUAL((m_fixed.template bottomRightCorner<M2,N2>()), mat22);
32 VERIFY_IS_EQUAL((m_fixed << mat12, mat11, matx21, mat22).finished(), (m_dynamic << mat12, matx11, matx21, mat22).finished());
35 if(N1 > 0)
37 VERIFY_RAISES_ASSERT((m_fixed << mat11, mat12, mat11, mat21, mat22));
38 VERIFY_RAISES_ASSERT((m_fixed << mat11, mat12, mat21, mat21, mat22));
40 else
42 // allow insertion of zero-column blocks:
43 VERIFY_IS_EQUAL((m_fixed << mat11, mat12, mat11, mat11, mat21, mat21, mat22).finished(), (m_dynamic << mat12, mat22).finished());
45 if(M1 != M2)
47 VERIFY_RAISES_ASSERT((m_fixed << mat11, mat21, mat12, mat22));
52 template<int N>
53 struct test_block_recursion
55 static void run()
57 test_blocks<(N>>6)&3, (N>>4)&3, (N>>2)&3, N & 3>();
58 test_block_recursion<N-1>::run();
62 template<>
63 struct test_block_recursion<-1>
65 static void run() { }
68 void test_commainitializer()
70 Matrix3d m3;
71 Matrix4d m4;
73 VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8) );
75 #ifndef _MSC_VER
76 VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) );
77 #endif
79 double data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
80 Matrix3d ref = Map<Matrix<double,3,3,RowMajor> >(data);
82 m3 = Matrix3d::Random();
83 m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
84 VERIFY_IS_APPROX(m3, ref );
86 Vector3d vec[3];
87 vec[0] << 1, 4, 7;
88 vec[1] << 2, 5, 8;
89 vec[2] << 3, 6, 9;
90 m3 = Matrix3d::Random();
91 m3 << vec[0], vec[1], vec[2];
92 VERIFY_IS_APPROX(m3, ref);
94 vec[0] << 1, 2, 3;
95 vec[1] << 4, 5, 6;
96 vec[2] << 7, 8, 9;
97 m3 = Matrix3d::Random();
98 m3 << vec[0].transpose(),
99 4, 5, 6,
100 vec[2].transpose();
101 VERIFY_IS_APPROX(m3, ref);
104 // recursively test all block-sizes from 0 to 3:
105 test_block_recursion<(1<<8) - 1>();