File: test_apply.cpp

package info (click to toggle)
libosmium 2.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,544 kB
  • sloc: cpp: 52,804; sh: 148; makefile: 19
file content (129 lines) | stat: -rw-r--r-- 3,286 bytes parent folder | download | duplicates (4)
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
#include "catch.hpp"

#include "utils.hpp"

#include <osmium/handler.hpp>
#include <osmium/handler/node_locations_for_ways.hpp>
#include <osmium/index/map/flex_mem.hpp>
#include <osmium/io/xml_input.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/visitor.hpp>

TEST_CASE("apply with lambdas on reader") {
    const osmium::io::File file{with_data_dir("t/relations/data.osm")};
    osmium::io::Reader reader{file};

    int count_n = 0;
    int count_w = 0;
    int count_r = 0;
    int count_o = 0;
    int count_a = 0;

    osmium::apply(reader,
        [&](const osmium::Node& node) {
            count_n += node.version();
        },
        [&](const osmium::Way& way) {
            if (way.id() == 20) {
                ++count_w;
            }
        },
        [&](const osmium::Relation& relation) {
            if (relation.id() > 30) {
                ++count_r;
            }
        },
        [&](const osmium::OSMObject& object) {
            if (object.id() % 10 == 0) {
                ++count_o;
            }
        },
        [&](const osmium::Way& way) {
            if (way.id() == 21) {
                ++count_w;
            }
        },
        [&](const osmium::Area& /*area*/) {
            ++count_a;
        }
    );

    REQUIRE(count_n == 5);
    REQUIRE(count_w == 2);
    REQUIRE(count_r == 2);
    REQUIRE(count_o == 3);
    REQUIRE(count_a == 0);
}

TEST_CASE("apply with lambda on buffer") {
    const osmium::io::File file{with_data_dir("t/relations/data.osm")};
    osmium::io::Reader reader{file};

    const auto buffer = reader.read();
    reader.close();

    std::size_t members = 0;

    osmium::apply(buffer, [&](const osmium::Relation& relation) {
        members += relation.members().size();
    });

    REQUIRE(members == 5);
}

TEST_CASE("apply on non-const buffer can change data") {
    const osmium::io::File file{with_data_dir("t/relations/data.osm")};
    osmium::io::Reader reader{file};

    auto buffer = reader.read();
    reader.close();

    int nodes = 0;

    osmium::apply(buffer,
        [&](osmium::Node& node) {
            node.set_version(123);
        },
        [&](const osmium::Node& node) {
            ++nodes;
            REQUIRE(node.version() == 123);
        }
    );

    REQUIRE(nodes == 5);
}

TEST_CASE("apply with handler and lambda") {
    using index_type = osmium::index::map::FlexMem<osmium::unsigned_object_id_type, osmium::Location>;
    using location_handler_type = osmium::handler::NodeLocationsForWays<index_type>;

    const osmium::io::File file{with_data_dir("t/relations/data.osm")};
    osmium::io::Reader reader{file};

    auto buffer = reader.read();
    reader.close();

    index_type index;
    location_handler_type location_handler{index};

    int64_t x = 0;
    int64_t y = 0;

    osmium::apply(buffer,
        [&](const osmium::Way& way) {
            REQUIRE_FALSE(way.nodes().front().location().valid());
        },
        location_handler,
        [&](const osmium::Way& way) {
            REQUIRE(way.nodes().front().location().valid());
            for (const auto& wn : way.nodes()) {
                x += wn.location().x();
                y += wn.location().y();
            }
        }
    );

    REQUIRE(x == 44000000);
    REQUIRE(y == 40000000);
}