File: test_dynamic_handler.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 (118 lines) | stat: -rw-r--r-- 2,600 bytes parent folder | download | duplicates (3)
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
#include "catch.hpp"

#include <osmium/builder/attr.hpp>
#include <osmium/dynamic_handler.hpp>
#include <osmium/visitor.hpp>

struct Handler1 : public osmium::handler::Handler {

    int& count; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)

    explicit Handler1(int& c) :
        count(c) {
    }

    void node(const osmium::Node& /*node*/) noexcept {
        ++count;
    }

    void way(const osmium::Way& /*way*/) noexcept {
        ++count;
    }

    void relation(const osmium::Relation& /*relation*/) noexcept {
        ++count;
    }

    void area(const osmium::Area& /*area*/) noexcept {
        ++count;
    }

    void changeset(const osmium::Changeset& /*changeset*/) noexcept {
        ++count;
    }

    void flush() noexcept {
        ++count;
    }

};

struct Handler2 : public osmium::handler::Handler {

    int& count; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)

    explicit Handler2(int& c) :
        count(c) {
    }

    void node(const osmium::Node& /*node*/) noexcept {
        count += 2;
    }

    void way(const osmium::Way& /*way*/) noexcept {
        count += 2;
    }

    void relation(const osmium::Relation& /*relation*/) noexcept {
        count += 2;
    }

    void area(const osmium::Area& /*area*/) noexcept {
        count += 2;
    }

    void changeset(const osmium::Changeset& /*changeset*/) noexcept {
        count += 2;
    }

};

namespace {

osmium::memory::Buffer fill_buffer() {
    using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
    osmium::memory::Buffer buffer{1024UL * 1024UL, osmium::memory::Buffer::auto_grow::yes};

    osmium::builder::add_node(buffer, _id(1));
    osmium::builder::add_way(buffer, _id(2));
    osmium::builder::add_relation(buffer, _id(3));
    osmium::builder::add_area(buffer, _id(4));
    osmium::builder::add_changeset(buffer, _cid(5));

    return buffer;
}

} // anonymous namespace

TEST_CASE("Base test: static handler") {
    const auto buffer = fill_buffer();

    int count = 0;
    Handler1 h1{count};
    osmium::apply(buffer, h1);
    REQUIRE(count == 6);

    count = 0;
    Handler2 h2{count};
    osmium::apply(buffer, h2);
    REQUIRE(count == 10);
}

TEST_CASE("Dynamic handler") {
    const auto buffer = fill_buffer();

    osmium::handler::DynamicHandler handler;
    osmium::apply(buffer, handler);

    int count = 0;
    handler.set<Handler1>(count);
    osmium::apply(buffer, handler);
    REQUIRE(count == 6);

    count = 0;
    handler.set<Handler2>(count);
    osmium::apply(buffer, handler);
    REQUIRE(count == 10);
}