Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #include <cstdlib>
     12 #include <cerrno>
     13 #include <ctime>
     14 #include <iostream>
     15 #include <fstream>
     16 #include <string>
     17 #include <sstream>
     18 #include <vector>
     19 #include <typeinfo>
     20 
     21 // The following includes of STL headers have to be done _before_ the
     22 // definition of macros min() and max().  The reason is that many STL
     23 // implementations will not work properly as the min and max symbols collide
     24 // with the STL functions std:min() and std::max().  The STL headers may check
     25 // for the macro definition of min/max and issue a warning or undefine the
     26 // macros.
     27 //
     28 // Still, Windows defines min() and max() in windef.h as part of the regular
     29 // Windows system interfaces and many other Windows APIs depend on these
     30 // macros being available.  To prevent the macro expansion of min/max and to
     31 // make Eigen compatible with the Windows environment all function calls of
     32 // std::min() and std::max() have to be written with parenthesis around the
     33 // function name.
     34 //
     35 // All STL headers used by Eigen should be included here.  Because main.h is
     36 // included before any Eigen header and because the STL headers are guarded
     37 // against multiple inclusions, no STL header will see our own min/max macro
     38 // definitions.
     39 #include <limits>
     40 #include <algorithm>
     41 #include <complex>
     42 #include <deque>
     43 #include <queue>
     44 #include <cassert>
     45 #include <list>
     46 #if __cplusplus >= 201103L
     47 #include <random>
     48 #ifdef EIGEN_USE_THREADS
     49 #include <future>
     50 #endif
     51 #endif
     52 
     53 // To test that all calls from Eigen code to std::min() and std::max() are
     54 // protected by parenthesis against macro expansion, the min()/max() macros
     55 // are defined here and any not-parenthesized min/max call will cause a
     56 // compiler error.
     57 #define min(A,B) please_protect_your_min_with_parentheses
     58 #define max(A,B) please_protect_your_max_with_parentheses
     59 #define isnan(X) please_protect_your_isnan_with_parentheses
     60 #define isinf(X) please_protect_your_isinf_with_parentheses
     61 #define isfinite(X) please_protect_your_isfinite_with_parentheses
     62 #ifdef M_PI
     63 #undef M_PI
     64 #endif
     65 #define M_PI please_use_EIGEN_PI_instead_of_M_PI
     66 
     67 #define FORBIDDEN_IDENTIFIER (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes
     68 // B0 is defined in POSIX header termios.h
     69 #define B0 FORBIDDEN_IDENTIFIER
     70 
     71 // Unit tests calling Eigen's blas library must preserve the default blocking size
     72 // to avoid troubles.
     73 #ifndef EIGEN_NO_DEBUG_SMALL_PRODUCT_BLOCKS
     74 #define EIGEN_DEBUG_SMALL_PRODUCT_BLOCKS
     75 #endif
     76 
     77 // shuts down ICC's remark #593: variable "XXX" was set but never used
     78 #define TEST_SET_BUT_UNUSED_VARIABLE(X) EIGEN_UNUSED_VARIABLE(X)
     79 
     80 #ifdef TEST_ENABLE_TEMPORARY_TRACKING
     81 
     82 static long int nb_temporaries;
     83 static long int nb_temporaries_on_assert = -1;
     84 
     85 inline void on_temporary_creation(long int size) {
     86   // here's a great place to set a breakpoint when debugging failures in this test!
     87   if(size!=0) nb_temporaries++;
     88   if(nb_temporaries_on_assert>0) assert(nb_temporaries<nb_temporaries_on_assert);
     89 }
     90 
     91 #define EIGEN_DENSE_STORAGE_CTOR_PLUGIN { on_temporary_creation(size); }
     92 
     93 #define VERIFY_EVALUATION_COUNT(XPR,N) {\
     94     nb_temporaries = 0; \
     95     XPR; \
     96     if(nb_temporaries!=N) { std::cerr << "nb_temporaries == " << nb_temporaries << "\n"; }\
     97     VERIFY( (#XPR) && nb_temporaries==N ); \
     98   }
     99 
    100 #endif
    101 
    102 // the following file is automatically generated by cmake
    103 #include "split_test_helper.h"
    104 
    105 #ifdef NDEBUG
    106 #undef NDEBUG
    107 #endif
    108 
    109 // On windows CE, NDEBUG is automatically defined <assert.h> if NDEBUG is not defined.
    110 #ifndef DEBUG
    111 #define DEBUG
    112 #endif
    113 
    114 // bounds integer values for AltiVec
    115 #if defined(__ALTIVEC__) || defined(__VSX__)
    116 #define EIGEN_MAKING_DOCS
    117 #endif
    118 
    119 #ifndef EIGEN_TEST_FUNC
    120 #error EIGEN_TEST_FUNC must be defined
    121 #endif
    122 
    123 #define DEFAULT_REPEAT 10
    124 
    125 namespace Eigen
    126 {
    127   static std::vector<std::string> g_test_stack;
    128   // level == 0 <=> abort if test fail
    129   // level >= 1 <=> warning message to std::cerr if test fail
    130   static int g_test_level = 0;
    131   static int g_repeat;
    132   static unsigned int g_seed;
    133   static bool g_has_set_repeat, g_has_set_seed;
    134 }
    135 
    136 #define TRACK std::cerr << __FILE__ << " " << __LINE__ << std::endl
    137 // #define TRACK while()
    138 
    139 #define EI_PP_MAKE_STRING2(S) #S
    140 #define EI_PP_MAKE_STRING(S) EI_PP_MAKE_STRING2(S)
    141 
    142 #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, "  ", "\n", "", "", "", "")
    143 
    144 #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__)
    145   #define EIGEN_EXCEPTIONS
    146 #endif
    147 
    148 #ifndef EIGEN_NO_ASSERTION_CHECKING
    149 
    150   namespace Eigen
    151   {
    152     static const bool should_raise_an_assert = false;
    153 
    154     // Used to avoid to raise two exceptions at a time in which
    155     // case the exception is not properly caught.
    156     // This may happen when a second exceptions is triggered in a destructor.
    157     static bool no_more_assert = false;
    158     static bool report_on_cerr_on_assert_failure = true;
    159 
    160     struct eigen_assert_exception
    161     {
    162       eigen_assert_exception(void) {}
    163       ~eigen_assert_exception() { Eigen::no_more_assert = false; }
    164     };
    165   }
    166   // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while
    167   // one should have been, then the list of excecuted assertions is printed out.
    168   //
    169   // EIGEN_DEBUG_ASSERTS is not enabled by default as it
    170   // significantly increases the compilation time
    171   // and might even introduce side effects that would hide
    172   // some memory errors.
    173   #ifdef EIGEN_DEBUG_ASSERTS
    174 
    175     namespace Eigen
    176     {
    177       namespace internal
    178       {
    179         static bool push_assert = false;
    180       }
    181       static std::vector<std::string> eigen_assert_list;
    182     }
    183     #define eigen_assert(a)                       \
    184       if( (!(a)) && (!no_more_assert) )     \
    185       { \
    186         if(report_on_cerr_on_assert_failure) \
    187           std::cerr <<  #a << " " __FILE__ << "(" << __LINE__ << ")\n"; \
    188         Eigen::no_more_assert = true;       \
    189         EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
    190       }                                     \
    191       else if (Eigen::internal::push_assert)       \
    192       {                                     \
    193         eigen_assert_list.push_back(std::string(EI_PP_MAKE_STRING(__FILE__) " (" EI_PP_MAKE_STRING(__LINE__) ") : " #a) ); \
    194       }
    195 
    196     #ifdef EIGEN_EXCEPTIONS
    197     #define VERIFY_RAISES_ASSERT(a)                                                   \
    198       {                                                                               \
    199         Eigen::no_more_assert = false;                                                \
    200         Eigen::eigen_assert_list.clear();                                             \
    201         Eigen::internal::push_assert = true;                                          \
    202         Eigen::report_on_cerr_on_assert_failure = false;                              \
    203         try {                                                                         \
    204           a;                                                                          \
    205           std::cerr << "One of the following asserts should have been triggered:\n";  \
    206           for (uint ai=0 ; ai<eigen_assert_list.size() ; ++ai)                        \
    207             std::cerr << "  " << eigen_assert_list[ai] << "\n";                       \
    208           VERIFY(Eigen::should_raise_an_assert && # a);                               \
    209         } catch (Eigen::eigen_assert_exception) {                                     \
    210           Eigen::internal::push_assert = false; VERIFY(true);                         \
    211         }                                                                             \
    212         Eigen::report_on_cerr_on_assert_failure = true;                               \
    213         Eigen::internal::push_assert = false;                                         \
    214       }
    215     #endif //EIGEN_EXCEPTIONS
    216 
    217   #elif !defined(__CUDACC__) // EIGEN_DEBUG_ASSERTS
    218     // see bug 89. The copy_bool here is working around a bug in gcc <= 4.3
    219     #define eigen_assert(a) \
    220       if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\
    221       {                                       \
    222         Eigen::no_more_assert = true;         \
    223         if(report_on_cerr_on_assert_failure)  \
    224           eigen_plain_assert(a);              \
    225         else                                  \
    226           EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
    227       }
    228     #ifdef EIGEN_EXCEPTIONS
    229       #define VERIFY_RAISES_ASSERT(a) {                           \
    230         Eigen::no_more_assert = false;                            \
    231         Eigen::report_on_cerr_on_assert_failure = false;          \
    232         try {                                                     \
    233           a;                                                      \
    234           VERIFY(Eigen::should_raise_an_assert && # a);           \
    235         }                                                         \
    236         catch (Eigen::eigen_assert_exception&) { VERIFY(true); }  \
    237         Eigen::report_on_cerr_on_assert_failure = true;           \
    238       }
    239     #endif //EIGEN_EXCEPTIONS
    240   #endif // EIGEN_DEBUG_ASSERTS
    241 
    242 #ifndef VERIFY_RAISES_ASSERT
    243   #define VERIFY_RAISES_ASSERT(a) \
    244     std::cout << "Can't VERIFY_RAISES_ASSERT( " #a " ) with exceptions disabled\n";
    245 #endif
    246 
    247   #if !defined(__CUDACC__)
    248   #define EIGEN_USE_CUSTOM_ASSERT
    249   #endif
    250 
    251 #else // EIGEN_NO_ASSERTION_CHECKING
    252 
    253   #define VERIFY_RAISES_ASSERT(a) {}
    254 
    255 #endif // EIGEN_NO_ASSERTION_CHECKING
    256 
    257 
    258 #define EIGEN_INTERNAL_DEBUGGING
    259 #include <Eigen/QR> // required for createRandomPIMatrixOfRank
    260 
    261 inline void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string)
    262 {
    263   if (!condition)
    264   {
    265     if(Eigen::g_test_level>0)
    266       std::cerr << "WARNING: ";
    267     std::cerr << "Test " << testname << " failed in " << file << " (" << line << ")"
    268       << std::endl << "    " << condition_as_string << std::endl;
    269     std::cerr << "Stack:\n";
    270     const int test_stack_size = static_cast<int>(Eigen::g_test_stack.size());
    271     for(int i=test_stack_size-1; i>=0; --i)
    272       std::cerr << "  - " << Eigen::g_test_stack[i] << "\n";
    273     std::cerr << "\n";
    274     if(Eigen::g_test_level==0)
    275       abort();
    276   }
    277 }
    278 
    279 #define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EI_PP_MAKE_STRING(a))
    280 
    281 #define VERIFY_GE(a, b) ::verify_impl(a >= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EI_PP_MAKE_STRING(a >= b))
    282 #define VERIFY_LE(a, b) ::verify_impl(a <= b, g_test_stack.back().c_str(), __FILE__, __LINE__, EI_PP_MAKE_STRING(a <= b))
    283 
    284 
    285 #define VERIFY_IS_EQUAL(a, b) VERIFY(test_is_equal(a, b, true))
    286 #define VERIFY_IS_NOT_EQUAL(a, b) VERIFY(test_is_equal(a, b, false))
    287 #define VERIFY_IS_APPROX(a, b) VERIFY(verifyIsApprox(a, b))
    288 #define VERIFY_IS_NOT_APPROX(a, b) VERIFY(!test_isApprox(a, b))
    289 #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) VERIFY(test_isMuchSmallerThan(a, b))
    290 #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b))
    291 #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b))
    292 #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b))
    293 
    294 #define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a))
    295 
    296 #define CALL_SUBTEST(FUNC) do { \
    297     g_test_stack.push_back(EI_PP_MAKE_STRING(FUNC)); \
    298     FUNC; \
    299     g_test_stack.pop_back(); \
    300   } while (0)
    301 
    302 
    303 namespace Eigen {
    304 
    305 template<typename T> inline typename NumTraits<T>::Real test_precision() { return NumTraits<T>::dummy_precision(); }
    306 template<> inline float test_precision<float>() { return 1e-3f; }
    307 template<> inline double test_precision<double>() { return 1e-6; }
    308 template<> inline long double test_precision<long double>() { return 1e-6l; }
    309 template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); }
    310 template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); }
    311 template<> inline long double test_precision<std::complex<long double> >() { return test_precision<long double>(); }
    312 
    313 inline bool test_isApprox(const short& a, const short& b)
    314 { return internal::isApprox(a, b, test_precision<short>()); }
    315 inline bool test_isApprox(const unsigned short& a, const unsigned short& b)
    316 { return internal::isApprox(a, b, test_precision<unsigned long>()); }
    317 inline bool test_isApprox(const unsigned int& a, const unsigned int& b)
    318 { return internal::isApprox(a, b, test_precision<unsigned int>()); }
    319 inline bool test_isApprox(const long& a, const long& b)
    320 { return internal::isApprox(a, b, test_precision<long>()); }
    321 inline bool test_isApprox(const unsigned long& a, const unsigned long& b)
    322 { return internal::isApprox(a, b, test_precision<unsigned long>()); }
    323 
    324 inline bool test_isApprox(const int& a, const int& b)
    325 { return internal::isApprox(a, b, test_precision<int>()); }
    326 inline bool test_isMuchSmallerThan(const int& a, const int& b)
    327 { return internal::isMuchSmallerThan(a, b, test_precision<int>()); }
    328 inline bool test_isApproxOrLessThan(const int& a, const int& b)
    329 { return internal::isApproxOrLessThan(a, b, test_precision<int>()); }
    330 
    331 inline bool test_isApprox(const float& a, const float& b)
    332 { return internal::isApprox(a, b, test_precision<float>()); }
    333 inline bool test_isMuchSmallerThan(const float& a, const float& b)
    334 { return internal::isMuchSmallerThan(a, b, test_precision<float>()); }
    335 inline bool test_isApproxOrLessThan(const float& a, const float& b)
    336 { return internal::isApproxOrLessThan(a, b, test_precision<float>()); }
    337 
    338 inline bool test_isApprox(const double& a, const double& b)
    339 { return internal::isApprox(a, b, test_precision<double>()); }
    340 inline bool test_isMuchSmallerThan(const double& a, const double& b)
    341 { return internal::isMuchSmallerThan(a, b, test_precision<double>()); }
    342 inline bool test_isApproxOrLessThan(const double& a, const double& b)
    343 { return internal::isApproxOrLessThan(a, b, test_precision<double>()); }
    344 
    345 #ifndef EIGEN_TEST_NO_COMPLEX
    346 inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
    347 { return internal::isApprox(a, b, test_precision<std::complex<float> >()); }
    348 inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
    349 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
    350 
    351 inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
    352 { return internal::isApprox(a, b, test_precision<std::complex<double> >()); }
    353 inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
    354 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
    355 
    356 #ifndef EIGEN_TEST_NO_LONGDOUBLE
    357 inline bool test_isApprox(const std::complex<long double>& a, const std::complex<long double>& b)
    358 { return internal::isApprox(a, b, test_precision<std::complex<long double> >()); }
    359 inline bool test_isMuchSmallerThan(const std::complex<long double>& a, const std::complex<long double>& b)
    360 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<long double> >()); }
    361 #endif
    362 #endif
    363 
    364 #ifndef EIGEN_TEST_NO_LONGDOUBLE
    365 inline bool test_isApprox(const long double& a, const long double& b)
    366 {
    367     bool ret = internal::isApprox(a, b, test_precision<long double>());
    368     if (!ret) std::cerr
    369         << std::endl << "    actual   = " << a
    370         << std::endl << "    expected = " << b << std::endl << std::endl;
    371     return ret;
    372 }
    373 
    374 inline bool test_isMuchSmallerThan(const long double& a, const long double& b)
    375 { return internal::isMuchSmallerThan(a, b, test_precision<long double>()); }
    376 inline bool test_isApproxOrLessThan(const long double& a, const long double& b)
    377 { return internal::isApproxOrLessThan(a, b, test_precision<long double>()); }
    378 #endif // EIGEN_TEST_NO_LONGDOUBLE
    379 
    380 inline bool test_isApprox(const half& a, const half& b)
    381 { return internal::isApprox(a, b, test_precision<half>()); }
    382 inline bool test_isMuchSmallerThan(const half& a, const half& b)
    383 { return internal::isMuchSmallerThan(a, b, test_precision<half>()); }
    384 inline bool test_isApproxOrLessThan(const half& a, const half& b)
    385 { return internal::isApproxOrLessThan(a, b, test_precision<half>()); }
    386 
    387 // test_relative_error returns the relative difference between a and b as a real scalar as used in isApprox.
    388 template<typename T1,typename T2>
    389 typename NumTraits<typename T1::RealScalar>::NonInteger test_relative_error(const EigenBase<T1> &a, const EigenBase<T2> &b)
    390 {
    391   using std::sqrt;
    392   typedef typename NumTraits<typename T1::RealScalar>::NonInteger RealScalar;
    393   typename internal::nested_eval<T1,2>::type ea(a.derived());
    394   typename internal::nested_eval<T2,2>::type eb(b.derived());
    395   return sqrt(RealScalar((ea-eb).cwiseAbs2().sum()) / RealScalar((std::min)(eb.cwiseAbs2().sum(),ea.cwiseAbs2().sum())));
    396 }
    397 
    398 template<typename T1,typename T2>
    399 typename T1::RealScalar test_relative_error(const T1 &a, const T2 &b, const typename T1::Coefficients* = 0)
    400 {
    401   return test_relative_error(a.coeffs(), b.coeffs());
    402 }
    403 
    404 template<typename T1,typename T2>
    405 typename T1::Scalar test_relative_error(const T1 &a, const T2 &b, const typename T1::MatrixType* = 0)
    406 {
    407   return test_relative_error(a.matrix(), b.matrix());
    408 }
    409 
    410 template<typename S, int D>
    411 S test_relative_error(const Translation<S,D> &a, const Translation<S,D> &b)
    412 {
    413   return test_relative_error(a.vector(), b.vector());
    414 }
    415 
    416 template <typename S, int D, int O>
    417 S test_relative_error(const ParametrizedLine<S,D,O> &a, const ParametrizedLine<S,D,O> &b)
    418 {
    419   return (std::max)(test_relative_error(a.origin(), b.origin()), test_relative_error(a.origin(), b.origin()));
    420 }
    421 
    422 template <typename S, int D>
    423 S test_relative_error(const AlignedBox<S,D> &a, const AlignedBox<S,D> &b)
    424 {
    425   return (std::max)(test_relative_error((a.min)(), (b.min)()), test_relative_error((a.max)(), (b.max)()));
    426 }
    427 
    428 template<typename Derived> class SparseMatrixBase;
    429 template<typename T1,typename T2>
    430 typename T1::RealScalar test_relative_error(const MatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
    431 {
    432   return test_relative_error(a,b.toDense());
    433 }
    434 
    435 template<typename Derived> class SparseMatrixBase;
    436 template<typename T1,typename T2>
    437 typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const MatrixBase<T2> &b)
    438 {
    439   return test_relative_error(a.toDense(),b);
    440 }
    441 
    442 template<typename Derived> class SparseMatrixBase;
    443 template<typename T1,typename T2>
    444 typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
    445 {
    446   return test_relative_error(a.toDense(),b.toDense());
    447 }
    448 
    449 template<typename T1,typename T2>
    450 typename NumTraits<typename NumTraits<T1>::Real>::NonInteger test_relative_error(const T1 &a, const T2 &b, typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T1>::Real>::value, T1>::type* = 0)
    451 {
    452   typedef typename NumTraits<typename NumTraits<T1>::Real>::NonInteger RealScalar;
    453   return numext::sqrt(RealScalar(numext::abs2(a-b))/RealScalar((numext::mini)(numext::abs2(a),numext::abs2(b))));
    454 }
    455 
    456 template<typename T>
    457 T test_relative_error(const Rotation2D<T> &a, const Rotation2D<T> &b)
    458 {
    459   return test_relative_error(a.angle(), b.angle());
    460 }
    461 
    462 template<typename T>
    463 T test_relative_error(const AngleAxis<T> &a, const AngleAxis<T> &b)
    464 {
    465   return (std::max)(test_relative_error(a.angle(), b.angle()), test_relative_error(a.axis(), b.axis()));
    466 }
    467 
    468 template<typename Type1, typename Type2>
    469 inline bool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
    470 {
    471   return a.isApprox(b, test_precision<typename Type1::Scalar>());
    472 }
    473 
    474 // get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions
    475 template<typename T>
    476 typename NumTraits<typename T::Scalar>::Real get_test_precision(const T&, const typename T::Scalar* = 0)
    477 {
    478   return test_precision<typename NumTraits<typename T::Scalar>::Real>();
    479 }
    480 
    481 template<typename T>
    482 typename NumTraits<T>::Real get_test_precision(const T&,typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>::type* = 0)
    483 {
    484   return test_precision<typename NumTraits<T>::Real>();
    485 }
    486 
    487 // verifyIsApprox is a wrapper to test_isApprox that outputs the relative difference magnitude if the test fails.
    488 template<typename Type1, typename Type2>
    489 inline bool verifyIsApprox(const Type1& a, const Type2& b)
    490 {
    491   bool ret = test_isApprox(a,b);
    492   if(!ret)
    493   {
    494     std::cerr << "Difference too large wrt tolerance " << get_test_precision(a)  << ", relative error is: " << test_relative_error(a,b) << std::endl;
    495   }
    496   return ret;
    497 }
    498 
    499 // The idea behind this function is to compare the two scalars a and b where
    500 // the scalar ref is a hint about the expected order of magnitude of a and b.
    501 // WARNING: the scalar a and b must be positive
    502 // Therefore, if for some reason a and b are very small compared to ref,
    503 // we won't issue a false negative.
    504 // This test could be: abs(a-b) <= eps * ref
    505 // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error.
    506 template<typename Scalar,typename ScalarRef>
    507 inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
    508 {
    509   return test_isApprox(a+ref, b+ref);
    510 }
    511 
    512 template<typename Derived1, typename Derived2>
    513 inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1,
    514                                    const MatrixBase<Derived2>& m2)
    515 {
    516   return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>());
    517 }
    518 
    519 template<typename Derived>
    520 inline bool test_isMuchSmallerThan(const MatrixBase<Derived>& m,
    521                                    const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s)
    522 {
    523   return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>());
    524 }
    525 
    526 template<typename Derived>
    527 inline bool test_isUnitary(const MatrixBase<Derived>& m)
    528 {
    529   return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>());
    530 }
    531 
    532 // Forward declaration to avoid ICC warning
    533 template<typename T, typename U>
    534 bool test_is_equal(const T& actual, const U& expected, bool expect_equal=true);
    535 
    536 template<typename T, typename U>
    537 bool test_is_equal(const T& actual, const U& expected, bool expect_equal)
    538 {
    539     if ((actual==expected) == expect_equal)
    540         return true;
    541     // false:
    542     std::cerr
    543         << "\n    actual   = " << actual
    544         << "\n    expected " << (expect_equal ? "= " : "!=") << expected << "\n\n";
    545     return false;
    546 }
    547 
    548 /** Creates a random Partial Isometry matrix of given rank.
    549   *
    550   * A partial isometry is a matrix all of whose singular values are either 0 or 1.
    551   * This is very useful to test rank-revealing algorithms.
    552   */
    553 // Forward declaration to avoid ICC warning
    554 template<typename MatrixType>
    555 void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m);
    556 template<typename MatrixType>
    557 void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m)
    558 {
    559   typedef typename internal::traits<MatrixType>::Scalar Scalar;
    560   enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
    561 
    562   typedef Matrix<Scalar, Dynamic, 1> VectorType;
    563   typedef Matrix<Scalar, Rows, Rows> MatrixAType;
    564   typedef Matrix<Scalar, Cols, Cols> MatrixBType;
    565 
    566   if(desired_rank == 0)
    567   {
    568     m.setZero(rows,cols);
    569     return;
    570   }
    571 
    572   if(desired_rank == 1)
    573   {
    574     // here we normalize the vectors to get a partial isometry
    575     m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose();
    576     return;
    577   }
    578 
    579   MatrixAType a = MatrixAType::Random(rows,rows);
    580   MatrixType d = MatrixType::Identity(rows,cols);
    581   MatrixBType  b = MatrixBType::Random(cols,cols);
    582 
    583   // set the diagonal such that only desired_rank non-zero entries reamain
    584   const Index diag_size = (std::min)(d.rows(),d.cols());
    585   if(diag_size != desired_rank)
    586     d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
    587 
    588   HouseholderQR<MatrixAType> qra(a);
    589   HouseholderQR<MatrixBType> qrb(b);
    590   m = qra.householderQ() * d * qrb.householderQ();
    591 }
    592 
    593 // Forward declaration to avoid ICC warning
    594 template<typename PermutationVectorType>
    595 void randomPermutationVector(PermutationVectorType& v, Index size);
    596 template<typename PermutationVectorType>
    597 void randomPermutationVector(PermutationVectorType& v, Index size)
    598 {
    599   typedef typename PermutationVectorType::Scalar Scalar;
    600   v.resize(size);
    601   for(Index i = 0; i < size; ++i) v(i) = Scalar(i);
    602   if(size == 1) return;
    603   for(Index n = 0; n < 3 * size; ++n)
    604   {
    605     Index i = internal::random<Index>(0, size-1);
    606     Index j;
    607     do j = internal::random<Index>(0, size-1); while(j==i);
    608     std::swap(v(i), v(j));
    609   }
    610 }
    611 
    612 template<typename T> bool isNotNaN(const T& x)
    613 {
    614   return x==x;
    615 }
    616 
    617 template<typename T> bool isPlusInf(const T& x)
    618 {
    619   return x > NumTraits<T>::highest();
    620 }
    621 
    622 template<typename T> bool isMinusInf(const T& x)
    623 {
    624   return x < NumTraits<T>::lowest();
    625 }
    626 
    627 } // end namespace Eigen
    628 
    629 template<typename T> struct GetDifferentType;
    630 
    631 template<> struct GetDifferentType<float> { typedef double type; };
    632 template<> struct GetDifferentType<double> { typedef float type; };
    633 template<typename T> struct GetDifferentType<std::complex<T> >
    634 { typedef std::complex<typename GetDifferentType<T>::type> type; };
    635 
    636 // Forward declaration to avoid ICC warning
    637 template<typename T> std::string type_name();
    638 template<typename T> std::string type_name()                    { return "other"; }
    639 template<> std::string type_name<float>()                       { return "float"; }
    640 template<> std::string type_name<double>()                      { return "double"; }
    641 template<> std::string type_name<long double>()                 { return "long double"; }
    642 template<> std::string type_name<int>()                         { return "int"; }
    643 template<> std::string type_name<std::complex<float> >()        { return "complex<float>"; }
    644 template<> std::string type_name<std::complex<double> >()       { return "complex<double>"; }
    645 template<> std::string type_name<std::complex<long double> >()  { return "complex<long double>"; }
    646 template<> std::string type_name<std::complex<int> >()          { return "complex<int>"; }
    647 
    648 // forward declaration of the main test function
    649 void EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
    650 
    651 using namespace Eigen;
    652 
    653 inline void set_repeat_from_string(const char *str)
    654 {
    655   errno = 0;
    656   g_repeat = int(strtoul(str, 0, 10));
    657   if(errno || g_repeat <= 0)
    658   {
    659     std::cout << "Invalid repeat value " << str << std::endl;
    660     exit(EXIT_FAILURE);
    661   }
    662   g_has_set_repeat = true;
    663 }
    664 
    665 inline void set_seed_from_string(const char *str)
    666 {
    667   errno = 0;
    668   g_seed = int(strtoul(str, 0, 10));
    669   if(errno || g_seed == 0)
    670   {
    671     std::cout << "Invalid seed value " << str << std::endl;
    672     exit(EXIT_FAILURE);
    673   }
    674   g_has_set_seed = true;
    675 }
    676 
    677 int main(int argc, char *argv[])
    678 {
    679     g_has_set_repeat = false;
    680     g_has_set_seed = false;
    681     bool need_help = false;
    682 
    683     for(int i = 1; i < argc; i++)
    684     {
    685       if(argv[i][0] == 'r')
    686       {
    687         if(g_has_set_repeat)
    688         {
    689           std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
    690           return 1;
    691         }
    692         set_repeat_from_string(argv[i]+1);
    693       }
    694       else if(argv[i][0] == 's')
    695       {
    696         if(g_has_set_seed)
    697         {
    698           std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
    699           return 1;
    700         }
    701          set_seed_from_string(argv[i]+1);
    702       }
    703       else
    704       {
    705         need_help = true;
    706       }
    707     }
    708 
    709     if(need_help)
    710     {
    711       std::cout << "This test application takes the following optional arguments:" << std::endl;
    712       std::cout << "  rN     Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
    713       std::cout << "  sN     Use N as seed for random numbers (default: based on current time)" << std::endl;
    714       std::cout << std::endl;
    715       std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
    716       std::cout << "will be used as default values for these parameters." << std::endl;
    717       return 1;
    718     }
    719 
    720     char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT");
    721     if(!g_has_set_repeat && env_EIGEN_REPEAT)
    722       set_repeat_from_string(env_EIGEN_REPEAT);
    723     char *env_EIGEN_SEED = getenv("EIGEN_SEED");
    724     if(!g_has_set_seed && env_EIGEN_SEED)
    725       set_seed_from_string(env_EIGEN_SEED);
    726 
    727     if(!g_has_set_seed) g_seed = (unsigned int) time(NULL);
    728     if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT;
    729 
    730     std::cout << "Initializing random number generator with seed " << g_seed << std::endl;
    731     std::stringstream ss;
    732     ss << "Seed: " << g_seed;
    733     g_test_stack.push_back(ss.str());
    734     srand(g_seed);
    735     std::cout << "Repeating each test " << g_repeat << " times" << std::endl;
    736 
    737     Eigen::g_test_stack.push_back(std::string(EI_PP_MAKE_STRING(EIGEN_TEST_FUNC)));
    738 
    739     EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
    740     return 0;
    741 }
    742 
    743 // These warning are disabled here such that they are still ON when parsing Eigen's header files.
    744 #if defined __INTEL_COMPILER
    745   // remark #383: value copied to temporary, reference to temporary used
    746   //  -> this warning is raised even for legal usage as: g_test_stack.push_back("foo"); where g_test_stack is a std::vector<std::string>
    747   // remark #1418: external function definition with no prior declaration
    748   //  -> this warning is raised for all our test functions. Declaring them static would fix the issue.
    749   // warning #279: controlling expression is constant
    750   // remark #1572: floating-point equality and inequality comparisons are unreliable
    751   #pragma warning disable 279 383 1418 1572
    752 #endif
    753 
    754 #ifdef _MSC_VER
    755   // 4503 - decorated name length exceeded, name was truncated
    756   #pragma warning( disable : 4503)
    757 #endif
    758