File: test-persistent-node-cache.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 (119 lines) | stat: -rw-r--r-- 3,382 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
#include <cassert>
#include <iostream>

#include "node-persistent-cache.hpp"
#include "options.hpp"

#include "tests/common-cleanup.hpp"

#define FLAT_NODES_FILE_NAME "tests/test_middle_flat.flat.nodes.bin"

template <typename T>
void assert_equal(T actual, T expected)
{
    if (actual != expected) {
        std::cerr << "Expected " << expected << ", but got " << actual << ".\n";
        exit(1);
    }
}

void write_and_read_location(node_persistent_cache &cache, osmid_t id, double x,
                             double y)
{
    cache.set(id, osmium::Location(x, y));
    assert_equal(osmium::Location(x, y), cache.get(id));
}

void read_invalid_location(node_persistent_cache &cache, osmid_t id)
{
    assert_equal(osmium::Location(), cache.get(id));
}

void read_location(node_persistent_cache &cache, osmid_t id, double x, double y)
{
    assert_equal(osmium::Location(x, y), cache.get(id));
}

void delete_location(node_persistent_cache &cache, osmid_t id)
{
    cache.set(id, osmium::Location());
    assert_equal(osmium::Location(), cache.get(id));
}

void test_create()
{
    options_t options;
    options.flat_node_file = boost::optional<std::string>(FLAT_NODES_FILE_NAME);

    auto ram_cache = std::make_shared<node_ram_cache>(0, 0); // empty cache

    node_persistent_cache cache(&options, ram_cache);

    // write in order
    write_and_read_location(cache, 10, 10.01, -45.3);
    write_and_read_location(cache, 11, -0.4538, 22.22);
    write_and_read_location(cache, 1058, 9.4, 9);
    write_and_read_location(cache, 502754, 0.0, 0.0);

    // write out-of-order
    write_and_read_location(cache, 9934, -179.999, 89.1);

    // read non-existing in middle
    read_invalid_location(cache, 0);
    read_invalid_location(cache, 1111);
    read_invalid_location(cache, 1);

    // read non-existing after the last node
    read_invalid_location(cache, 502755);
    read_invalid_location(cache, 7772947204);
}

void test_append()
{
    options_t options;
    options.flat_node_file = boost::optional<std::string>(FLAT_NODES_FILE_NAME);

    auto ram_cache = std::make_shared<node_ram_cache>(0, 0); // empty cache

    node_persistent_cache cache(&options, ram_cache);

    // read all previously written locations
    read_location(cache, 10, 10.01, -45.3);
    read_location(cache, 11, -0.4538, 22.22);
    read_location(cache, 1058, 9.4, 9);
    read_location(cache, 502754, 0.0, 0.0);
    read_location(cache, 9934, -179.999, 89.1);

    // everything else should still be invalid
    read_invalid_location(cache, 0);
    read_invalid_location(cache, 12);
    read_invalid_location(cache, 1059);
    read_invalid_location(cache, 1);
    read_invalid_location(cache, 1057);
    read_invalid_location(cache, 502753);
    read_invalid_location(cache, 502755);
    read_invalid_location(cache, 77729404);

    // write new data in the middle
    write_and_read_location(cache, 13, 10.01, -45.3);
    write_and_read_location(cache, 3000, 45, 45);

    // append new data
    write_and_read_location(cache, 502755, 87, 0.45);
    write_and_read_location(cache, 502756, 87.12, 0.46);
    write_and_read_location(cache, 510000, 44, 0.0);

    // delete existing
    delete_location(cache, 11);

    // delete non-existing
    delete_location(cache, 21);
}

int main()
{
    cleanup::file flat_nodes_file(FLAT_NODES_FILE_NAME);

    test_create();
    test_append();
}