PageRenderTime 40ms CodeModel.GetById 15ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/geometry/test/policies/compare.cpp

https://github.com/delalond/boost_1_54_0-bgq
C++ | 245 lines | 167 code | 51 blank | 27 comment | 2 complexity | 7656c04034be6d6fc1abc5dbc9db3d04 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  5
  6// Use, modification and distribution is subject to the Boost Software License,
  7// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8// http://www.boost.org/LICENSE_1_0.txt)
  9
 10#include <geometry_test_common.hpp>
 11
 12#include <algorithm>
 13
 14#include <boost/geometry/algorithms/make.hpp>
 15#include <boost/geometry/io/dsv/write.hpp>
 16
 17#include <boost/geometry/policies/compare.hpp>
 18
 19#include <boost/geometry/strategies/strategies.hpp>
 20
 21#include <boost/geometry/geometries/point.hpp>
 22#include <boost/geometry/geometries/adapted/c_array.hpp>
 23#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
 24
 25#include <test_common/test_point.hpp>
 26
 27BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
 28BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
 29
 30
 31template <typename Container>
 32inline std::string coordinates(Container const& points)
 33{
 34    std::ostringstream out;
 35    typedef typename boost::range_value<Container>::type point_type;
 36    for (typename boost::range_const_iterator<Container>::type it = boost::begin(points);
 37        it != boost::end(points);
 38        ++it)
 39    {
 40        out << bg::dsv(*it);
 41    }
 42    return out.str();
 43}
 44
 45template <typename P>
 46void test_2d_compare()
 47{
 48    P p1 = bg::make<P>(3, 1);
 49    P p2 = bg::make<P>(3, 1);
 50    P p3 = bg::make<P>(1, 3);
 51    P p4 = bg::make<P>(5, 2);
 52    P p5 = bg::make<P>(3, 2);
 53
 54    // Test in all dimensions
 55    {
 56        bg::equal_to<P> et;
 57        bg::less<P> lt;
 58        bg::greater<P> gt;
 59
 60        BOOST_CHECK_EQUAL(et(p1, p2), true);
 61        BOOST_CHECK_EQUAL(et(p1, p3), false);
 62        BOOST_CHECK_EQUAL(et(p1, p4), false);
 63        BOOST_CHECK_EQUAL(et(p1, p5), false);
 64        BOOST_CHECK_EQUAL(et(p3, p4), false);
 65
 66        BOOST_CHECK_EQUAL(lt(p1, p2), false);
 67        BOOST_CHECK_EQUAL(lt(p1, p3), false);
 68        BOOST_CHECK_EQUAL(lt(p1, p4), true);
 69        BOOST_CHECK_EQUAL(lt(p1, p5), true);
 70        BOOST_CHECK_EQUAL(lt(p3, p4), true);
 71
 72        BOOST_CHECK_EQUAL(gt(p1, p2), false);
 73        BOOST_CHECK_EQUAL(gt(p1, p3), true);
 74        BOOST_CHECK_EQUAL(gt(p1, p4), false);
 75        BOOST_CHECK_EQUAL(gt(p1, p5), false);
 76        BOOST_CHECK_EQUAL(gt(p3, p4), false);
 77    }
 78
 79
 80    // Test in dimension 0, X
 81    {
 82        bg::equal_to<P, 0> et;
 83        bg::less<P, 0> lt;
 84        bg::greater<P, 0> gt;
 85
 86        BOOST_CHECK_EQUAL(et(p1, p2), true);
 87        BOOST_CHECK_EQUAL(et(p1, p3), false);
 88        BOOST_CHECK_EQUAL(et(p1, p4), false);
 89        BOOST_CHECK_EQUAL(et(p1, p5), true);
 90        BOOST_CHECK_EQUAL(et(p3, p4), false);
 91
 92        BOOST_CHECK_EQUAL(lt(p1, p2), false);
 93        BOOST_CHECK_EQUAL(lt(p1, p3), false);
 94        BOOST_CHECK_EQUAL(lt(p1, p4), true);
 95        BOOST_CHECK_EQUAL(lt(p1, p5), false);
 96        BOOST_CHECK_EQUAL(lt(p3, p4), true);
 97
 98        BOOST_CHECK_EQUAL(gt(p1, p2), false);
 99        BOOST_CHECK_EQUAL(gt(p1, p3), true);
100        BOOST_CHECK_EQUAL(gt(p1, p4), false);
101        BOOST_CHECK_EQUAL(gt(p1, p5), false);
102        BOOST_CHECK_EQUAL(gt(p3, p4), false);
103    }
104
105    // Test in dimension 1, Y
106    {
107        bg::equal_to<P, 1> et;
108        bg::less<P, 1> lt;
109        bg::greater<P, 1> gt;
110
111        BOOST_CHECK_EQUAL(et(p1, p2), true);
112        BOOST_CHECK_EQUAL(et(p1, p3), false);
113        BOOST_CHECK_EQUAL(et(p1, p4), false);
114        BOOST_CHECK_EQUAL(et(p1, p5), false);
115        BOOST_CHECK_EQUAL(et(p3, p4), false);
116
117        BOOST_CHECK_EQUAL(lt(p1, p2), false);
118        BOOST_CHECK_EQUAL(lt(p1, p3), true);
119        BOOST_CHECK_EQUAL(lt(p1, p4), true);
120        BOOST_CHECK_EQUAL(lt(p1, p5), true);
121        BOOST_CHECK_EQUAL(lt(p3, p4), false);
122
123        BOOST_CHECK_EQUAL(gt(p1, p2), false);
124        BOOST_CHECK_EQUAL(gt(p1, p3), false);
125        BOOST_CHECK_EQUAL(gt(p1, p4), false);
126        BOOST_CHECK_EQUAL(gt(p1, p5), false);
127        BOOST_CHECK_EQUAL(gt(p3, p4), true);
128    }
129}
130
131
132template <typename P>
133void test_2d_sort()
134{
135    typedef typename bg::coordinate_type<P>::type ct;
136
137    std::vector<P> v;
138    v.push_back(bg::make<P>(3, 1));
139    v.push_back(bg::make<P>(2, 3));
140    v.push_back(bg::make<P>(2, 2));
141    v.push_back(bg::make<P>(1, 3));
142
143    // Sort on coordinates in order x,y,z
144    std::sort(v.begin(), v.end(), bg::less<P>());
145    std::string s = coordinates(v);
146    BOOST_CHECK_EQUAL(s, "(1, 3)(2, 2)(2, 3)(3, 1)");
147
148    // Reverse sort
149    std::sort(v.begin(), v.end(), bg::greater<P>());
150    s = coordinates(v);
151    BOOST_CHECK_EQUAL(s, "(3, 1)(2, 3)(2, 2)(1, 3)");
152
153    // Sort backwards on coordinates in order x,y,z
154    //std::sort(v.begin(), v.end(), bg::greater<P>());
155    //std::string s = coordinates(v);
156    //BOOST_CHECK_EQUAL(s, "(1, 3)(2, 2)(2, 3)(3, 1)");
157
158    // Refill to remove duplicate coordinates
159    v.clear();
160    v.push_back(bg::make<P>(4, 1));
161    v.push_back(bg::make<P>(3, 2));
162    v.push_back(bg::make<P>(2, 3));
163    v.push_back(bg::make<P>(1, 4));
164
165    // Sort ascending on only x-coordinate
166    std::sort(v.begin(), v.end(), bg::less<P, 0>());
167    s = coordinates(v);
168    BOOST_CHECK_EQUAL(s, "(1, 4)(2, 3)(3, 2)(4, 1)");
169
170    // Sort ascending on only y-coordinate
171    std::sort(v.begin(), v.end(), bg::less<P, 1>());
172    s = coordinates(v);
173    BOOST_CHECK_EQUAL(s, "(4, 1)(3, 2)(2, 3)(1, 4)");
174
175    // Sort descending on only x-coordinate
176    std::sort(v.begin(), v.end(), bg::greater<P, 0>());
177    s = coordinates(v);
178    //BOOST_CHECK_EQUAL(s, "(4, 1)(3, 2)(2, 3)(1, 4)");
179
180    // Sort descending on only y-coordinate
181    std::sort(v.begin(), v.end(), bg::greater<P, 1>());
182    s = coordinates(v);
183    BOOST_CHECK_EQUAL(s, "(1, 4)(2, 3)(3, 2)(4, 1)");
184
185    // Make non-unique vector
186    v.push_back(bg::make<P>(4, 1));
187    v.push_back(bg::make<P>(3, 2));
188    v.push_back(bg::make<P>(2, 3));
189    v.push_back(bg::make<P>(1, 4));
190    v.push_back(bg::make<P>(1, 5));
191    std::sort(v.begin(), v.end(), bg::less<P>());
192    s = coordinates(v);
193    BOOST_CHECK_EQUAL(s, "(1, 4)(1, 4)(1, 5)(2, 3)(2, 3)(3, 2)(3, 2)(4, 1)(4, 1)");
194
195
196    std::vector<P> v2;
197    std::unique_copy(v.begin(), v.end(), std::back_inserter(v2), bg::equal_to<P>());
198    s = coordinates(v2);
199    BOOST_CHECK_EQUAL(s, "(1, 4)(1, 5)(2, 3)(3, 2)(4, 1)");
200}
201
202
203template <typename P>
204void test_spherical()
205{
206    typedef typename bg::coordinate_type<P>::type ct;
207
208    std::vector<P> v;
209    v.push_back(bg::make<P>( 179.73, 71.56)); // east
210    v.push_back(bg::make<P>( 177.47, 71.23)); // less east
211    v.push_back(bg::make<P>(-178.78, 70.78)); // further east, = west, this is the most right point
212
213    // Sort on coordinates in order x,y,z
214    std::sort(v.begin(), v.end(), bg::less<P>());
215    std::string s = coordinates(v);
216    BOOST_CHECK_EQUAL(s, "(177.47, 71.23)(179.73, 71.56)(-178.78, 70.78)");
217
218    // Sort ascending on only x-coordinate
219    std::sort(v.begin(), v.end(), bg::less<P, 0>());
220    s = coordinates(v);
221    BOOST_CHECK_EQUAL(s, "(177.47, 71.23)(179.73, 71.56)(-178.78, 70.78)");
222
223    // Sort ascending on only x-coordinate, but override with std-comparison,
224    // (so this is the normal sorting behaviour that would have been used
225    // if it would not have been spherical)
226    std::sort(v.begin(), v.end(), bg::less<P, 0, std::less<ct> >());
227    s = coordinates(v);
228    BOOST_CHECK_EQUAL(s, "(-178.78, 70.78)(177.47, 71.23)(179.73, 71.56)");
229}
230
231
232int test_main(int, char* [])
233{
234    test_2d_compare<bg::model::point<int, 2, bg::cs::cartesian> >();
235    test_2d_compare<bg::model::point<double, 2, bg::cs::cartesian> >();
236
237    test_2d_sort<bg::model::point<int, 2, bg::cs::cartesian> >();
238    test_2d_sort<bg::model::point<float, 2, bg::cs::cartesian> >();
239    test_2d_sort<boost::tuple<double, double> >();
240    test_2d_sort<bg::model::point<double, 2, bg::cs::cartesian> >();
241
242    test_spherical<bg::model::point<double, 2, bg::cs::spherical<bg::degree> > >();
243
244    return 0;
245}