| 
									
										
										
										
											2014-07-06 12:58:13 +08:00
										 |  |  | #include "main.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <exception>  // std::exception
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Foo { | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |   static Index object_count; | 
					
						
							|  |  |  |   static Index object_limit; | 
					
						
							| 
									
										
										
										
											2014-07-06 12:58:13 +08:00
										 |  |  |   int dummy; | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 22:37:09 +08:00
										 |  |  |   Foo() : dummy(0) { | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | #ifdef EIGEN_EXCEPTIONS
 | 
					
						
							|  |  |  |     // TODO: Is this the correct way to handle this?
 | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |     if (Foo::object_count > Foo::object_limit) { | 
					
						
							|  |  |  |       std::cout << "\nThrow!\n"; | 
					
						
							|  |  |  |       throw Foo::Fail(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |     std::cout << '+'; | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  |     ++Foo::object_count; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ~Foo() { | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |     std::cout << '-'; | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  |     --Foo::object_count; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-06 12:58:13 +08:00
										 |  |  |   class Fail : public std::exception {}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  | Index Foo::object_count = 0; | 
					
						
							|  |  |  | Index Foo::object_limit = 0; | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  | #undef EIGEN_TEST_MAX_SIZE
 | 
					
						
							|  |  |  | #define EIGEN_TEST_MAX_SIZE 3
 | 
					
						
							| 
									
										
										
										
											2014-07-06 12:58:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 20:46:15 +08:00
										 |  |  | EIGEN_DECLARE_TEST(ctorleak) { | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |   typedef Matrix<Foo, Dynamic, Dynamic> MatrixX; | 
					
						
							|  |  |  |   typedef Matrix<Foo, Dynamic, 1> VectorX; | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  |   Foo::object_count = 0; | 
					
						
							| 
									
										
										
										
											2014-07-22 19:16:44 +08:00
										 |  |  |   for (int i = 0; i < g_repeat; i++) { | 
					
						
							|  |  |  |     Index rows = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE), | 
					
						
							|  |  |  |           cols = internal::random<Index>(2, EIGEN_TEST_MAX_SIZE); | 
					
						
							| 
									
										
										
										
											2018-11-23 22:37:09 +08:00
										 |  |  |     Foo::object_limit = rows * cols; | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       MatrixX r(rows, cols); | 
					
						
							|  |  |  |       Foo::object_limit = r.size() + internal::random<Index>(0, rows * cols - 2); | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |       std::cout << "object_limit =" << Foo::object_limit << std::endl; | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | #ifdef EIGEN_EXCEPTIONS
 | 
					
						
							| 
									
										
										
										
											2014-07-22 19:16:44 +08:00
										 |  |  |       try { | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-11-23 22:37:09 +08:00
										 |  |  |         if (internal::random<bool>()) { | 
					
						
							|  |  |  |           std::cout << "\nMatrixX m(" << rows << ", " << cols << ");\n"; | 
					
						
							|  |  |  |           MatrixX m(rows, cols); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           std::cout << "\nMatrixX m(r);\n"; | 
					
						
							|  |  |  |           MatrixX m(r); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | #ifdef EIGEN_EXCEPTIONS
 | 
					
						
							| 
									
										
										
										
											2014-07-22 19:16:44 +08:00
										 |  |  |         VERIFY(false);             // not reached if exceptions are enabled
 | 
					
						
							|  |  |  |       } catch (const Foo::Fail&) { /* ignore */ | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2014-07-19 05:19:56 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-11-23 22:37:09 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-04-16 19:25:20 +08:00
										 |  |  |     VERIFY_IS_EQUAL(Index(0), Foo::object_count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       Foo::object_limit = (rows + 1) * (cols + 1); | 
					
						
							|  |  |  |       MatrixX A(rows, cols); | 
					
						
							|  |  |  |       VERIFY_IS_EQUAL(Foo::object_count, rows * cols); | 
					
						
							|  |  |  |       VectorX v = A.row(0); | 
					
						
							|  |  |  |       VERIFY_IS_EQUAL(Foo::object_count, (rows + 1) * cols); | 
					
						
							|  |  |  |       v = A.col(0); | 
					
						
							|  |  |  |       VERIFY_IS_EQUAL(Foo::object_count, rows * (cols + 1)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(Index(0), Foo::object_count); | 
					
						
							| 
									
										
										
										
											2014-07-22 19:16:44 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-23 22:37:09 +08:00
										 |  |  |   std::cout << "\n"; | 
					
						
							| 
									
										
										
										
											2014-07-06 12:58:13 +08:00
										 |  |  | } |