1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com> 5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud (at) inria.fr> 6 // 7 // This Source Code Form is subject to the terms of the Mozilla 8 // Public License v. 2.0. If a copy of the MPL was not distributed 9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 11 #include <cstdlib> 12 #include <cerrno> 13 #include <ctime> 14 #include <iostream> 15 #include <fstream> 16 #include <string> 17 #include <sstream> 18 #include <vector> 19 #include <typeinfo> 20 #include <limits> 21 #include <algorithm> 22 #include <sstream> 23 #include <complex> 24 #include <deque> 25 #include <queue> 26 27 #define min(A,B) please_protect_your_min_with_parentheses 28 #define max(A,B) please_protect_your_max_with_parentheses 29 30 #define FORBIDDEN_IDENTIFIER (this_identifier_is_forbidden_to_avoid_clashes) this_identifier_is_forbidden_to_avoid_clashes 31 // B0 is defined in POSIX header termios.h 32 #define B0 FORBIDDEN_IDENTIFIER 33 34 35 // shuts down ICC's remark #593: variable "XXX" was set but never used 36 #define TEST_SET_BUT_UNUSED_VARIABLE(X) X = X + 0; 37 38 // the following file is automatically generated by cmake 39 #include "split_test_helper.h" 40 41 #ifdef NDEBUG 42 #undef NDEBUG 43 #endif 44 45 // On windows CE, NDEBUG is automatically defined <assert.h> if NDEBUG is not defined. 46 #ifndef DEBUG 47 #define DEBUG 48 #endif 49 50 // bounds integer values for AltiVec 51 #ifdef __ALTIVEC__ 52 #define EIGEN_MAKING_DOCS 53 #endif 54 55 #ifndef EIGEN_TEST_FUNC 56 #error EIGEN_TEST_FUNC must be defined 57 #endif 58 59 #define DEFAULT_REPEAT 10 60 61 namespace Eigen 62 { 63 static std::vector<std::string> g_test_stack; 64 static int g_repeat; 65 static unsigned int g_seed; 66 static bool g_has_set_repeat, g_has_set_seed; 67 } 68 69 #define EI_PP_MAKE_STRING2(S) #S 70 #define EI_PP_MAKE_STRING(S) EI_PP_MAKE_STRING2(S) 71 72 #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, " ", "\n", "", "", "", "") 73 74 #ifndef EIGEN_NO_ASSERTION_CHECKING 75 76 namespace Eigen 77 { 78 static const bool should_raise_an_assert = false; 79 80 // Used to avoid to raise two exceptions at a time in which 81 // case the exception is not properly caught. 82 // This may happen when a second exceptions is triggered in a destructor. 83 static bool no_more_assert = false; 84 static bool report_on_cerr_on_assert_failure = true; 85 86 struct eigen_assert_exception 87 { 88 eigen_assert_exception(void) {} 89 ~eigen_assert_exception() { Eigen::no_more_assert = false; } 90 }; 91 } 92 // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is triggered while 93 // one should have been, then the list of excecuted assertions is printed out. 94 // 95 // EIGEN_DEBUG_ASSERTS is not enabled by default as it 96 // significantly increases the compilation time 97 // and might even introduce side effects that would hide 98 // some memory errors. 99 #ifdef EIGEN_DEBUG_ASSERTS 100 101 namespace Eigen 102 { 103 namespace internal 104 { 105 static bool push_assert = false; 106 } 107 static std::vector<std::string> eigen_assert_list; 108 } 109 #define eigen_assert(a) \ 110 if( (!(a)) && (!no_more_assert) ) \ 111 { \ 112 if(report_on_cerr_on_assert_failure) \ 113 std::cerr << #a << " " __FILE__ << "(" << __LINE__ << ")\n"; \ 114 Eigen::no_more_assert = true; \ 115 throw Eigen::eigen_assert_exception(); \ 116 } \ 117 else if (Eigen::internal::push_assert) \ 118 { \ 119 eigen_assert_list.push_back(std::string(EI_PP_MAKE_STRING(__FILE__) " (" EI_PP_MAKE_STRING(__LINE__) ") : " #a) ); \ 120 } 121 122 #define VERIFY_RAISES_ASSERT(a) \ 123 { \ 124 Eigen::no_more_assert = false; \ 125 Eigen::eigen_assert_list.clear(); \ 126 Eigen::internal::push_assert = true; \ 127 Eigen::report_on_cerr_on_assert_failure = false; \ 128 try { \ 129 a; \ 130 std::cerr << "One of the following asserts should have been triggered:\n"; \ 131 for (uint ai=0 ; ai<eigen_assert_list.size() ; ++ai) \ 132 std::cerr << " " << eigen_assert_list[ai] << "\n"; \ 133 VERIFY(Eigen::should_raise_an_assert && # a); \ 134 } catch (Eigen::eigen_assert_exception) { \ 135 Eigen::internal::push_assert = false; VERIFY(true); \ 136 } \ 137 Eigen::report_on_cerr_on_assert_failure = true; \ 138 Eigen::internal::push_assert = false; \ 139 } 140 141 #else // EIGEN_DEBUG_ASSERTS 142 // see bug 89. The copy_bool here is working around a bug in gcc <= 4.3 143 #define eigen_assert(a) \ 144 if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\ 145 { \ 146 Eigen::no_more_assert = true; \ 147 if(report_on_cerr_on_assert_failure) \ 148 eigen_plain_assert(a); \ 149 else \ 150 throw Eigen::eigen_assert_exception(); \ 151 } 152 #define VERIFY_RAISES_ASSERT(a) { \ 153 Eigen::no_more_assert = false; \ 154 Eigen::report_on_cerr_on_assert_failure = false; \ 155 try { \ 156 a; \ 157 VERIFY(Eigen::should_raise_an_assert && # a); \ 158 } \ 159 catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \ 160 Eigen::report_on_cerr_on_assert_failure = true; \ 161 } 162 163 #endif // EIGEN_DEBUG_ASSERTS 164 165 #define EIGEN_USE_CUSTOM_ASSERT 166 167 #else // EIGEN_NO_ASSERTION_CHECKING 168 169 #define VERIFY_RAISES_ASSERT(a) {} 170 171 #endif // EIGEN_NO_ASSERTION_CHECKING 172 173 174 #define EIGEN_INTERNAL_DEBUGGING 175 #include <Eigen/QR> // required for createRandomPIMatrixOfRank 176 177 inline void verify_impl(bool condition, const char *testname, const char *file, int line, const char *condition_as_string) 178 { 179 if (!condition) 180 { 181 std::cerr << "Test " << testname << " failed in " << file << " (" << line << ")" 182 << std::endl << " " << condition_as_string << std::endl; 183 std::cerr << "Stack:\n"; 184 const int test_stack_size = static_cast<int>(Eigen::g_test_stack.size()); 185 for(int i=test_stack_size-1; i>=0; --i) 186 std::cerr << " - " << Eigen::g_test_stack[i] << "\n"; 187 std::cerr << "\n"; 188 abort(); 189 } 190 } 191 192 #define VERIFY(a) ::verify_impl(a, g_test_stack.back().c_str(), __FILE__, __LINE__, EI_PP_MAKE_STRING(a)) 193 194 #define VERIFY_IS_EQUAL(a, b) VERIFY(test_is_equal(a, b)) 195 #define VERIFY_IS_APPROX(a, b) VERIFY(test_isApprox(a, b)) 196 #define VERIFY_IS_NOT_APPROX(a, b) VERIFY(!test_isApprox(a, b)) 197 #define VERIFY_IS_MUCH_SMALLER_THAN(a, b) VERIFY(test_isMuchSmallerThan(a, b)) 198 #define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b) VERIFY(!test_isMuchSmallerThan(a, b)) 199 #define VERIFY_IS_APPROX_OR_LESS_THAN(a, b) VERIFY(test_isApproxOrLessThan(a, b)) 200 #define VERIFY_IS_NOT_APPROX_OR_LESS_THAN(a, b) VERIFY(!test_isApproxOrLessThan(a, b)) 201 202 #define VERIFY_IS_UNITARY(a) VERIFY(test_isUnitary(a)) 203 204 #define CALL_SUBTEST(FUNC) do { \ 205 g_test_stack.push_back(EI_PP_MAKE_STRING(FUNC)); \ 206 FUNC; \ 207 g_test_stack.pop_back(); \ 208 } while (0) 209 210 211 namespace Eigen { 212 213 template<typename T> inline typename NumTraits<T>::Real test_precision() { return NumTraits<T>::dummy_precision(); } 214 template<> inline float test_precision<float>() { return 1e-3f; } 215 template<> inline double test_precision<double>() { return 1e-6; } 216 template<> inline float test_precision<std::complex<float> >() { return test_precision<float>(); } 217 template<> inline double test_precision<std::complex<double> >() { return test_precision<double>(); } 218 template<> inline long double test_precision<long double>() { return 1e-6; } 219 220 inline bool test_isApprox(const int& a, const int& b) 221 { return internal::isApprox(a, b, test_precision<int>()); } 222 inline bool test_isMuchSmallerThan(const int& a, const int& b) 223 { return internal::isMuchSmallerThan(a, b, test_precision<int>()); } 224 inline bool test_isApproxOrLessThan(const int& a, const int& b) 225 { return internal::isApproxOrLessThan(a, b, test_precision<int>()); } 226 227 inline bool test_isApprox(const float& a, const float& b) 228 { return internal::isApprox(a, b, test_precision<float>()); } 229 inline bool test_isMuchSmallerThan(const float& a, const float& b) 230 { return internal::isMuchSmallerThan(a, b, test_precision<float>()); } 231 inline bool test_isApproxOrLessThan(const float& a, const float& b) 232 { return internal::isApproxOrLessThan(a, b, test_precision<float>()); } 233 inline bool test_isApprox(const double& a, const double& b) 234 { return internal::isApprox(a, b, test_precision<double>()); } 235 236 inline bool test_isMuchSmallerThan(const double& a, const double& b) 237 { return internal::isMuchSmallerThan(a, b, test_precision<double>()); } 238 inline bool test_isApproxOrLessThan(const double& a, const double& b) 239 { return internal::isApproxOrLessThan(a, b, test_precision<double>()); } 240 241 inline bool test_isApprox(const std::complex<float>& a, const std::complex<float>& b) 242 { return internal::isApprox(a, b, test_precision<std::complex<float> >()); } 243 inline bool test_isMuchSmallerThan(const std::complex<float>& a, const std::complex<float>& b) 244 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<float> >()); } 245 246 inline bool test_isApprox(const std::complex<double>& a, const std::complex<double>& b) 247 { return internal::isApprox(a, b, test_precision<std::complex<double> >()); } 248 inline bool test_isMuchSmallerThan(const std::complex<double>& a, const std::complex<double>& b) 249 { return internal::isMuchSmallerThan(a, b, test_precision<std::complex<double> >()); } 250 251 inline bool test_isApprox(const long double& a, const long double& b) 252 { 253 bool ret = internal::isApprox(a, b, test_precision<long double>()); 254 if (!ret) std::cerr 255 << std::endl << " actual = " << a 256 << std::endl << " expected = " << b << std::endl << std::endl; 257 return ret; 258 } 259 260 inline bool test_isMuchSmallerThan(const long double& a, const long double& b) 261 { return internal::isMuchSmallerThan(a, b, test_precision<long double>()); } 262 inline bool test_isApproxOrLessThan(const long double& a, const long double& b) 263 { return internal::isApproxOrLessThan(a, b, test_precision<long double>()); } 264 265 template<typename Type1, typename Type2> 266 inline bool test_isApprox(const Type1& a, const Type2& b) 267 { 268 return a.isApprox(b, test_precision<typename Type1::Scalar>()); 269 } 270 271 // The idea behind this function is to compare the two scalars a and b where 272 // the scalar ref is a hint about the expected order of magnitude of a and b. 273 // WARNING: the scalar a and b must be positive 274 // Therefore, if for some reason a and b are very small compared to ref, 275 // we won't issue a false negative. 276 // This test could be: abs(a-b) <= eps * ref 277 // However, it seems that simply comparing a+ref and b+ref is more sensitive to true error. 278 template<typename Scalar,typename ScalarRef> 279 inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref) 280 { 281 return test_isApprox(a+ref, b+ref); 282 } 283 284 template<typename Derived1, typename Derived2> 285 inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1, 286 const MatrixBase<Derived2>& m2) 287 { 288 return m1.isMuchSmallerThan(m2, test_precision<typename internal::traits<Derived1>::Scalar>()); 289 } 290 291 template<typename Derived> 292 inline bool test_isMuchSmallerThan(const MatrixBase<Derived>& m, 293 const typename NumTraits<typename internal::traits<Derived>::Scalar>::Real& s) 294 { 295 return m.isMuchSmallerThan(s, test_precision<typename internal::traits<Derived>::Scalar>()); 296 } 297 298 template<typename Derived> 299 inline bool test_isUnitary(const MatrixBase<Derived>& m) 300 { 301 return m.isUnitary(test_precision<typename internal::traits<Derived>::Scalar>()); 302 } 303 304 // Forward declaration to avoid ICC warning 305 template<typename T, typename U> 306 bool test_is_equal(const T& actual, const U& expected); 307 308 template<typename T, typename U> 309 bool test_is_equal(const T& actual, const U& expected) 310 { 311 if (actual==expected) 312 return true; 313 // false: 314 std::cerr 315 << std::endl << " actual = " << actual 316 << std::endl << " expected = " << expected << std::endl << std::endl; 317 return false; 318 } 319 320 /** Creates a random Partial Isometry matrix of given rank. 321 * 322 * A partial isometry is a matrix all of whose singular values are either 0 or 1. 323 * This is very useful to test rank-revealing algorithms. 324 */ 325 // Forward declaration to avoid ICC warning 326 template<typename MatrixType> 327 void createRandomPIMatrixOfRank(typename MatrixType::Index desired_rank, typename MatrixType::Index rows, typename MatrixType::Index cols, MatrixType& m); 328 template<typename MatrixType> 329 void createRandomPIMatrixOfRank(typename MatrixType::Index desired_rank, typename MatrixType::Index rows, typename MatrixType::Index cols, MatrixType& m) 330 { 331 typedef typename internal::traits<MatrixType>::Index Index; 332 typedef typename internal::traits<MatrixType>::Scalar Scalar; 333 enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime }; 334 335 typedef Matrix<Scalar, Dynamic, 1> VectorType; 336 typedef Matrix<Scalar, Rows, Rows> MatrixAType; 337 typedef Matrix<Scalar, Cols, Cols> MatrixBType; 338 339 if(desired_rank == 0) 340 { 341 m.setZero(rows,cols); 342 return; 343 } 344 345 if(desired_rank == 1) 346 { 347 // here we normalize the vectors to get a partial isometry 348 m = VectorType::Random(rows).normalized() * VectorType::Random(cols).normalized().transpose(); 349 return; 350 } 351 352 MatrixAType a = MatrixAType::Random(rows,rows); 353 MatrixType d = MatrixType::Identity(rows,cols); 354 MatrixBType b = MatrixBType::Random(cols,cols); 355 356 // set the diagonal such that only desired_rank non-zero entries reamain 357 const Index diag_size = (std::min)(d.rows(),d.cols()); 358 if(diag_size != desired_rank) 359 d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank); 360 361 HouseholderQR<MatrixAType> qra(a); 362 HouseholderQR<MatrixBType> qrb(b); 363 m = qra.householderQ() * d * qrb.householderQ(); 364 } 365 366 // Forward declaration to avoid ICC warning 367 template<typename PermutationVectorType> 368 void randomPermutationVector(PermutationVectorType& v, typename PermutationVectorType::Index size); 369 template<typename PermutationVectorType> 370 void randomPermutationVector(PermutationVectorType& v, typename PermutationVectorType::Index size) 371 { 372 typedef typename PermutationVectorType::Index Index; 373 typedef typename PermutationVectorType::Scalar Scalar; 374 v.resize(size); 375 for(Index i = 0; i < size; ++i) v(i) = Scalar(i); 376 if(size == 1) return; 377 for(Index n = 0; n < 3 * size; ++n) 378 { 379 Index i = internal::random<Index>(0, size-1); 380 Index j; 381 do j = internal::random<Index>(0, size-1); while(j==i); 382 std::swap(v(i), v(j)); 383 } 384 } 385 386 } // end namespace Eigen 387 388 template<typename T> struct GetDifferentType; 389 390 template<> struct GetDifferentType<float> { typedef double type; }; 391 template<> struct GetDifferentType<double> { typedef float type; }; 392 template<typename T> struct GetDifferentType<std::complex<T> > 393 { typedef std::complex<typename GetDifferentType<T>::type> type; }; 394 395 // Forward declaration to avoid ICC warning 396 template<typename T> std::string type_name(); 397 template<typename T> std::string type_name() { return "other"; } 398 template<> std::string type_name<float>() { return "float"; } 399 template<> std::string type_name<double>() { return "double"; } 400 template<> std::string type_name<int>() { return "int"; } 401 template<> std::string type_name<std::complex<float> >() { return "complex<float>"; } 402 template<> std::string type_name<std::complex<double> >() { return "complex<double>"; } 403 template<> std::string type_name<std::complex<int> >() { return "complex<int>"; } 404 405 // forward declaration of the main test function 406 void EIGEN_CAT(test_,EIGEN_TEST_FUNC)(); 407 408 using namespace Eigen; 409 410 inline void set_repeat_from_string(const char *str) 411 { 412 errno = 0; 413 g_repeat = int(strtoul(str, 0, 10)); 414 if(errno || g_repeat <= 0) 415 { 416 std::cout << "Invalid repeat value " << str << std::endl; 417 exit(EXIT_FAILURE); 418 } 419 g_has_set_repeat = true; 420 } 421 422 inline void set_seed_from_string(const char *str) 423 { 424 errno = 0; 425 g_seed = int(strtoul(str, 0, 10)); 426 if(errno || g_seed == 0) 427 { 428 std::cout << "Invalid seed value " << str << std::endl; 429 exit(EXIT_FAILURE); 430 } 431 g_has_set_seed = true; 432 } 433 434 int main(int argc, char *argv[]) 435 { 436 g_has_set_repeat = false; 437 g_has_set_seed = false; 438 bool need_help = false; 439 440 for(int i = 1; i < argc; i++) 441 { 442 if(argv[i][0] == 'r') 443 { 444 if(g_has_set_repeat) 445 { 446 std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl; 447 return 1; 448 } 449 set_repeat_from_string(argv[i]+1); 450 } 451 else if(argv[i][0] == 's') 452 { 453 if(g_has_set_seed) 454 { 455 std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl; 456 return 1; 457 } 458 set_seed_from_string(argv[i]+1); 459 } 460 else 461 { 462 need_help = true; 463 } 464 } 465 466 if(need_help) 467 { 468 std::cout << "This test application takes the following optional arguments:" << std::endl; 469 std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl; 470 std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl; 471 std::cout << std::endl; 472 std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl; 473 std::cout << "will be used as default values for these parameters." << std::endl; 474 return 1; 475 } 476 477 char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT"); 478 if(!g_has_set_repeat && env_EIGEN_REPEAT) 479 set_repeat_from_string(env_EIGEN_REPEAT); 480 char *env_EIGEN_SEED = getenv("EIGEN_SEED"); 481 if(!g_has_set_seed && env_EIGEN_SEED) 482 set_seed_from_string(env_EIGEN_SEED); 483 484 if(!g_has_set_seed) g_seed = (unsigned int) time(NULL); 485 if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT; 486 487 std::cout << "Initializing random number generator with seed " << g_seed << std::endl; 488 std::stringstream ss; 489 ss << "Seed: " << g_seed; 490 g_test_stack.push_back(ss.str()); 491 srand(g_seed); 492 std::cout << "Repeating each test " << g_repeat << " times" << std::endl; 493 494 Eigen::g_test_stack.push_back(std::string(EI_PP_MAKE_STRING(EIGEN_TEST_FUNC))); 495 496 EIGEN_CAT(test_,EIGEN_TEST_FUNC)(); 497 return 0; 498 } 499 500 // These warning are disabled here such that they are still ON when parsing Eigen's header files. 501 #if defined __INTEL_COMPILER 502 // remark #383: value copied to temporary, reference to temporary used 503 // -> 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> 504 // remark #1418: external function definition with no prior declaration 505 // -> this warning is raised for all our test functions. Declaring them static would fix the issue. 506 // warning #279: controlling expression is constant 507 // remark #1572: floating-point equality and inequality comparisons are unreliable 508 #pragma warning disable 279 383 1418 1572 509 #endif 510