• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra. Eigen itself is part of the KDE project.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 // this hack is needed to make this file compiles with -pedantic (gcc)
12 #ifdef __GNUC__
13 #define throw(X)
14 #endif
15 // discard stack allocation as that too bypasses malloc
16 #define EIGEN_STACK_ALLOCATION_LIMIT 0
17 // any heap allocation will raise an assert
18 #define EIGEN_NO_MALLOC
19 
20 #include "main.h"
21 
nomalloc(const MatrixType & m)22 template<typename MatrixType> void nomalloc(const MatrixType& m)
23 {
24   /* this test check no dynamic memory allocation are issued with fixed-size matrices
25   */
26 
27   typedef typename MatrixType::Scalar Scalar;
28 
29   int rows = m.rows();
30   int cols = m.cols();
31 
32   MatrixType m1 = MatrixType::Random(rows, cols),
33              m2 = MatrixType::Random(rows, cols);
34 
35   Scalar s1 = ei_random<Scalar>();
36 
37   int r = ei_random<int>(0, rows-1),
38       c = ei_random<int>(0, cols-1);
39 
40   VERIFY_IS_APPROX((m1+m2)*s1,              s1*m1+s1*m2);
41   VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
42   VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
43   VERIFY_IS_APPROX((m1*m1.transpose())*m2,  m1*(m1.transpose()*m2));
44 }
45 
test_eigen2_nomalloc()46 void test_eigen2_nomalloc()
47 {
48   // check that our operator new is indeed called:
49   VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::Random(3,3));
50   CALL_SUBTEST_1( nomalloc(Matrix<float, 1, 1>()) );
51   CALL_SUBTEST_2( nomalloc(Matrix4d()) );
52   CALL_SUBTEST_3( nomalloc(Matrix<float,32,32>()) );
53 }
54