File: closeable_view.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (134 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2010-2012 Barend Gehrels, Amsterdam, the Netherlands.

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>

#include <geometry_test_common.hpp>

#include <boost/geometry/views/closeable_view.hpp>

#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/io/dsv/write.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)



// The closeable view should also work on normal std:: containers
void test_non_geometry()
{
    typedef bg::closeable_view
        <
            std::vector<int> const, bg::open
        >::type view_type;

    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    view_type view(v);

    typedef boost::range_iterator<view_type const>::type iterator;
    iterator it = boost::begin(view);
    iterator end = boost::end(view);

    std::ostringstream out;
    for ( ; it != end; ++it)
    {
        out << *it;
    }
    BOOST_CHECK_EQUAL(out.str(), "1231");

    // Check operators =, ++, --, +=, -=
    it = boost::begin(view);
    BOOST_CHECK_EQUAL(*it, 1);
    it += 2;
    BOOST_CHECK_EQUAL(*it, 3);
    it -= 2;
    BOOST_CHECK_EQUAL(*it, 1);
    it++;
    BOOST_CHECK_EQUAL(*it, 2);
    it--;
    BOOST_CHECK_EQUAL(*it, 1);

    // Also check them in the last regions
    it = boost::begin(view) + 3;
    BOOST_CHECK_EQUAL(*it, 1);
    it--;
    BOOST_CHECK_EQUAL(*it, 3);
    it++;
    BOOST_CHECK_EQUAL(*it, 1);
    it -= 2;
    BOOST_CHECK_EQUAL(*it, 2);
    it += 2;
    BOOST_CHECK_EQUAL(*it, 1);

    BOOST_CHECK_EQUAL(boost::size(view), 4u);
}


template <bg::closure_selector Closure, typename Range>
void test_optionally_closing(Range const& range, std::string const& expected)
{
    typedef typename bg::closeable_view<Range const, Closure>::type view_type;
    typedef typename boost::range_iterator<view_type const>::type iterator;

    view_type view(range);

    bool first = true;
    std::ostringstream out;
    iterator end = boost::end(view);
    for (iterator it = boost::begin(view); it != end; ++it, first = false)
    {
        out << (first ? "" : " ") << bg::dsv(*it);
    }
    BOOST_CHECK_EQUAL(out.str(), expected);
}


template <typename Geometry>
void test_geometry(std::string const& wkt,
            std::string const& expected_false,
            std::string const& expected_true)
{
    Geometry geo;
    bg::read_wkt(wkt, geo);

    test_optionally_closing<bg::closed>(geo, expected_false);
    test_optionally_closing<bg::open>(geo, expected_true);
}


template <typename P>
void test_all()
{
    test_geometry<bg::model::ring<P> >(
            "POLYGON((1 1,1 4,4 4,4 1))",
            "(1, 1) (1, 4) (4, 4) (4, 1)",
            "(1, 1) (1, 4) (4, 4) (4, 1) (1, 1)");
}


int test_main(int, char* [])
{
    test_non_geometry();

    test_all<bg::model::d2::point_xy<double> >();
    test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
    test_all<boost::tuple<double, double> >();

    return 0;
}