math_equals.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2015 Oracle and/or its affiliates.
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <geometry_test_common.hpp>
  9. #include <limits>
  10. #include <boost/geometry/util/condition.hpp>
  11. #include <boost/geometry/util/math.hpp>
  12. namespace bgm = bg::math;
  13. template <typename T>
  14. void test_all()
  15. {
  16. BOOST_CHECK(bgm::equals(0, 0));
  17. BOOST_CHECK(bgm::equals(1, 1));
  18. BOOST_CHECK(bgm::equals(123456, 123456));
  19. T eps = std::numeric_limits<T>::epsilon();
  20. if ( eps > 0 )
  21. {
  22. BOOST_CHECK(bgm::equals(0, 0+eps));
  23. BOOST_CHECK(bgm::equals(0+eps, 0));
  24. BOOST_CHECK(bgm::equals(1, 1+eps));
  25. BOOST_CHECK(bgm::equals(1+eps, 1));
  26. BOOST_CHECK(bgm::equals(12345+eps, 12345));
  27. }
  28. if (BOOST_GEOMETRY_CONDITION(std::numeric_limits<T>::has_infinity))
  29. {
  30. T inf = std::numeric_limits<T>::infinity();
  31. BOOST_CHECK(!bgm::equals(0, inf));
  32. BOOST_CHECK(!bgm::equals(0, -inf));
  33. BOOST_CHECK(!bgm::equals(1, inf));
  34. BOOST_CHECK(!bgm::equals(1, -inf));
  35. BOOST_CHECK(!bgm::equals(12345, inf));
  36. BOOST_CHECK(!bgm::equals(12345, -inf));
  37. BOOST_CHECK(!bgm::equals(inf, 0));
  38. BOOST_CHECK(!bgm::equals(-inf, 0));
  39. BOOST_CHECK(!bgm::equals(inf, 1));
  40. BOOST_CHECK(!bgm::equals(-inf, 1));
  41. BOOST_CHECK(!bgm::equals(inf, 12345));
  42. BOOST_CHECK(!bgm::equals(-inf, 12345));
  43. BOOST_CHECK(bgm::equals(inf, inf));
  44. BOOST_CHECK(bgm::equals(-inf, -inf));
  45. BOOST_CHECK(!bgm::equals(inf, -inf));
  46. BOOST_CHECK(!bgm::equals(-inf, inf));
  47. }
  48. if (BOOST_GEOMETRY_CONDITION(std::numeric_limits<T>::has_quiet_NaN))
  49. {
  50. T nan = std::numeric_limits<T>::quiet_NaN();
  51. BOOST_CHECK(!bgm::equals(0, nan));
  52. BOOST_CHECK(!bgm::equals(nan, 0));
  53. BOOST_CHECK(!bgm::equals(nan, nan));
  54. BOOST_CHECK(!bgm::equals(1, nan));
  55. BOOST_CHECK(!bgm::equals(nan, 1));
  56. BOOST_CHECK(!bgm::equals(12345, nan));
  57. BOOST_CHECK(!bgm::equals(nan, 12345));
  58. }
  59. }
  60. int test_main(int, char* [])
  61. {
  62. test_all<int>();
  63. test_all<float>();
  64. test_all<double>();
  65. return 0;
  66. }