File: test_members_database.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 (200 lines) | stat: -rw-r--r-- 6,002 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "catch.hpp"

#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/relations/members_database.hpp>
#include <osmium/relations/relations_database.hpp>
#include <osmium/storage/item_stash.hpp>

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_relation(buffer,
        _id(20),
        _member(osmium::item_type::way, 10, "outer")
    );

    osmium::builder::add_relation(buffer,
        _id(21),
        _member(osmium::item_type::way, 11, "outer"),
        _member(osmium::item_type::way, 12, "outer")
    );

    osmium::builder::add_relation(buffer,
        _id(22),
        _member(osmium::item_type::way, 13, "outer"),
        _member(osmium::item_type::way, 10, "inner"),
        _member(osmium::item_type::way, 14, "inner")
    );

    osmium::builder::add_way(buffer, _id(10));
    osmium::builder::add_way(buffer, _id(11));
    osmium::builder::add_way(buffer, _id(12));
    osmium::builder::add_way(buffer, _id(13));
    osmium::builder::add_way(buffer, _id(14));
    osmium::builder::add_way(buffer, _id(15));

    return buffer;
}

} // anonymous namespace

TEST_CASE("Fill member database") {
    const auto buffer = fill_buffer();

    osmium::ItemStash stash;
    osmium::relations::RelationsDatabase rdb{stash};
    osmium::relations::MembersDatabase<osmium::Way> mdb{stash, rdb};

    REQUIRE(mdb.used_memory() < 100);

    for (const auto& relation : buffer.select<osmium::Relation>()) {
        auto handle = rdb.add(relation);
        int n = 0;
        for (const auto& member : relation.members()) {
            mdb.track(handle, member.ref(), n);
            ++n;
        }
    }

    mdb.prepare_for_lookup();

    int n = 0;
    int match = 0;
    for (const auto& way : buffer.select<osmium::Way>()) {
        const bool added = mdb.add(way, [&](osmium::relations::RelationHandle& rel_handle) {
            ++match;
            switch (n) {
                case 0: // added w10
                    REQUIRE(rel_handle->id() == 20);
                    break;
                case 2: // added w11 and w12
                    REQUIRE(rel_handle->id() == 21);
                    break;
                case 4: // added w13 and w14
                    REQUIRE(rel_handle->id() == 22);
                    break;
                default:
                    REQUIRE(false);
                    break;
            }
        });

        REQUIRE(added == (way.id() != 15));

        if (way.id() == 11) {
            const auto* way_ptr = mdb.get(way.id());
            REQUIRE(way_ptr);
            REQUIRE(*way_ptr == way);
            const auto* object = mdb.get_object(way.id());
            REQUIRE(object);
            REQUIRE(object->id() == way.id());
        }

        ++n;
    }

    REQUIRE(match == 3);
    REQUIRE(mdb.used_memory() > 100);
}

TEST_CASE("Member database with duplicate member in relation") {
    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_relation(buffer,
        _id(20),
        _member(osmium::item_type::way, 10, "outer"),
        _member(osmium::item_type::way, 11, "inner"),
        _member(osmium::item_type::way, 12, "inner"),
        _member(osmium::item_type::way, 11, "inner")
    );

    osmium::builder::add_way(buffer, _id(10));
    osmium::builder::add_way(buffer, _id(11));
    osmium::builder::add_way(buffer, _id(12));

    osmium::ItemStash stash;
    osmium::relations::RelationsDatabase rdb{stash};
    osmium::relations::MembersDatabase<osmium::Way> mdb{stash, rdb};

    for (const auto& relation : buffer.select<osmium::Relation>()) {
        auto handle = rdb.add(relation);
        int n = 0;
        for (const auto& member : relation.members()) {
            mdb.track(handle, member.ref(), n);
            ++n;
        }
    }

    mdb.prepare_for_lookup();

    REQUIRE(mdb.size() == 4);
    {
        const auto counts = mdb.count();
        REQUIRE(counts.tracked   == 4);
        REQUIRE(counts.available == 0);
        REQUIRE(counts.removed   == 0);
    }

    int n = 0;
    for (const auto& way : buffer.select<osmium::Way>()) {
        mdb.add(way, [&](osmium::relations::RelationHandle& rel_handle) {
            ++n;
            REQUIRE(rel_handle->id() == 20);
            {
                const auto counts = mdb.count();
                REQUIRE(counts.tracked   == 0);
                REQUIRE(counts.available == 4);
                REQUIRE(counts.removed   == 0);
            }

            // relation is complete here, normal code would handle it here

            for (const auto& member : rel_handle->members()) {
                mdb.remove(member.ref(), rel_handle->id());
            }
            rel_handle.remove();
        });
    }

    REQUIRE(n == 1);

    REQUIRE(rdb.size() == 1);
    REQUIRE(rdb.count_relations() == 0);

    REQUIRE(mdb.size() == 4);
    {
        const auto counts = mdb.count();
        REQUIRE(counts.tracked   == 0);
        REQUIRE(counts.available == 0);
        REQUIRE(counts.removed   == 4);
    }
}

TEST_CASE("Remove non-existing object from members database doesn't do anything") {
    const auto buffer = fill_buffer();

    osmium::ItemStash stash;
    osmium::relations::RelationsDatabase rdb{stash};
    osmium::relations::MembersDatabase<osmium::Way> mdb{stash, rdb};

    for (const auto& relation : buffer.select<osmium::Relation>()) {
        auto handle = rdb.add(relation);
        int n = 0;
        for (const auto& member : relation.members()) {
            mdb.track(handle, member.ref(), n);
            ++n;
        }
    }

    mdb.prepare_for_lookup();

    REQUIRE(mdb.size() == 6);
    mdb.remove(100, 100);
    REQUIRE(mdb.size() == 6);
}