File: test-ordered-index.cpp

package info (click to toggle)
osm2pgsql 2.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,652 kB
  • sloc: cpp: 59,934; python: 1,039; ansic: 763; sh: 25; makefile: 14
file content (146 lines) | stat: -rw-r--r-- 4,416 bytes parent folder | download | duplicates (2)
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
/**
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This file is part of osm2pgsql (https://osm2pgsql.org/).
 *
 * Copyright (C) 2006-2025 by the osm2pgsql developer community.
 * For a full list of authors see the git log.
 */

#include <catch.hpp>

#include "ordered-index.hpp"

TEST_CASE("ordered index basics", "[NoDB]")
{
    constexpr std::size_t const block_size = 16;
    ordered_index_t index{block_size};

    REQUIRE(index.size() == 0);
    REQUIRE(index.capacity() == 0);
    REQUIRE(index.used_memory() == 0);

    index.add(17, 32);
    REQUIRE(index.size() == 1);
    REQUIRE(index.capacity() == block_size);
    REQUIRE(index.used_memory() > 0);

    index.add(19, 33);
    REQUIRE(index.size() == 2);
    REQUIRE(index.capacity() == block_size);
    REQUIRE(index.used_memory() > 0);

    index.clear();
    REQUIRE(index.size() == 0);
    REQUIRE(index.capacity() == 0);
    REQUIRE(index.used_memory() == 0);
}

TEST_CASE("ordered index set/get", "[NoDB]")
{
    constexpr std::size_t const block_size = 16;
    ordered_index_t index{block_size};

    index.add(19, 0);
    index.add(22, 10);
    index.add(23, 22);
    index.add(26, 24);
    REQUIRE(index.size() == 4);

    REQUIRE(index.get(22) == 10);
    REQUIRE(index.get(23) == 22);
    REQUIRE(index.get(26) == 24);
    REQUIRE(index.get(19) == 0);

    REQUIRE(index.get(0) == index.not_found_value());
    REQUIRE(index.get(20) == index.not_found_value());
    REQUIRE(index.get(27) == index.not_found_value());

    REQUIRE(index.get_block(0) == index.not_found_value());
    REQUIRE(index.get_block(20) == 0);
    REQUIRE(index.get_block(27) == 24);
    REQUIRE(index.get_block(99999) == 24);
}

TEST_CASE("ordered index set/get with multiple second-level blocks", "[NoDB]")
{
    constexpr std::size_t const block_size = 4;
    ordered_index_t index{block_size};

    index.add(19, 0);
    index.add(22, 10);
    index.add(23, 22);
    index.add(26, 24);
    REQUIRE(index.size() == 4);
    REQUIRE(index.capacity() == block_size);

    REQUIRE(index.get(31) == index.not_found_value());

    index.add(31, 25);
    index.add(42, 30);
    index.add(65, 32);
    REQUIRE(index.size() == 7);
    REQUIRE(index.capacity() == block_size * (1 + 2));

    REQUIRE(index.get(22) == 10);
    REQUIRE(index.get(23) == 22);
    REQUIRE(index.get(26) == 24);
    REQUIRE(index.get(19) == 0);
    REQUIRE(index.get(42) == 30);
    REQUIRE(index.get(31) == 25);
    REQUIRE(index.get(65) == 32);

    REQUIRE(index.get(0) == index.not_found_value());
    REQUIRE(index.get(27) == index.not_found_value());
    REQUIRE(index.get(30) == index.not_found_value());
    REQUIRE(index.get(66) == index.not_found_value());
    REQUIRE(index.get(99) == index.not_found_value());

    REQUIRE(index.get_block(0) == index.not_found_value());
    REQUIRE(index.get_block(18) == index.not_found_value());
    REQUIRE(index.get_block(22) == 10);
    REQUIRE(index.get_block(24) == 22);
    REQUIRE(index.get_block(50) == 30);
    REQUIRE(index.get_block(66) == 32);
}

TEST_CASE("ordered index with huge gaps in ids", "[NoDB]")
{
    constexpr std::size_t const block_size = 4;
    ordered_index_t index{block_size};

    index.add(1, 0);
    REQUIRE(index.size() == 1);
    REQUIRE(index.capacity() == block_size);

    index.add((1ULL << 32U) + 3U, 1);
    REQUIRE(index.size() == 2);
    REQUIRE(index.capacity() == block_size * (1 + 2));

    index.add((1ULL << 32U) + 4U, 2);
    REQUIRE(index.size() == 3);
    REQUIRE(index.capacity() == block_size * (1 + 2));

    index.add((2ULL << 32U) + 9U, 3);
    REQUIRE(index.size() == 4);
    REQUIRE(index.capacity() == block_size * (1 + 2 + 4));

    REQUIRE(index.used_memory() > (index.capacity() * 8));

    REQUIRE(index.get(1) == 0);
    REQUIRE(index.get((1ULL << 32U) + 3U) == 1);
    REQUIRE(index.get((1ULL << 32U) + 4U) == 2);
    REQUIRE(index.get((2ULL << 32U) + 9U) == 3);
    REQUIRE(index.get(2) == index.not_found_value());

    REQUIRE(index.get_block(1) == 0);
    REQUIRE(index.get_block(2) == 0);
    REQUIRE(index.get_block((1ULL << 32U) + 2U) == 0);
    REQUIRE(index.get_block((1ULL << 32U) + 3U) == 1);
    REQUIRE(index.get_block((1ULL << 32U) + 4U) == 2);
    REQUIRE(index.get_block((1ULL << 32U) + 5U) == 2);
    REQUIRE(index.get_block((2ULL << 32U) + 8U) == 2);
    REQUIRE(index.get_block((2ULL << 32U) + 9U) == 3);
    REQUIRE(index.get_block((3ULL << 32U) + 2U) == 3);
}