1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
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/.
11 // Various sanity tests with exceptions:
12 // - no memory leak when a custom scalar type trow an exceptions
13 // - todo: complete the list of tests!
15 #define EIGEN_STACK_ALLOCATION_LIMIT 100000000
25 class ScalarWithExceptions
28 ScalarWithExceptions() { init(); }
29 ScalarWithExceptions(const float& _v
) { init(); *v
= _v
; }
30 ScalarWithExceptions(const ScalarWithExceptions
& other
) { init(); *v
= *(other
.v
); }
31 ~ScalarWithExceptions() {
41 ScalarWithExceptions
operator+(const ScalarWithExceptions
& other
) const
46 return ScalarWithExceptions(*v
+*other
.v
);
49 ScalarWithExceptions
operator-(const ScalarWithExceptions
& other
) const
50 { return ScalarWithExceptions(*v
-*other
.v
); }
52 ScalarWithExceptions
operator*(const ScalarWithExceptions
& other
) const
53 { return ScalarWithExceptions((*v
)*(*other
.v
)); }
55 ScalarWithExceptions
& operator+=(const ScalarWithExceptions
& other
)
56 { *v
+=*other
.v
; return *this; }
57 ScalarWithExceptions
& operator-=(const ScalarWithExceptions
& other
)
58 { *v
-=*other
.v
; return *this; }
59 ScalarWithExceptions
& operator=(const ScalarWithExceptions
& other
)
60 { *v
= *(other
.v
); return *this; }
62 bool operator==(const ScalarWithExceptions
& other
) const
63 { return *v
==*other
.v
; }
64 bool operator!=(const ScalarWithExceptions
& other
) const
65 { return *v
!=*other
.v
; }
72 ScalarWithExceptions
real(const ScalarWithExceptions
&x
) { return x
; }
73 ScalarWithExceptions
imag(const ScalarWithExceptions
& ) { return 0; }
74 ScalarWithExceptions
conj(const ScalarWithExceptions
&x
) { return x
; }
76 int ScalarWithExceptions::instances
= 0;
77 int ScalarWithExceptions::countdown
= 0;
80 #define CHECK_MEMLEAK(OP) { \
81 ScalarWithExceptions::countdown = 100; \
82 int before = ScalarWithExceptions::instances; \
83 bool exception_thrown = false; \
85 catch (my_exception) { \
86 exception_thrown = true; \
87 VERIFY(ScalarWithExceptions::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
89 VERIFY(exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)); \
94 typedef Eigen::Matrix
<ScalarWithExceptions
,Dynamic
,1> VectorType
;
95 typedef Eigen::Matrix
<ScalarWithExceptions
,Dynamic
,Dynamic
> MatrixType
;
99 VectorType
v0(n
), v1(n
);
100 MatrixType
m0(n
,n
), m1(n
,n
), m2(n
,n
);
101 v0
.setOnes(); v1
.setOnes();
102 m0
.setOnes(); m1
.setOnes(); m2
.setOnes();
103 CHECK_MEMLEAK(v0
= m0
* m1
* v1
);
104 CHECK_MEMLEAK(m2
= m0
* m1
* m2
);
105 CHECK_MEMLEAK((v0
+v1
).dot(v0
+v1
));
107 VERIFY(ScalarWithExceptions::instances
==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP
)); \
110 void test_exceptions()
112 CALL_SUBTEST( memoryleak() );