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