rational.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <geometry_test_common.hpp>
  12. #include <boost/geometry/geometries/geometries.hpp>
  13. #include <boost/geometry/io/wkt/wkt.hpp>
  14. #include <boost/geometry/util/rational.hpp>
  15. void test_coordinate_cast(std::string const& s, int expected_nom, int expected_denom)
  16. {
  17. boost::rational<int> a = bg::detail::coordinate_cast<boost::rational<int> >::apply(s);
  18. BOOST_CHECK_EQUAL(a.numerator(), expected_nom);
  19. BOOST_CHECK_EQUAL(a.denominator(), expected_denom);
  20. }
  21. void test_wkt(std::string const& wkt, std::string const expected_wkt)
  22. {
  23. bg::model::point<boost::rational<int>, 2, bg::cs::cartesian> p;
  24. bg::read_wkt(wkt, p);
  25. std::ostringstream out;
  26. out << bg::wkt(p);
  27. BOOST_CHECK_EQUAL(out.str(), expected_wkt);
  28. }
  29. int test_main(int, char* [])
  30. {
  31. test_coordinate_cast("0", 0, 1);
  32. test_coordinate_cast("1", 1, 1);
  33. test_coordinate_cast("-1", -1, 1);
  34. test_coordinate_cast("-0.5", -1, 2);
  35. test_coordinate_cast("-1.5", -3, 2);
  36. test_coordinate_cast("0.5", 1, 2);
  37. test_coordinate_cast("1.5", 3, 2);
  38. test_coordinate_cast("2.12345", 42469, 20000);
  39. test_coordinate_cast("1.", 1, 1);
  40. test_coordinate_cast("3/2", 3, 2);
  41. test_coordinate_cast("-3/2", -3, 2);
  42. test_wkt("POINT(1.5 2.75)", "POINT(3/2 11/4)");
  43. test_wkt("POINT(3/2 11/4)", "POINT(3/2 11/4)");
  44. test_wkt("POINT(-1.5 2.75)", "POINT(-3/2 11/4)");
  45. test_wkt("POINT(-3/2 11/4)", "POINT(-3/2 11/4)");
  46. return 0;
  47. }