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.

69 lines
1.7 KiB

  1. #include "main.h"
  2. #include <exception> // std::exception
  3. struct Foo
  4. {
  5. static Index object_count;
  6. static Index object_limit;
  7. int dummy;
  8. Foo()
  9. {
  10. #ifdef EIGEN_EXCEPTIONS
  11. // TODO: Is this the correct way to handle this?
  12. if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); }
  13. #endif
  14. std::cout << '+';
  15. ++Foo::object_count;
  16. }
  17. ~Foo()
  18. {
  19. std::cout << '-';
  20. --Foo::object_count;
  21. }
  22. class Fail : public std::exception {};
  23. };
  24. Index Foo::object_count = 0;
  25. Index Foo::object_limit = 0;
  26. #undef EIGEN_TEST_MAX_SIZE
  27. #define EIGEN_TEST_MAX_SIZE 3
  28. void test_ctorleak()
  29. {
  30. typedef Matrix<Foo, Dynamic, Dynamic> MatrixX;
  31. typedef Matrix<Foo, Dynamic, 1> VectorX;
  32. Foo::object_count = 0;
  33. for(int i = 0; i < g_repeat; i++) {
  34. Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
  35. Foo::object_limit = internal::random<Index>(0, rows*cols - 2);
  36. std::cout << "object_limit =" << Foo::object_limit << std::endl;
  37. #ifdef EIGEN_EXCEPTIONS
  38. try
  39. {
  40. #endif
  41. std::cout << "\nMatrixX m(" << rows << ", " << cols << ");\n";
  42. MatrixX m(rows, cols);
  43. #ifdef EIGEN_EXCEPTIONS
  44. VERIFY(false); // not reached if exceptions are enabled
  45. }
  46. catch (const Foo::Fail&) { /* ignore */ }
  47. #endif
  48. VERIFY_IS_EQUAL(Index(0), Foo::object_count);
  49. {
  50. Foo::object_limit = (rows+1)*(cols+1);
  51. MatrixX A(rows, cols);
  52. VERIFY_IS_EQUAL(Foo::object_count, rows*cols);
  53. VectorX v=A.row(0);
  54. VERIFY_IS_EQUAL(Foo::object_count, (rows+1)*cols);
  55. v = A.col(0);
  56. VERIFY_IS_EQUAL(Foo::object_count, rows*(cols+1));
  57. }
  58. VERIFY_IS_EQUAL(Index(0), Foo::object_count);
  59. }
  60. }