Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 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 | // |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 5 | // 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 Guennebaud | e3fac69 | 2008-06-08 15:03:23 +0000 | [diff] [blame] | 26 | // this hack is needed to make this file compiles with -pedantic (gcc) |
Gael Guennebaud | 60726f9 | 2008-06-04 10:15:48 +0000 | [diff] [blame] | 27 | #define throw(X) |
Gael Guennebaud | e3fac69 | 2008-06-08 15:03:23 +0000 | [diff] [blame] | 28 | // discard vectorization since operator new is not called in that case |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 29 | #define EIGEN_DONT_VECTORIZE 1 |
| 30 | #include "main.h" |
| 31 | |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 32 | void* operator new[] (size_t n) |
Gael Guennebaud | f52d119 | 2008-09-03 00:32:56 +0000 | [diff] [blame] | 33 | { |
| 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 Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 41 | |
| 42 | void operator delete[](void* p) throw() |
| 43 | { |
| 44 | free(p); |
| 45 | } |
| 46 | |
| 47 | template<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 Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 58 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 59 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 60 | m3(rows, cols), |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 61 | mzero = MatrixType::Zero(rows, cols), |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 62 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 63 | ::Identity(rows, rows), |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 64 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 65 | ::Random(rows, rows); |
| 66 | VectorType v1 = VectorType::Random(rows), |
| 67 | v2 = VectorType::Random(rows), |
| 68 | vzero = VectorType::Zero(rows); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 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))); |
Benoit Jacob | f5791ee | 2008-07-08 00:49:10 +0000 | [diff] [blame] | 77 | VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 78 | VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2)); |
| 79 | } |
| 80 | |
| 81 | void test_nomalloc() |
| 82 | { |
| 83 | // check that our operator new is indeed called: |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 84 | VERIFY_RAISES_ASSERT(MatrixXd dummy = MatrixXd::Random(3,3)); |
Gael Guennebaud | f2ebbee | 2008-06-02 14:54:52 +0000 | [diff] [blame] | 85 | CALL_SUBTEST( nomalloc(Matrix<float, 1, 1>()) ); |
| 86 | CALL_SUBTEST( nomalloc(Matrix4d()) ); |
| 87 | } |