for_each_coordinate.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <sstream>
  12. #include <geometry_test_common.hpp>
  13. #include <boost/geometry/util/for_each_coordinate.hpp>
  14. #include <boost/geometry/algorithms/assign.hpp>
  15. #include <boost/geometry/geometries/point.hpp>
  16. #include <boost/geometry/geometries/adapted/c_array.hpp>
  17. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  18. #include <test_common/test_point.hpp>
  19. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  20. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  21. struct test_operation
  22. {
  23. template <typename P, int I>
  24. static void apply(P& p)
  25. {
  26. bg::set<I>(p, bg::get<I>(p) * 10);
  27. }
  28. };
  29. struct get_operation
  30. {
  31. std::string s;
  32. template <typename P, int I>
  33. inline void apply(P const& p)
  34. {
  35. std::ostringstream out;
  36. out << bg::get<I>(p);
  37. s += out.str();
  38. }
  39. };
  40. template <typename P>
  41. void test_all()
  42. {
  43. P p;
  44. bg::assign_values(p, 1, 2, 3);
  45. bg::for_each_coordinate(p, test_operation());
  46. BOOST_CHECK(bg::get<0>(p) == 10);
  47. BOOST_CHECK(bg::get<1>(p) == 20);
  48. BOOST_CHECK(bg::get<2>(p) == 30);
  49. P const& cp = p;
  50. get_operation op;
  51. op = bg::for_each_coordinate(cp, op);
  52. BOOST_CHECK(op.s == std::string("102030"));
  53. }
  54. int test_main(int, char* [])
  55. {
  56. test_all<int[3]>();
  57. test_all<float[3]>();
  58. test_all<double[3]>();
  59. test_all<test::test_point>();
  60. test_all<bg::model::point<int, 3, bg::cs::cartesian> >();
  61. test_all<bg::model::point<float, 3, bg::cs::cartesian> >();
  62. test_all<bg::model::point<double, 3, bg::cs::cartesian> >();
  63. return 0;
  64. }