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