blob: 8c868ee79e76509ffd1423fecdb3d4c07fb36a80 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// 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>
Austin Schuh189376f2018-12-20 22:11:15 +110044#include <cassert>
Brian Silverman72890c22015-09-19 14:37:37 -040045#include <list>
Austin Schuh189376f2018-12-20 22:11:15 +110046#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
Brian Silverman72890c22015-09-19 14:37:37 -040065
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
Austin Schuh189376f2018-12-20 22:11:15 +110072#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
Brian Silverman72890c22015-09-19 14:37:37 -040079
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
Austin Schuh189376f2018-12-20 22:11:15 +110084// 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
Brian Silverman72890c22015-09-19 14:37:37 -040089
90// shuts down ICC's remark #593: variable "XXX" was set but never used
Austin Schuh189376f2018-12-20 22:11:15 +110091#define TEST_SET_BUT_UNUSED_VARIABLE(X) EIGEN_UNUSED_VARIABLE(X)
92
93#ifdef TEST_ENABLE_TEMPORARY_TRACKING
94
95static long int nb_temporaries;
96static long int nb_temporaries_on_assert = -1;
97
98inline 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
Brian Silverman72890c22015-09-19 14:37:37 -0400114
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
Austin Schuh189376f2018-12-20 22:11:15 +1100128#if defined(__ALTIVEC__) || defined(__VSX__)
Brian Silverman72890c22015-09-19 14:37:37 -0400129#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
138namespace Eigen
139{
140 static std::vector<std::string> g_test_stack;
Austin Schuh189376f2018-12-20 22:11:15 +1100141 // level == 0 <=> abort if test fail
142 // level >= 1 <=> warning message to std::cerr if test fail
143 static int g_test_level = 0;
Brian Silverman72890c22015-09-19 14:37:37 -0400144 static int g_repeat;
145 static unsigned int g_seed;
146 static bool g_has_set_repeat, g_has_set_seed;
147}
148
Austin Schuh189376f2018-12-20 22:11:15 +1100149#define TRACK std::cerr << __FILE__ << " " << __LINE__ << std::endl
150// #define TRACK while()
151
Brian Silverman72890c22015-09-19 14:37:37 -0400152#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
Austin Schuh189376f2018-12-20 22:11:15 +1100157#if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__)
158 #define EIGEN_EXCEPTIONS
159#endif
160
Brian Silverman72890c22015-09-19 14:37:37 -0400161#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 {
175 eigen_assert_exception(void) {}
176 ~eigen_assert_exception() { Eigen::no_more_assert = false; }
177 };
Austin Schuh189376f2018-12-20 22:11:15 +1100178
179 struct eigen_static_assert_exception
180 {
181 eigen_static_assert_exception(void) {}
182 ~eigen_static_assert_exception() { Eigen::no_more_assert = false; }
183 };
Brian Silverman72890c22015-09-19 14:37:37 -0400184 }
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; \
Austin Schuh189376f2018-12-20 22:11:15 +1100208 EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
Brian Silverman72890c22015-09-19 14:37:37 -0400209 } \
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
Austin Schuh189376f2018-12-20 22:11:15 +1100215 #ifdef EIGEN_EXCEPTIONS
Brian Silverman72890c22015-09-19 14:37:37 -0400216 #define VERIFY_RAISES_ASSERT(a) \
217 { \
218 Eigen::no_more_assert = false; \
Austin Schuh189376f2018-12-20 22:11:15 +1100219 Eigen::eigen_assert_list.clear(); \
220 Eigen::internal::push_assert = true; \
Brian Silverman72890c22015-09-19 14:37:37 -0400221 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"; \
Austin Schuh189376f2018-12-20 22:11:15 +1100225 for (uint ai=0 ; ai<eigen_assert_list.size() ; ++ai) \
226 std::cerr << " " << eigen_assert_list[ai] << "\n"; \
Brian Silverman72890c22015-09-19 14:37:37 -0400227 VERIFY(Eigen::should_raise_an_assert && # a); \
Austin Schuh189376f2018-12-20 22:11:15 +1100228 } catch (Eigen::eigen_assert_exception) { \
229 Eigen::internal::push_assert = false; VERIFY(true); \
Brian Silverman72890c22015-09-19 14:37:37 -0400230 } \
231 Eigen::report_on_cerr_on_assert_failure = true; \
Austin Schuh189376f2018-12-20 22:11:15 +1100232 Eigen::internal::push_assert = false; \
Brian Silverman72890c22015-09-19 14:37:37 -0400233 }
Austin Schuh189376f2018-12-20 22:11:15 +1100234 #endif //EIGEN_EXCEPTIONS
Brian Silverman72890c22015-09-19 14:37:37 -0400235
Austin Schuh189376f2018-12-20 22:11:15 +1100236 #elif !defined(__CUDACC__) // EIGEN_DEBUG_ASSERTS
Brian Silverman72890c22015-09-19 14:37:37 -0400237 // 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 \
Austin Schuh189376f2018-12-20 22:11:15 +1100245 EIGEN_THROW_X(Eigen::eigen_assert_exception()); \
Brian Silverman72890c22015-09-19 14:37:37 -0400246 }
Austin Schuh189376f2018-12-20 22:11:15 +1100247
248 #ifdef EIGEN_EXCEPTIONS
249 #define VERIFY_RAISES_ASSERT(a) { \
Brian Silverman72890c22015-09-19 14:37:37 -0400250 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 } \
Austin Schuh189376f2018-12-20 22:11:15 +1100256 catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \
Brian Silverman72890c22015-09-19 14:37:37 -0400257 Eigen::report_on_cerr_on_assert_failure = true; \
258 }
Austin Schuh189376f2018-12-20 22:11:15 +1100259 #endif // EIGEN_EXCEPTIONS
Brian Silverman72890c22015-09-19 14:37:37 -0400260 #endif // EIGEN_DEBUG_ASSERTS
261
Austin Schuh189376f2018-12-20 22:11:15 +1100262 #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__)
Brian Silverman72890c22015-09-19 14:37:37 -0400294 #define EIGEN_USE_CUSTOM_ASSERT
Austin Schuh189376f2018-12-20 22:11:15 +1100295 #endif
Brian Silverman72890c22015-09-19 14:37:37 -0400296
297#else // EIGEN_NO_ASSERTION_CHECKING
298
299 #define VERIFY_RAISES_ASSERT(a) {}
Austin Schuh189376f2018-12-20 22:11:15 +1100300 #define VERIFY_RAISES_STATIC_ASSERT(a) {}
Brian Silverman72890c22015-09-19 14:37:37 -0400301
302#endif // EIGEN_NO_ASSERTION_CHECKING
303
Brian Silverman72890c22015-09-19 14:37:37 -0400304#define EIGEN_INTERNAL_DEBUGGING
305#include <Eigen/QR> // required for createRandomPIMatrixOfRank
306
307inline void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string)
308{
309 if (!condition)
310 {
Austin Schuh189376f2018-12-20 22:11:15 +1100311 if(Eigen::g_test_level>0)
312 std::cerr << "WARNING: ";
Brian Silverman72890c22015-09-19 14:37:37 -0400313 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";
Austin Schuh189376f2018-12-20 22:11:15 +1100320 if(Eigen::g_test_level==0)
321 abort();
Brian Silverman72890c22015-09-19 14:37:37 -0400322 }
323}
324
325#define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EI_PP_MAKE_STRING(a))
326
Austin Schuh189376f2018-12-20 22:11:15 +1100327#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))
Brian Silverman72890c22015-09-19 14:37:37 -0400334#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
349namespace Eigen {
350
351template<typename T> inline typename NumTraits<T>::Real test_precision() { return NumTraits<T>::dummy_precision(); }
352template<> inline float test_precision<float>() { return 1e-3f; }
353template<> inline double test_precision<double>() { return 1e-6; }
Austin Schuh189376f2018-12-20 22:11:15 +1100354template<> inline long double test_precision<long double>() { return 1e-6l; }
Brian Silverman72890c22015-09-19 14:37:37 -0400355template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); }
356template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); }
Austin Schuh189376f2018-12-20 22:11:15 +1100357template<> inline long double test_precision<std::complex<long double> >() { return test_precision<long double>(); }
358
359inline bool test_isApprox(const short& a, const short& b)
360{ return internal::isApprox(a, b, test_precision<short>()); }
361inline bool test_isApprox(const unsigned short& a, const unsigned short& b)
362{ return internal::isApprox(a, b, test_precision<unsigned short>()); }
363inline bool test_isApprox(const unsigned int& a, const unsigned int& b)
364{ return internal::isApprox(a, b, test_precision<unsigned int>()); }
365inline bool test_isApprox(const long& a, const long& b)
366{ return internal::isApprox(a, b, test_precision<long>()); }
367inline bool test_isApprox(const unsigned long& a, const unsigned long& b)
368{ return internal::isApprox(a, b, test_precision<unsigned long>()); }
Brian Silverman72890c22015-09-19 14:37:37 -0400369
370inline bool test_isApprox(const int& a, const int& b)
371{ return internal::isApprox(a, b, test_precision<int>()); }
372inline bool test_isMuchSmallerThan(const int& a, const int& b)
373{ return internal::isMuchSmallerThan(a, b, test_precision<int>()); }
374inline bool test_isApproxOrLessThan(const int& a, const int& b)
375{ return internal::isApproxOrLessThan(a, b, test_precision<int>()); }
376
377inline bool test_isApprox(const float& a, const float& b)
378{ return internal::isApprox(a, b, test_precision<float>()); }
379inline bool test_isMuchSmallerThan(const float& a, const float& b)
380{ return internal::isMuchSmallerThan(a, b, test_precision<float>()); }
381inline bool test_isApproxOrLessThan(const float& a, const float& b)
382{ return internal::isApproxOrLessThan(a, b, test_precision<float>()); }
Austin Schuh189376f2018-12-20 22:11:15 +1100383
Brian Silverman72890c22015-09-19 14:37:37 -0400384inline bool test_isApprox(const double& a, const double& b)
385{ return internal::isApprox(a, b, test_precision<double>()); }
Brian Silverman72890c22015-09-19 14:37:37 -0400386inline bool test_isMuchSmallerThan(const double& a, const double& b)
387{ return internal::isMuchSmallerThan(a, b, test_precision<double>()); }
388inline bool test_isApproxOrLessThan(const double& a, const double& b)
389{ return internal::isApproxOrLessThan(a, b, test_precision<double>()); }
390
Austin Schuh189376f2018-12-20 22:11:15 +1100391#ifndef EIGEN_TEST_NO_COMPLEX
Brian Silverman72890c22015-09-19 14:37:37 -0400392inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b)
393{ return internal::isApprox(a, b, test_precision<std::complex<float> >()); }
394inline 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
397inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b)
398{ return internal::isApprox(a, b, test_precision<std::complex<double> >()); }
399inline 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
Austin Schuh189376f2018-12-20 22:11:15 +1100402#ifndef EIGEN_TEST_NO_LONGDOUBLE
403inline 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> >()); }
405inline 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
Brian Silverman72890c22015-09-19 14:37:37 -0400411inline 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
420inline bool test_isMuchSmallerThan(const long double& a, const long double& b)
421{ return internal::isMuchSmallerThan(a, b, test_precision<long double>()); }
422inline bool test_isApproxOrLessThan(const long double& a, const long double& b)
423{ return internal::isApproxOrLessThan(a, b, test_precision<long double>()); }
Austin Schuh189376f2018-12-20 22:11:15 +1100424#endif // EIGEN_TEST_NO_LONGDOUBLE
425
426inline bool test_isApprox(const half& a, const half& b)
427{ return internal::isApprox(a, b, test_precision<half>()); }
428inline bool test_isMuchSmallerThan(const half& a, const half& b)
429{ return internal::isMuchSmallerThan(a, b, test_precision<half>()); }
430inline 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.
434template<typename T1,typename T2>
435typename 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
444template<typename T1,typename T2>
445typename 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
450template<typename T1,typename T2>
451typename 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
456template<typename S, int D>
457S 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
462template <typename S, int D, int O>
463S 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
468template <typename S, int D>
469S 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
474template<typename Derived> class SparseMatrixBase;
475template<typename T1,typename T2>
476typename T1::RealScalar test_relative_error(const MatrixBase<T1> &a, const SparseMatrixBase<T2> &b)
477{
478 return test_relative_error(a,b.toDense());
479}
480
481template<typename Derived> class SparseMatrixBase;
482template<typename T1,typename T2>
483typename T1::RealScalar test_relative_error(const SparseMatrixBase<T1> &a, const MatrixBase<T2> &b)
484{
485 return test_relative_error(a.toDense(),b);
486}
487
488template<typename Derived> class SparseMatrixBase;
489template<typename T1,typename T2>
490typename 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
495template<typename T1,typename T2>
496typename 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
502template<typename T>
503T test_relative_error(const Rotation2D<T> &a, const Rotation2D<T> &b)
504{
505 return test_relative_error(a.angle(), b.angle());
506}
507
508template<typename T>
509T 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}
Brian Silverman72890c22015-09-19 14:37:37 -0400513
514template<typename Type1, typename Type2>
Austin Schuh189376f2018-12-20 22:11:15 +1100515inline bool test_isApprox(const Type1& a, const Type2& b, typename Type1::Scalar* = 0) // Enabled for Eigen's type only
Brian Silverman72890c22015-09-19 14:37:37 -0400516{
517 return a.isApprox(b, test_precision<typename Type1::Scalar>());
518}
519
Austin Schuh189376f2018-12-20 22:11:15 +1100520// get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions
521template<typename T>
522typename 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
527template<typename T>
528typename 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.
534template<typename Type1, typename Type2>
535inline 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
Brian Silverman72890c22015-09-19 14:37:37 -0400545// 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.
552template<typename Scalar,typename ScalarRef>
553inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref)
554{
555 return test_isApprox(a+ref, b+ref);
556}
557
558template<typename Derived1, typename Derived2>
559inline 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
565template<typename Derived>
566inline 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
572template<typename Derived>
573inline 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
579template<typename T, typename U>
Austin Schuh189376f2018-12-20 22:11:15 +1100580bool test_is_equal(const T& actual, const U& expected, bool expect_equal=true);
Brian Silverman72890c22015-09-19 14:37:37 -0400581
582template<typename T, typename U>
Austin Schuh189376f2018-12-20 22:11:15 +1100583bool test_is_equal(const T& actual, const U& expected, bool expect_equal)
Brian Silverman72890c22015-09-19 14:37:37 -0400584{
Austin Schuh189376f2018-12-20 22:11:15 +1100585 if ((actual==expected) == expect_equal)
Brian Silverman72890c22015-09-19 14:37:37 -0400586 return true;
587 // false:
588 std::cerr
Austin Schuh189376f2018-12-20 22:11:15 +1100589 << "\n actual = " << actual
590 << "\n expected " << (expect_equal ? "= " : "!=") << expected << "\n\n";
Brian Silverman72890c22015-09-19 14:37:37 -0400591 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
600template<typename MatrixType>
Austin Schuh189376f2018-12-20 22:11:15 +1100601void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m);
Brian Silverman72890c22015-09-19 14:37:37 -0400602template<typename MatrixType>
Austin Schuh189376f2018-12-20 22:11:15 +1100603void createRandomPIMatrixOfRank(Index desired_rank, Index rows, Index cols, MatrixType& m)
Brian Silverman72890c22015-09-19 14:37:37 -0400604{
Brian Silverman72890c22015-09-19 14:37:37 -0400605 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
640template<typename PermutationVectorType>
Austin Schuh189376f2018-12-20 22:11:15 +1100641void randomPermutationVector(PermutationVectorType& v, Index size);
Brian Silverman72890c22015-09-19 14:37:37 -0400642template<typename PermutationVectorType>
Austin Schuh189376f2018-12-20 22:11:15 +1100643void randomPermutationVector(PermutationVectorType& v, Index size)
Brian Silverman72890c22015-09-19 14:37:37 -0400644{
Brian Silverman72890c22015-09-19 14:37:37 -0400645 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
658template<typename T> bool isNotNaN(const T& x)
659{
660 return x==x;
661}
662
Austin Schuh189376f2018-12-20 22:11:15 +1100663template<typename T> bool isPlusInf(const T& x)
Brian Silverman72890c22015-09-19 14:37:37 -0400664{
665 return x > NumTraits<T>::highest();
666}
667
668template<typename T> bool isMinusInf(const T& x)
669{
670 return x < NumTraits<T>::lowest();
671}
672
673} // end namespace Eigen
674
675template<typename T> struct GetDifferentType;
676
677template<> struct GetDifferentType<float> { typedef double type; };
678template<> struct GetDifferentType<double> { typedef float type; };
679template<typename T> struct GetDifferentType<std::complex<T> >
680{ typedef std::complex<typename GetDifferentType<T>::type> type; };
681
682// Forward declaration to avoid ICC warning
683template<typename T> std::string type_name();
Austin Schuh189376f2018-12-20 22:11:15 +1100684template<typename T> std::string type_name() { return "other"; }
685template<> std::string type_name<float>() { return "float"; }
686template<> std::string type_name<double>() { return "double"; }
687template<> std::string type_name<long double>() { return "long double"; }
688template<> std::string type_name<int>() { return "int"; }
689template<> std::string type_name<std::complex<float> >() { return "complex<float>"; }
690template<> std::string type_name<std::complex<double> >() { return "complex<double>"; }
691template<> std::string type_name<std::complex<long double> >() { return "complex<long double>"; }
692template<> std::string type_name<std::complex<int> >() { return "complex<int>"; }
Brian Silverman72890c22015-09-19 14:37:37 -0400693
694// forward declaration of the main test function
695void EIGEN_CAT(test_,EIGEN_TEST_FUNC)();
696
697using namespace Eigen;
698
699inline 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
711inline 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
723int 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
Austin Schuh189376f2018-12-20 22:11:15 +1100799
800#ifdef _MSC_VER
801 // 4503 - decorated name length exceeded, name was truncated
802 #pragma warning( disable : 4503)
803#endif