You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.3 KiB

  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. // this hack is needed to make this file compiles with -pedantic (gcc)
  11. #ifdef __GNUC__
  12. #define throw(X)
  13. #endif
  14. // discard stack allocation as that too bypasses malloc
  15. #define EIGEN_STACK_ALLOCATION_LIMIT 0
  16. // any heap allocation will raise an assert
  17. #define EIGEN_NO_MALLOC
  18. #include "main.h"
  19. template<typename MatrixType> void nomalloc(const MatrixType& m)
  20. {
  21. /* this test check no dynamic memory allocation are issued with fixed-size matrices
  22. */
  23. typedef typename MatrixType::Scalar Scalar;
  24. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  25. int rows = m.rows();
  26. int cols = m.cols();
  27. MatrixType m1 = MatrixType::Random(rows, cols),
  28. m2 = MatrixType::Random(rows, cols),
  29. m3(rows, cols),
  30. mzero = MatrixType::Zero(rows, cols),
  31. identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  32. ::Identity(rows, rows),
  33. square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
  34. ::Random(rows, rows);
  35. VectorType v1 = VectorType::Random(rows),
  36. v2 = VectorType::Random(rows),
  37. vzero = VectorType::Zero(rows);
  38. Scalar s1 = ei_random<Scalar>();
  39. int r = ei_random<int>(0, rows-1),
  40. c = ei_random<int>(0, cols-1);
  41. VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2);
  42. VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c)));
  43. VERIFY_IS_APPROX(m1.cwise() * m1.block(0,0,rows,cols), m1.cwise() * m1);
  44. VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
  45. }
  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. }