blob: 277e9863055aab8af5cb57bed73ac9717b8a996d [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
25#define EIGEN_DONT_VECTORIZE 1
26#include "main.h"
27
28// #define CUSTOM_NEW_DELETE(TYPE)
29
30void* operator new[] (size_t n)
31 {
32 ei_assert(false && "operator new should never be called with fixed size path");
33 // the following is in case assertion are disabled
34 std::cerr << "operator new should never be called with fixed size path" << std::endl;
35 exit(2);
36 void* p = malloc(n);
37 return p;
38 }
39
40void operator delete[](void* p) throw()
41{
42 free(p);
43}
44
45template<typename MatrixType> void nomalloc(const MatrixType& m)
46{
47 /* this test check no dynamic memory allocation are issued with fixed-size matrices
48 */
49
50 typedef typename MatrixType::Scalar Scalar;
51 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
52
53 int rows = m.rows();
54 int cols = m.cols();
55
56 // this test relies a lot on Random.h, and there's not much more that we can do
57 // to test it, hence I consider that we will have tested Random.h
58 MatrixType m1 = MatrixType::random(rows, cols),
59 m2 = MatrixType::random(rows, cols),
60 m3(rows, cols),
61 mzero = MatrixType::zero(rows, cols),
62 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
63 ::identity(rows, rows),
64 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
65 ::random(rows, rows);
66 VectorType v1 = VectorType::random(rows),
67 v2 = VectorType::random(rows),
68 vzero = VectorType::zero(rows);
69
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)));
77 VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1));
78 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:
84 VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::random(3,3));
85 CALL_SUBTEST( nomalloc(Matrix<float, 1, 1>()) );
86 CALL_SUBTEST( nomalloc(Matrix4d()) );
87}