exceptions.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. // Various sanity tests with exceptions and non trivially copyable scalar type.
  10. // - no memory leak when a custom scalar type trow an exceptions
  11. // - todo: complete the list of tests!
  12. #define EIGEN_STACK_ALLOCATION_LIMIT 100000000
  13. #include "main.h"
  14. #include "AnnoyingScalar.h"
  15. #define CHECK_MEMLEAK(OP) { \
  16. AnnoyingScalar::countdown = 100; \
  17. int before = AnnoyingScalar::instances; \
  18. bool exception_thrown = false; \
  19. try { OP; } \
  20. catch (my_exception) { \
  21. exception_thrown = true; \
  22. VERIFY(AnnoyingScalar::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
  23. } \
  24. VERIFY( (AnnoyingScalar::dont_throw) || (exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)) ); \
  25. }
  26. EIGEN_DECLARE_TEST(exceptions)
  27. {
  28. typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType;
  29. typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType;
  30. {
  31. AnnoyingScalar::dont_throw = false;
  32. int n = 50;
  33. VectorType v0(n), v1(n);
  34. MatrixType m0(n,n), m1(n,n), m2(n,n);
  35. v0.setOnes(); v1.setOnes();
  36. m0.setOnes(); m1.setOnes(); m2.setOnes();
  37. CHECK_MEMLEAK(v0 = m0 * m1 * v1);
  38. CHECK_MEMLEAK(m2 = m0 * m1 * m2);
  39. CHECK_MEMLEAK((v0+v1).dot(v0+v1));
  40. }
  41. VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP));
  42. }