Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 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 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 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/. |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 9 | |
| 10 | |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 11 | // Various sanity tests with exceptions and non trivially copyable scalar type. |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 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" |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 18 | #include "AnnoyingScalar.h" |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 19 | |
| 20 | #define CHECK_MEMLEAK(OP) { \ |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 21 | AnnoyingScalar::countdown = 100; \ |
| 22 | int before = AnnoyingScalar::instances; \ |
| 23 | bool exception_thrown = false; \ |
| 24 | try { OP; } \ |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 25 | catch (my_exception) { \ |
| 26 | exception_thrown = true; \ |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 27 | VERIFY(AnnoyingScalar::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \ |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 28 | } \ |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 29 | VERIFY( (AnnoyingScalar::dont_throw) || (exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)) ); \ |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 32 | EIGEN_DECLARE_TEST(exceptions) |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 33 | { |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 34 | typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType; |
| 35 | typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType; |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 36 | |
| 37 | { |
Gael Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 38 | AnnoyingScalar::dont_throw = false; |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 39 | 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 Guennebaud | 053ed97 | 2018-07-18 23:27:37 +0200 | [diff] [blame^] | 48 | VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP)); |
Gael Guennebaud | 5541bcb | 2011-05-23 14:20:49 +0200 | [diff] [blame] | 49 | } |