blob: 82119e9b3f2ce1cf76d998ddf90d90201fe2f30c [file] [log] [blame]
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra. Eigen itself is part of the KDE project.
3//
Gael Guennebaudf52d1192008-09-03 00:32:56 +00004// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
Benoit Jacob00f89a82008-11-24 13:40:43 +00005// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +00006//
7// Eigen is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Lesser General Public
9// License as published by the Free Software Foundation; either
10// version 3 of the License, or (at your option) any later version.
11//
12// Alternatively, you can redistribute it and/or
13// modify it under the terms of the GNU General Public License as
14// published by the Free Software Foundation; either version 2 of
15// the License, or (at your option) any later version.
16//
17// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public
23// License and a copy of the GNU General Public License along with
24// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
Gael Guennebaude3fac692008-06-08 15:03:23 +000026// this hack is needed to make this file compiles with -pedantic (gcc)
Gael Guennebaud60726f92008-06-04 10:15:48 +000027#define throw(X)
Benoit Jacob1d52bd42009-01-08 15:20:21 +000028// discard stack allocation as that too bypasses malloc
Benoit Jacobd6712052008-12-31 00:25:31 +000029#define EIGEN_STACK_ALLOCATION_LIMIT 0
Benoit Jacob1d52bd42009-01-08 15:20:21 +000030// any heap allocation will raise an assert
31#define EIGEN_NO_MALLOC
Benoit Jacobd6712052008-12-31 00:25:31 +000032
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000033#include "main.h"
34
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000035template<typename MatrixType> void nomalloc(const MatrixType& m)
36{
37 /* this test check no dynamic memory allocation are issued with fixed-size matrices
38 */
39
40 typedef typename MatrixType::Scalar Scalar;
41 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
42
43 int rows = m.rows();
44 int cols = m.cols();
45
Gael Guennebaudc10f0692008-07-21 00:34:46 +000046 MatrixType m1 = MatrixType::Random(rows, cols),
47 m2 = MatrixType::Random(rows, cols),
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000048 m3(rows, cols),
Gael Guennebaudc10f0692008-07-21 00:34:46 +000049 mzero = MatrixType::Zero(rows, cols),
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000050 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Gael Guennebaudc10f0692008-07-21 00:34:46 +000051 ::Identity(rows, rows),
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000052 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Gael Guennebaudc10f0692008-07-21 00:34:46 +000053 ::Random(rows, rows);
54 VectorType v1 = VectorType::Random(rows),
55 v2 = VectorType::Random(rows),
56 vzero = VectorType::Zero(rows);
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000057
58 Scalar s1 = ei_random<Scalar>();
59
60 int r = ei_random<int>(0, rows-1),
61 c = ei_random<int>(0, cols-1);
62
63 VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
64 VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
Benoit Jacobf5791ee2008-07-08 00:49:10 +000065 VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000066 VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
67}
68
69void test_nomalloc()
70{
71 // check that our operator new is indeed called:
Gael Guennebaudc10f0692008-07-21 00:34:46 +000072 VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::Random(3,3));
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000073 CALL_SUBTEST( nomalloc(Matrix<float, 1, 1>()) );
74 CALL_SUBTEST( nomalloc(Matrix4d()) );
Benoit Jacobd6712052008-12-31 00:25:31 +000075 CALL_SUBTEST( nomalloc(Matrix<float,32,32>()) );
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000076}