You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. template<typename Scalar> Scalar check_in_range(Scalar x, Scalar y)
  11. {
  12. Scalar r = internal::random<Scalar>(x,y);
  13. VERIFY(r>=x);
  14. if(y>=x)
  15. {
  16. VERIFY(r<=y);
  17. }
  18. return r;
  19. }
  20. template<typename Scalar> void check_all_in_range(Scalar x, Scalar y)
  21. {
  22. Array<int,1,Dynamic> mask(y-x+1);
  23. mask.fill(0);
  24. long n = (y-x+1)*32;
  25. for(long k=0; k<n; ++k)
  26. {
  27. mask( check_in_range(x,y)-x )++;
  28. }
  29. VERIFY( (mask>0).all() );
  30. }
  31. void test_rand()
  32. {
  33. long long_ref = NumTraits<long>::highest()/10;
  34. signed char char_offset = (std::min)(g_repeat,64);
  35. signed char short_offset = (std::min)(g_repeat,16000);
  36. for(int i = 0; i < g_repeat*10; i++) {
  37. CALL_SUBTEST(check_in_range<float>(10,11));
  38. CALL_SUBTEST(check_in_range<float>(1.24234523,1.24234523));
  39. CALL_SUBTEST(check_in_range<float>(-1,1));
  40. CALL_SUBTEST(check_in_range<float>(-1432.2352,-1432.2352));
  41. CALL_SUBTEST(check_in_range<double>(10,11));
  42. CALL_SUBTEST(check_in_range<double>(1.24234523,1.24234523));
  43. CALL_SUBTEST(check_in_range<double>(-1,1));
  44. CALL_SUBTEST(check_in_range<double>(-1432.2352,-1432.2352));
  45. CALL_SUBTEST(check_in_range<int>(0,-1));
  46. CALL_SUBTEST(check_in_range<short>(0,-1));
  47. CALL_SUBTEST(check_in_range<long>(0,-1));
  48. CALL_SUBTEST(check_in_range<int>(-673456,673456));
  49. CALL_SUBTEST(check_in_range<short>(-24345,24345));
  50. CALL_SUBTEST(check_in_range<long>(-long_ref,long_ref));
  51. }
  52. CALL_SUBTEST(check_all_in_range<signed char>(11,11));
  53. CALL_SUBTEST(check_all_in_range<signed char>(11,11+char_offset));
  54. CALL_SUBTEST(check_all_in_range<signed char>(-5,5));
  55. CALL_SUBTEST(check_all_in_range<signed char>(-11-char_offset,-11));
  56. CALL_SUBTEST(check_all_in_range<signed char>(-126,-126+char_offset));
  57. CALL_SUBTEST(check_all_in_range<signed char>(126-char_offset,126));
  58. CALL_SUBTEST(check_all_in_range<signed char>(-126,126));
  59. CALL_SUBTEST(check_all_in_range<short>(11,11));
  60. CALL_SUBTEST(check_all_in_range<short>(11,11+short_offset));
  61. CALL_SUBTEST(check_all_in_range<short>(-5,5));
  62. CALL_SUBTEST(check_all_in_range<short>(-11-short_offset,-11));
  63. CALL_SUBTEST(check_all_in_range<short>(-24345,-24345+short_offset));
  64. CALL_SUBTEST(check_all_in_range<short>(24345,24345+short_offset));
  65. CALL_SUBTEST(check_all_in_range<int>(11,11));
  66. CALL_SUBTEST(check_all_in_range<int>(11,11+g_repeat));
  67. CALL_SUBTEST(check_all_in_range<int>(-5,5));
  68. CALL_SUBTEST(check_all_in_range<int>(-11-g_repeat,-11));
  69. CALL_SUBTEST(check_all_in_range<int>(-673456,-673456+g_repeat));
  70. CALL_SUBTEST(check_all_in_range<int>(673456,673456+g_repeat));
  71. CALL_SUBTEST(check_all_in_range<long>(11,11));
  72. CALL_SUBTEST(check_all_in_range<long>(11,11+g_repeat));
  73. CALL_SUBTEST(check_all_in_range<long>(-5,5));
  74. CALL_SUBTEST(check_all_in_range<long>(-11-g_repeat,-11));
  75. CALL_SUBTEST(check_all_in_range<long>(-long_ref,-long_ref+g_repeat));
  76. CALL_SUBTEST(check_all_in_range<long>( long_ref, long_ref+g_repeat));
  77. }