cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

cxx11_maxsizevector.cpp (1839B)


      1 #include "main.h"
      2 
      3 #include <exception>  // std::exception
      4 
      5 #include <unsupported/Eigen/CXX11/Tensor>
      6 
      7 struct Foo
      8 {
      9   static Index object_count;
     10   static Index object_limit;
     11   EIGEN_ALIGN_TO_BOUNDARY(128) int dummy;
     12 
     13   Foo(int x=0) : dummy(x)
     14   {
     15 #ifdef EIGEN_EXCEPTIONS
     16     // TODO: Is this the correct way to handle this?
     17     if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); }
     18 #endif
     19     std::cout << '+';
     20     ++Foo::object_count;
     21     eigen_assert((internal::UIntPtr(this) & (127)) == 0);
     22   }
     23   Foo(const Foo&)
     24   {
     25     std::cout << 'c';
     26     ++Foo::object_count;
     27     eigen_assert((internal::UIntPtr(this) & (127)) == 0);
     28   }
     29 
     30   ~Foo()
     31   {
     32     std::cout << '~';
     33     --Foo::object_count;
     34   }
     35 
     36   class Fail : public std::exception {};
     37 };
     38 
     39 Index Foo::object_count = 0;
     40 Index Foo::object_limit = 0;
     41 
     42 
     43 
     44 EIGEN_DECLARE_TEST(cxx11_maxsizevector)
     45 {
     46   typedef MaxSizeVector<Foo> VectorX;
     47   Foo::object_count = 0;
     48   for(int r = 0; r < g_repeat; r++) {
     49     Index rows = internal::random<Index>(3,30);
     50     Foo::object_limit = internal::random<Index>(0, rows - 2);
     51     std::cout << "object_limit = " << Foo::object_limit << std::endl;
     52     bool exception_raised = false;
     53 #ifdef EIGEN_EXCEPTIONS
     54     try
     55     {
     56 #endif
     57       std::cout <<       "\nVectorX m(" << rows << ");\n";
     58       VectorX vect(rows);
     59       for(int i=0; i<rows; ++i)
     60           vect.push_back(Foo());
     61 #ifdef EIGEN_EXCEPTIONS
     62       VERIFY(false);  // not reached if exceptions are enabled
     63     }
     64     catch (const Foo::Fail&) { exception_raised = true; }
     65     VERIFY(exception_raised);
     66 #endif
     67     VERIFY_IS_EQUAL(Index(0), Foo::object_count);
     68 
     69     {
     70       Foo::object_limit = rows+1;
     71       VectorX vect2(rows, Foo());
     72       VERIFY_IS_EQUAL(Foo::object_count, rows);
     73     }
     74     VERIFY_IS_EQUAL(Index(0), Foo::object_count);
     75     std::cout << '\n';
     76   }
     77 }