test_limits.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. /**
  11. \file
  12. \brief test_limits.cpp
  13. \details
  14. Test numeric_limits specialization.
  15. Output:
  16. @verbatim
  17. @endverbatim
  18. **/
  19. #include <complex>
  20. #include <limits>
  21. #include <boost/units/limits.hpp>
  22. #include <boost/units/cmath.hpp>
  23. #include "test_header.hpp"
  24. typedef boost::units::length unit_type;
  25. using boost::units::quantity;
  26. template<bool>
  27. struct check_quiet_NaN;
  28. template<>
  29. struct check_quiet_NaN<true> {
  30. template<class T>
  31. static void apply() {
  32. quantity<unit_type, T> q((std::numeric_limits<quantity<unit_type, T> >::quiet_NaN)());
  33. bool test = isnan BOOST_PREVENT_MACRO_SUBSTITUTION (q);
  34. BOOST_CHECK(test);
  35. }
  36. };
  37. template<>
  38. struct check_quiet_NaN<false> {
  39. template<class T>
  40. static void apply() {}
  41. };
  42. template<bool>
  43. struct check_signaling_NaN;
  44. template<>
  45. struct check_signaling_NaN<true> {
  46. template<class T>
  47. static void apply() {
  48. quantity<unit_type, T> q((std::numeric_limits<quantity<unit_type, T> >::signaling_NaN)());
  49. bool test = isnan BOOST_PREVENT_MACRO_SUBSTITUTION (q);
  50. BOOST_CHECK(test);
  51. }
  52. };
  53. template<>
  54. struct check_signaling_NaN<false> {
  55. template<class T>
  56. static void apply() {}
  57. };
  58. template<class T>
  59. void do_check() {
  60. #define CHECK_FUNCTION(name) BOOST_CHECK(((std::numeric_limits<T>::name)() == (std::numeric_limits<quantity<unit_type, T> >::name)().value()))
  61. #define CHECK_CONSTANT(name) BOOST_CHECK((std::numeric_limits<T>::name == std::numeric_limits<quantity<unit_type, T> >::name))
  62. CHECK_FUNCTION(min);
  63. CHECK_FUNCTION(max);
  64. CHECK_FUNCTION(epsilon);
  65. CHECK_FUNCTION(round_error);
  66. CHECK_FUNCTION(infinity);
  67. CHECK_FUNCTION(denorm_min);
  68. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  69. CHECK_FUNCTION(lowest);
  70. #endif
  71. CHECK_CONSTANT(is_specialized);
  72. CHECK_CONSTANT(digits);
  73. CHECK_CONSTANT(digits10);
  74. #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
  75. CHECK_CONSTANT(max_digits10);
  76. #endif
  77. CHECK_CONSTANT(is_signed);
  78. CHECK_CONSTANT(is_integer);
  79. CHECK_CONSTANT(is_exact);
  80. CHECK_CONSTANT(radix);
  81. CHECK_CONSTANT(min_exponent);
  82. CHECK_CONSTANT(min_exponent10);
  83. CHECK_CONSTANT(max_exponent);
  84. CHECK_CONSTANT(max_exponent10);
  85. CHECK_CONSTANT(has_infinity);
  86. CHECK_CONSTANT(has_quiet_NaN);
  87. CHECK_CONSTANT(has_signaling_NaN);
  88. CHECK_CONSTANT(has_denorm);
  89. CHECK_CONSTANT(has_denorm_loss);
  90. CHECK_CONSTANT(is_iec559);
  91. CHECK_CONSTANT(is_bounded);
  92. CHECK_CONSTANT(is_modulo);
  93. CHECK_CONSTANT(traps);
  94. CHECK_CONSTANT(tinyness_before);
  95. CHECK_CONSTANT(round_style);
  96. check_quiet_NaN<std::numeric_limits<quantity<unit_type, T> >::has_quiet_NaN>::template apply<T>();
  97. check_signaling_NaN<std::numeric_limits<quantity<unit_type, T> >::has_signaling_NaN>::template apply<T>();
  98. }
  99. int test_main(int,char *[])
  100. {
  101. do_check<float>();
  102. do_check<double>();
  103. do_check<int>();
  104. do_check<long>();
  105. do_check<unsigned>();
  106. do_check<std::complex<double> >();
  107. return(0);
  108. }