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