File: middle-tests.cpp

package info (click to toggle)
osm2pgsql 0.96.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,304 kB
  • sloc: cpp: 11,462; python: 543; sh: 98; makefile: 17
file content (286 lines) | stat: -rw-r--r-- 8,821 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cassert>
#include <list>
#include <tuple>

#include "osmtypes.hpp"
#include "reprojection.hpp"
#include "tests/middle-tests.hpp"

#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>

#define BLOCK_SHIFT 13
#define PER_BLOCK  (((osmid_t)1) << BLOCK_SHIFT)

// simple osmium buffer to store all the objects in
osmium::memory::Buffer buffer(4096, osmium::memory::Buffer::auto_grow::yes);
// do not project anything
std::shared_ptr<reprojection>
    proj(reprojection::create_projection(PROJ_LATLONG));

#define ALLOWED_ERROR 10e-9
bool node_okay(osmium::Location loc, osmium::Node const &expected)
{
    if ((loc.lat() > expected.location().lat() + ALLOWED_ERROR) ||
        (loc.lat() < expected.location().lat() - ALLOWED_ERROR)) {
        std::cerr << "ERROR: Node should have lat=" << expected.location().lat()
                  << ", but got back " << loc.lat() << " from middle.\n";
        return false;
    }
    if ((loc.lon() > expected.location().lon() + ALLOWED_ERROR) ||
        (loc.lon() < expected.location().lon() - ALLOWED_ERROR)) {
        std::cerr << "ERROR: Node should have lon=" << expected.location().lon()
                  << ", but got back " << loc.lon() << " from middle.\n";
        return false;
    }
    return true;
}

size_t add_node(osmid_t id, double lat, double lon)
{
    using namespace osmium::builder::attr;
    return osmium::builder::add_node(buffer, _id(id), _location(lon, lat));
}

size_t way_with_nodes(std::vector<osmid_t> const &ids)
{
    using namespace osmium::builder::attr;
    return osmium::builder::add_way(buffer, _nodes(ids));
}

int test_node_set(middle_t *mid)
{
    buffer.clear();

    auto const &node = buffer.get<osmium::Node>(add_node(1234, 12.3456789, 98.7654321));
    auto &way = buffer.get<osmium::Way>(way_with_nodes({node.id()}));

    // set the node
    mid->nodes_set(node);

    // get it back
    if (mid->nodes_get_list(&(way.nodes())) != way.nodes().size()) {
        std::cerr << "ERROR: Unable to get node list.\n";
        return 1;
    }

    // check that it's the same
    if (!node_okay(way.nodes()[0].location(), node)) {
        return 1;
    }

    return 0;
}

inline double test_lat(osmid_t id) {
    return 1 + 1e-5 * id;
}

int test_nodes_comprehensive_set(middle_t *mid)
{
    std::vector<size_t> expected_nodes;
    expected_nodes.reserve(PER_BLOCK*8+1);

    buffer.clear();

    // 2 dense blocks, the second partially filled at the star
    for (osmid_t id = 0; id < (PER_BLOCK+(PER_BLOCK >> 1) + 1); ++id)
    {
        expected_nodes.emplace_back(add_node(id, test_lat(id), 0.0));
    }

    // 1 dense block, 75% filled
    for (osmid_t id = PER_BLOCK*2; id < PER_BLOCK*3; ++id)
    {
        if ((id % 4 == 0) || (id % 4 == 1) || (id % 4 == 2))
            expected_nodes.emplace_back(add_node(id, test_lat(id), 0.0));
    }

    // 1 dense block, sparsly filled
    for (osmid_t id = PER_BLOCK*3; id < PER_BLOCK*4; ++id)
    {
        if (id % 4 == 0)
            expected_nodes.emplace_back(add_node(id, test_lat(id), 0.0));
    }

    // A lone sparse node
    expected_nodes.emplace_back(add_node(PER_BLOCK*5, test_lat(PER_BLOCK*5), 0.0));

    // A dense block of alternating positions of zero/non-zero
    for (osmid_t id = PER_BLOCK*6; id < PER_BLOCK*7; ++id)
    {
        if (id % 2 == 0)
            expected_nodes.emplace_back(add_node(id, 0.0, 0.0));
        else
            expected_nodes.emplace_back(add_node(id, test_lat(id), 0.0));
    }
    expected_nodes.emplace_back(add_node(PER_BLOCK*8, 0.0, 0.0));
    expected_nodes.emplace_back(add_node(PER_BLOCK*8+1, 0.0, 0.0));

    // Load up the nodes into the middle
    std::vector<osmid_t> ids;

    for (auto pos : expected_nodes)
    {
        auto const &node = buffer.get<osmium::Node>(pos);
        mid->nodes_set(node);
        ids.push_back(node.id());
    }

    auto &way = buffer.get<osmium::Way>(way_with_nodes(ids));

    if (mid->nodes_get_list(&(way.nodes())) != ids.size()) {
        std::cerr << "ERROR: Unable to get node list.\n";
        return 1;
    }

    for (size_t i = 0; i < ids.size(); ++i) {
        auto const &node = buffer.get<osmium::Node>(expected_nodes[i]);
        if (!node_okay(way.nodes()[i].location(), node)) {
            return 1;
        }
    }

    return 0;
}

struct test_pending_processor : public middle_t::pending_processor {
    test_pending_processor(): pending_ways(), pending_rels() {}
    ~test_pending_processor() {}
    void enqueue_ways(osmid_t id) override
    {
        pending_ways.push_back(id);
    }
    void process_ways() override
    {
        pending_ways.clear();
    }
    void enqueue_relations(osmid_t id) override
    {
        pending_rels.push_back(id);
    }
    void process_relations() override
    {
        pending_rels.clear();
    }
    std::list<osmid_t> pending_ways;
    std::list<osmid_t> pending_rels;
};

int test_way_set(middle_t *mid)
{
    buffer.clear();

    osmid_t way_id = 1;
    double lat = 12.3456789;
    double lon = 98.7654321;
    idlist_t nds;
    std::vector<size_t> nodes;

    // set nodes
    for (osmid_t i = 1; i <= 10; ++i) {
        nds.push_back(i);
        nodes.push_back(add_node(i, lat, lon));
        auto const &node = buffer.get<osmium::Node>(nodes.back());
        mid->nodes_set(node);
    }

    // set the way
    {
        using namespace osmium::builder::attr;
        auto pos = osmium::builder::add_way(buffer, _id(way_id), _nodes(nds));
        auto const &way = buffer.get<osmium::Way>(pos);
        mid->ways_set(way);
    }

    // commit the setup data
    mid->commit();

    // get it back
    osmium::memory::Buffer relbuf(4096, osmium::memory::Buffer::auto_grow::yes);
    {
        using namespace osmium::builder::attr;
        osmium::builder::add_relation(
            relbuf, _id(123), _member(osmium::item_type::node, 132),
            _member(osmium::item_type::way, way_id, "outer"));
    }

    auto const &rel = relbuf.get<osmium::Relation>(0);

    auto buf_pos = buffer.committed();
    rolelist_t roles;
    size_t way_count = mid->rel_way_members_get(rel, &roles, buffer);
    if (way_count != 1) { std::cerr << "ERROR: Unable to get way list.\n"; return 1; }

    if (roles.size() != 1) {
        std::cerr << "Bad length of role ist. Expected 1, got " << roles.size()
                  << ".\n";
        return 1;
    }

    if (strcmp(roles[0], "outer") != 0) {
        std::cerr << "Bad role. Expected 'outer', got '" << roles[0] << "'.\n";
        return 1;
    }

    auto &way = buffer.get<osmium::Way>(buf_pos);
    // check that it's the same
    if (way.nodes().size() != nds.size()) {
        std::cerr << "ERROR: Way should have " << nds.size() << " nodes, but got back "
            << way.nodes().size() << " from middle.\n";
        return 1;
    }
    if (way.id() != way_id) {
        std::cerr << "ERROR: Way should have id=" << way_id << ", but got back "
            << way.id() << " from middle.\n";
        return 1;
    }
    mid->nodes_get_list(&(way.nodes()));
    for (size_t i = 0; i < nds.size(); ++i) {
        if (way.nodes()[i].location().lon() != lon) {
            std::cerr << "ERROR: Way node should have lon=" << lon
                      << ", but got back " << way.nodes()[i].location().lon()
                      << " from middle.\n";
            return 1;
        }
        if (way.nodes()[i].location().lat() != lat) {
            std::cerr << "ERROR: Way node should have lat=" << lat
                      << ", but got back " << way.nodes()[i].location().lat()
                      << " from middle.\n";
            return 1;
        }
    }

    // the way we just inserted should not be pending
    test_pending_processor tpp;
    mid->iterate_ways(tpp);
    if (mid->pending_count() != 0) {
        std::cerr << "ERROR: Was expecting no pending ways, but got "
            << mid->pending_count() << " from middle.\n";
        return 1;
    }

    // some middles don't support changing the nodes - they
    // don't have diff update ability. here, we will just
    // skip the test for that.
    if (dynamic_cast<slim_middle_t *>(mid)) {
        slim_middle_t *slim = dynamic_cast<slim_middle_t *>(mid);

        // finally, try touching a node on a non-pending way. that should
        // make it become pending. we just checked that the way is not
        // pending, so any change must be due to the node changing.
        slim->node_changed(nds[0]);
        slim->iterate_ways(tpp);
        if (slim->pending_count() != 1) {
            std::cerr << "ERROR: Was expecting a single pending way from node update, but got "
                << slim->pending_count() << " from middle.\n";
            return 1;
        }
    }

    return 0;
}