aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/exceptions.cpp
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2018-07-18 23:27:37 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2018-07-18 23:27:37 +0200
commit053ed97c72543d513e92cdef1b14839844664c34 (patch)
tree5438b919d9cf5e1bc483ec4a665690ca3f490d96 /test/exceptions.cpp
parenta503fc87254aca625d7b128a2260599d40dc0889 (diff)
Generalize ScalarWithExceptions to a full non-copyable and trowing scalar type to be used in other unit tests.
Diffstat (limited to 'test/exceptions.cpp')
-rw-r--r--test/exceptions.cpp90
1 files changed, 13 insertions, 77 deletions
diff --git a/test/exceptions.cpp b/test/exceptions.cpp
index 182082e39..3d93060ab 100644
--- a/test/exceptions.cpp
+++ b/test/exceptions.cpp
@@ -8,93 +8,34 @@
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
-// Various sanity tests with exceptions:
+// Various sanity tests with exceptions and non trivially copyable scalar type.
// - no memory leak when a custom scalar type trow an exceptions
// - todo: complete the list of tests!
#define EIGEN_STACK_ALLOCATION_LIMIT 100000000
#include "main.h"
-
-struct my_exception
-{
- my_exception() {}
- ~my_exception() {}
-};
-
-class ScalarWithExceptions
-{
- public:
- ScalarWithExceptions() { init(); }
- ScalarWithExceptions(const float& _v) { init(); *v = _v; }
- ScalarWithExceptions(const ScalarWithExceptions& other) { init(); *v = *(other.v); }
- ~ScalarWithExceptions() {
- delete v;
- instances--;
- }
-
- void init() {
- v = new float;
- instances++;
- }
-
- ScalarWithExceptions operator+(const ScalarWithExceptions& other) const
- {
- countdown--;
- if(countdown<=0)
- throw my_exception();
- return ScalarWithExceptions(*v+*other.v);
- }
-
- ScalarWithExceptions operator-(const ScalarWithExceptions& other) const
- { return ScalarWithExceptions(*v-*other.v); }
-
- ScalarWithExceptions operator*(const ScalarWithExceptions& other) const
- { return ScalarWithExceptions((*v)*(*other.v)); }
-
- ScalarWithExceptions& operator+=(const ScalarWithExceptions& other)
- { *v+=*other.v; return *this; }
- ScalarWithExceptions& operator-=(const ScalarWithExceptions& other)
- { *v-=*other.v; return *this; }
- ScalarWithExceptions& operator=(const ScalarWithExceptions& other)
- { *v = *(other.v); return *this; }
-
- bool operator==(const ScalarWithExceptions& other) const
- { return *v==*other.v; }
- bool operator!=(const ScalarWithExceptions& other) const
- { return *v!=*other.v; }
-
- float* v;
- static int instances;
- static int countdown;
-};
-
-ScalarWithExceptions real(const ScalarWithExceptions &x) { return x; }
-ScalarWithExceptions imag(const ScalarWithExceptions & ) { return 0; }
-ScalarWithExceptions conj(const ScalarWithExceptions &x) { return x; }
-
-int ScalarWithExceptions::instances = 0;
-int ScalarWithExceptions::countdown = 0;
-
+#include "AnnoyingScalar.h"
#define CHECK_MEMLEAK(OP) { \
- ScalarWithExceptions::countdown = 100; \
- int before = ScalarWithExceptions::instances; \
- bool exception_thrown = false; \
- try { OP; } \
+ AnnoyingScalar::countdown = 100; \
+ int before = AnnoyingScalar::instances; \
+ bool exception_thrown = false; \
+ try { OP; } \
catch (my_exception) { \
exception_thrown = true; \
- VERIFY(ScalarWithExceptions::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
+ VERIFY(AnnoyingScalar::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
} \
- VERIFY(exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)); \
+ VERIFY( (AnnoyingScalar::dont_throw) || (exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)) ); \
}
-void memoryleak()
+EIGEN_DECLARE_TEST(exceptions)
{
- typedef Eigen::Matrix<ScalarWithExceptions,Dynamic,1> VectorType;
- typedef Eigen::Matrix<ScalarWithExceptions,Dynamic,Dynamic> MatrixType;
+ typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType;
+ typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType;
{
+ AnnoyingScalar::dont_throw = false;
int n = 50;
VectorType v0(n), v1(n);
MatrixType m0(n,n), m1(n,n), m2(n,n);
@@ -104,10 +45,5 @@ void memoryleak()
CHECK_MEMLEAK(m2 = m0 * m1 * m2);
CHECK_MEMLEAK((v0+v1).dot(v0+v1));
}
- VERIFY(ScalarWithExceptions::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP)); \
-}
-
-EIGEN_DECLARE_TEST(exceptions)
-{
- CALL_SUBTEST( memoryleak() );
+ VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP));
}