blob: 78bb12a8aa9c580f7c1bc11ae8e5a40a61859643 [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>
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +00005// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
6//
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)
Gael Guennebaude3fac692008-06-08 15:03:23 +000028// discard vectorization since operator new is not called in that case
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000029#define EIGEN_DONT_VECTORIZE 1
30#include "main.h"
31
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000032void* operator new[] (size_t n)
Gael Guennebaudf52d1192008-09-03 00:32:56 +000033{
34 ei_assert(false && "operator new should never be called with fixed size path");
35 // the following is in case assertion are disabled
36 std::cerr << "operator new should never be called with fixed size path" << std::endl;
37 exit(2);
38 void* p = malloc(n);
39 return p;
40}
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000041
42void operator delete[](void* p) throw()
43{
44 free(p);
45}
46
47template<typename MatrixType> void nomalloc(const MatrixType& m)
48{
49 /* this test check no dynamic memory allocation are issued with fixed-size matrices
50 */
51
52 typedef typename MatrixType::Scalar Scalar;
53 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
54
55 int rows = m.rows();
56 int cols = m.cols();
57
Gael Guennebaudc10f0692008-07-21 00:34:46 +000058 MatrixType m1 = MatrixType::Random(rows, cols),
59 m2 = MatrixType::Random(rows, cols),
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000060 m3(rows, cols),
Gael Guennebaudc10f0692008-07-21 00:34:46 +000061 mzero = MatrixType::Zero(rows, cols),
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000062 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Gael Guennebaudc10f0692008-07-21 00:34:46 +000063 ::Identity(rows, rows),
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000064 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Gael Guennebaudc10f0692008-07-21 00:34:46 +000065 ::Random(rows, rows);
66 VectorType v1 = VectorType::Random(rows),
67 v2 = VectorType::Random(rows),
68 vzero = VectorType::Zero(rows);
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000069
70 Scalar s1 = ei_random<Scalar>();
71
72 int r = ei_random<int>(0, rows-1),
73 c = ei_random<int>(0, cols-1);
74
75 VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
76 VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
Benoit Jacobf5791ee2008-07-08 00:49:10 +000077 VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000078 VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
79}
80
81void test_nomalloc()
82{
83 // check that our operator new is indeed called:
Gael Guennebaudc10f0692008-07-21 00:34:46 +000084 VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::Random(3,3));
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000085 CALL_SUBTEST( nomalloc(Matrix<float, 1, 1>()) );
86 CALL_SUBTEST( nomalloc(Matrix4d()) );
87}