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
|
#include <iostream>
#include "external/minunit.h"
#include "sorted_way_store.h"
#include "node_store.h"
class TestNodeStore : public NodeStore {
void clear() override {}
void reopen() override {}
void batchStart() override {}
void finalize(size_t threadNum) override {}
size_t size() const override { return 1; }
LatpLon at(NodeID id) const override {
return { (int32_t)id, -(int32_t)id };
}
void insert(const std::vector<std::pair<NodeID, LatpLon>>& elements) override {}
bool contains(size_t shard, NodeID id) const override { return true; }
NodeStore& shard(size_t shard) override { return *this; }
const NodeStore& shard(size_t shard) const override { return *this; }
size_t shards() const override { return 1; }
};
void roundtripWay(const std::vector<NodeID>& way) {
bool compress = false;
for (int i = 0; i < 2; i++) {
std::vector<uint8_t> output;
uint16_t flags = SortedWayStore::encodeWay(way, output, compress);
if (false) {
std::cout << "input=";
for (const auto& node : way) {
std::cout << node << " ";
}
std::cout << std::endl;
std::cout << "flags=" << flags << ", output.size()=" << output.size() << ", ";
for (const uint8_t byte : output)
std::cout << " " << std::to_string(byte);
std::cout << std::endl;
}
const std::vector<NodeID> roundtrip = SortedWayStore::decodeWay(flags, &output[0]);
mu_check(roundtrip.size() == way.size());
for (int i = 0; i < way.size(); i++) {
//std::cout << "roundtrip[" << i << "]=" << roundtrip[i] << ", way[" << i << "]=" << way[i] << std::endl;
mu_check(roundtrip[i] == way[i]);
}
compress = !compress;
}
}
MU_TEST(test_encode_way) {
roundtripWay({ 1 });
roundtripWay({ 1, 2 });
roundtripWay({ 1, 2, 1 });
roundtripWay({ 1, 2, 3, 4 });
roundtripWay({ 4294967295, 4294967297, 8589934592, 4, 5 });
// 11386679771 uses the full lower 32-bits, so is a good test case that
// zigzag encoding hasn't broken anything.
roundtripWay({ 5056880431, 538663248, 538663257, 538663260, 538663263, 11386679771, 538663266 });
// When the high bytes are all the same, it should take
// less space to encode.
{
std::vector<uint8_t> output;
SortedWayStore::encodeWay({ 1, 2, 3, 4 }, output, false);
const uint16_t l1 = output.size();
SortedWayStore::encodeWay({ 1, 8589934592, 3, 4 }, output, false);
const uint16_t l2 = output.size();
mu_check(l1 < l2);
}
}
MU_TEST(test_multiple_stores) {
bool compressed = false;
for (int i = 0; i < 2; i++) {
compressed = !compressed;
TestNodeStore ns;
SortedWayStore s1(compressed, ns), s2(compressed, ns);
s1.batchStart();
s2.batchStart();
s1.insertNodes({{ 1, { 1 } }});
// We store small ways differently than large ways, so
// store both kinds for testing.
std::vector<NodeID> longWay;
for (int i = 200; i < 2048; i++)
longWay.push_back(i + 3 * (i % 37));
s1.insertNodes({{ 42, longWay }});
s2.insertNodes({{ 2, { 2 } }});
s1.finalize(1);
s2.finalize(1);
mu_check(s1.size() == 2);
mu_check(s2.size() == 1);
mu_check(s1.contains(0, 1));
mu_check(s1.contains(0, 42));
mu_check(!s1.contains(0, 2));
}
}
MU_TEST(test_way_store) {
TestNodeStore ns;
SortedWayStore sws(true, ns);
sws.batchStart();
std::vector<std::pair<WayID, std::vector<NodeID>>> ways;
std::vector<NodeID> shortWay;
shortWay.push_back(123);
ways.push_back(std::make_pair(1, shortWay));
ways.push_back(std::make_pair(2, shortWay));
ways.push_back(std::make_pair(513, shortWay));
std::vector<NodeID> longWay;
for(int i = 200; i < 300; i++)
longWay.push_back(i);
ways.push_back(std::make_pair(65536, longWay));
ways.push_back(std::make_pair(131072, longWay));
sws.insertNodes(ways);
sws.finalize(1);
mu_check(sws.size() == 5);
{
const auto& rv = sws.at(1);
mu_check(rv.size() == 1);
mu_check(rv[0].latp == 123);
}
{
const auto& rv = sws.at(2);
mu_check(rv.size() == 1);
mu_check(rv[0].latp == 123);
}
{
const auto& rv = sws.at(513);
mu_check(rv.size() == 1);
mu_check(rv[0].latp == 123);
}
{
const auto& rv = sws.at(65536);
mu_check(rv.size() == 100);
mu_check(rv[0].latp == 200);
mu_check(rv[99].latp == 299);
}
{
const auto& rv = sws.at(131072);
mu_check(rv.size() == 100);
mu_check(rv[0].latp == 200);
mu_check(rv[99].latp == 299);
}
// missing things should throw std::out_of_range
bool threw = false;
try {
sws.at(123123123);
} catch (std::out_of_range &e) {
threw = true;
} catch (...) {}
mu_check(threw == true);
threw = false;
try {
sws.at(3);
} catch (std::out_of_range &e) {
threw = true;
} catch (...) {}
mu_check(threw == true);
}
MU_TEST(test_populate_mask) {
uint8_t mask[32];
std::vector<uint8_t> ids;
{
// No ids: all 0s
populateMask(mask, ids);
for(int i = 0; i < 32; i++)
mu_check(mask[i] == 0);
}
{
// Every id: all 1s
for(int i = 0; i < 256; i++)
ids.push_back(i);
populateMask(mask, ids);
for(int i = 0; i < 32; i++)
mu_check(mask[i] == 255);
}
{
// Every other ID
ids.clear();
for (int i = 0; i < 256; i += 2)
ids.push_back(i);
populateMask(mask, ids);
for(int i = 0; i < 32; i++)
mu_check(mask[i] == 0b01010101);
}
}
MU_TEST_SUITE(test_suite_sorted_way_store) {
MU_RUN_TEST(test_encode_way);
MU_RUN_TEST(test_multiple_stores);
MU_RUN_TEST(test_way_store);
}
MU_TEST_SUITE(test_suite_bitmask) {
MU_RUN_TEST(test_populate_mask);
}
int main() {
MU_RUN_SUITE(test_suite_sorted_way_store);
MU_RUN_SUITE(test_suite_bitmask);
MU_REPORT();
return MU_EXIT_CODE;
}
|