1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Benoit Jacob <jacob.benoit.1@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
zeroReduction(const MatrixType & m)13 template<typename MatrixType> void zeroReduction(const MatrixType& m) {
14 // Reductions that must hold for zero sized objects
15 VERIFY(m.all());
16 VERIFY(!m.any());
17 VERIFY(m.prod()==1);
18 VERIFY(m.sum()==0);
19 VERIFY(m.count()==0);
20 VERIFY(m.allFinite());
21 VERIFY(!m.hasNaN());
22 }
23
24
zeroSizedMatrix()25 template<typename MatrixType> void zeroSizedMatrix()
26 {
27 MatrixType t1;
28 typedef typename MatrixType::Scalar Scalar;
29
30 if (MatrixType::SizeAtCompileTime == Dynamic || MatrixType::SizeAtCompileTime == 0)
31 {
32 zeroReduction(t1);
33 if (MatrixType::RowsAtCompileTime == Dynamic)
34 VERIFY(t1.rows() == 0);
35 if (MatrixType::ColsAtCompileTime == Dynamic)
36 VERIFY(t1.cols() == 0);
37
38 if (MatrixType::RowsAtCompileTime == Dynamic && MatrixType::ColsAtCompileTime == Dynamic)
39 {
40
41 MatrixType t2(0, 0), t3(t1);
42 VERIFY(t2.rows() == 0);
43 VERIFY(t2.cols() == 0);
44
45 zeroReduction(t2);
46 VERIFY(t1==t2);
47 }
48 }
49
50 if(MatrixType::MaxColsAtCompileTime!=0 && MatrixType::MaxRowsAtCompileTime!=0)
51 {
52 Index rows = MatrixType::RowsAtCompileTime==Dynamic ? internal::random<Index>(1,10) : Index(MatrixType::RowsAtCompileTime);
53 Index cols = MatrixType::ColsAtCompileTime==Dynamic ? internal::random<Index>(1,10) : Index(MatrixType::ColsAtCompileTime);
54 MatrixType m(rows,cols);
55 zeroReduction(m.template block<0,MatrixType::ColsAtCompileTime>(0,0,0,cols));
56 zeroReduction(m.template block<MatrixType::RowsAtCompileTime,0>(0,0,rows,0));
57 zeroReduction(m.template block<0,1>(0,0));
58 zeroReduction(m.template block<1,0>(0,0));
59 Matrix<Scalar,Dynamic,Dynamic> prod = m.template block<MatrixType::RowsAtCompileTime,0>(0,0,rows,0) * m.template block<0,MatrixType::ColsAtCompileTime>(0,0,0,cols);
60 VERIFY(prod.rows()==rows && prod.cols()==cols);
61 VERIFY(prod.isZero());
62 prod = m.template block<1,0>(0,0) * m.template block<0,1>(0,0);
63 VERIFY(prod.size()==1);
64 VERIFY(prod.isZero());
65 }
66 }
67
zeroSizedVector()68 template<typename VectorType> void zeroSizedVector()
69 {
70 VectorType t1;
71
72 if (VectorType::SizeAtCompileTime == Dynamic || VectorType::SizeAtCompileTime==0)
73 {
74 zeroReduction(t1);
75 VERIFY(t1.size() == 0);
76 VectorType t2(DenseIndex(0)); // DenseIndex disambiguates with 0-the-null-pointer (error with gcc 4.4 and MSVC8)
77 VERIFY(t2.size() == 0);
78 zeroReduction(t2);
79
80 VERIFY(t1==t2);
81 }
82 }
83
test_zerosized()84 void test_zerosized()
85 {
86 zeroSizedMatrix<Matrix2d>();
87 zeroSizedMatrix<Matrix3i>();
88 zeroSizedMatrix<Matrix<float, 2, Dynamic> >();
89 zeroSizedMatrix<MatrixXf>();
90 zeroSizedMatrix<Matrix<float, 0, 0> >();
91 zeroSizedMatrix<Matrix<float, Dynamic, 0, 0, 0, 0> >();
92 zeroSizedMatrix<Matrix<float, 0, Dynamic, 0, 0, 0> >();
93 zeroSizedMatrix<Matrix<float, Dynamic, Dynamic, 0, 0, 0> >();
94 zeroSizedMatrix<Matrix<float, 0, 4> >();
95 zeroSizedMatrix<Matrix<float, 4, 0> >();
96
97 zeroSizedVector<Vector2d>();
98 zeroSizedVector<Vector3i>();
99 zeroSizedVector<VectorXf>();
100 zeroSizedVector<Matrix<float, 0, 1> >();
101 zeroSizedVector<Matrix<float, 1, 0> >();
102 }
103