blob: d9653588d883d9a5e1285f98c55c891ad1e01376 [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//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
5//
6// Eigen is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License as published by the Free Software Foundation; either
9// version 3 of the License, or (at your option) any later version.
10//
11// Alternatively, you can redistribute it and/or
12// modify it under the terms of the GNU General Public License as
13// published by the Free Software Foundation; either version 2 of
14// the License, or (at your option) any later version.
15//
16// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
24
Gael Guennebaude3fac692008-06-08 15:03:23 +000025// this hack is needed to make this file compiles with -pedantic (gcc)
Gael Guennebaud60726f92008-06-04 10:15:48 +000026#define throw(X)
Gael Guennebaude3fac692008-06-08 15:03:23 +000027// discard vectorization since operator new is not called in that case
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000028#define EIGEN_DONT_VECTORIZE 1
29#include "main.h"
30
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000031void* operator new[] (size_t n)
32 {
33 ei_assert(false && "operator new should never be called with fixed size path");
34 // the following is in case assertion are disabled
35 std::cerr << "operator new should never be called with fixed size path" << std::endl;
36 exit(2);
37 void* p = malloc(n);
38 return p;
39 }
40
41void operator delete[](void* p) throw()
42{
43 free(p);
44}
45
46template<typename MatrixType> void nomalloc(const MatrixType& m)
47{
48 /* this test check no dynamic memory allocation are issued with fixed-size matrices
49 */
50
51 typedef typename MatrixType::Scalar Scalar;
52 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
53
54 int rows = m.rows();
55 int cols = m.cols();
56
57 // this test relies a lot on Random.h, and there's not much more that we can do
58 // to test it, hence I consider that we will have tested Random.h
59 MatrixType m1 = MatrixType::random(rows, cols),
60 m2 = MatrixType::random(rows, cols),
61 m3(rows, cols),
62 mzero = MatrixType::zero(rows, cols),
63 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
64 ::identity(rows, rows),
65 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
66 ::random(rows, rows);
67 VectorType v1 = VectorType::random(rows),
68 v2 = VectorType::random(rows),
69 vzero = VectorType::zero(rows);
70
71 Scalar s1 = ei_random<Scalar>();
72
73 int r = ei_random<int>(0, rows-1),
74 c = ei_random<int>(0, cols-1);
75
76 VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
77 VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
Benoit Jacobf5791ee2008-07-08 00:49:10 +000078 VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
Gael Guennebaudf2ebbee2008-06-02 14:54:52 +000079 VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
80}
81
82void test_nomalloc()
83{
84 // check that our operator new is indeed called:
85 VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::random(3,3));
86 CALL_SUBTEST( nomalloc(Matrix<float, 1, 1>()) );
87 CALL_SUBTEST( nomalloc(Matrix4d()) );
88}