blob: 3d93060aba2c31e028624b087cede4ffa6054c24 [file] [log] [blame]
Gael Guennebaud5541bcb2011-05-23 14:20:49 +02001// 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//
Benoit Jacob69124cf2012-07-13 14:42:47 -04006// 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/.
Gael Guennebaud5541bcb2011-05-23 14:20:49 +02009
10
Gael Guennebaud053ed972018-07-18 23:27:37 +020011// Various sanity tests with exceptions and non trivially copyable scalar type.
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020012// - 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"
Gael Guennebaud053ed972018-07-18 23:27:37 +020018#include "AnnoyingScalar.h"
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020019
20#define CHECK_MEMLEAK(OP) { \
Gael Guennebaud053ed972018-07-18 23:27:37 +020021 AnnoyingScalar::countdown = 100; \
22 int before = AnnoyingScalar::instances; \
23 bool exception_thrown = false; \
24 try { OP; } \
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020025 catch (my_exception) { \
26 exception_thrown = true; \
Gael Guennebaud053ed972018-07-18 23:27:37 +020027 VERIFY(AnnoyingScalar::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020028 } \
Gael Guennebaud053ed972018-07-18 23:27:37 +020029 VERIFY( (AnnoyingScalar::dont_throw) || (exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)) ); \
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020030 }
31
Gael Guennebaud053ed972018-07-18 23:27:37 +020032EIGEN_DECLARE_TEST(exceptions)
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020033{
Gael Guennebaud053ed972018-07-18 23:27:37 +020034 typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType;
35 typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType;
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020036
37 {
Gael Guennebaud053ed972018-07-18 23:27:37 +020038 AnnoyingScalar::dont_throw = false;
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020039 int n = 50;
40 VectorType v0(n), v1(n);
41 MatrixType m0(n,n), m1(n,n), m2(n,n);
42 v0.setOnes(); v1.setOnes();
43 m0.setOnes(); m1.setOnes(); m2.setOnes();
44 CHECK_MEMLEAK(v0 = m0 * m1 * v1);
45 CHECK_MEMLEAK(m2 = m0 * m1 * m2);
46 CHECK_MEMLEAK((v0+v1).dot(v0+v1));
47 }
Gael Guennebaud053ed972018-07-18 23:27:37 +020048 VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP));
Gael Guennebaud5541bcb2011-05-23 14:20:49 +020049}