exceptions.cpp
Go to the documentation of this file.
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 
10 
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!
14 
15 #define EIGEN_STACK_ALLOCATION_LIMIT 100000000
16 
17 #include "main.h"
18 
20 {
23 };
24 
26 {
27  public:
29  ScalarWithExceptions(const float& _v) { init(); *v = _v; }
30  ScalarWithExceptions(const ScalarWithExceptions& other) { init(); *v = *(other.v); }
32  delete v;
33  instances--;
34  }
35 
36  void init() {
37  v = new float;
38  instances++;
39  }
40 
42  {
43  countdown--;
44  if(countdown<=0)
45  throw my_exception();
46  return ScalarWithExceptions(*v+*other.v);
47  }
48 
50  { return ScalarWithExceptions(*v-*other.v); }
51 
53  { return ScalarWithExceptions((*v)*(*other.v)); }
54 
56  { *v+=*other.v; return *this; }
58  { *v-=*other.v; return *this; }
60  { *v = *(other.v); return *this; }
61 
62  bool operator==(const ScalarWithExceptions& other) const
63  { return *v==*other.v; }
64  bool operator!=(const ScalarWithExceptions& other) const
65  { return *v!=*other.v; }
66 
67  float* v;
68  static int instances;
69  static int countdown;
70 };
71 
75 
78 
79 
80 #define CHECK_MEMLEAK(OP) { \
81  ScalarWithExceptions::countdown = 100; \
82  int before = ScalarWithExceptions::instances; \
83  bool exception_thrown = false; \
84  try { OP; } \
85  catch (my_exception) { \
86  exception_thrown = true; \
87  VERIFY(ScalarWithExceptions::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
88  } \
89  VERIFY(exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)); \
90  }
91 
92 void memoryleak()
93 {
96 
97  {
98  int n = 50;
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));
106  }
107  VERIFY(ScalarWithExceptions::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP)); \
108 }
109 
111 {
113 }
ScalarWithExceptions operator*(const ScalarWithExceptions &other) const
Definition: exceptions.cpp:52
ScalarWithExceptions real(const ScalarWithExceptions &x)
Definition: exceptions.cpp:72
#define EIGEN_MAKESTRING(a)
Definition: Macros.h:485
Vector v1
ScalarWithExceptions(const ScalarWithExceptions &other)
Definition: exceptions.cpp:30
void memoryleak()
Definition: exceptions.cpp:92
MatrixType m2(n_dims)
ArrayXcf v
Definition: Cwise_arg.cpp:1
int n
MatrixXf MatrixType
ScalarWithExceptions(const float &_v)
Definition: exceptions.cpp:29
ScalarWithExceptions operator-(const ScalarWithExceptions &other) const
Definition: exceptions.cpp:49
ScalarWithExceptions imag(const ScalarWithExceptions &)
Definition: exceptions.cpp:73
ScalarWithExceptions & operator+=(const ScalarWithExceptions &other)
Definition: exceptions.cpp:55
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Matrix3d m1
Definition: IOFormat.cpp:2
ScalarWithExceptions operator+(const ScalarWithExceptions &other) const
Definition: exceptions.cpp:41
bool operator==(const ScalarWithExceptions &other) const
Definition: exceptions.cpp:62
ScalarWithExceptions & operator-=(const ScalarWithExceptions &other)
Definition: exceptions.cpp:57
static int instances
Definition: exceptions.cpp:68
bool operator!=(const ScalarWithExceptions &other) const
Definition: exceptions.cpp:64
void test_exceptions()
Definition: exceptions.cpp:110
#define CALL_SUBTEST(FUNC)
Definition: main.h:342
#define VERIFY(a)
Definition: main.h:325
static int countdown
Definition: exceptions.cpp:69
static const double v0
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1460
ScalarWithExceptions & operator=(const ScalarWithExceptions &other)
Definition: exceptions.cpp:59
#define CHECK_MEMLEAK(OP)
Definition: exceptions.cpp:80
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
ScalarWithExceptions conj(const ScalarWithExceptions &x)
Definition: exceptions.cpp:74


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:02