exceptions.cpp (1916B)
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 and non trivially copyable scalar type. 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 #include "AnnoyingScalar.h" 19 20 #define CHECK_MEMLEAK(OP) { \ 21 AnnoyingScalar::countdown = 100; \ 22 int before = AnnoyingScalar::instances; \ 23 bool exception_thrown = false; \ 24 try { OP; } \ 25 catch (my_exception) { \ 26 exception_thrown = true; \ 27 VERIFY(AnnoyingScalar::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \ 28 } \ 29 VERIFY( (AnnoyingScalar::dont_throw) || (exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)) ); \ 30 } 31 32 EIGEN_DECLARE_TEST(exceptions) 33 { 34 typedef Eigen::Matrix<AnnoyingScalar,Dynamic,1> VectorType; 35 typedef Eigen::Matrix<AnnoyingScalar,Dynamic,Dynamic> MatrixType; 36 37 { 38 AnnoyingScalar::dont_throw = false; 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 } 48 VERIFY(AnnoyingScalar::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP)); 49 }