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@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@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 
on_temporary_creation(long int size)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     {
eigen_assert_exceptioneigen_assert_exception162       eigen_assert_exception(void) {}
~eigen_assert_exceptioneigen_assert_exception163       ~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 
verify_impl(bool condition,const char * testname,const char * file,int line,const char * condition_as_string)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 
test_precision()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 
test_isApprox(const int & a,const int & b)313 inline bool test_isApprox(const int& a, const int& b)
314 { return internal::isApprox(a, b, test_precision<int>()); }
test_isMuchSmallerThan(const int & a,const int & b)315 inline bool test_isMuchSmallerThan(const int& a, const int& b)
316 { return internal::isMuchSmallerThan(a, b, test_precision<int>()); }
test_isApproxOrLessThan(const int & a,const int & b)317 inline bool test_isApproxOrLessThan(const int& a, const int& b)
318 { return internal::isApproxOrLessThan(a, b, test_precision<int>()); }
319 
test_isApprox(const float & a,const float & b)320 inline bool test_isApprox(const float& a, const float& b)
321 { return internal::isApprox(a, b, test_precision<float>()); }
test_isMuchSmallerThan(const float & a,const float & b)322 inline bool test_isMuchSmallerThan(const float& a, const float& b)
323 { return internal::isMuchSmallerThan(a, b, test_precision<float>()); }
test_isApproxOrLessThan(const float & a,const float & b)324 inline bool test_isApproxOrLessThan(const float& a, const float& b)
325 { return internal::isApproxOrLessThan(a, b, test_precision<float>()); }
326 
test_isApprox(const double & a,const double & b)327 inline bool test_isApprox(const double& a, const double& b)
328 { return internal::isApprox(a, b, test_precision<double>()); }
test_isMuchSmallerThan(const double & a,const double & b)329 inline bool test_isMuchSmallerThan(const double& a, const double& b)
330 { return internal::isMuchSmallerThan(a, b, test_precision<double>()); }
test_isApproxOrLessThan(const double & a,const double & b)331 inline bool test_isApproxOrLessThan(const double& a, const double& b)
332 { return internal::isApproxOrLessThan(a, b, test_precision<double>()); }
333 
334 #ifndef EIGEN_TEST_NO_COMPLEX
test_isApprox(const std::complex<float> & a,const std::complex<float> & b)335 inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
336 { return internal::isApprox(a, b, test_precision<std::complex<float> >()); }
test_isMuchSmallerThan(const std::complex<float> & a,const std::complex<float> & b)337 inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b)
338 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); }
339 
test_isApprox(const std::complex<double> & a,const std::complex<double> & b)340 inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
341 { return internal::isApprox(a, b, test_precision<std::complex<double> >()); }
test_isMuchSmallerThan(const std::complex<double> & a,const std::complex<double> & b)342 inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b)
343 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); }
344 
345 #ifndef EIGEN_TEST_NO_LONGDOUBLE
test_isApprox(const std::complex<long double> & a,const std::complex<long double> & b)346 inline bool test_isApprox(const std::complex<long double>& a, const std::complex<long double>& b)
347 { return internal::isApprox(a, b, test_precision<std::complex<long double> >()); }
test_isMuchSmallerThan(const std::complex<long double> & a,const std::complex<long double> & b)348 inline bool test_isMuchSmallerThan(const std::complex<long double>& a, const std::complex<long double>& b)
349 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<long double> >()); }
350 #endif
351 #endif
352 
353 #ifndef EIGEN_TEST_NO_LONGDOUBLE
test_isApprox(const long double & a,const long double & b)354 inline bool test_isApprox(const long double& a, const long double& b)
355 {
356     bool ret = internal::isApprox(a, b, test_precision<long double>());
357     if (!ret) std::cerr
358         << std::endl << "    actual   = " << a
359         << std::endl << "    expected = " << b << std::endl << std::endl;
360     return ret;
361 }
362 
test_isMuchSmallerThan(const long double & a,const long double & b)363 inline bool test_isMuchSmallerThan(const long double& a, const long double& b)
364 { return internal::isMuchSmallerThan(a, b, test_precision<long double>()); }
test_isApproxOrLessThan(const long double & a,const long double & b)365 inline bool test_isApproxOrLessThan(const long double& a, const long double& b)
366 { return internal::isApproxOrLessThan(a, b, test_precision<long double>()); }
367 #endif // EIGEN_TEST_NO_LONGDOUBLE
368 
test_isApprox(const half & a,const half & b)369 inline bool test_isApprox(const half& a, const half& b)
370 { return internal::isApprox(a, b, test_precision<half>()); }
test_isMuchSmallerThan(const half & a,const half & b)371 inline bool test_isMuchSmallerThan(const half& a, const half& b)
372 { return internal::isMuchSmallerThan(a, b, test_precision<half>()); }
test_isApproxOrLessThan(const half & a,const half & b)373 inline bool test_isApproxOrLessThan(const half& a, const half& b)
374 { return internal::isApproxOrLessThan(a, b, test_precision<half>()); }
375 
376 // test_relative_error returns the relative difference between a and b as a real scalar as used in isApprox.
377 template<typename T1,typename T2>
test_relative_error(const EigenBase<T1> & a,const EigenBase<T2> & b)378 typename NumTraits<typename T1::RealScalar>::NonInteger test_relative_error(const EigenBase<T1> &a, const EigenBase<T2> &b)
379 {
380   using std::sqrt;
381   typedef typename NumTraits<typename T1::RealScalar>::NonInteger RealScalar;
382   typename internal::nested_eval<T1,2>::type ea(a.derived());
383   typename internal::nested_eval<T2,2>::type eb(b.derived());
384   return sqrt(RealScalar((ea-eb).cwiseAbs2().sum()) / RealScalar((std::min)(eb.cwiseAbs2().sum(),ea.cwiseAbs2().sum())));
385 }
386 
387 template<typename T1,typename T2>
388 typename T1::RealScalar test_relative_error(const T1 &a, const T2 &b, const typename T1::Coefficients* = 0)
389 {
390   return test_relative_error(a.coeffs(), b.coeffs());
391 }
392 
393 template<typename T1,typename T2>
394 typename T1::Scalar test_relative_error(const T1 &a, const T2 &b, const typename T1::MatrixType* = 0)
395 {
396   return test_relative_error(a.matrix(), b.matrix());
397 }
398 
399 template<typename S, int D>
test_relative_error(const Translation<S,D> & a,const Translation<S,D> & b)400 S test_relative_error(const Translation<S,D> &a, const Translation<S,D> &b)
401 {
402   return test_relative_error(a.vector(), b.vector());
403 }
404 
405 template <typename S, int D, int O>
test_relative_error(const ParametrizedLine<S,D,O> & a,const ParametrizedLine<S,D,O> & b)406 S test_relative_error(const ParametrizedLine<S,D,O> &a, const ParametrizedLine<S,D,O> &b)
407 {
408   return (std::max)(test_relative_error(a.origin(), b.origin()), test_relative_error(a.origin(), b.origin()));
409 }
410 
411 template <typename S, int D>
test_relative_error(const AlignedBox<S,D> & a,const AlignedBox<S,D> & b)412 S test_relative_error(const AlignedBox<S,D> &a, const AlignedBox<S,D> &b)
413 {
414   return (std::max)(test_relative_error((a.min)(), (b.min)()), test_relative_error((a.max)(), (b.max)()));
415 }
416 
417 template<typename Derived> class SparseMatrixBase;
418 template<typename T1,typename T2>
test_relative_error(const MatrixBase<T1> & a,const SparseMatrixBase<T2> & b)419 typename T1::RealScalar test_relative_error(const MatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
420 {
421   return test_relative_error(a,b.toDense());
422 }
423 
424 template<typename Derived> class SparseMatrixBase;
425 template<typename T1,typename T2>
test_relative_error(const SparseMatrixBase<T1> & a,const MatrixBase<T2> & b)426 typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const MatrixBase<T2> &b)
427 {
428   return test_relative_error(a.toDense(),b);
429 }
430 
431 template<typename Derived> class SparseMatrixBase;
432 template<typename T1,typename T2>
test_relative_error(const SparseMatrixBase<T1> & a,const SparseMatrixBase<T2> & b)433 typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
434 {
435   return test_relative_error(a.toDense(),b.toDense());
436 }
437 
438 template<typename T1,typename T2>
439 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)
440 {
441   typedef typename NumTraits<typename NumTraits<T1>::Real>::NonInteger RealScalar;
442   return numext::sqrt(RealScalar(numext::abs2(a-b))/RealScalar((numext::mini)(numext::abs2(a),numext::abs2(b))));
443 }
444 
445 template<typename T>
test_relative_error(const Rotation2D<T> & a,const Rotation2D<T> & b)446 T test_relative_error(const Rotation2D<T> &a, const Rotation2D<T> &b)
447 {
448   return test_relative_error(a.angle(), b.angle());
449 }
450 
451 template<typename T>
test_relative_error(const AngleAxis<T> & a,const AngleAxis<T> & b)452 T test_relative_error(const AngleAxis<T> &a, const AngleAxis<T> &b)
453 {
454   return (std::max)(test_relative_error(a.angle(), b.angle()), test_relative_error(a.axis(), b.axis()));
455 }
456 
457 template<typename Type1, typename Type2>
458 inline bool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
459 {
460   return a.isApprox(b, test_precision<typename Type1::Scalar>());
461 }
462 
463 // get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions
464 template<typename T>
465 typename NumTraits<typename T::Scalar>::Real get_test_precision(const T&, const typename T::Scalar* = 0)
466 {
467   return test_precision<typename NumTraits<typename T::Scalar>::Real>();
468 }
469 
470 template<typename T>
471 typename NumTraits<T>::Real get_test_precision(const T&,typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>::type* = 0)
472 {
473   return test_precision<typename NumTraits<T>::Real>();
474 }
475 
476 // verifyIsApprox is a wrapper to test_isApprox that outputs the relative difference magnitude if the test fails.
477 template<typename Type1, typename Type2>
verifyIsApprox(const Type1 & a,const Type2 & b)478 inline bool verifyIsApprox(const Type1& a, const Type2& b)
479 {
480   bool ret = test_isApprox(a,b);
481   if(!ret)
482   {
483     std::cerr << "Difference too large wrt tolerance " << get_test_precision(a)  << ", relative error is: " << test_relative_error(a,b) << std::endl;
484   }
485   return ret;
486 }
487 
488 // The idea behind this function is to compare the two scalars a and b where
489 // the scalar ref is a hint about the expected order of magnitude of a and b.
490 // WARNING: the scalar a and b must be positive
491 // Therefore, if for some reason a and b are very small compared to ref,
492 // we won't issue a false negative.
493 // This test could be: abs(a-b) <= eps * ref
494 // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error.
495 template<typename Scalar,typename ScalarRef>
test_isApproxWithRef(const Scalar & a,const Scalar & b,const ScalarRef & ref)496 inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
497 {
498   return test_isApprox(a+ref, b+ref);
499 }
500 
501 template<typename Derived1, typename Derived2>
test_isMuchSmallerThan(const MatrixBase<Derived1> & m1,const MatrixBase<Derived2> & m2)502 inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1,
503                                    const MatrixBase<Derived2>& m2)
504 {
505   return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>());
506 }
507 
508 template<typename Derived>
test_isMuchSmallerThan(const MatrixBase<Derived> & m,const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real & s)509 inline bool test_isMuchSmallerThan(const MatrixBase<Derived>& m,
510                                    const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s)
511 {
512   return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>());
513 }
514 
515 template<typename Derived>
test_isUnitary(const MatrixBase<Derived> & m)516 inline bool test_isUnitary(const MatrixBase<Derived>& m)
517 {
518   return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>());
519 }
520 
521 // Forward declaration to avoid ICC warning
522 template<typename T, typename U>
523 bool test_is_equal(const T& actual, const U& expected, bool expect_equal=true);
524 
525 template<typename T, typename U>
test_is_equal(const T & actual,const U & expected,bool expect_equal)526 bool test_is_equal(const T& actual, const U& expected, bool expect_equal)
527 {
528     if ((actual==expected) == expect_equal)
529         return true;
530     // false:
531     std::cerr
532         << "\n    actual   = " << actual
533         << "\n    expected " << (expect_equal ? "= " : "!=") << expected << "\n\n";
534     return false;
535 }
536 
537 /** Creates a random Partial Isometry matrix of given rank.
538   *
539   * A partial isometry is a matrix all of whose singular values are either 0 or 1.
540   * This is very useful to test rank-revealing algorithms.
541   */
542 // Forward declaration to avoid ICC warning
543 template<typename MatrixType>
544 void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m);
545 template<typename MatrixType>
createRandomPIMatrixOfRank(Index desired_rank,Index rows,Index cols,MatrixType & m)546 void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m)
547 {
548   typedef typename internal::traits<MatrixType>::Scalar Scalar;
549   enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
550 
551   typedef Matrix<Scalar, Dynamic, 1> VectorType;
552   typedef Matrix<Scalar, Rows, Rows> MatrixAType;
553   typedef Matrix<Scalar, Cols, Cols> MatrixBType;
554 
555   if(desired_rank == 0)
556   {
557     m.setZero(rows,cols);
558     return;
559   }
560 
561   if(desired_rank == 1)
562   {
563     // here we normalize the vectors to get a partial isometry
564     m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose();
565     return;
566   }
567 
568   MatrixAType a = MatrixAType::Random(rows,rows);
569   MatrixType d = MatrixType::Identity(rows,cols);
570   MatrixBType  b = MatrixBType::Random(cols,cols);
571 
572   // set the diagonal such that only desired_rank non-zero entries reamain
573   const Index diag_size = (std::min)(d.rows(),d.cols());
574   if(diag_size != desired_rank)
575     d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank);
576 
577   HouseholderQR<MatrixAType> qra(a);
578   HouseholderQR<MatrixBType> qrb(b);
579   m = qra.householderQ() * d * qrb.householderQ();
580 }
581 
582 // Forward declaration to avoid ICC warning
583 template<typename PermutationVectorType>
584 void randomPermutationVector(PermutationVectorType& v, Index size);
585 template<typename PermutationVectorType>
randomPermutationVector(PermutationVectorType & v,Index size)586 void randomPermutationVector(PermutationVectorType& v, Index size)
587 {
588   typedef typename PermutationVectorType::Scalar Scalar;
589   v.resize(size);
590   for(Index i = 0; i < size; ++i) v(i) = Scalar(i);
591   if(size == 1) return;
592   for(Index n = 0; n < 3 * size; ++n)
593   {
594     Index i = internal::random<Index>(0, size-1);
595     Index j;
596     do j = internal::random<Index>(0, size-1); while(j==i);
597     std::swap(v(i), v(j));
598   }
599 }
600 
isNotNaN(const T & x)601 template<typename T> bool isNotNaN(const T& x)
602 {
603   return x==x;
604 }
605 
isPlusInf(const T & x)606 template<typename T> bool isPlusInf(const T& x)
607 {
608   return x > NumTraits<T>::highest();
609 }
610 
isMinusInf(const T & x)611 template<typename T> bool isMinusInf(const T& x)
612 {
613   return x < NumTraits<T>::lowest();
614 }
615 
616 } // end namespace Eigen
617 
618 template<typename T> struct GetDifferentType;
619 
620 template<> struct GetDifferentType<float> { typedef double type; };
621 template<> struct GetDifferentType<double> { typedef float type; };
622 template<typename T> struct GetDifferentType<std::complex<T> >
623 { typedef std::complex<typename GetDifferentType<T>::type> type; };
624 
625 // Forward declaration to avoid ICC warning
626 template<typename T> std::string type_name();
627 template<typename T> std::string type_name()                    { return "other"; }
628 template<> std::string type_name<float>()                       { return "float"; }
629 template<> std::string type_name<double>()                      { return "double"; }
630 template<> std::string type_name<long double>()                 { return "long double"; }
631 template<> std::string type_name<int>()                         { return "int"; }
632 template<> std::string type_name<std::complex<float> >()        { return "complex<float>"; }
633 template<> std::string type_name<std::complex<double> >()       { return "complex<double>"; }
634 template<> std::string type_name<std::complex<long double> >()  { return "complex<long double>"; }
635 template<> std::string type_name<std::complex<int> >()          { return "complex<int>"; }
636 
637 // forward declaration of the main test function
638 void EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
639 
640 using namespace Eigen;
641 
642 inline void set_repeat_from_string(const char *str)
643 {
644   errno = 0;
645   g_repeat = int(strtoul(str, 0, 10));
646   if(errno || g_repeat <= 0)
647   {
648     std::cout << "Invalid repeat value " << str << std::endl;
649     exit(EXIT_FAILURE);
650   }
651   g_has_set_repeat = true;
652 }
653 
654 inline void set_seed_from_string(const char *str)
655 {
656   errno = 0;
657   g_seed = int(strtoul(str, 0, 10));
658   if(errno || g_seed == 0)
659   {
660     std::cout << "Invalid seed value " << str << std::endl;
661     exit(EXIT_FAILURE);
662   }
663   g_has_set_seed = true;
664 }
665 
666 int main(int argc, char *argv[])
667 {
668     g_has_set_repeat = false;
669     g_has_set_seed = false;
670     bool need_help = false;
671 
672     for(int i = 1; i < argc; i++)
673     {
674       if(argv[i][0] == 'r')
675       {
676         if(g_has_set_repeat)
677         {
678           std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
679           return 1;
680         }
681         set_repeat_from_string(argv[i]+1);
682       }
683       else if(argv[i][0] == 's')
684       {
685         if(g_has_set_seed)
686         {
687           std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl;
688           return 1;
689         }
690          set_seed_from_string(argv[i]+1);
691       }
692       else
693       {
694         need_help = true;
695       }
696     }
697 
698     if(need_help)
699     {
700       std::cout << "This test application takes the following optional arguments:" << std::endl;
701       std::cout << "  rN     Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl;
702       std::cout << "  sN     Use N as seed for random numbers (default: based on current time)" << std::endl;
703       std::cout << std::endl;
704       std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl;
705       std::cout << "will be used as default values for these parameters." << std::endl;
706       return 1;
707     }
708 
709     char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT");
710     if(!g_has_set_repeat && env_EIGEN_REPEAT)
711       set_repeat_from_string(env_EIGEN_REPEAT);
712     char *env_EIGEN_SEED = getenv("EIGEN_SEED");
713     if(!g_has_set_seed && env_EIGEN_SEED)
714       set_seed_from_string(env_EIGEN_SEED);
715 
716     if(!g_has_set_seed) g_seed = (unsigned int) time(NULL);
717     if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT;
718 
719     std::cout << "Initializing random number generator with seed " << g_seed << std::endl;
720     std::stringstream ss;
721     ss << "Seed: " << g_seed;
722     g_test_stack.push_back(ss.str());
723     srand(g_seed);
724     std::cout << "Repeating each test " << g_repeat << " times" << std::endl;
725 
726     Eigen::g_test_stack.push_back(std::string(EI_PP_MAKE_STRING(EIGEN_TEST_FUNC)));
727 
728     EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
729     return 0;
730 }
731 
732 // These warning are disabled here such that they are still ON when parsing Eigen's header files.
733 #if defined __INTEL_COMPILER
734   // remark #383: value copied to temporary, reference to temporary used
735   //  -> 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>
736   // remark #1418: external function definition with no prior declaration
737   //  -> this warning is raised for all our test functions. Declaring them static would fix the issue.
738   // warning #279: controlling expression is constant
739   // remark #1572: floating-point equality and inequality comparisons are unreliable
740   #pragma warning disable 279 383 1418 1572
741 #endif
742 
743 #ifdef _MSC_VER
744   // 4503 - decorated name length exceeded, name was truncated
745   #pragma warning( disable : 4503)
746 #endif
747