math_abs.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  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. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_TEST_MODULE
  8. #define BOOST_TEST_MODULE test_math_abs
  9. #endif
  10. #include <cmath>
  11. #include <iostream>
  12. #include <boost/test/included/unit_test.hpp>
  13. #include <boost/config.hpp>
  14. #include "number_types.hpp"
  15. // important: the include above must precede the include below,
  16. // otherwise the test will fail for the custom number type:
  17. // custom_with_global_sqrt
  18. #include <boost/geometry/util/math.hpp>
  19. #include <boost/geometry/algorithms/not_implemented.hpp>
  20. #ifdef HAVE_TTMATH
  21. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  22. #endif
  23. namespace bg = boost::geometry;
  24. namespace bgm = boost::geometry::math;
  25. template <typename T>
  26. bool eq(T const& l, T const& r)
  27. {
  28. return !(l < r || r < l);
  29. }
  30. BOOST_AUTO_TEST_CASE( test_math_abs )
  31. {
  32. {
  33. float p1 = bgm::pi<float>();
  34. double p2 = bgm::pi<double>();
  35. long double p3 = bgm::pi<long double>();
  36. BOOST_CHECK(bgm::abs(p1) == p1);
  37. BOOST_CHECK(bgm::abs(p2) == p2);
  38. BOOST_CHECK(bgm::abs(p3) == p3);
  39. float n1 = -p1;
  40. double n2 = -p2;
  41. long double n3 = -p3;
  42. BOOST_CHECK(bgm::abs(n1) == p1);
  43. BOOST_CHECK(bgm::abs(n2) == p2);
  44. BOOST_CHECK(bgm::abs(n3) == p3);
  45. }
  46. {
  47. number_types::custom<double> p1(bgm::pi<double>());
  48. number_types::custom_with_global_sqrt<double> p2(bgm::pi<double>());
  49. custom_global<double> p3(bgm::pi<double>());
  50. custom_raw<double> p4(bgm::pi<double>());
  51. BOOST_CHECK(eq(bgm::abs(p1), p1));
  52. BOOST_CHECK(eq(bgm::abs(p2), p2));
  53. BOOST_CHECK(eq(bgm::abs(p3), p3));
  54. BOOST_CHECK(eq(bgm::abs(p4), p4));
  55. number_types::custom<double> n1 = -p1;
  56. number_types::custom_with_global_sqrt<double> n2 = -p2;
  57. custom_global<double> n3 = -p3;
  58. custom_raw<double> n4 = -p4;
  59. BOOST_CHECK(eq(bgm::abs(n1), p1));
  60. BOOST_CHECK(eq(bgm::abs(n2), p2));
  61. BOOST_CHECK(eq(bgm::abs(n3), p3));
  62. BOOST_CHECK(eq(bgm::abs(n4), p4));
  63. }
  64. #ifdef HAVE_TTMATH
  65. {
  66. ttmath_big p1 = bgm::pi<ttmath_big>();
  67. ttmath::Big<1, 4> p1 = bgm::pi<ttmath::Big<1, 4> >();
  68. BOOST_CHECK(bgm::abs(p1) == p1);
  69. BOOST_CHECK(bgm::abs(p2) == p2);
  70. ttmath_big n1 = -p1;
  71. ttmath::Big<1, 4> n2 = -p2;
  72. BOOST_CHECK(bgm::abs(n1) == p1);
  73. BOOST_CHECK(bgm::abs(n2) == p2);
  74. }
  75. #endif
  76. }