First Commit

This commit is contained in:
Hamza committed 2026-02-26 13:00:32 +01:00
commit 8a6c7106e3
1312 files changed
+425396

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
#include "catch.hpp"
#include <osmium/osm/area.hpp>
TEST_CASE("object_id to area_id conversion") {
REQUIRE( 46 == osmium::object_id_to_area_id( 23, osmium::item_type::way));
REQUIRE( 47 == osmium::object_id_to_area_id( 23, osmium::item_type::relation));
REQUIRE( 0 == osmium::object_id_to_area_id( 0, osmium::item_type::way));
REQUIRE( 1 == osmium::object_id_to_area_id( 0, osmium::item_type::relation));
REQUIRE(-24 == osmium::object_id_to_area_id(-12, osmium::item_type::way));
REQUIRE(-25 == osmium::object_id_to_area_id(-12, osmium::item_type::relation));
}
TEST_CASE("area_id to object_id conversion") {
REQUIRE( 23 == osmium::area_id_to_object_id( 46));
REQUIRE( 23 == osmium::area_id_to_object_id( 47));
REQUIRE( 0 == osmium::area_id_to_object_id( 0));
REQUIRE( 0 == osmium::area_id_to_object_id( 1));
REQUIRE(-12 == osmium::area_id_to_object_id(-24));
REQUIRE(-12 == osmium::area_id_to_object_id(-25));
}
+143
View File
@@ -0,0 +1,143 @@
#include "catch.hpp"
#include <osmium/area/assembler.hpp>
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/area.hpp>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Build area from way") {
osmium::memory::Buffer buffer{10240};
const auto wpos = osmium::builder::add_way(buffer,
_id(1),
_nodes({
{1, {1.0, 1.0}},
{2, {1.0, 2.0}},
{3, {2.0, 2.0}},
{4, {2.0, 1.0}},
{1, {1.0, 1.0}}
})
);
const osmium::area::AssemblerConfig config;
osmium::area::Assembler assembler{config};
osmium::memory::Buffer area_buffer{10240};
REQUIRE(assembler(buffer.get<osmium::Way>(wpos), area_buffer));
const auto& area = area_buffer.get<osmium::Area>(0);
REQUIRE(area.from_way());
REQUIRE(area.id() == 2);
const auto it = area.outer_rings().begin();
REQUIRE(it != area.outer_rings().end());
REQUIRE(it->size() == 5);
const auto& s = assembler.stats();
REQUIRE(s.area_simple_case == 1);
REQUIRE(s.from_ways == 1);
REQUIRE(s.nodes == 4);
REQUIRE(s.duplicate_nodes == 0);
REQUIRE(s.invalid_locations == 0);
}
TEST_CASE("Build area from way with duplicate nodes") {
osmium::memory::Buffer buffer{10240};
const auto wpos = osmium::builder::add_way(buffer,
_id(1),
_nodes({
{1, {1.0, 1.0}},
{2, {1.0, 2.0}},
{3, {2.0, 2.0}},
{3, {2.0, 2.0}},
{4, {2.0, 1.0}},
{1, {1.0, 1.0}}
})
);
const osmium::area::AssemblerConfig config;
osmium::area::Assembler assembler{config};
osmium::memory::Buffer area_buffer{10240};
REQUIRE(assembler(buffer.get<osmium::Way>(wpos), area_buffer));
const auto& area = area_buffer.get<osmium::Area>(0);
REQUIRE(area.from_way());
REQUIRE(area.id() == 2);
const auto it = area.outer_rings().begin();
REQUIRE(it != area.outer_rings().end());
REQUIRE(it->size() == 5);
const auto& s = assembler.stats();
REQUIRE(s.area_simple_case == 1);
REQUIRE(s.from_ways == 1);
REQUIRE(s.nodes == 4);
REQUIRE(s.duplicate_nodes == 1);
REQUIRE(s.invalid_locations == 0);
}
TEST_CASE("Build area from way with invalid location") {
osmium::memory::Buffer buffer{10240};
const auto wpos = osmium::builder::add_way(buffer,
_id(1),
_nodes({
{1, {1.0, 1.0}},
{2, {1.0, 2.0}},
{3},
{4, {2.0, 1.0}},
{1, {1.0, 1.0}}
})
);
const osmium::area::AssemblerConfig config;
osmium::area::Assembler assembler{config};
osmium::memory::Buffer area_buffer{10240};
REQUIRE_FALSE(assembler(buffer.get<osmium::Way>(wpos), area_buffer));
const auto& s = assembler.stats();
REQUIRE(s.duplicate_nodes == 0);
REQUIRE(s.invalid_locations == 1);
}
TEST_CASE("Build area from way with ignored invalid location") {
osmium::memory::Buffer buffer{10240};
const auto wpos = osmium::builder::add_way(buffer,
_id(1),
_nodes({
{1, {1.0, 1.0}},
{2, {1.0, 2.0}},
{3},
{4, {2.0, 1.0}},
{1, {1.0, 1.0}}
})
);
osmium::area::AssemblerConfig config;
config.ignore_invalid_locations = true;
osmium::area::Assembler assembler{config};
osmium::memory::Buffer area_buffer{10240};
REQUIRE(assembler(buffer.get<osmium::Way>(wpos), area_buffer));
const auto& area = area_buffer.get<osmium::Area>(0);
REQUIRE(area.from_way());
REQUIRE(area.id() == 2);
const auto it = area.outer_rings().begin();
REQUIRE(it != area.outer_rings().end());
REQUIRE(it->size() == 4);
const auto& s = assembler.stats();
REQUIRE(s.area_simple_case == 1);
REQUIRE(s.from_ways == 1);
REQUIRE(s.nodes == 3);
REQUIRE(s.invalid_locations == 1);
}
@@ -0,0 +1,205 @@
#include "catch.hpp"
#include <osmium/area/detail/node_ref_segment.hpp>
using osmium::area::detail::NodeRefSegment;
using osmium::area::detail::role_type;
TEST_CASE("Default construction of NodeRefSegment") {
const NodeRefSegment s;
REQUIRE(s.first().ref() == 0);
REQUIRE(s.first().location() == osmium::Location());
REQUIRE(s.second().ref() == 0);
REQUIRE(s.second().location() == osmium::Location());
}
TEST_CASE("Construction of NodeRefSegment with NodeRefs") {
const osmium::NodeRef nr1{1, {1.2, 3.4}};
const osmium::NodeRef nr2{2, {1.4, 3.1}};
const osmium::NodeRef nr3{3, {1.2, 3.6}};
const osmium::NodeRef nr4{4, {1.2, 3.7}};
const NodeRefSegment s1{nr1, nr2, role_type::unknown, nullptr};
REQUIRE(s1.first().ref() == 1);
REQUIRE(s1.second().ref() == 2);
const NodeRefSegment s2{nr2, nr3, role_type::unknown, nullptr};
REQUIRE(s2.first().ref() == 3);
REQUIRE(s2.second().ref() == 2);
const NodeRefSegment s3{nr3, nr4, role_type::unknown, nullptr};
REQUIRE(s3.first().ref() == 3);
REQUIRE(s3.second().ref() == 4);
}
TEST_CASE("Intersection of NodeRefSegments") {
const NodeRefSegment s1{{ 1, {0.0, 0.0}}, { 2, {2.0, 2.0}}, role_type::unknown, nullptr};
const NodeRefSegment s2{{ 3, {0.0, 2.0}}, { 4, {2.0, 0.0}}, role_type::unknown, nullptr};
const NodeRefSegment s3{{ 5, {2.0, 0.0}}, { 6, {4.0, 2.0}}, role_type::unknown, nullptr};
const NodeRefSegment s4{{ 7, {1.0, 0.0}}, { 8, {3.0, 2.0}}, role_type::unknown, nullptr};
const NodeRefSegment s5{{ 9, {0.0, 4.0}}, {10, {4.0, 0.0}}, role_type::unknown, nullptr};
const NodeRefSegment s6{{11, {0.0, 0.0}}, {12, {1.0, 1.0}}, role_type::unknown, nullptr};
const NodeRefSegment s7{{13, {1.0, 1.0}}, {14, {3.0, 3.0}}, role_type::unknown, nullptr};
REQUIRE(calculate_intersection(s1, s2) == osmium::Location(1.0, 1.0));
REQUIRE(calculate_intersection(s2, s1) == osmium::Location(1.0, 1.0));
REQUIRE(calculate_intersection(s1, s3) == osmium::Location());
REQUIRE(calculate_intersection(s3, s1) == osmium::Location());
REQUIRE(calculate_intersection(s2, s3) == osmium::Location());
REQUIRE(calculate_intersection(s3, s2) == osmium::Location());
REQUIRE(calculate_intersection(s1, s4) == osmium::Location());
REQUIRE(calculate_intersection(s4, s1) == osmium::Location());
REQUIRE(calculate_intersection(s1, s5) == osmium::Location(2.0, 2.0));
REQUIRE(calculate_intersection(s5, s1) == osmium::Location(2.0, 2.0));
REQUIRE(calculate_intersection(s1, s6) == osmium::Location(1.0, 1.0));
REQUIRE(calculate_intersection(s6, s1) == osmium::Location(1.0, 1.0));
REQUIRE(calculate_intersection(s1, s7) == osmium::Location(1.0, 1.0));
REQUIRE(calculate_intersection(s7, s1) == osmium::Location(1.0, 1.0));
REQUIRE(calculate_intersection(s6, s7) == osmium::Location());
REQUIRE(calculate_intersection(s7, s6) == osmium::Location());
}
TEST_CASE("Intersection of collinear NodeRefSegments") {
const NodeRefSegment s1{{ 1, {0.0, 0.0}}, { 2, {2.0, 0.0}}, role_type::unknown, nullptr}; // *---*
const NodeRefSegment s2{{ 3, {2.0, 0.0}}, { 4, {4.0, 0.0}}, role_type::unknown, nullptr}; // *---*
const NodeRefSegment s3{{ 5, {0.0, 0.0}}, { 6, {1.0, 0.0}}, role_type::unknown, nullptr}; // *-*
const NodeRefSegment s4{{ 7, {1.0, 0.0}}, { 8, {2.0, 0.0}}, role_type::unknown, nullptr}; // *-*
const NodeRefSegment s5{{ 9, {1.0, 0.0}}, {10, {3.0, 0.0}}, role_type::unknown, nullptr}; // *---*
const NodeRefSegment s6{{11, {0.0, 0.0}}, {12, {4.0, 0.0}}, role_type::unknown, nullptr}; // *-------*
const NodeRefSegment s7{{13, {0.0, 0.0}}, {14, {5.0, 0.0}}, role_type::unknown, nullptr}; // *---------*
const NodeRefSegment s8{{13, {1.0, 0.0}}, {14, {5.0, 0.0}}, role_type::unknown, nullptr}; // *-------*
const NodeRefSegment s9{{13, {3.0, 0.0}}, {14, {4.0, 0.0}}, role_type::unknown, nullptr}; // *-*
REQUIRE(calculate_intersection(s1, s1) == osmium::Location());
REQUIRE(calculate_intersection(s1, s2) == osmium::Location());
REQUIRE(calculate_intersection(s2, s1) == osmium::Location());
REQUIRE(calculate_intersection(s1, s3) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s3, s1) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s1, s4) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s4, s1) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s1, s5) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s5, s1) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s1, s6) == osmium::Location(2.0, 0.0));
REQUIRE(calculate_intersection(s6, s1) == osmium::Location(2.0, 0.0));
REQUIRE(calculate_intersection(s1, s7) == osmium::Location(2.0, 0.0));
REQUIRE(calculate_intersection(s7, s1) == osmium::Location(2.0, 0.0));
REQUIRE(calculate_intersection(s1, s8) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s8, s1) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s1, s9) == osmium::Location());
REQUIRE(calculate_intersection(s9, s1) == osmium::Location());
REQUIRE(calculate_intersection(s5, s6) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s6, s5) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s7, s8) == osmium::Location(1.0, 0.0));
REQUIRE(calculate_intersection(s8, s7) == osmium::Location(1.0, 0.0));
}
TEST_CASE("Intersection of very long NodeRefSegments") {
const NodeRefSegment s1{{1, {90.0, 90.0}}, {2, {-90.0, -90.0}}, role_type::unknown, nullptr};
const NodeRefSegment s2{{1, {-90.0, 90.0}}, {2, {90.0, -90.0}}, role_type::unknown, nullptr};
REQUIRE(calculate_intersection(s1, s2) == osmium::Location(0.0, 0.0));
const NodeRefSegment s3{{1, {-90.0, -90.0}}, {2, {90.0, 90.0}}, role_type::unknown, nullptr};
const NodeRefSegment s4{{1, {-90.0, 90.0}}, {2, {90.0, -90.0}}, role_type::unknown, nullptr};
REQUIRE(calculate_intersection(s3, s4) == osmium::Location(0.0, 0.0));
const NodeRefSegment s5{{1, {-90.00000001, -90.0}}, {2, {90.0, 90.0}}, role_type::unknown, nullptr};
const NodeRefSegment s6{{1, {-90.0, 90.0}}, {2, {90.0, -90.0}}, role_type::unknown, nullptr};
REQUIRE(calculate_intersection(s5, s6) == osmium::Location(0.0, 0.0));
}
TEST_CASE("Ordering of NodeRefSegements") {
const osmium::NodeRef node_ref1{1, {1.0, 3.0}};
const osmium::NodeRef node_ref2{2, {1.4, 2.9}};
const osmium::NodeRef node_ref3{3, {1.2, 3.0}};
const osmium::NodeRef node_ref4{4, {1.2, 3.3}};
REQUIRE(node_ref1 < node_ref2);
REQUIRE(node_ref2 < node_ref3);
REQUIRE(node_ref1 < node_ref3);
REQUIRE(node_ref1 >= node_ref1);
REQUIRE( osmium::location_less()(node_ref1, node_ref2));
REQUIRE_FALSE(osmium::location_less()(node_ref2, node_ref3));
REQUIRE( osmium::location_less()(node_ref1, node_ref3));
REQUIRE( osmium::location_less()(node_ref3, node_ref4));
REQUIRE_FALSE(osmium::location_less()(node_ref1, node_ref1));
}
TEST_CASE("More ordering of NodeRefSegments") {
const osmium::NodeRef nr0{0, {0.0, 0.0}};
const osmium::NodeRef nr1{1, {1.0, 0.0}};
const osmium::NodeRef nr2{2, {0.0, 1.0}};
const osmium::NodeRef nr3{3, {2.0, 0.0}};
const osmium::NodeRef nr4{4, {0.0, 2.0}};
const osmium::NodeRef nr5{5, {1.0, 1.0}};
const osmium::NodeRef nr6{6, {2.0, 2.0}};
const osmium::NodeRef nr7{6, {1.0, 2.0}};
const NodeRefSegment s1{nr0, nr1, role_type::unknown, nullptr};
const NodeRefSegment s2{nr0, nr2, role_type::unknown, nullptr};
const NodeRefSegment s3{nr0, nr3, role_type::unknown, nullptr};
const NodeRefSegment s4{nr0, nr4, role_type::unknown, nullptr};
const NodeRefSegment s5{nr0, nr5, role_type::unknown, nullptr};
const NodeRefSegment s6{nr0, nr6, role_type::unknown, nullptr};
const NodeRefSegment s7{nr0, nr7, role_type::unknown, nullptr};
// s1
REQUIRE_FALSE(s1 < s1);
REQUIRE(s2 < s1);
REQUIRE(s1 < s3);
REQUIRE(s4 < s1);
REQUIRE(s5 < s1);
REQUIRE(s6 < s1);
REQUIRE(s7 < s1);
// s2
REQUIRE_FALSE(s2 < s2);
REQUIRE(s2 < s3);
REQUIRE(s2 < s4);
REQUIRE(s2 < s5);
REQUIRE(s2 < s6);
REQUIRE(s2 < s7);
// s3
REQUIRE_FALSE(s3 < s3);
REQUIRE(s4 < s3);
REQUIRE(s5 < s3);
REQUIRE(s6 < s3);
REQUIRE(s7 < s3);
// s4
REQUIRE_FALSE(s4 < s4);
REQUIRE(s4 < s5);
REQUIRE(s4 < s6);
REQUIRE(s4 < s7);
// s5
REQUIRE_FALSE(s5 < s5);
REQUIRE(s5 < s6);
REQUIRE(s7 < s5);
// s6
REQUIRE_FALSE(s6 < s6);
REQUIRE(s7 < s6);
// s7
REQUIRE_FALSE(s7 < s7);
}
+634
View File
@@ -0,0 +1,634 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm.hpp>
#include <osmium/osm/types.hpp>
#include <cstdint>
#include <initializer_list>
#include <iterator>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
constexpr const std::size_t test_buffer_size = 1024UL * 10UL;
TEST_CASE("create node using builders: add node with only id") {
osmium::memory::Buffer buffer{test_buffer_size};
const auto pos = osmium::builder::add_node(buffer, _id(22));
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 22);
REQUIRE(node.version() == 0);
REQUIRE(node.timestamp() == osmium::Timestamp{});
REQUIRE(node.changeset() == 0);
REQUIRE(node.uid() == 0);
REQUIRE(node.user()[0] == '\0');
REQUIRE(node.location() == osmium::Location{});
REQUIRE(node.tags().empty());
}
TEST_CASE("create node using builders: add node with complete info but no tags") {
osmium::memory::Buffer buffer{test_buffer_size};
const osmium::Location loc{3.14, 1.59};
const auto pos = osmium::builder::add_node(buffer,
_id(1),
_version(17),
_timestamp(osmium::Timestamp{"2015-01-01T10:20:30Z"}),
_cid(21),
_uid(222),
_location(loc),
_user("foo")
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 1);
REQUIRE(node.version() == 17);
REQUIRE(node.timestamp() == osmium::Timestamp{"2015-01-01T10:20:30Z"});
REQUIRE(node.changeset() == 21);
REQUIRE(node.uid() == 222);
REQUIRE(std::string{node.user()} == "foo");
REQUIRE(node.location() == loc);
REQUIRE(node.tags().empty());
REQUIRE(std::distance(node.cbegin(), node.cend()) == 0);
}
TEST_CASE("create node using builders: visible/deleted flag") {
osmium::memory::Buffer buffer{test_buffer_size};
osmium::builder::add_node(buffer, _id(1), _deleted());
osmium::builder::add_node(buffer, _id(2), _deleted(true));
osmium::builder::add_node(buffer, _id(3), _deleted(false));
osmium::builder::add_node(buffer, _id(4), _visible());
osmium::builder::add_node(buffer, _id(5), _visible(true));
osmium::builder::add_node(buffer, _id(6), _visible(false));
auto it = buffer.select<osmium::Node>().cbegin();
REQUIRE_FALSE(it++->visible());
REQUIRE_FALSE(it++->visible());
REQUIRE(it++->visible());
REQUIRE(it++->visible());
REQUIRE(it++->visible());
REQUIRE_FALSE(it++->visible());
REQUIRE(it == buffer.select<osmium::Node>().cend());
}
TEST_CASE("create node using builders: order of attributes doesn't matter") {
osmium::memory::Buffer buffer{test_buffer_size};
const osmium::Location loc{3.14, 1.59};
const auto pos = osmium::builder::add_node(buffer,
_timestamp("2015-01-01T10:20:30Z"),
_version(17),
_cid(21),
_uid(222),
_user(std::string{"foo"}),
_id(1),
_location(3.14, 1.59)
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 1);
REQUIRE(node.version() == 17);
REQUIRE(node.timestamp() == osmium::Timestamp{"2015-01-01T10:20:30Z"});
REQUIRE(node.changeset() == 21);
REQUIRE(node.uid() == 222);
REQUIRE(std::string{node.user()} == "foo");
REQUIRE(node.location() == loc);
REQUIRE(node.tags().empty());
}
TEST_CASE("create node with tags using builders: add tags using _tag") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::pair<const char*, const char*> t1 = {"name", "Node Inn"};
const std::pair<std::string, std::string> t2 = {"phone", "+1-123-555-4567"};
const auto pos = osmium::builder::add_node(buffer,
_id(2),
_tag("amenity", "restaurant"),
_tag(t1),
_tag(t2),
_tag(std::string{"cuisine"}, std::string{"italian"})
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 2);
REQUIRE(node.tags().size() == 4);
REQUIRE(std::distance(node.cbegin(), node.cend()) == 1);
auto it = node.tags().cbegin();
REQUIRE(std::string{it->key()} == "amenity");
REQUIRE(std::string{it->value()} == "restaurant");
++it;
REQUIRE(std::string{it->key()} == "name");
REQUIRE(std::string{it->value()} == "Node Inn");
++it;
REQUIRE(std::string{it->key()} == "phone");
REQUIRE(std::string{it->value()} == "+1-123-555-4567");
++it;
REQUIRE(std::string{it->key()} == "cuisine");
REQUIRE(std::string{it->value()} == "italian");
++it;
REQUIRE(it == node.tags().cend());
}
TEST_CASE("create node with tags using builders: add tags using _tag with equal sign in single cstring") {
osmium::memory::Buffer buffer{test_buffer_size};
const auto pos = osmium::builder::add_node(buffer,
_id(2),
_tag("amenity=restaurant"),
_tag("name="),
_tag("phone"),
_tag(std::string{"cuisine=italian"})
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 2);
REQUIRE(node.tags().size() == 4);
REQUIRE(std::distance(node.cbegin(), node.cend()) == 1);
auto it = node.tags().cbegin();
REQUIRE(std::string{it->key()} == "amenity");
REQUIRE(std::string{it->value()} == "restaurant");
++it;
REQUIRE(std::string{it->key()} == "name");
REQUIRE(it->value()[0] == '\0');
++it;
REQUIRE(std::string{it->key()} == "phone");
REQUIRE(it->value()[0] == '\0');
++it;
REQUIRE(std::string{it->key()} == "cuisine");
REQUIRE(std::string{it->value()} == "italian");
++it;
REQUIRE(it == node.tags().cend());
}
TEST_CASE("create node with tags using builders: add tags using _tags from initializer list") {
osmium::memory::Buffer buffer{test_buffer_size};
const auto pos = osmium::builder::add_node(buffer,
_id(3),
_tags({{"amenity", "post_box"}})
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 3);
REQUIRE(node.tags().size() == 1);
auto it = node.tags().cbegin();
REQUIRE(std::string{it->key()} == "amenity");
REQUIRE(std::string{it->value()} == "post_box");
++it;
REQUIRE(it == node.tags().cend());
REQUIRE(std::distance(node.cbegin(), node.cend()) == 1);
}
TEST_CASE("create node with tags using builders: add tags using _tags from TagList") {
osmium::memory::Buffer buffer{test_buffer_size};
const auto pos1 = osmium::builder::add_node(buffer,
_id(3),
_tag("a", "d"),
_tag("b", "e"),
_tag("c", "f")
);
const auto& node1 = buffer.get<osmium::Node>(pos1);
const auto pos2 = osmium::builder::add_node(buffer,
_id(4),
_tags(node1.tags())
);
const auto& node2 = buffer.get<osmium::Node>(pos2);
REQUIRE(node2.id() == 4);
REQUIRE(node2.tags().size() == 3);
auto it = node2.tags().cbegin();
REQUIRE(std::string{it++->key()} == "a");
REQUIRE(std::string{it++->key()} == "b");
REQUIRE(std::string{it++->key()} == "c");
REQUIRE(it == node2.tags().cend());
REQUIRE(std::distance(node2.cbegin(), node2.cend()) == 1);
}
TEST_CASE("create node with tags using builders: add tags using mixed tag sources") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::vector<pair_of_cstrings> tags = {
{"t5", "t5"},
{"t6", "t6"}
};
const auto pos = osmium::builder::add_node(buffer,
_id(4),
_tag("t1=t1"),
_tags({{"t2", "t2"}, {"t3", "t3"}}),
_tag("t4", "t4"),
_tags(tags)
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 4);
REQUIRE(node.tags().size() == 6);
auto it = node.tags().cbegin();
REQUIRE(std::string{it->key()} == "t1");
++it;
REQUIRE(std::string{it->key()} == "t2");
++it;
REQUIRE(std::string{it->key()} == "t3");
++it;
REQUIRE(std::string{it->key()} == "t4");
++it;
REQUIRE(std::string{it->key()} == "t5");
++it;
REQUIRE(std::string{it->key()} == "t6");
++it;
REQUIRE(it == node.tags().cend());
REQUIRE(std::distance(node.cbegin(), node.cend()) == 1);
}
TEST_CASE("create node with tags using builders: add tags using _t with string") {
osmium::memory::Buffer buffer{test_buffer_size};
const auto pos = osmium::builder::add_node(buffer,
_id(5),
_t("amenity=post_box,,empty,also_empty=,operator=Deutsche Post")
);
const auto& node = buffer.get<osmium::Node>(pos);
REQUIRE(node.id() == 5);
REQUIRE(node.tags().size() == 4);
auto it = node.tags().cbegin();
REQUIRE(std::string{it->key()} == "amenity");
REQUIRE(std::string{it->value()} == "post_box");
++it;
REQUIRE(std::string{it->key()} == "empty");
REQUIRE(it->value()[0] == '\0');
++it;
REQUIRE(std::string{it->key()} == "also_empty");
REQUIRE(it->value()[0] == '\0');
++it;
REQUIRE(std::string{it->key()} == "operator");
REQUIRE(std::string{it->value()} == "Deutsche Post");
++it;
REQUIRE(it == node.tags().cend());
REQUIRE(std::distance(node.cbegin(), node.cend()) == 1);
}
TEST_CASE("create way using builders") {
osmium::memory::Buffer buffer{test_buffer_size};
SECTION("add way without nodes") {
const auto pos = osmium::builder::add_way(buffer,
_id(999),
_cid(21),
_uid(222),
_user("foo")
);
const auto& way = buffer.get<osmium::Way>(pos);
REQUIRE(way.id() == 999);
REQUIRE(way.version() == 0);
REQUIRE(way.timestamp() == osmium::Timestamp{});
REQUIRE(way.changeset() == 21);
REQUIRE(way.uid() == 222);
REQUIRE(std::string{way.user()} == "foo");
REQUIRE(way.tags().empty());
REQUIRE(way.nodes().empty());
REQUIRE(std::distance(way.cbegin(), way.cend()) == 0);
}
}
TEST_CASE("create way with nodes") {
std::vector<osmium::NodeRef> nrvec = {
{ 1, osmium::Location{1.1, 0.1} },
{ 2, osmium::Location{2.2, 0.2} },
{ 4, osmium::Location{4.4, 0.4} },
{ 8, osmium::Location{8.8, 0.8} }
};
osmium::memory::Buffer wbuffer{test_buffer_size};
osmium::builder::add_way(wbuffer,
_id(1),
_nodes({1, 2, 4, 8})
);
const osmium::NodeRefList& nodes = wbuffer.get<osmium::Way>(0).nodes();
osmium::memory::Buffer buffer{test_buffer_size};
SECTION("add nodes using an OSM object id or NodeRef") {
osmium::builder::add_way(buffer,
_id(1),
_node(1),
_node(2),
_node(osmium::NodeRef{4}),
_node(8)
);
}
SECTION("add nodes using iterator list with object ids") {
osmium::builder::add_way(buffer,
_id(1),
_nodes({1, 2, 4, 8})
);
}
SECTION("add way with nodes in initializer_list of NodeRefs") {
osmium::builder::add_way(buffer,
_id(1),
_nodes({
{ 1, {1.1, 0.1} },
{ 2, {2.2, 0.2} },
{ 4, {4.4, 0.4} },
{ 8, {8.8, 0.8} }
})
);
}
SECTION("add nodes using WayNodeList") {
osmium::builder::add_way(buffer,
_id(1),
_nodes(nodes)
);
}
SECTION("add nodes using vector of OSM object ids") {
const std::vector<osmium::object_id_type> some_nodes = {
1, 2, 4, 8
};
osmium::builder::add_way(buffer,
_id(1),
_nodes(some_nodes)
);
}
SECTION("add nodes using vector of NodeRefs") {
osmium::builder::add_way(buffer,
_id(1),
_nodes(nrvec)
);
}
SECTION("add nodes using different means together") {
osmium::builder::add_way(buffer,
_id(1),
_node(1),
_nodes({2, 4}),
_node(8)
);
}
SECTION("add nodes using different means together") {
osmium::builder::add_way(buffer,
_id(1),
_nodes(nodes.begin(), nodes.begin() + 1),
_nodes({2, 4, 8})
);
}
const auto& way = buffer.get<osmium::Way>(0);
REQUIRE(way.id() == 1);
REQUIRE(way.nodes().size() == 4);
REQUIRE(std::distance(way.cbegin(), way.cend()) == 1);
const auto* it = way.nodes().cbegin();
REQUIRE(it->ref() == 1);
if (it->location().valid()) {
REQUIRE(*it == nrvec[0]);
}
it++;
REQUIRE(it->ref() == 2);
if (it->location().valid()) {
REQUIRE(*it == nrvec[1]);
}
it++;
REQUIRE(it->ref() == 4);
if (it->location().valid()) {
REQUIRE(*it == nrvec[2]);
}
it++;
REQUIRE(it->ref() == 8);
if (it->location().valid()) {
REQUIRE(*it == nrvec[3]);
}
it++;
REQUIRE(it == way.nodes().cend());
}
TEST_CASE("create relation using builders: create relation") {
osmium::memory::Buffer buffer{test_buffer_size};
const osmium::builder::attr::member_type m{osmium::item_type::way, 113, "inner"};
osmium::builder::add_relation(buffer,
_id(123),
_member(osmium::item_type::node, 123, ""),
_member(osmium::item_type::node, 132),
_member(osmium::item_type::way, 111, "outer"),
_member(osmium::builder::attr::member_type{osmium::item_type::way, 112, "inner"}),
_member(m)
);
const auto& relation = buffer.get<osmium::Relation>(0);
REQUIRE(relation.id() == 123);
REQUIRE(relation.members().size() == 5);
REQUIRE(std::distance(relation.cbegin(), relation.cend()) == 1);
auto it = relation.members().begin();
REQUIRE(it->type() == osmium::item_type::node);
REQUIRE(it->ref() == 123);
REQUIRE(it->role()[0] == '\0');
++it;
REQUIRE(it->type() == osmium::item_type::node);
REQUIRE(it->ref() == 132);
REQUIRE(it->role()[0] == '\0');
++it;
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 111);
REQUIRE(std::string{it->role()} == "outer");
++it;
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 112);
REQUIRE(std::string{it->role()} == "inner");
++it;
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 113);
REQUIRE(std::string{it->role()} == "inner");
++it;
REQUIRE(it == relation.members().end());
}
TEST_CASE("create relation using builders: create relation member from existing relation member") {
osmium::memory::Buffer buffer{test_buffer_size};
osmium::builder::add_relation(buffer,
_id(123),
_member(osmium::item_type::way, 111, "outer"),
_member(osmium::item_type::way, 112, "inner")
);
const auto& relation1 = buffer.get<osmium::Relation>(0);
const auto pos = osmium::builder::add_relation(buffer,
_id(124),
_member(*relation1.members().begin()),
_members(std::next(relation1.members().begin()), relation1.members().end())
);
const auto& relation = buffer.get<osmium::Relation>(pos);
REQUIRE(relation.id() == 124);
REQUIRE(relation.members().size() == 2);
auto it = relation.members().begin();
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 111);
REQUIRE(std::string{it->role()} == "outer");
++it;
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 112);
REQUIRE(std::string{it->role()} == "inner");
++it;
REQUIRE(it == relation.members().end());
}
TEST_CASE("create relation using builders: create relation with members from initializer list") {
osmium::memory::Buffer buffer{test_buffer_size};
const auto pos = osmium::builder::add_relation(buffer,
_id(123),
_members({
{osmium::item_type::node, 123, ""},
{osmium::item_type::way, 111, "outer"}
})
);
const auto& relation = buffer.get<osmium::Relation>(pos);
REQUIRE(relation.id() == 123);
REQUIRE(relation.members().size() == 2);
REQUIRE(std::distance(relation.cbegin(), relation.cend()) == 1);
auto it = relation.members().begin();
REQUIRE(it->type() == osmium::item_type::node);
REQUIRE(it->ref() == 123);
REQUIRE(it->role()[0] == '\0');
++it;
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 111);
REQUIRE(std::string{it->role()} == "outer");
++it;
REQUIRE(it == relation.members().end());
}
TEST_CASE("create relation using builders: create relation with members from iterators and some tags") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::vector<member_type> members = {
{osmium::item_type::node, 123},
{osmium::item_type::way, 111, "outer"}
};
SECTION("using iterators") {
osmium::builder::add_relation(buffer,
_id(123),
_members(members.begin(), members.end()),
_tag("a", "x"),
_tag("b", "y")
);
}
SECTION("using container") {
osmium::builder::add_relation(buffer,
_id(123),
_members(members),
_tag("a", "x"),
_tag("b", "y")
);
}
const auto& relation = buffer.get<osmium::Relation>(0);
REQUIRE(relation.id() == 123);
REQUIRE(relation.members().size() == 2);
REQUIRE(relation.tags().size() == 2);
REQUIRE(std::distance(relation.cbegin(), relation.cend()) == 2);
auto it = relation.members().begin();
REQUIRE(it->type() == osmium::item_type::node);
REQUIRE(it->ref() == 123);
REQUIRE(it->role()[0] == '\0');
++it;
REQUIRE(it->type() == osmium::item_type::way);
REQUIRE(it->ref() == 111);
REQUIRE(std::string{it->role()} == "outer");
++it;
REQUIRE(it == relation.members().end());
}
TEST_CASE("create area using builders") {
osmium::memory::Buffer buffer{test_buffer_size};
SECTION("add area without rings") {
const auto pos = osmium::builder::add_area(buffer,
_id(999),
_cid(21),
_uid(222),
_user("foo"),
_tag("landuse", "residential")
);
const auto& area = buffer.get<osmium::Area>(pos);
REQUIRE(area.id() == 999);
REQUIRE(area.version() == 0);
REQUIRE(area.timestamp() == osmium::Timestamp{});
REQUIRE(area.changeset() == 21);
REQUIRE(area.uid() == 222);
REQUIRE(std::string{area.user()} == "foo");
REQUIRE(area.tags().size() == 1);
REQUIRE(std::distance(area.cbegin(), area.cend()) == 1);
}
}
@@ -0,0 +1,484 @@
#include "catch.hpp"
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm.hpp>
#include <string>
constexpr const std::size_t test_buffer_size = 1024UL * 10UL;
TEST_CASE("create objects using builder") {
osmium::memory::Buffer buffer{test_buffer_size};
std::string user;
SECTION("complete node with tags") {
SECTION("user length 0") {
user = "";
}
SECTION("user length 1") {
user = "1";
}
SECTION("user length 2") {
user = "12";
}
SECTION("user length 3") {
user = "123";
}
SECTION("user length 4") {
user = "1234";
}
SECTION("user length 5") {
user = "12345";
}
SECTION("user length 6") {
user = "123456";
}
SECTION("user length 7") {
user = "1234567";
}
SECTION("user length 8") {
user = "12345678";
}
SECTION("user length 9") {
user = "123456789";
}
SECTION("user length 10") {
user = "1234567890";
}
SECTION("user length 11") {
user = "12345678901";
}
SECTION("user length 12") {
user = "123456789012";
}
SECTION("user length 13") {
user = "1234567890123";
}
SECTION("user length 14") {
user = "12345678901234";
}
SECTION("user length 15") {
user = "123456789012345";
}
SECTION("user length 16") {
user = "1234567890123456";
}
SECTION("user length 17") {
user = "12345678901234567";
}
SECTION("user length 18") {
user = "123456789012345678";
}
const osmium::Location loc{1.2, 3.4};
{
osmium::builder::NodeBuilder builder{buffer};
builder.set_id(17)
.set_visible(true)
.set_version(1)
.set_changeset(123)
.set_uid(555)
.set_timestamp("2015-07-01T00:00:01Z")
.set_location(loc)
.set_user(user);
builder.add_tags({{"highway", "primary"}, {"oneway", "yes"}});
}
const auto& node = buffer.get<osmium::Node>(buffer.commit());
REQUIRE(node.id() == 17);
REQUIRE(node.version() == 1);
REQUIRE(node.changeset() == 123);
REQUIRE(node.uid() == 555);
REQUIRE(node.timestamp() == osmium::Timestamp{"2015-07-01T00:00:01Z"});
REQUIRE(node.location() == loc);
REQUIRE(user == node.user());
REQUIRE(node.tags().size() == 2);
}
SECTION("complete way with tags") {
SECTION("user length 0") {
user = "";
}
SECTION("user length 1") {
user = "1";
}
SECTION("user length 2") {
user = "12";
}
SECTION("user length 3") {
user = "123";
}
SECTION("user length 4") {
user = "1234";
}
SECTION("user length 5") {
user = "12345";
}
SECTION("user length 6") {
user = "123456";
}
SECTION("user length 7") {
user = "1234567";
}
SECTION("user length 8") {
user = "12345678";
}
SECTION("user length 9") {
user = "123456789";
}
SECTION("user length 10") {
user = "1234567890";
}
SECTION("user length 11") {
user = "12345678901";
}
SECTION("user length 12") {
user = "123456789012";
}
SECTION("user length 13") {
user = "1234567890123";
}
SECTION("user length 14") {
user = "12345678901234";
}
SECTION("user length 15") {
user = "123456789012345";
}
SECTION("user length 16") {
user = "1234567890123456";
}
SECTION("user length 17") {
user = "12345678901234567";
}
SECTION("user length 18") {
user = "123456789012345678";
}
{
osmium::builder::WayBuilder builder{buffer};
builder.set_id(17)
.set_visible(true)
.set_version(1)
.set_changeset(123)
.set_uid(555)
.set_timestamp("2015-07-01T00:00:01Z")
.set_user(user);
builder.add_tags({{"highway", "primary"}, {"oneway", "yes"}});
}
const auto& way = buffer.get<osmium::Way>(buffer.commit());
REQUIRE(way.id() == 17);
REQUIRE(way.version() == 1);
REQUIRE(way.changeset() == 123);
REQUIRE(way.uid() == 555);
REQUIRE(way.timestamp() == osmium::Timestamp{"2015-07-01T00:00:01Z"});
REQUIRE(user == way.user());
REQUIRE(way.tags().size() == 2);
}
SECTION("complete relation with tags") {
SECTION("user length 0") {
user = "";
}
SECTION("user length 1") {
user = "1";
}
SECTION("user length 2") {
user = "12";
}
SECTION("user length 3") {
user = "123";
}
SECTION("user length 4") {
user = "1234";
}
SECTION("user length 5") {
user = "12345";
}
SECTION("user length 6") {
user = "123456";
}
SECTION("user length 7") {
user = "1234567";
}
SECTION("user length 8") {
user = "12345678";
}
SECTION("user length 9") {
user = "123456789";
}
SECTION("user length 10") {
user = "1234567890";
}
SECTION("user length 11") {
user = "12345678901";
}
SECTION("user length 12") {
user = "123456789012";
}
SECTION("user length 13") {
user = "1234567890123";
}
SECTION("user length 14") {
user = "12345678901234";
}
SECTION("user length 15") {
user = "123456789012345";
}
SECTION("user length 16") {
user = "1234567890123456";
}
SECTION("user length 17") {
user = "12345678901234567";
}
SECTION("user length 18") {
user = "123456789012345678";
}
{
osmium::builder::RelationBuilder builder{buffer};
builder.set_id(17)
.set_visible(true)
.set_version(1)
.set_changeset(123)
.set_uid(555)
.set_timestamp("2015-07-01T00:00:01Z")
.set_user(user);
builder.add_tags({{"highway", "primary"}, {"oneway", "yes"}});
}
const auto& relation = buffer.get<osmium::Relation>(buffer.commit());
REQUIRE(relation.id() == 17);
REQUIRE(relation.version() == 1);
REQUIRE(relation.changeset() == 123);
REQUIRE(relation.uid() == 555);
REQUIRE(relation.timestamp() == osmium::Timestamp{"2015-07-01T00:00:01Z"});
REQUIRE(user == relation.user());
REQUIRE(relation.tags().size() == 2);
}
SECTION("complete changeset with tags") {
const osmium::Location bl{-1.2, -3.4};
const osmium::Location tr{1.2, 3.4};
SECTION("user length 0") {
user = "";
}
SECTION("user length 1") {
user = "1";
}
SECTION("user length 2") {
user = "12";
}
SECTION("user length 3") {
user = "123";
}
SECTION("user length 4") {
user = "1234";
}
SECTION("user length 5") {
user = "12345";
}
SECTION("user length 6") {
user = "123456";
}
SECTION("user length 7") {
user = "1234567";
}
SECTION("user length 8") {
user = "12345678";
}
SECTION("user length 9") {
user = "123456789";
}
SECTION("user length 10") {
user = "1234567890";
}
SECTION("user length 11") {
user = "12345678901";
}
SECTION("user length 12") {
user = "123456789012";
}
SECTION("user length 13") {
user = "1234567890123";
}
SECTION("user length 14") {
user = "12345678901234";
}
SECTION("user length 15") {
user = "123456789012345";
}
SECTION("user length 16") {
user = "1234567890123456";
}
SECTION("user length 17") {
user = "12345678901234567";
}
SECTION("user length 18") {
user = "123456789012345678";
}
{
osmium::builder::ChangesetBuilder builder{buffer};
builder.set_id(17)
.set_uid(222)
.set_created_at(osmium::Timestamp{"2016-07-03T01:23:45Z"})
.set_closed_at(osmium::Timestamp{"2016-07-03T01:23:48Z"})
.set_num_changes(3)
.set_num_comments(2)
.set_bounds(osmium::Box{bl, tr})
.set_user(user);
}
const auto& changeset = buffer.get<osmium::Changeset>(buffer.commit());
REQUIRE(changeset.id() == 17);
REQUIRE(changeset.uid() == 222);
REQUIRE(changeset.created_at() == osmium::Timestamp{"2016-07-03T01:23:45Z"});
REQUIRE(changeset.closed_at() == osmium::Timestamp{"2016-07-03T01:23:48Z"});
REQUIRE(changeset.num_changes() == 3);
REQUIRE(changeset.num_comments() == 2);
const auto& box = changeset.bounds();
REQUIRE(box.bottom_left() == bl);
REQUIRE(box.top_right() == tr);
REQUIRE(user == changeset.user());
}
}
TEST_CASE("no call to set_user on node") {
osmium::memory::Buffer buffer{test_buffer_size};
{
const osmium::builder::NodeBuilder builder{buffer};
}
const auto& node = buffer.get<osmium::Node>(buffer.commit());
REQUIRE(*node.user() == '\0');
}
TEST_CASE("set_user with length on node") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::string user = "userx";
{
osmium::builder::NodeBuilder builder{buffer};
builder.set_user(user.c_str(), 4);
}
const auto& node = buffer.get<osmium::Node>(buffer.commit());
REQUIRE(std::string{"user"} == node.user());
}
TEST_CASE("no call to set_user on way") {
osmium::memory::Buffer buffer{test_buffer_size};
{
const osmium::builder::WayBuilder builder{buffer};
}
const auto& way = buffer.get<osmium::Way>(buffer.commit());
REQUIRE(*way.user() == '\0');
}
TEST_CASE("set_user with length on way") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::string user = "userx";
{
osmium::builder::WayBuilder builder{buffer};
builder.set_user(user.c_str(), 4);
}
const auto& way = buffer.get<osmium::Way>(buffer.commit());
REQUIRE(std::string{"user"} == way.user());
}
TEST_CASE("no call to set_user on changeset") {
osmium::memory::Buffer buffer{test_buffer_size};
{
const osmium::builder::ChangesetBuilder builder{buffer};
}
const auto& changeset = buffer.get<osmium::Changeset>(buffer.commit());
REQUIRE(*changeset.user() == '\0');
}
TEST_CASE("set_user with length on changeset") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::string user = "userx";
{
osmium::builder::ChangesetBuilder builder{buffer};
builder.set_user(user.c_str(), 4);
}
const auto& changeset = buffer.get<osmium::Changeset>(buffer.commit());
REQUIRE(std::string{"user"} == changeset.user());
}
TEST_CASE("clear_user should clear the user field but nothing else") {
osmium::memory::Buffer buffer{test_buffer_size};
const std::string user = "user";
{
osmium::builder::NodeBuilder builder{buffer};
builder.set_id(17)
.set_visible(true)
.set_version(1)
.set_changeset(123)
.set_uid(555)
.set_timestamp("2015-07-01T00:00:01Z")
.set_location(osmium::Location{1.2, 3.4})
.set_user(user);
builder.add_tags({{"highway", "primary"}, {"oneway", "yes"}});
}
auto& node = buffer.get<osmium::Node>(buffer.commit());
REQUIRE(std::string{"user"} == node.user());
node.clear_user();
REQUIRE(std::string{""} == node.user());
REQUIRE(node.uid() == 555);
REQUIRE(node.tags().size() == 2);
auto it = node.tags().begin();
REQUIRE(it->key() == std::string{"highway"});
REQUIRE(it->value() == std::string{"primary"});
++it;
REQUIRE(it->key() == std::string{"oneway"});
REQUIRE(it->value() == std::string{"yes"});
++it;
REQUIRE(it == node.tags().end());
}
+81
View File
@@ -0,0 +1,81 @@
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/area.hpp>
using namespace osmium::builder::attr;
inline const osmium::Area& create_test_area_1outer_0inner(osmium::memory::Buffer& buffer) {
osmium::builder::add_area(buffer,
_user("foo"),
_tag("building", "true"),
_outer_ring({
{1, {3.2, 4.2}},
{2, {3.5, 4.7}},
{3, {3.6, 4.9}},
{1, {3.2, 4.2}}
})
);
return buffer.get<osmium::Area>(0);
}
inline const osmium::Area& create_test_area_1outer_1inner(osmium::memory::Buffer& buffer) {
osmium::builder::add_area(buffer,
_user("foo"),
_tag("building", "true"),
_outer_ring({
{1, {0.1, 0.1}},
{2, {9.1, 0.1}},
{3, {9.1, 9.1}},
{4, {0.1, 9.1}},
{1, {0.1, 0.1}}
}),
_inner_ring({
{5, {1.0, 1.0}},
{6, {8.0, 1.0}},
{7, {8.0, 8.0}},
{8, {1.0, 8.0}},
{5, {1.0, 1.0}}
})
);
return buffer.get<osmium::Area>(0);
}
inline const osmium::Area& create_test_area_2outer_2inner(osmium::memory::Buffer& buffer) {
osmium::builder::add_area(buffer,
_user("foo"),
_tag("building", "true"),
_outer_ring({
{1, {0.1, 0.1}},
{2, {9.1, 0.1}},
{3, {9.1, 9.1}},
{4, {0.1, 9.1}},
{1, {0.1, 0.1}}
}),
_inner_ring({
{5, {1.0, 1.0}},
{6, {4.0, 1.0}},
{7, {4.0, 4.0}},
{8, {1.0, 4.0}},
{5, {1.0, 1.0}}
}),
_inner_ring({
{10, {5.0, 5.0}},
{11, {5.0, 7.0}},
{12, {7.0, 7.0}},
{10, {5.0, 5.0}}
}),
_outer_ring({
{100, {10.0, 10.0}},
{101, {11.0, 10.0}},
{102, {11.0, 11.0}},
{103, {10.0, 11.0}},
{100, {10.0, 10.0}}
})
);
return buffer.get<osmium::Area>(0);
}
+73
View File
@@ -0,0 +1,73 @@
#include "catch.hpp"
#include <osmium/geom/coordinates.hpp>
#include <string>
TEST_CASE("Default constructed coordinates are invalid") {
const osmium::geom::Coordinates c;
REQUIRE_FALSE(c.valid());
}
TEST_CASE("Coordinates constructed from doubles are valid") {
const osmium::geom::Coordinates c{1.2, 3.4};
REQUIRE(c.valid());
REQUIRE(c.x == Approx(1.2));
REQUIRE(c.y == Approx(3.4));
}
TEST_CASE("Coordinates constructed from a location are valid") {
const osmium::Location loc{1.2, 3.4};
const osmium::geom::Coordinates c{loc};
REQUIRE(c.valid());
REQUIRE(c.x == Approx(1.2));
REQUIRE(c.y == Approx(3.4));
}
TEST_CASE("Comparing coordinates") {
const osmium::geom::Coordinates ci1;
const osmium::geom::Coordinates ci2;
const osmium::geom::Coordinates cv1{1.2, 3.4};
const osmium::geom::Coordinates cv2{1.2, 3.4};
const osmium::geom::Coordinates cv3{2.1, 4.3};
REQUIRE(ci1 == ci2);
REQUIRE_FALSE(ci1 == cv1);
REQUIRE(cv1 == cv2);
REQUIRE_FALSE(cv1 == cv3);
}
TEST_CASE("Write coordinates to string") {
const osmium::geom::Coordinates c{0.1234567, 1.89898989};
std::string out;
SECTION("precision 7") {
c.append_to_string(out, ',', 7);
REQUIRE(out == "0.1234567,1.8989899");
}
SECTION("precision 3") {
c.append_to_string(out, ',', 3);
REQUIRE(out == "0.123,1.899");
}
SECTION("with prefix and suffix") {
c.append_to_string(out, '(', ',', ')', 3);
REQUIRE(out == "(0.123,1.899)");
}
}
TEST_CASE("Write invalid coordinates to string") {
const osmium::geom::Coordinates c;
std::string out;
SECTION("with infix only") {
c.append_to_string(out, ',', 7);
REQUIRE(out == "invalid");
}
SECTION("with prefix and suffix") {
c.append_to_string(out, '(', ',', ')', 3);
REQUIRE(out == "(invalid)");
}
}
+16
View File
@@ -0,0 +1,16 @@
#include "catch.hpp"
#include <osmium/geom/projection.hpp>
TEST_CASE("CRS") {
const osmium::geom::CRS wgs84{4326};
const osmium::geom::CRS mercator{3857};
const osmium::geom::Coordinates c{osmium::geom::deg_to_rad(1.2), osmium::geom::deg_to_rad(3.4)};
const auto ct = osmium::geom::transform(wgs84, mercator, c);
const auto c2 = osmium::geom::transform(mercator, wgs84, ct);
REQUIRE(c.x == Approx(c2.x));
REQUIRE(c.y == Approx(c2.y));
}
+14
View File
@@ -0,0 +1,14 @@
#include "catch.hpp"
#include <osmium/geom/factory.hpp>
#include <string>
TEST_CASE("Geometry exception") {
const osmium::geometry_error e{"some error message", "node", 17};
REQUIRE(e.id() == 17);
REQUIRE(std::string{e.what()} == "some error message (node_id=17)");
}
@@ -0,0 +1,23 @@
#include "catch.hpp"
#include <osmium/geom/mercator_projection.hpp>
#include <osmium/geom/projection.hpp>
#include <osmium/geom/wkb.hpp>
#include <osmium/geom/wkt.hpp>
#include <string>
TEST_CASE("Projection using MercatorProjection class to WKT") {
osmium::geom::WKTFactory<osmium::geom::MercatorProjection> factory{2};
const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(wkt == "POINT(356222.37 467961.14)");
}
TEST_CASE("Projection using Projection class to WKT") {
osmium::geom::WKTFactory<osmium::geom::Projection> factory{osmium::geom::Projection{3857}, 2};
const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(wkt == "POINT(356222.37 467961.14)");
}
+173
View File
@@ -0,0 +1,173 @@
#include "catch.hpp"
#include "area_helper.hpp"
#include "wnl_helper.hpp"
#include <osmium/geom/geojson.hpp>
#include <iterator>
#include <string>
TEST_CASE("GeoJSON point geometry") {
const osmium::geom::GeoJSONFactory<> factory;
const std::string json{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(std::string{"{\"type\":\"Point\",\"coordinates\":[3.2,4.2]}"} == json);
}
TEST_CASE("GeoJSON empty point geometry") {
const osmium::geom::GeoJSONFactory<> factory;
REQUIRE_THROWS_AS(factory.create_point(osmium::Location{}), osmium::invalid_location);
}
TEST_CASE("GeoJSON linestring geometry") {
osmium::geom::GeoJSONFactory<> factory;
osmium::memory::Buffer buffer{1000};
SECTION("linestring, default") {
const auto& wnl = create_test_wnl_okay(buffer);
const std::string json{factory.create_linestring(wnl)};
REQUIRE(std::string{"{\"type\":\"LineString\",\"coordinates\":[[3.2,4.2],[3.5,4.7],[3.6,4.9]]}"} == json);
}
SECTION("linestring, unique, backwards") {
const auto& wnl = create_test_wnl_okay(buffer);
const std::string json{factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(std::string{"{\"type\":\"LineString\",\"coordinates\":[[3.6,4.9],[3.5,4.7],[3.2,4.2]]}"} == json);
}
SECTION("linestring, all") {
const auto& wnl = create_test_wnl_okay(buffer);
const std::string json{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(std::string{"{\"type\":\"LineString\",\"coordinates\":[[3.2,4.2],[3.5,4.7],[3.5,4.7],[3.6,4.9]]}"} == json);
}
SECTION("linestring, all, backwards") {
const auto& wnl = create_test_wnl_okay(buffer);
const std::string json{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(std::string{"{\"type\":\"LineString\",\"coordinates\":[[3.6,4.9],[3.5,4.7],[3.5,4.7],[3.2,4.2]]}"} == json);
}
SECTION("empty_linestring") {
const auto& wnl = create_test_wnl_empty(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::all), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward), osmium::geometry_error);
}
SECTION("linestring with two same locations") {
const auto& wnl = create_test_wnl_same_location(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward), osmium::geometry_error);
{
const std::string json{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(std::string{"{\"type\":\"LineString\",\"coordinates\":[[3.5,4.7],[3.5,4.7]]}"} == json);
}
{
const std::string json{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(std::string{"{\"type\":\"LineString\",\"coordinates\":[[3.5,4.7],[3.5,4.7]]}"} == json);
}
}
SECTION("linestring with undefined location") {
const auto& wnl = create_test_wnl_undefined_location(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::invalid_location);
}
}
TEST_CASE("GeoJSON polygon geometry") {
osmium::geom::GeoJSONFactory<> factory;
osmium::memory::Buffer buffer{1000};
const auto& wnl = create_test_wnl_closed(buffer);
SECTION("unique forwards (default)") {
const std::string wkt{factory.create_polygon(wnl)};
REQUIRE(wkt == "{\"type\":\"Polygon\",\"coordinates\":[[[3,3],[4.1,4.1],[3.6,4.1],[3.1,3.5],[3,3]]]}");
}
SECTION("unique backwards") {
const std::string wkt{factory.create_polygon(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(wkt == "{\"type\":\"Polygon\",\"coordinates\":[[[3,3],[3.1,3.5],[3.6,4.1],[4.1,4.1],[3,3]]]}");
}
SECTION("all forwards") {
const std::string wkt{factory.create_polygon(wnl, osmium::geom::use_nodes::all)};
REQUIRE(wkt == "{\"type\":\"Polygon\",\"coordinates\":[[[3,3],[4.1,4.1],[4.1,4.1],[3.6,4.1],[3.1,3.5],[3,3]]]}");
}
SECTION("all backwards") {
const std::string wkt{factory.create_polygon(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(wkt == "{\"type\":\"Polygon\",\"coordinates\":[[[3,3],[3.1,3.5],[3.6,4.1],[4.1,4.1],[4.1,4.1],[3,3]]]}");
}
}
TEST_CASE("GeoJSON area geometry") {
osmium::geom::GeoJSONFactory<> factory;
osmium::memory::Buffer buffer{1000};
SECTION("area_1outer_0inner") {
const osmium::Area& area = create_test_area_1outer_0inner(buffer);
REQUIRE_FALSE(area.is_multipolygon());
REQUIRE(std::distance(area.cbegin(), area.cend()) == 2);
REQUIRE(area.subitems<osmium::OuterRing>().size() == area.num_rings().first);
const std::string json{factory.create_multipolygon(area)};
REQUIRE(std::string{"{\"type\":\"MultiPolygon\",\"coordinates\":[[[[3.2,4.2],[3.5,4.7],[3.6,4.9],[3.2,4.2]]]]}"} == json);
}
SECTION("area_1outer_1inner") {
const osmium::Area& area = create_test_area_1outer_1inner(buffer);
REQUIRE_FALSE(area.is_multipolygon());
REQUIRE(std::distance(area.cbegin(), area.cend()) == 3);
REQUIRE(area.subitems<osmium::OuterRing>().size() == area.num_rings().first);
REQUIRE(area.subitems<osmium::InnerRing>().size() == area.num_rings().second);
const std::string json{factory.create_multipolygon(area)};
REQUIRE(std::string{"{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.1,0.1],[9.1,0.1],[9.1,9.1],[0.1,9.1],[0.1,0.1]],[[1,1],[8,1],[8,8],[1,8],[1,1]]]]}"} == json);
}
SECTION("area_2outer_2inner") {
const osmium::Area& area = create_test_area_2outer_2inner(buffer);
REQUIRE(area.is_multipolygon());
REQUIRE(std::distance(area.cbegin(), area.cend()) == 5);
REQUIRE(area.subitems<osmium::OuterRing>().size() == area.num_rings().first);
REQUIRE(area.subitems<osmium::InnerRing>().size() == area.num_rings().second);
int outer_ring=0;
int inner_ring=0;
for (const auto& outer : area.outer_rings()) {
if (outer_ring == 0) {
REQUIRE(outer.front().ref() == 1);
} else if (outer_ring == 1) {
REQUIRE(outer.front().ref() == 100);
} else {
REQUIRE(false);
}
for (const auto& inner : area.inner_rings(outer)) {
if (outer_ring == 0 && inner_ring == 0) {
REQUIRE(inner.front().ref() == 5);
} else if (outer_ring == 0 && inner_ring == 1) {
REQUIRE(inner.front().ref() == 10);
} else {
REQUIRE(false);
}
++inner_ring;
}
inner_ring = 0;
++outer_ring;
}
const std::string json{factory.create_multipolygon(area)};
REQUIRE(std::string{"{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.1,0.1],[9.1,0.1],[9.1,9.1],[0.1,9.1],[0.1,0.1]],[[1,1],[4,1],[4,4],[1,4],[1,1]],[[5,5],[5,7],[7,7],[5,5]]],[[[10,10],[11,10],[11,11],[10,11],[10,10]]]]}"} == json);
}
}
+154
View File
@@ -0,0 +1,154 @@
#include <osmium/geom/geos.hpp>
#ifdef OSMIUM_WITH_GEOS
#include "catch.hpp"
#include "area_helper.hpp"
#include "wnl_helper.hpp"
#include <osmium/geom/mercator_projection.hpp>
#include <memory>
TEST_CASE("GEOS geometry factory - create point") {
osmium::geom::GEOSFactory<> factory;
const std::unique_ptr<geos::geom::Point> point{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(3.2 == point->getX());
REQUIRE(4.2 == point->getY());
REQUIRE(4326 == point->getSRID());
}
TEST_CASE("GEOS geometry factory - create point in web mercator") {
osmium::geom::GEOSFactory<osmium::geom::MercatorProjection> factory;
const std::unique_ptr<geos::geom::Point> point{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(Approx(356222.3705384755l) == point->getX());
REQUIRE(Approx(467961.143605213l) == point->getY());
REQUIRE(3857 == point->getSRID());
}
TEST_CASE("GEOS geometry factory - create point with externally created GEOS factory") {
geos::geom::GeometryFactory geos_factory;
osmium::geom::GEOSFactory<> factory{geos_factory};
const std::unique_ptr<geos::geom::Point> point{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(3.2 == point->getX());
REQUIRE(4.2 == point->getY());
REQUIRE(0 == point->getSRID());
}
TEST_CASE("GEOS geometry factory - can not create from invalid location") {
osmium::geom::GEOSFactory<> factory;
REQUIRE_THROWS_AS(factory.create_point(osmium::Location{}), osmium::invalid_location);
}
TEST_CASE("GEOS geometry factory - create linestring") {
osmium::geom::GEOSFactory<> factory;
osmium::memory::Buffer buffer{10000};
const auto& wnl = create_test_wnl_okay(buffer);
SECTION("from way node list") {
const std::unique_ptr<geos::geom::LineString> linestring{factory.create_linestring(wnl)};
REQUIRE(3 == linestring->getNumPoints());
const auto p0 = std::unique_ptr<geos::geom::Point>(linestring->getPointN(0));
REQUIRE(3.2 == p0->getX());
const auto p2 = std::unique_ptr<geos::geom::Point>(linestring->getPointN(2));
REQUIRE(3.6 == p2->getX());
}
SECTION("without duplicates and backwards") {
const std::unique_ptr<geos::geom::LineString> linestring{factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(3 == linestring->getNumPoints());
const auto p0 = std::unique_ptr<geos::geom::Point>(linestring->getPointN(0));
REQUIRE(3.6 == p0->getX());
const auto p2 = std::unique_ptr<geos::geom::Point>(linestring->getPointN(2));
REQUIRE(3.2 == p2->getX());
}
SECTION("with duplicates") {
const std::unique_ptr<geos::geom::LineString> linestring{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(4 == linestring->getNumPoints());
const auto p0 = std::unique_ptr<geos::geom::Point>(linestring->getPointN(0));
REQUIRE(3.2 == p0->getX());
}
SECTION("with duplicates and backwards") {
const std::unique_ptr<geos::geom::LineString> linestring{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(4 == linestring->getNumPoints());
const auto p0 = std::unique_ptr<geos::geom::Point>(linestring->getPointN(0));
REQUIRE(3.6 == p0->getX());
}
}
TEST_CASE("GEOS geometry factory - create area with one outer and no inner rings") {
osmium::geom::GEOSFactory<> factory;
osmium::memory::Buffer buffer{10000};
const osmium::Area& area = create_test_area_1outer_0inner(buffer);
const std::unique_ptr<geos::geom::MultiPolygon> mp{factory.create_multipolygon(area)};
REQUIRE(1 == mp->getNumGeometries());
const auto* p0 = dynamic_cast<const geos::geom::Polygon*>(mp->getGeometryN(0));
REQUIRE(p0);
REQUIRE(0 == p0->getNumInteriorRing());
const geos::geom::LineString* l0e = p0->getExteriorRing();
REQUIRE(4 == l0e->getNumPoints());
const auto l0e_p0 = std::unique_ptr<geos::geom::Point>(l0e->getPointN(1));
REQUIRE(3.5 == l0e_p0->getX());
}
TEST_CASE("GEOS geometry factory - create area with one outer and one inner ring") {
osmium::geom::GEOSFactory<> factory;
osmium::memory::Buffer buffer{10000};
const osmium::Area& area = create_test_area_1outer_1inner(buffer);
const std::unique_ptr<geos::geom::MultiPolygon> mp{factory.create_multipolygon(area)};
REQUIRE(1 == mp->getNumGeometries());
const auto* p0 = dynamic_cast<const geos::geom::Polygon*>(mp->getGeometryN(0));
REQUIRE(p0);
REQUIRE(1 == p0->getNumInteriorRing());
const geos::geom::LineString* l0e = p0->getExteriorRing();
REQUIRE(5 == l0e->getNumPoints());
const geos::geom::LineString* l0i0 = p0->getInteriorRingN(0);
REQUIRE(5 == l0i0->getNumPoints());
}
TEST_CASE("GEOS geometry factory - create area with two outer and two inner rings") {
osmium::geom::GEOSFactory<> factory;
osmium::memory::Buffer buffer{10000};
const osmium::Area& area = create_test_area_2outer_2inner(buffer);
const std::unique_ptr<geos::geom::MultiPolygon> mp{factory.create_multipolygon(area)};
REQUIRE(2 == mp->getNumGeometries());
const auto* p0 = dynamic_cast<const geos::geom::Polygon*>(mp->getGeometryN(0));
REQUIRE(p0);
REQUIRE(2 == p0->getNumInteriorRing());
const geos::geom::LineString* l0e = p0->getExteriorRing();
REQUIRE(5 == l0e->getNumPoints());
const auto* p1 = dynamic_cast<const geos::geom::Polygon*>(mp->getGeometryN(1));
REQUIRE(p1);
REQUIRE(0 == p1->getNumInteriorRing());
const geos::geom::LineString* l1e = p1->getExteriorRing();
REQUIRE(5 == l1e->getNumPoints());
}
#endif
+34
View File
@@ -0,0 +1,34 @@
#include "catch.hpp"
#include <osmium/geom/mercator_projection.hpp>
TEST_CASE("Mercator projection") {
const osmium::geom::MercatorProjection projection;
REQUIRE(3857 == projection.epsg());
REQUIRE("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs" == projection.proj_string());
}
TEST_CASE("Low level mercator functions") {
const osmium::geom::Coordinates c1{17.839, -3.249};
const osmium::geom::Coordinates r1 = osmium::geom::mercator_to_lonlat(osmium::geom::lonlat_to_mercator(c1));
REQUIRE(r1.x == Approx(c1.x).epsilon(0.000001));
REQUIRE(r1.y == Approx(c1.y).epsilon(0.000001));
const osmium::geom::Coordinates c2{-89.2, 15.915};
const osmium::geom::Coordinates r2 = osmium::geom::mercator_to_lonlat(osmium::geom::lonlat_to_mercator(c2));
REQUIRE(r2.x == Approx(c2.x).epsilon(0.000001));
REQUIRE(r2.y == Approx(c2.y).epsilon(0.000001));
const osmium::geom::Coordinates c3{180.0, 85.0};
const osmium::geom::Coordinates r3 = osmium::geom::mercator_to_lonlat(osmium::geom::lonlat_to_mercator(c3));
REQUIRE(r3.x == Approx(c3.x).epsilon(0.000001));
REQUIRE(r3.y == Approx(c3.y).epsilon(0.000001));
}
TEST_CASE("Mercator bounds") {
const osmium::Location mmax{180.0, osmium::geom::MERCATOR_MAX_LAT};
const osmium::geom::Coordinates c = osmium::geom::lonlat_to_mercator(mmax);
REQUIRE(c.x == Approx(c.y).epsilon(0.001));
REQUIRE(osmium::geom::detail::y_to_lat(osmium::geom::detail::lon_to_x(180.0)) == Approx(osmium::geom::MERCATOR_MAX_LAT).epsilon(0.0000001));
}
+118
View File
@@ -0,0 +1,118 @@
#include "catch.hpp"
#include "area_helper.hpp"
#include "wnl_helper.hpp"
#include <osmium/geom/ogr.hpp>
#include <memory>
TEST_CASE("OGR point geometry") {
const osmium::geom::OGRFactory<> factory;
std::unique_ptr<OGRPoint> point{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(3.2 == point->getX());
REQUIRE(4.2 == point->getY());
}
TEST_CASE("OGR empty point geometry") {
const osmium::geom::OGRFactory<> factory;
REQUIRE_THROWS_AS(factory.create_point(osmium::Location()), osmium::invalid_location);
}
TEST_CASE("OGR linestring geometry") {
osmium::geom::OGRFactory<> factory;
osmium::memory::Buffer buffer{10000};
const auto& wnl = create_test_wnl_okay(buffer);
SECTION("linestring, default") {
std::unique_ptr<OGRLineString> linestring{factory.create_linestring(wnl)};
REQUIRE(3 == linestring->getNumPoints());
REQUIRE(3.2 == linestring->getX(0));
REQUIRE(3.6 == linestring->getX(2));
}
SECTION("linestring, unique nodes, backwards") {
std::unique_ptr<OGRLineString> linestring{factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(3 == linestring->getNumPoints());
REQUIRE(3.6 == linestring->getX(0));
REQUIRE(3.2 == linestring->getX(2));
}
SECTION("linestring, all nodes") {
std::unique_ptr<OGRLineString> linestring{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(4 == linestring->getNumPoints());
REQUIRE(3.2 == linestring->getX(0));
}
SECTION("linestring, all nodes, backwards") {
std::unique_ptr<OGRLineString> linestring{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(4 == linestring->getNumPoints());
REQUIRE(3.6 == linestring->getX(0));
}
}
TEST_CASE("OGR area geometry") {
osmium::geom::OGRFactory<> factory;
osmium::memory::Buffer buffer{10000};
SECTION("area_1outer_0inner") {
const osmium::Area& area = create_test_area_1outer_0inner(buffer);
std::unique_ptr<OGRMultiPolygon> mp {factory.create_multipolygon(area)};
REQUIRE(1 == mp->getNumGeometries());
const auto* p0 = dynamic_cast<const OGRPolygon*>(mp->getGeometryRef(0));
REQUIRE(p0);
REQUIRE(0 == p0->getNumInteriorRings());
const OGRLineString* l0e = p0->getExteriorRing();
REQUIRE(4 == l0e->getNumPoints());
REQUIRE(3.5 == l0e->getX(1));
}
SECTION("area_1outer_1inner") {
const osmium::Area& area = create_test_area_1outer_1inner(buffer);
std::unique_ptr<OGRMultiPolygon> mp {factory.create_multipolygon(area)};
REQUIRE(1 == mp->getNumGeometries());
const auto* p0 = dynamic_cast<const OGRPolygon*>(mp->getGeometryRef(0));
REQUIRE(p0);
REQUIRE(1 == p0->getNumInteriorRings());
const OGRLineString* l0e = p0->getExteriorRing();
REQUIRE(5 == l0e->getNumPoints());
const OGRLineString* l0i0 = p0->getInteriorRing(0);
REQUIRE(5 == l0i0->getNumPoints());
}
SECTION("area_2outer_2inner") {
const osmium::Area& area = create_test_area_2outer_2inner(buffer);
std::unique_ptr<OGRMultiPolygon> mp {factory.create_multipolygon(area)};
REQUIRE(2 == mp->getNumGeometries());
const auto* p0 = dynamic_cast<const OGRPolygon*>(mp->getGeometryRef(0));
REQUIRE(p0);
REQUIRE(2 == p0->getNumInteriorRings());
const OGRLineString* l0e = p0->getExteriorRing();
REQUIRE(5 == l0e->getNumPoints());
const auto* p1 = dynamic_cast<const OGRPolygon*>(mp->getGeometryRef(1));
REQUIRE(p1);
REQUIRE(0 == p1->getNumInteriorRings());
const OGRLineString* l1e = p1->getExteriorRing();
REQUIRE(5 == l1e->getNumPoints());
}
}
+100
View File
@@ -0,0 +1,100 @@
#include "catch.hpp"
#include <osmium/geom/ogr.hpp>
#include <osmium/geom/wkb.hpp>
#include <osmium/util/endian.hpp>
#include <memory>
#include <sstream>
#include <string>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#include "area_helper.hpp"
#include "wnl_helper.hpp"
std::string to_wkb(const OGRGeometry* geometry) {
std::string buffer;
buffer.resize(geometry->WkbSize());
geometry->exportToWkb(wkbNDR, reinterpret_cast<unsigned char*>(&*buffer.begin()));
return buffer;
}
TEST_CASE("compare WKB point against GDAL/OGR") {
const osmium::geom::WKBFactory<> wkb_factory{osmium::geom::wkb_type::wkb};
const osmium::geom::OGRFactory<> ogr_factory;
const osmium::Location loc{3.2, 4.2};
const std::string wkb{wkb_factory.create_point(loc)};
const std::unique_ptr<OGRPoint> geometry = ogr_factory.create_point(loc);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
TEST_CASE("compare WKB linestring against GDAL/OGR") {
osmium::geom::WKBFactory<> wkb_factory{osmium::geom::wkb_type::wkb};
osmium::geom::OGRFactory<> ogr_factory;
osmium::memory::Buffer buffer{10000};
const auto& wnl = create_test_wnl_okay(buffer);
SECTION("linestring") {
const std::string wkb{wkb_factory.create_linestring(wnl)};
const std::unique_ptr<OGRLineString> geometry = ogr_factory.create_linestring(wnl);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
SECTION("linestring, unique nodes, backwards") {
const std::string wkb{wkb_factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
const std::unique_ptr<OGRLineString> geometry = ogr_factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
SECTION("linestring, all nodes, forwards") {
const std::string wkb{wkb_factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
const std::unique_ptr<OGRLineString> geometry = ogr_factory.create_linestring(wnl, osmium::geom::use_nodes::all);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
SECTION("linestring, all nodes, backwards") {
const std::string wkb{wkb_factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
const std::unique_ptr<OGRLineString> geometry = ogr_factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
}
TEST_CASE("compare WKB area against GDAL/OGR") {
osmium::geom::WKBFactory<> wkb_factory{osmium::geom::wkb_type::wkb};
osmium::geom::OGRFactory<> ogr_factory;
osmium::memory::Buffer buffer{10000};
SECTION("area_1outer_0inner") {
const osmium::Area& area = create_test_area_1outer_0inner(buffer);
const std::string wkb{wkb_factory.create_multipolygon(area)};
const std::unique_ptr<OGRMultiPolygon> geometry = ogr_factory.create_multipolygon(area);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
SECTION("area_1outer_1inner") {
const osmium::Area& area = create_test_area_1outer_1inner(buffer);
const std::string wkb{wkb_factory.create_multipolygon(area)};
const std::unique_ptr<OGRMultiPolygon> geometry = ogr_factory.create_multipolygon(area);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
SECTION("area_2outer_2inner") {
const osmium::Area& area = create_test_area_2outer_2inner(buffer);
const std::string wkb{wkb_factory.create_multipolygon(area)};
const std::unique_ptr<OGRMultiPolygon> geometry = ogr_factory.create_multipolygon(area);
REQUIRE(to_wkb(geometry.get()) == wkb);
}
}
#endif
+108
View File
@@ -0,0 +1,108 @@
#include "catch.hpp"
#include <osmium/geom/factory.hpp>
#include <osmium/geom/mercator_projection.hpp>
#include <osmium/geom/projection.hpp>
TEST_CASE("Indentity Projection") {
const osmium::geom::IdentityProjection projection;
REQUIRE(4326 == projection.epsg());
REQUIRE("+proj=longlat +datum=WGS84 +no_defs" == projection.proj_string());
}
TEST_CASE("Projection 4326") {
const osmium::geom::Projection projection{4326};
REQUIRE(4326 == projection.epsg());
REQUIRE("+init=epsg:4326" == projection.proj_string());
const osmium::Location loc{1.0, 2.0};
const osmium::geom::Coordinates c{1.0, 2.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
TEST_CASE("Projection 4326 from init string") {
const osmium::geom::Projection projection{"+init=epsg:4326"};
REQUIRE(-1 == projection.epsg());
REQUIRE("+init=epsg:4326" == projection.proj_string());
const osmium::Location loc{1.0, 2.0};
const osmium::geom::Coordinates c{1.0, 2.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
TEST_CASE("Creating projection from unknown init string") {
REQUIRE_THROWS_AS(osmium::geom::Projection{"abc"}, osmium::projection_error);
}
TEST_CASE("Creating projection from unknown EPSG code") {
REQUIRE_THROWS_AS(osmium::geom::Projection{9999999}, osmium::projection_error);
}
TEST_CASE("Projection 3857") {
const osmium::geom::Projection projection{3857};
REQUIRE(3857 == projection.epsg());
REQUIRE("+init=epsg:3857" == projection.proj_string());
SECTION("Zero coordinates") {
const osmium::Location loc{0.0, 0.0};
const osmium::geom::Coordinates c{0.0, 0.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
SECTION("Max longitude") {
const osmium::Location loc{180.0, 0.0};
const osmium::geom::Coordinates c{20037508.34, 0.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
SECTION("Min longitude") {
const osmium::Location loc{-180.0, 0.0};
const osmium::geom::Coordinates c{-20037508.34, 0.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
SECTION("Max latitude") {
const osmium::Location loc{0.0, 85.0511288};
const osmium::geom::Coordinates c{0.0, 20037508.34};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
}
TEST_CASE("MercatorProjection: Zero coordinates") {
const osmium::geom::MercatorProjection projection;
const osmium::Location loc{0.0, 0.0};
const osmium::geom::Coordinates c{0.0, 0.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
TEST_CASE("MercatorProjection: Max longitude") {
const osmium::geom::MercatorProjection projection;
const osmium::Location loc{180.0, 0.0};
const osmium::geom::Coordinates c{20037508.34, 0.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
TEST_CASE("MercatorProjection: Min longitude") {
const osmium::geom::MercatorProjection projection;
const osmium::Location loc{-180.0, 0.0};
const osmium::geom::Coordinates c{-20037508.34, 0.0};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
TEST_CASE("MercatorProjection: Max latitude") {
const osmium::geom::MercatorProjection projection;
const osmium::Location loc{0.0, 85.0511288};
const osmium::geom::Coordinates c{0.0, 20037508.34};
REQUIRE(projection(loc).x == Approx(c.x).epsilon(0.00001));
REQUIRE(projection(loc).y == Approx(c.y).epsilon(0.00001));
}
+158
View File
@@ -0,0 +1,158 @@
#include "catch.hpp"
#include "test_tile_data.hpp"
#include <osmium/geom/tile.hpp>
#include <cmath>
#include <sstream>
TEST_CASE("Helper functions") {
REQUIRE(osmium::geom::num_tiles_in_zoom(0) == 1);
REQUIRE(osmium::geom::num_tiles_in_zoom(1) == 2);
REQUIRE(osmium::geom::num_tiles_in_zoom(12) == 4096);
REQUIRE(osmium::geom::tile_extent_in_zoom(1) == osmium::geom::detail::max_coordinate_epsg3857);
}
TEST_CASE("Tile from x0.0 y0.0 at zoom 0") {
const osmium::Location l{0.0, 0.0};
const osmium::geom::Tile t{0, l};
REQUIRE(t.x == 0);
REQUIRE(t.y == 0);
REQUIRE(t.z == 0);
REQUIRE(t.valid());
}
TEST_CASE("Tile from x180.0 y90.0 at zoom 0") {
const osmium::Location l{180.0, 90.0};
const osmium::geom::Tile t{0, l};
REQUIRE(t.x == 0);
REQUIRE(t.y == 0);
REQUIRE(t.z == 0);
REQUIRE(t.valid());
}
TEST_CASE("Tile from x180.0 y90.0 at zoom 4") {
const osmium::Location l{180.0, 90.0};
const osmium::geom::Tile t{4, l};
REQUIRE(t.x == (1U << 4U) - 1);
REQUIRE(t.y == 0);
REQUIRE(t.z == 4);
REQUIRE(t.valid());
}
TEST_CASE("Tile from x0.0 y0.0 at zoom 4") {
const osmium::Location l{0.0, 0.0};
const osmium::geom::Tile t{4, l};
const auto n = 1U << (4U - 1U);
REQUIRE(t.x == n);
REQUIRE(t.y == n);
REQUIRE(t.z == 4);
REQUIRE(t.valid());
}
TEST_CASE("Tile from max values at zoom 4") {
const osmium::geom::Tile t{4U, 15U, 15U};
REQUIRE(t.valid());
}
TEST_CASE("Tile from max values at zoom 30") {
const osmium::geom::Tile t{30U, (1U << 30U) - 1, (1U << 30U) - 1};
REQUIRE(t.valid());
}
TEST_CASE("Tile from coordinates") {
const osmium::geom::Coordinates c{9.99312, 53.55078};
const osmium::geom::Tile t{12, osmium::geom::lonlat_to_mercator(c)};
REQUIRE(t.valid());
REQUIRE(t.x == 2161);
REQUIRE(t.y == 1323);
}
TEST_CASE("Tile equality") {
const osmium::geom::Tile a{4, 3, 4};
const osmium::geom::Tile b{4, 3, 4};
const osmium::geom::Tile c{4, 4, 3};
REQUIRE(a == b);
REQUIRE(a != c);
REQUIRE(b != c);
}
TEST_CASE("Tile order") {
const osmium::geom::Tile a{4, 3, 4};
const osmium::geom::Tile b{6, 3, 4};
const osmium::geom::Tile c{6, 4, 3};
const osmium::geom::Tile d{6, 4, 2};
REQUIRE(a < b);
REQUIRE(a < c);
REQUIRE(b < c);
REQUIRE(d < c);
}
TEST_CASE("Check a random list of tiles") {
std::istringstream input_data{s};
int n = 0;
while (input_data) {
double lon = NAN;
double lat = NAN;
uint32_t x = 0;
uint32_t y = 0;
uint32_t zoom = 0;
input_data >> lon;
input_data >> lat;
input_data >> x;
input_data >> y;
input_data >> zoom;
if (std::isnan(lon)) {
break;
}
const osmium::Location l{lon, lat};
const osmium::geom::Tile t{zoom, l};
REQUIRE(t.x == x);
REQUIRE(t.y == y);
++n;
}
REQUIRE(n == 472);
}
TEST_CASE("Invalid tiles") {
osmium::geom::Tile tile{0, 0, 0};
REQUIRE(tile.valid());
SECTION("Zoom level out of bounds") {
tile.z = 100;
}
SECTION("x out of bounds") {
tile.x = 1;
}
SECTION("y out of bounds") {
tile.y = 1;
}
SECTION("x out of bounds") {
tile.z = 4;
tile.x = 100;
}
SECTION("y out of bounds") {
tile.z = 4;
tile.y = 100;
}
REQUIRE_FALSE(tile.valid());
}
+477
View File
@@ -0,0 +1,477 @@
#include <string>
static std::string s = R"(127.4864358 16.8380041 223904 118630 18
163.1103174 39.4760232 121 48 7
-4.1372725 -22.5105386 31 36 6
98.7193066 -36.2312406 1 1 1
63.5773661 -13.47636 21 17 5
-88.4518148 37.9805485 260 395 10
-14.5903133 -28.2989812 3763 4767 13
-13.4971239 -34.4080035 14 19 5
-169.156223 -64.0900356 3 93 7
27.1473191 -4.1993125 4713 4191 13
-160.9733129 54.3684314 13 81 8
129.0194139 14.2576156 439 235 9
-69.5085993 -56.8253221 10057 22700 15
-77.8387486 18.1961517 1162 1837 12
-76.2695325 -18.2494296 147 282 9
-91.594905 7.6698071 1 3 3
-116.7926741 -20.6060813 179 571 10
109.0552776 -1.9947569 52620 33131 16
-156.1846426 -79.3817554 33 449 9
-95.3403755 -27.8978407 1 4 3
-55.1827573 -73.2293796 44 103 7
-108.5207885 -48.0099293 50 167 8
23.7540108 -15.3395164 9273 8898 14
-155.6662842 -68.3295899 0 0 0
75.8139119 30.9914252 363 209 9
-135.8034544 64.7242469 0 1 2
-48.743352 70.9392876 23 13 6
-38.6968026 7.7867812 0 0 0
-18.5234838 11.8557704 29395 30594 16
-152.5632568 19.4069834 78 455 10
-63.2089431 -80.5909713 0 0 0
-94.1255611 -81.2028822 244 930 10
175.0862205 -33.0865142 3 2 2
-179.6241926 -37.0256609 1 625 10
135.6783824 -38.6011643 459739 323170 19
-139.6407533 -83.2495209 0 7 3
-14.1336447 -56.5465949 58 88 7
-30.7414568 -32.9543731 26 38 6
10.3306861 73.2444693 1082 399 11
-84.8379263 29.2363222 16 26 6
-94.0685822 -39.5503996 7821 20309 15
-86.5944356 -41.7491891 265 642 10
11.9182172 -80.5613703 34937 58784 16
91.8773752 -32.1741317 0 0 0
126.2879157 20.5759564 1742 904 11
-160.7743029 -47.3192128 27999 340565 19
-4.2449045 -50.3288332 31 42 6
-66.6857158 61.4380757 10 9 5
169.7372317 -74.3365704 3 3 2
87.4815328 75.6218888 95 21 7
60.3544927 28.3165267 0 0 0
-48.9614619 -59.3292497 2 5 3
-123.2935018 -59.5454886 80 362 9
-31.5909316 -14.8985476 13 17 5
58.1862173 59.0957666 2710 1209 12
-72.8881665 -2.2648849 1218 2073 12
-33.7267461 8.6006817 106512 124785 18
175.723181 46.4929742 7 2 3
-127.1963326 76.0328786 0 0 0
-161.7444367 -26.7634497 13293 151310 18
-163.976298 -8.1169845 91 1070 11
63.7757109 5.6049418 2 1 2
12.7012434 22.1157713 70160 57276 17
19.5832849 -25.0284049 1135 1171 11
-22.6619642 54.8831276 111 81 8
78.7736907 24.0919863 5888 3530 13
121.9003045 -64.6256685 107 94 7
-64.8766793 61.6655916 81 71 8
113.0498445 -70.0016518 416 397 9
-51.5116259 68.1532424 46781 31217 17
-89.9005747 87.9523248 512 0 11
59.2536822 -75.1520493 10 13 4
17.375372 74.9259262 287448 93371 19
75.6426945 8.4808632 186153 124873 18
144.89589 75.1647661 14786 2875 14
174.4350898 -29.268169 258091 153376 18
-91.8773113 50.6182166 0 0 0
5.3822308 45.1391794 134991 94156 18
43.0978373 -10.0764237 324909 276895 19
55.6682917 -40.9015342 21451 20470 15
-37.584032 52.253723 6 5 4
131.0141997 29.0271798 28309 13621 15
69.8830721 -86.8548645 363918 524287 19
-107.7917548 -76.9626654 26290 110787 17
-57.1736642 49.9345991 2 2 3
165.5170226 -84.9735363 982 1021 10
-139.7208984 -73.8754873 1 12 4
-154.5959463 -10.596718 4 33 6
-106.4164918 36.5042686 3348 6405 14
-92.9688259 -11.2262505 0 2 2
138.2722283 8.1109779 7 3 3
123.1389319 -40.3505677 862 637 10
-171.4780435 24.9112482 0 13 5
89.3668763 -63.1809434 392293 381781 19
-79.6906176 -13.376312 9130 17612 15
133.1329522 -28.9032634 445 298 9
115.0369496 70.6965823 6 1 3
-74.8926697 -78.9770185 2391 7144 13
51.9175766 73.0184277 164 50 8
178.1319784 43.7111421 509 186 9
162.4098389 61.1595443 0 0 0
7.6241126 -75.6182944 2 3 2
172.8902767 28.5068027 125 53 7
156.4030739 -76.7309238 478 431 9
-131.3963189 62.2039684 1 2 3
34.6057088 -36.2933264 2441 2491 12
-3.3896413 -48.2734347 15 20 5
83.528842 -48.219886 2998 2675 12
16.6000512 -31.6322244 17894 19421 15
-18.2425749 21.0749052 14 14 5
35.6035336 46.9916228 78498 46105 17
-109.3453091 -34.2312523 12 38 6
88.4909962 58.6104749 47 19 6
-129.7372783 21.7408241 571 1794 12
25.7269392 -40.1240139 1 1 1
26.3829579 5.248998 37570 31811 16
141.1768247 -41.4653038 14617 10269 14
151.3788752 -38.0160512 241302 161042 18
-92.7471483 -35.391745 15883 39664 16
142.5902623 -14.1023743 28 17 5
179.3461485 -86.7573357 3 3 2
-40.9746194 61.5689546 790 576 11
128.6399735 -54.8895009 56186 44772 16
-141.391583 -63.8272 0 2 2
-3.5004218 19.3089998 4016 3648 13
16.138208 -42.7868627 1 1 1
78.502315 -18.91667 2 2 2
-44.6311826 -15.7483106 98572 142686 18
-120.431941 -39.3431529 0 0 0
-70.4915024 25.4763412 9967 13984 15
118.0711114 -66.5691073 211 192 8
-114.945538 38.1764389 0 0 0
142.991315 -46.3378741 14699 10577 14
34.2770562 -84.7173756 312063 518834 19
109.5563415 -85.9138266 105424 131071 17
-171.8032643 70.1948223 0 3 4
-90.8434294 89.7252122 126 0 9
163.5677082 -53.9885419 3 2 2
128.884016 -63.0732584 112461 95358 17
116.5348575 -56.1616143 843 705 10
-171.5770602 37.9225943 11 197 9
48.1396653 -49.3932234 5191 5392 13
-157.6786731 15.3895763 32507 239456 19
15.7385145 -37.1364397 1113 1251 11
137.5179466 61.2025671 1 0 1
-70.4767722 42.9798424 19938 24086 16
27.8867901 43.4642562 9461 5991 14
68.5677462 -84.9560937 11312 16334 14
-56.790151 -67.124147 179437 395476 19
108.850832 43.3493384 26291 11996 15
-139.0655153 8.2673118 1 7 4
59.3726661 83.3857699 21788 1515 15
-158.4718709 -12.2313178 1 17 5
30.358316 -83.020501 2393 3871 12
-169.9667863 71.1152107 1 13 6
-89.9418297 -7.7258969 131156 273429 19
-16.5050312 47.1843923 116 89 8
-151.9641886 -36.3579042 5103 39881 16
169.7406916 60.0950674 62 18 6
32.6694543 -1.8435981 2 2 2
97.7880811 77.5295169 50569 9674 16
87.7554889 -85.6741368 389947 524287 19
-19.0174909 29.1940122 0 0 1
-86.6180019 82.6445919 1 0 2
-50.1997802 -21.1913243 1476 2294 12
73.8710121 75.2201385 5 1 3
41.7434624 79.1410045 322937 65770 19
-122.4312562 -68.4513916 10 48 6
-54.6116448 -23.1761137 1 2 2
-35.8774263 -51.5317442 102 170 8
179.2976644 -76.2729885 127 107 7
-111.6934402 -85.2696836 1 7 3
-24.2137947 28.4102025 3 3 3
-168.0816395 -64.0317696 16 375 9
43.2514876 6.4266793 2540 1974 12
25.5877932 49.3486828 4 2 3
-133.3671657 -25.0795973 8 36 6
-140.2002274 -37.3713396 452 2507 12
73.6326557 -21.9898207 360 288 9
-83.0901448 -69.1893461 2205 6305 13
-32.83227 -11.1061854 3348 4350 13
-151.8897244 14.6891958 0 0 0
-118.1125151 82.3085937 704 288 12
119.9903922 1.4884267 53 31 6
-116.9455865 -48.1971821 0 2 2
111.4279587 -52.6024326 828 688 10
-78.9207774 28.6183104 147207 218615 19
45.1367631 24.1416251 40 27 6
115.0905685 7.2971256 6714 3929 13
-58.0837371 -55.4033522 43 87 7
-44.6785779 83.0751777 6158 877 14
80.2002966 84.3087089 2960 91 12
-167.3118039 -59.2698371 72 1445 11
139.9974902 -74.5994693 455 419 9
-27.5858357 -36.1890547 6936 9960 14
68.8572424 20.2096749 353 226 9
-168.7172825 69.1607462 2053 15104 16
62.1361934 74.6007777 44079 11896 16
-102.9645124 65.3735325 112191 135144 19
178.8434887 18.6231394 65325 29316 16
-138.0494072 -80.3786289 29 228 8
33.3858934 62.9029909 155382 71699 18
-1.6915643 74.737228 1 0 2
18.2680756 6.6441482 1 0 1
34.0511738 -28.6862516 2 2 2
-24.0705994 -0.6026568 28386 32877 16
-176.8765092 -14.3252022 568 35403 16
154.6553417 -79.7390666 1903 1809 11
151.6055263 59.552346 14 4 4
-101.7681729 45.3498263 113933 187876 19
-52.3703658 -89.8260406 181 511 9
-85.7937259 16.5632413 2143 3713 13
114.7030999 6.6846014 104 61 7
13.6027861 88.6863877 2 0 2
60.5726928 38.256536 43794 25219 16
141.0903381 -35.4468007 7306 4959 13
-38.8182454 -55.2332792 200 350 9
179.7818018 -79.6600052 523970 462627 19
-45.2102175 82.7381225 196301 32058 19
-36.5811826 -35.3991346 0 0 0
-26.7719575 -75.7014102 223154 435372 19
77.3451937 -65.4613594 1 1 1
-37.3286225 38.3250599 51945 50407 17
70.8235495 35.3870419 365288 206979 19
99.8381373 -83.4101655 1 1 1
25.8851923 67.415543 9370 3991 14
-12.3393758 68.5972027 7 3 4
-142.9400273 -36.2904852 1 9 4
110.085689 -50.9514972 422467 348655 19
121.5093657 53.9939447 54888 21044 16
151.1307841 -33.0798435 58 38 6
100.4346499 -57.0129759 6 5 3
114.0564882 63.4987339 428250 141474 19
-17.3978586 19.2981593 57 57 7
-129.101039 -66.3423574 579 3067 12
117.6639917 14.0568892 433504 241463 19
97.7014577 69.1261732 202216 60490 18
-174.7931333 81.4174709 7583 46045 19
47.205341 -48.8608019 20680 21495 15
167.416796 -7.5063098 247 133 8
63.6744589 21.3392888 0 0 0
-106.0719817 33.8353547 53832 104862 18
-48.4051268 27.0438152 191648 221209 19
64.5675545 -27.0683253 5 4 3
110.7800249 -35.8217841 0 0 0
164.8954138 81.9558801 31393 2538 15
-54.4595565 -23.0128432 357 579 10
43.2542709 67.694011 2540 989 12
137.6537233 -78.7052092 28913 28450 15
76.9271563 -60.0777348 0 0 0
-145.0240216 47.3869213 6367 22947 16
136.2328853 -73.4534547 3598 3304 12
48.8047148 74.3001023 81 23 7
-144.6978756 33.6270048 200 820 11
64.6723407 -37.196427 21 19 5
-58.3356482 -19.0812366 22148 36307 16
19.611535 -31.3013119 0 0 0
-63.8923439 -25.9560287 1 2 2
-152.5288047 -30.9747477 0 1 1
-99.8509683 -69.0258965 7295 25181 15
-22.9015207 -21.0915082 13 17 5
161.4402438 61.1430241 15539 4652 14
-102.9499976 -1.2629414 1 4 3
-159.7995801 51.1570142 14 85 8
10.5265485 -86.8526122 67 127 7
47.1581041 -73.1628608 646 823 10
2.7577906 -66.3257104 66540 98133 17
96.9653593 -9.8258675 6 4 3
-55.4199846 25.3325116 2834 3499 13
-47.3590106 59.5085133 2 2 3
179.1441501 22.1324479 7 3 3
16.2649007 47.8480398 4 2 3
-27.8373963 -0.3926597 27700 32839 16
-99.7938802 -48.8013068 912 2685 12
133.4858024 28.457146 3 1 2
-174.7621207 -25.8318084 238 9409 14
106.856378 19.640472 203 113 8
31.4361995 -80.2488406 76981 116886 17
148.6539554 -70.0347611 116 99 7
45.1821773 17.5961595 80 57 7
-30.3630114 71.3238998 54481 27877 17
106.547704 44.9731358 6520 2947 13
-132.8057564 23.3928795 67 221 9
116.6455816 -10.1097592 52 33 6
172.5512559 -53.2307289 3 2 2
-114.2084519 42.834332 2994 6030 14
91.0172087 87.6025 1 0 1
-101.666812 -84.7858729 7130 32495 15
-14.8074931 68.3277393 0 0 0
140.3354277 -7.8709618 14 8 4
-130.4961987 -69.4777523 9011 50594 16
-121.239479 1.0896367 1 3 3
-141.2241052 3.4495348 56471 257117 19
42.6041377 -19.1328846 2532 2269 12
141.3750885 -1.8110503 468036 264781 19
-26.545021 -30.9641274 218 302 9
87.6154866 10.1978751 5 3 3
88.002559 -33.4108714 762 612 10
170.9910642 -7.1925019 3993 2130 12
-66.5680487 -88.1144993 165197 524287 19
-71.5925378 45.720316 0 0 0
-16.457642 57.1688038 930 625 11
20.8379624 -11.1485568 35 33 6
-72.9399538 -16.6504502 1 2 2
52.8746845 -71.5213577 10598 12927 14
93.2392918 12.5073156 48 29 6
-3.7803834 73.7748237 128319 49794 18
93.5713795 -38.8308882 24 19 5
87.3474619 -6.9970464 23 16 5
161.2199467 48.5612779 30 11 5
87.343969 -31.3153618 47 37 6
60.5318627 58.0869948 42 19 6
161.4388458 42.7612385 1 0 1
14.1262999 -26.2101142 4 4 3
-135.776346 -21.4248586 503 2297 12
-100.3980364 82.0236388 1 0 3
-55.8093833 -87.2455194 0 0 0
-15.6397352 64.454076 935 540 11
131.5974423 39.1251372 110 48 7
56.8045509 27.5658629 168 107 8
146.3634996 14.9287416 3 1 2
51.5699041 74.0370231 82 23 7
29.3100901 33.7208834 152414 104963 18
19.0622442 1.6781772 141 126 8
-22.2575472 73.6467471 114864 50126 18
125.0208495 47.8533914 3470 1426 12
-92.4804503 29.8409387 995 1691 12
113.6362202 86.1158625 0 0 0
113.6203756 28.4267778 26 13 5
124.472202 25.8347062 13856 6974 14
79.3654306 -33.7864728 46 38 6
-121.169233 -66.7642843 2 12 4
179.5816311 1.0182064 0 0 0
179.1123794 53.367624 0 0 0
-25.6611689 32.6575586 14048 13236 15
92.3370554 58.8306651 12 4 4
142.2756448 63.6603101 3 1 2
-162.8914282 -84.54406 6229 129034 17
-54.0759935 53.1086102 0 0 0
9.1647045 -39.4876873 34436 40604 16
59.3708822 -43.9789068 10894 10425 14
-16.6397572 46.5985252 929 723 11
-36.1053642 -67.6882841 0 1 1
-82.5851985 -69.5277766 4 12 4
117.0059969 -63.2697116 432546 382068 19
127.9740251 85.5341901 112129 0 17
-13.9255265 59.7720207 120931 76457 18
-167.7778382 41.7974194 69 761 11
-100.1217856 -70.5624949 454 1599 11
-42.4790343 -2.0016191 25034 33132 16
-8.0782317 65.9680074 30 16 6
-37.0481466 -55.2856125 203 350 9
149.074382 16.7121888 58 28 6
-53.1010518 -15.8492068 0 0 0
50.8517919 -30.8366257 42025 38674 16
-175.0355994 77.1929422 0 4 5
-33.3221217 72.5636809 6 3 4
-139.9201802 52.3400823 7296 21546 16
86.0299659 23.4535286 1513 886 11
105.6297238 -50.5521342 25998 21733 15
104.2700533 36.5417507 206999 102450 18
58.5238253 84.0320186 86843 3912 17
-121.9177826 65.4393267 1321 2108 13
-152.689211 57.4112922 39774 159515 19
94.2583831 80.8625455 6240 801 13
118.8199874 -37.6116567 53 39 6
5.787144 7.0599251 4227 3934 13
152.1682213 11.778732 236 119 8
37.4503636 64.311498 2474 1084 12
35.2616139 -59.8196291 38 45 6
-20.2808572 -26.9983008 29075 37875 16
-88.8541607 -20.4896703 66370 146320 18
125.7726709 41.2603793 27 11 5
-55.4322696 22.1767236 11338 14313 15
-38.2393439 -56.4681037 6 11 4
-139.4517643 -81.3621632 3 29 5
-146.7954207 -74.6050826 6044 53642 16
161.6654898 -71.7313805 15549 12957 14
-85.4720514 -73.177675 2 6 3
-25.7457381 -42.9842376 13 20 5
-2.0766311 51.0142455 0 0 1
-82.9179334 1.4865326 8836 16248 15
140.5661302 61.5056993 28 9 5
-30.2614099 35.5786378 54518 51659 17
-49.2072142 -38.6965571 0 1 1
124.6587534 9.5039576 433 242 9
-113.4516741 81.4585593 24229 11410 17
-4.5134723 68.229217 3 1 3
75.4838529 -44.997406 2906 2622 12
6.4715239 28.6900832 8486 6828 14
91.9187555 -24.8192643 6187 4679 13
-28.2510181 -42.6238777 26 40 6
-151.1042237 -21.4553189 2630 18384 15
-149.6272551 0.4235911 1382 8172 14
-51.9171488 74.630423 0 0 0
-36.5660117 37.242858 101 99 8
91.7136677 -30.9077617 772 604 10
61.6846009 -85.9378164 5 7 3
11.3772008 69.40183 34839 14980 16
82.6825938 9.4496443 11954 7759 14
61.8446231 -40.0114106 2751 2545 12
-51.6223196 -11.7880324 5 8 4
113.0309076 -73.4376173 13336 13217 14
-169.3808275 -72.7209175 0 25 5
-98.5653414 -80.0893122 231 910 10
-20.4653707 29.9801495 3 3 3
78.9156686 2.599349 0 0 0
76.0635255 -71.0823347 2913 3216 12
-26.1185551 22.3616029 28013 28589 16
177.8803853 -56.3662368 4071 2828 12
-157.7926463 78.4998022 15 34 8
168.6834344 -34.5535211 7934 4934 13
-77.208013 -44.0964079 0 1 1
-56.6162078 28.1240365 10 13 5
8.6548899 72.178831 137374 53767 18
-27.9342497 8.2525327 1 1 2
91.6356971 -13.5230128 6181 4406 13
-161.9980398 -75.4443511 0 13 4
46.8556576 -27.1078679 5162 4737 13
147.2806954 -48.1491071 465 334 9
-168.2679875 -29.0171568 0 2 2
10.0251187 -3.144812 8 8 4
109.0281873 81.9713348 26307 2528 15
-129.6281276 -36.9614028 73359 320148 19
7.3831244 -18.3270273 2132 2260 12
-34.4625217 53.2837646 52988 42524 17
129.8855275 -26.30807 3525 2358 12
-69.6374195 -45.7769025 10045 21081 15
59.9868336 50.3716565 43688 22120 16
126.4653338 -75.607391 1743 1698 11
-34.6459616 53.2502443 51 41 7
152.2415169 -71.7528837 1 1 1
-83.2126752 32.6685119 35239 52939 17
178.6388805 70.5727365 31 7 5
-153.5646223 -81.9491561 2 29 5
178.2668159 11.8222247 0 0 0
-27.136902 18.4287365 6 7 4
-104.7744923 -64.7769211 54777 193540 18
58.2318889 9.5897974 2710 1938 12
138.9707487 -65.2734433 14516 12149 14
-115.29331 -84.3402223 2944 16033 14
-66.3487123 -81.8340211 20 58 6
76.1802855 40.4007156 364 193 9
-77.5026834 -30.653231 145 301 9
-52.6095007 -79.7155889 11595 28942 15
-2.5594899 -31.6276806 7 9 4
-58.6267217 41.4851391 2 2 3
-0.7245205 -70.7329433 509 801 10
161.2822996 -79.0311957 124257 114418 17
144.8720197 80.1059269 14785 1811 14
159.3764075 -23.5100054 0 0 0
82.3109493 -7.0673699 95504 68115 17
94.3931949 63.0474034 97 34 7
87.4523391 -73.2690527 3043 3297 12
101.4256455 -0.8267694 12 8 4
-112.7666152 -82.3670281 12239 61007 16
-82.0948447 -38.0810449 8 19 5
113.2906728 -19.193243 104 70 7
16.953131 78.402684 35 8 6
-81.7974685 -53.9596787 34 86 7
69.8051889 58.1416894 181902 78760 18
-9.2435464 -53.4296594 3 5 3
30.0415066 11.4884737 4 3 3
125.2704157 -69.6324197 54 49 6
-41.2060435 63.8501787 789 548 11
120.407662 -4.9889504 6835 4209 13
92.0345558 -8.0809262 24761 17121 15
127.1061722 83.1311204 6988 428 13
-178.9414629 64.0086678 770 69897 18
49.0743035 -4.3000419 10425 8387 14
-45.109002 -55.1435498 1534 2803 12
3.2795498 75.95918 32 10 6
)";
+148
View File
@@ -0,0 +1,148 @@
#include "catch.hpp"
#include "wnl_helper.hpp"
#include <osmium/geom/mercator_projection.hpp>
#include <osmium/geom/wkb.hpp>
#include <osmium/util/endian.hpp>
#include <string>
#if __BYTE_ORDER == __LITTLE_ENDIAN
TEST_CASE("WKB geometry factory (byte-order-dependent), point in WKB") {
const osmium::Location loc{3.2, 4.2};
const osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
const std::string wkb{factory.create_point(loc)};
REQUIRE(wkb == "01010000009A99999999990940CDCCCCCCCCCC1040");
}
TEST_CASE("WKB geometry factory (byte-order-dependent), point in EWKB") {
const osmium::Location loc{3.2, 4.2};
const osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::ewkb, osmium::geom::out_type::hex};
const std::string wkb{factory.create_point(loc)};
REQUIRE(wkb == "0101000020E61000009A99999999990940CDCCCCCCCCCC1040");
}
#ifndef OSMIUM_USE_SLOW_MERCATOR_PROJECTION
TEST_CASE("WKB geometry factory (byte-order-dependent), point in web mercator WKB") {
const osmium::Location loc{3.2, 4.2};
const osmium::geom::WKBFactory<osmium::geom::MercatorProjection> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
const std::string wkb{factory.create_point(loc)};
REQUIRE(wkb.substr(0, 10) == "0101000000"); // little endian, point type
REQUIRE(wkb.substr(10 + 2, 16 - 2) == "706E7BF9BD1541"); // x coordinate (without first (least significant) byte)
REQUIRE(wkb.substr(26 + 2, 16 - 2) == "A90093E48F1C41"); // y coordinate (without first (least significant) byte)
}
TEST_CASE("WKB geometry factory (byte-order-dependent), point in web mercator EWKB") {
const osmium::Location loc{3.2, 4.2};
const osmium::geom::WKBFactory<osmium::geom::MercatorProjection> factory{osmium::geom::wkb_type::ewkb, osmium::geom::out_type::hex};
const std::string wkb{factory.create_point(loc)};
REQUIRE(wkb.substr(0, 10) == "0101000020"); // little endian, point type (extended)
REQUIRE(wkb.substr(10, 8) == "110F0000"); // SRID 3857
REQUIRE(wkb.substr(18 + 2, 16 - 2) == "706E7BF9BD1541"); // x coordinate (without first (least significant) byte)
REQUIRE(wkb.substr(34 + 2, 16 - 2) == "A90093E48F1C41"); // y coordinate (without first (least significant) byte)
}
#endif
TEST_CASE("WKB geometry factory (byte-order-dependent): linestring") {
osmium::memory::Buffer buffer{10000};
osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
const auto& wnl = create_test_wnl_okay(buffer);
{
const std::string wkb{factory.create_linestring(wnl)};
REQUIRE(wkb == "0102000000030000009A99999999990940CDCCCCCCCCCC10400000000000000C40CDCCCCCCCCCC1240CDCCCCCCCCCC0C409A99999999991340");
}
{
const std::string wkb{factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(wkb == "010200000003000000CDCCCCCCCCCC0C409A999999999913400000000000000C40CDCCCCCCCCCC12409A99999999990940CDCCCCCCCCCC1040");
}
{
const std::string wkb{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(wkb == "0102000000040000009A99999999990940CDCCCCCCCCCC10400000000000000C40CDCCCCCCCCCC12400000000000000C40CDCCCCCCCCCC1240CDCCCCCCCCCC0C409A99999999991340");
}
{
const std::string wkb{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(wkb == "010200000004000000CDCCCCCCCCCC0C409A999999999913400000000000000C40CDCCCCCCCCCC12400000000000000C40CDCCCCCCCCCC12409A99999999990940CDCCCCCCCCCC1040");
}
}
TEST_CASE("WKB geometry factory (byte-order-dependent): linestring as ewkb") {
osmium::memory::Buffer buffer{10000};
osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::ewkb, osmium::geom::out_type::hex};
const auto& wnl = create_test_wnl_okay(buffer);
const std::string ewkb{factory.create_linestring(wnl)};
REQUIRE(ewkb == "0102000020E6100000030000009A99999999990940CDCCCCCCCCCC10400000000000000C40CDCCCCCCCCCC1240CDCCCCCCCCCC0C409A99999999991340");
}
TEST_CASE("WKB geometry factory (byte-order-dependent): linestring with two same locations") {
osmium::memory::Buffer buffer{10000};
osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
const auto& wnl = create_test_wnl_same_location(buffer);
SECTION("unique forwards (default)") {
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::geometry_error);
}
SECTION("unique backwards") {
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward), osmium::geometry_error);
}
SECTION("all forwards") {
const std::string wkb{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(wkb == "0102000000020000000000000000000C40CDCCCCCCCCCC12400000000000000C40CDCCCCCCCCCC1240");
}
SECTION("all backwards") {
const std::string wkb{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(wkb == "0102000000020000000000000000000C40CDCCCCCCCCCC12400000000000000C40CDCCCCCCCCCC1240");
}
}
TEST_CASE("WKB geometry factory (byte-order-dependent): linestring with undefined location") {
osmium::memory::Buffer buffer{10000};
osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
const auto& wnl = create_test_wnl_undefined_location(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::invalid_location);
}
TEST_CASE("WKB geometry factory (byte-order-dependent): polygon") {
osmium::memory::Buffer buffer{10000};
osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
const auto& wnl = create_test_wnl_closed(buffer);
const std::string wkb{factory.create_polygon(wnl)};
REQUIRE(wkb == "010300000001000000050000000000000000000840000000000000084066666666666610406666666666661040CDCCCCCCCCCC0C406666666666661040CDCCCCCCCCCC08400000000000000C4000000000000008400000000000000840");
}
#endif
TEST_CASE("WKB geometry (byte-order-independent) of empty point") {
const osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
REQUIRE_THROWS_AS(factory.create_point(osmium::Location{}), osmium::invalid_location);
}
TEST_CASE("WKB geometry (byte-order-independent) of empty linestring") {
osmium::geom::WKBFactory<> factory{osmium::geom::wkb_type::wkb, osmium::geom::out_type::hex};
osmium::memory::Buffer buffer{10000};
const auto& wnl = create_test_wnl_empty(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::all), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward), osmium::geometry_error);
}
+155
View File
@@ -0,0 +1,155 @@
#include "catch.hpp"
#include "area_helper.hpp"
#include "wnl_helper.hpp"
#include <osmium/geom/mercator_projection.hpp>
#include <osmium/geom/wkt.hpp>
#include <string>
TEST_CASE("WKT geometry for point") {
const osmium::geom::WKTFactory<> factory;
const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(wkt == "POINT(3.2 4.2)");
}
TEST_CASE("WKT geometry for empty point") {
const osmium::geom::WKTFactory<> factory;
REQUIRE_THROWS_AS(factory.create_point(osmium::Location()), osmium::invalid_location);
}
TEST_CASE("WKT geometry for point in ekwt") {
const osmium::geom::WKTFactory<> factory{7, osmium::geom::wkt_type::ewkt};
const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(wkt == "SRID=4326;POINT(3.2 4.2)");
}
TEST_CASE("WKT geometry for point in ekwt in web mercator") {
const osmium::geom::WKTFactory<osmium::geom::MercatorProjection> factory{2, osmium::geom::wkt_type::ewkt};
const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})};
REQUIRE(wkt == "SRID=3857;POINT(356222.37 467961.14)");
}
TEST_CASE("WKT geometry factory") {
osmium::geom::WKTFactory<> factory;
osmium::memory::Buffer buffer{10000};
SECTION("linestring") {
const auto& wnl = create_test_wnl_okay(buffer);
SECTION("unique forwards (default)") {
const std::string wkt{factory.create_linestring(wnl)};
REQUIRE(wkt == "LINESTRING(3.2 4.2,3.5 4.7,3.6 4.9)");
}
SECTION("unique backwards") {
const std::string wkt{factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(wkt == "LINESTRING(3.6 4.9,3.5 4.7,3.2 4.2)");
}
SECTION("all forwards") {
const std::string wkt{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(wkt == "LINESTRING(3.2 4.2,3.5 4.7,3.5 4.7,3.6 4.9)");
}
SECTION("all backwards") {
const std::string wkt{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(wkt == "LINESTRING(3.6 4.9,3.5 4.7,3.5 4.7,3.2 4.2)");
}
}
SECTION("polygon") {
const auto& wnl = create_test_wnl_closed(buffer);
SECTION("unique forwards (default)") {
const std::string wkt{factory.create_polygon(wnl)};
REQUIRE(wkt == "POLYGON((3 3,4.1 4.1,3.6 4.1,3.1 3.5,3 3))");
}
SECTION("unique backwards") {
const std::string wkt{factory.create_polygon(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward)};
REQUIRE(wkt == "POLYGON((3 3,3.1 3.5,3.6 4.1,4.1 4.1,3 3))");
}
SECTION("all forwards") {
const std::string wkt{factory.create_polygon(wnl, osmium::geom::use_nodes::all)};
REQUIRE(wkt == "POLYGON((3 3,4.1 4.1,4.1 4.1,3.6 4.1,3.1 3.5,3 3))");
}
SECTION("all backwards") {
const std::string wkt{factory.create_polygon(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(wkt == "POLYGON((3 3,3.1 3.5,3.6 4.1,4.1 4.1,4.1 4.1,3 3))");
}
}
SECTION("empty linestring") {
const auto& wnl = create_test_wnl_empty(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::all), osmium::geometry_error);
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward), osmium::geometry_error);
}
SECTION("linestring with two same locations") {
const auto& wnl = create_test_wnl_same_location(buffer);
SECTION("unique forwards") {
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::geometry_error);
try {
factory.create_linestring(wnl);
} catch (const osmium::geometry_error& e) {
REQUIRE(e.id() == 0);
REQUIRE(std::string(e.what()) == "need at least two points for linestring");
}
}
SECTION("unique backwards") {
REQUIRE_THROWS_AS(factory.create_linestring(wnl, osmium::geom::use_nodes::unique, osmium::geom::direction::backward), osmium::geometry_error);
}
SECTION("all forwards") {
const std::string wkt{factory.create_linestring(wnl, osmium::geom::use_nodes::all)};
REQUIRE(wkt == "LINESTRING(3.5 4.7,3.5 4.7)");
}
SECTION("all backwards") {
const std::string wkt{factory.create_linestring(wnl, osmium::geom::use_nodes::all, osmium::geom::direction::backward)};
REQUIRE(wkt == "LINESTRING(3.5 4.7,3.5 4.7)");
}
}
SECTION("linestring with undefined location") {
const auto& wnl = create_test_wnl_undefined_location(buffer);
REQUIRE_THROWS_AS(factory.create_linestring(wnl), osmium::invalid_location);
}
SECTION("area with one outer and no inner rings") {
const osmium::Area& area = create_test_area_1outer_0inner(buffer);
const std::string wkt{factory.create_multipolygon(area)};
REQUIRE(wkt == "MULTIPOLYGON(((3.2 4.2,3.5 4.7,3.6 4.9,3.2 4.2)))");
}
SECTION("area with one outer and one inner ring") {
const osmium::Area& area = create_test_area_1outer_1inner(buffer);
const std::string wkt{factory.create_multipolygon(area)};
REQUIRE(wkt == "MULTIPOLYGON(((0.1 0.1,9.1 0.1,9.1 9.1,0.1 9.1,0.1 0.1),(1 1,8 1,8 8,1 8,1 1)))");
}
SECTION("area with two outer and two inner rings") {
const osmium::Area& area = create_test_area_2outer_2inner(buffer);
const std::string wkt{factory.create_multipolygon(area)};
REQUIRE(wkt == "MULTIPOLYGON(((0.1 0.1,9.1 0.1,9.1 9.1,0.1 9.1,0.1 0.1),(1 1,4 1,4 4,1 4,1 1),(5 5,5 7,7 7,5 5)),((10 10,11 10,11 11,10 11,10 10)))");
}
}
+57
View File
@@ -0,0 +1,57 @@
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/node_ref_list.hpp>
using namespace osmium::builder::attr;
inline const osmium::WayNodeList& create_test_wnl_okay(osmium::memory::Buffer& buffer) {
const auto pos = osmium::builder::add_way_node_list(buffer, _nodes({
{1, {3.2, 4.2}},
{3, {3.5, 4.7}},
{4, {3.5, 4.7}},
{2, {3.6, 4.9}}
}));
return buffer.get<osmium::WayNodeList>(pos);
}
inline const osmium::WayNodeList& create_test_wnl_closed(osmium::memory::Buffer& buffer) {
const auto pos = osmium::builder::add_way_node_list(buffer, _nodes({
{1, {3.0, 3.0}},
{2, {4.1, 4.1}},
{3, {4.1, 4.1}},
{4, {3.6, 4.1}},
{5, {3.1, 3.5}},
{6, {3.0, 3.0}},
}));
return buffer.get<osmium::WayNodeList>(pos);
}
inline const osmium::WayNodeList& create_test_wnl_empty(osmium::memory::Buffer& buffer) {
{
osmium::builder::WayNodeListBuilder wnl_builder(buffer);
}
return buffer.get<osmium::WayNodeList>(buffer.commit());
}
inline const osmium::WayNodeList& create_test_wnl_same_location(osmium::memory::Buffer& buffer) {
const auto pos = osmium::builder::add_way_node_list(buffer, _nodes({
{1, {3.5, 4.7}},
{2, {3.5, 4.7}}
}));
return buffer.get<osmium::WayNodeList>(pos);
}
inline const osmium::WayNodeList& create_test_wnl_undefined_location(osmium::memory::Buffer& buffer) {
const auto pos = osmium::builder::add_way_node_list(buffer, _nodes({
{1, {3.5, 4.7}},
{2, osmium::Location()}
}));
return buffer.get<osmium::WayNodeList>(pos);
}
+129
View File
@@ -0,0 +1,129 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/handler.hpp>
#include <osmium/handler/node_locations_for_ways.hpp>
#include <osmium/index/map/flex_mem.hpp>
#include <osmium/io/xml_input.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/visitor.hpp>
TEST_CASE("apply with lambdas on reader") {
const osmium::io::File file{with_data_dir("t/relations/data.osm")};
osmium::io::Reader reader{file};
int count_n = 0;
int count_w = 0;
int count_r = 0;
int count_o = 0;
int count_a = 0;
osmium::apply(reader,
[&](const osmium::Node& node) {
count_n += node.version();
},
[&](const osmium::Way& way) {
if (way.id() == 20) {
++count_w;
}
},
[&](const osmium::Relation& relation) {
if (relation.id() > 30) {
++count_r;
}
},
[&](const osmium::OSMObject& object) {
if (object.id() % 10 == 0) {
++count_o;
}
},
[&](const osmium::Way& way) {
if (way.id() == 21) {
++count_w;
}
},
[&](const osmium::Area& /*area*/) {
++count_a;
}
);
REQUIRE(count_n == 5);
REQUIRE(count_w == 2);
REQUIRE(count_r == 2);
REQUIRE(count_o == 3);
REQUIRE(count_a == 0);
}
TEST_CASE("apply with lambda on buffer") {
const osmium::io::File file{with_data_dir("t/relations/data.osm")};
osmium::io::Reader reader{file};
const auto buffer = reader.read();
reader.close();
std::size_t members = 0;
osmium::apply(buffer, [&](const osmium::Relation& relation) {
members += relation.members().size();
});
REQUIRE(members == 5);
}
TEST_CASE("apply on non-const buffer can change data") {
const osmium::io::File file{with_data_dir("t/relations/data.osm")};
osmium::io::Reader reader{file};
auto buffer = reader.read();
reader.close();
int nodes = 0;
osmium::apply(buffer,
[&](osmium::Node& node) {
node.set_version(123);
},
[&](const osmium::Node& node) {
++nodes;
REQUIRE(node.version() == 123);
}
);
REQUIRE(nodes == 5);
}
TEST_CASE("apply with handler and lambda") {
using index_type = osmium::index::map::FlexMem<osmium::unsigned_object_id_type, osmium::Location>;
using location_handler_type = osmium::handler::NodeLocationsForWays<index_type>;
const osmium::io::File file{with_data_dir("t/relations/data.osm")};
osmium::io::Reader reader{file};
auto buffer = reader.read();
reader.close();
index_type index;
location_handler_type location_handler{index};
int64_t x = 0;
int64_t y = 0;
osmium::apply(buffer,
[&](const osmium::Way& way) {
REQUIRE_FALSE(way.nodes().front().location().valid());
},
location_handler,
[&](const osmium::Way& way) {
REQUIRE(way.nodes().front().location().valid());
for (const auto& wn : way.nodes()) {
x += wn.location().x();
y += wn.location().y();
}
}
);
REQUIRE(x == 44000000);
REQUIRE(y == 40000000);
}
@@ -0,0 +1,128 @@
#include "catch.hpp"
#include <osmium/handler/check_order.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/opl.hpp>
#include <osmium/visitor.hpp>
TEST_CASE("CheckOrder handler if everything is in order") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("n-126", buffer));
REQUIRE(osmium::opl_parse("n123", buffer));
REQUIRE(osmium::opl_parse("n124", buffer));
REQUIRE(osmium::opl_parse("n128", buffer));
REQUIRE(osmium::opl_parse("w-100", buffer));
REQUIRE(osmium::opl_parse("w100", buffer));
REQUIRE(osmium::opl_parse("w102", buffer));
REQUIRE(osmium::opl_parse("r-200", buffer));
REQUIRE(osmium::opl_parse("r100", buffer));
osmium::handler::CheckOrder handler;
osmium::apply(buffer, handler);
REQUIRE(handler.max_node_id() == 128);
REQUIRE(handler.max_way_id() == 102);
REQUIRE(handler.max_relation_id() == 100);
}
TEST_CASE("CheckOrder handler: Nodes must be in order") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("n3", buffer));
SECTION("Positive ID") {
REQUIRE(osmium::opl_parse("n2", buffer));
}
SECTION("Negative ID") {
REQUIRE(osmium::opl_parse("n-2", buffer));
}
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
TEST_CASE("CheckOrder handler: Ways must be in order") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("w3", buffer));
SECTION("Positive ID") {
REQUIRE(osmium::opl_parse("w2", buffer));
}
SECTION("Negative ID") {
REQUIRE(osmium::opl_parse("w-2", buffer));
}
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
TEST_CASE("CheckOrder handler: Relations must be in order") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("r3", buffer));
SECTION("Positive ID") {
REQUIRE(osmium::opl_parse("r2", buffer));
}
SECTION("Negative ID") {
REQUIRE(osmium::opl_parse("r-2", buffer));
}
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
TEST_CASE("CheckOrder handler: Same id twice is not allowed") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("n3", buffer));
REQUIRE(osmium::opl_parse("n3", buffer));
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
TEST_CASE("CheckOrder handler: Nodes after ways") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("w50", buffer));
SECTION("Positive ID") {
REQUIRE(osmium::opl_parse("n30", buffer));
}
SECTION("Negative ID") {
REQUIRE(osmium::opl_parse("n-30", buffer));
}
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
TEST_CASE("CheckOrder handler: Nodes after relations") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("r50", buffer));
SECTION("Positive ID") {
REQUIRE(osmium::opl_parse("n30", buffer));
}
SECTION("Negative ID") {
REQUIRE(osmium::opl_parse("n-30", buffer));
}
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
TEST_CASE("CheckOrder handler: Ways after relations") {
osmium::memory::Buffer buffer{1024};
REQUIRE(osmium::opl_parse("r50", buffer));
SECTION("Positive ID") {
REQUIRE(osmium::opl_parse("w30", buffer));
}
SECTION("Negative ID") {
REQUIRE(osmium::opl_parse("w-30", buffer));
}
osmium::handler::CheckOrder handler;
REQUIRE_THROWS_AS(osmium::apply(buffer, handler), osmium::out_of_order_error);
}
@@ -0,0 +1,114 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/dynamic_handler.hpp>
#include <osmium/visitor.hpp>
struct Handler1 : public osmium::handler::Handler {
int& count;
explicit Handler1(int& c) :
count(c) {
}
void node(const osmium::Node& /*node*/) noexcept {
++count;
}
void way(const osmium::Way& /*way*/) noexcept {
++count;
}
void relation(const osmium::Relation& /*relation*/) noexcept {
++count;
}
void area(const osmium::Area& /*area*/) noexcept {
++count;
}
void changeset(const osmium::Changeset& /*changeset*/) noexcept {
++count;
}
void flush() noexcept {
++count;
}
};
struct Handler2 : public osmium::handler::Handler {
int& count;
explicit Handler2(int& c) :
count(c) {
}
void node(const osmium::Node& /*node*/) noexcept {
count += 2;
}
void way(const osmium::Way& /*way*/) noexcept {
count += 2;
}
void relation(const osmium::Relation& /*relation*/) noexcept {
count += 2;
}
void area(const osmium::Area& /*area*/) noexcept {
count += 2;
}
void changeset(const osmium::Changeset& /*changeset*/) noexcept {
count += 2;
}
};
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_node(buffer, _id(1));
osmium::builder::add_way(buffer, _id(2));
osmium::builder::add_relation(buffer, _id(3));
osmium::builder::add_area(buffer, _id(4));
osmium::builder::add_changeset(buffer, _cid(5));
return buffer;
}
TEST_CASE("Base test: static handler") {
const auto buffer = fill_buffer();
int count = 0;
Handler1 h1{count};
osmium::apply(buffer, h1);
REQUIRE(count == 6);
count = 0;
Handler2 h2{count};
osmium::apply(buffer, h2);
REQUIRE(count == 10);
}
TEST_CASE("Dynamic handler") {
const auto buffer = fill_buffer();
osmium::handler::DynamicHandler handler;
osmium::apply(buffer, handler);
int count = 0;
handler.set<Handler1>(count);
osmium::apply(buffer, handler);
REQUIRE(count == 6);
count = 0;
handler.set<Handler2>(count);
osmium::apply(buffer, handler);
REQUIRE(count == 10);
}
@@ -0,0 +1,84 @@
#include "catch.hpp"
#include <osmium/index/detail/tmpfile.hpp>
#include <osmium/index/map/dense_file_array.hpp>
#include <osmium/index/map/dense_mem_array.hpp>
#include <osmium/index/map/dense_mmap_array.hpp>
#include <osmium/index/map/sparse_file_array.hpp>
#include <osmium/index/map/sparse_mem_array.hpp>
#include <osmium/index/map/sparse_mmap_array.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/osm/types.hpp>
using dense_file_array = osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
using sparse_file_array = osmium::index::map::SparseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
template <class TMemoryIndex, class TFileIndex>
void test_index(std::function<void(TMemoryIndex&, const int)> dump_method) {
const int fd = osmium::detail::create_tmp_file();
REQUIRE(osmium::file_size(fd) == 0);
const osmium::unsigned_object_id_type id1 = 12;
const osmium::unsigned_object_id_type id2 = 3;
const osmium::unsigned_object_id_type id3 = 7;
const osmium::Location loc1{1.2, 4.5};
const osmium::Location loc2{3.5, -7.2};
const osmium::Location loc3{-12.7, 14.5};
TMemoryIndex index;
index.set(id1, loc1);
index.set(id2, loc2);
index.set(id3, loc3);
// implementation of TMemoryIndex::sort should be empty if it is a dense index
index.sort();
dump_method(index, fd);
REQUIRE(osmium::file_size(fd) >= (3 * sizeof(typename TFileIndex::element_type)));
// load index from file
TFileIndex file_index{fd};
// test retrievals
REQUIRE(loc1 == file_index.get(id1));
REQUIRE(loc2 == file_index.get(id2));
REQUIRE(loc3 == file_index.get(id3));
REQUIRE_THROWS_AS(file_index.get(5), osmium::not_found);
REQUIRE_THROWS_AS(file_index.get(6), osmium::not_found);
REQUIRE_THROWS_AS(file_index.get(200), osmium::not_found);
}
#ifdef __linux__
using dense_mmap_array = osmium::index::map::DenseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
TEST_CASE("Dump DenseMmapArray, load as DenseFileArray") {
auto dump_method = [](dense_mmap_array& index, const int fd) { index.dump_as_array(fd);};
test_index<dense_mmap_array, dense_file_array>(dump_method);
}
#else
# pragma message("not running 'DenseMmapArray' test case on this machine")
#endif
using dense_mem_array = osmium::index::map::DenseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
TEST_CASE("Dump DenseMemArray, load as DenseFileArray") {
auto dump_method = [](dense_mem_array& index, const int fd) { index.dump_as_array(fd);};
test_index<dense_mem_array, dense_file_array>(dump_method);
}
#ifdef __linux__
using sparse_mmap_array = osmium::index::map::SparseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
TEST_CASE("Dump SparseMmapArray, load as SparseFileArray") {
auto dump_method = [](sparse_mmap_array& index, const int fd) { index.dump_as_list(fd);};
test_index<sparse_mmap_array, sparse_file_array>(dump_method);
}
#else
# pragma message("not running 'SparseMmapArray' test case on this machine")
#endif
using sparse_mem_array = osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
TEST_CASE("Dump SparseMemArray, load as SparseFileArray") {
auto dump_method = [](sparse_mem_array& index, const int fd) { index.dump_as_list(fd);};
test_index<sparse_mem_array, sparse_file_array>(dump_method);
}
@@ -0,0 +1,85 @@
#include "catch.hpp"
#include <osmium/index/detail/tmpfile.hpp>
#include <osmium/index/map/dense_file_array.hpp>
#include <osmium/index/map/sparse_mem_array.hpp>
#include <osmium/index/map/sparse_mmap_array.hpp>
#include <osmium/index/node_locations_map.hpp>
#include <osmium/osm/node_ref.hpp>
#include <osmium/osm/types.hpp>
#include <osmium/util/file.hpp>
#include <vector>
using dense_file_array = osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
template <class TSparseIndex>
void test_index() {
const int fd = osmium::detail::create_tmp_file();
constexpr const size_t value_size = sizeof(typename TSparseIndex::element_type::second_type);
constexpr const size_t buffer_size = (10L * 1024L * 1024L) / value_size;
REQUIRE(osmium::file_size(fd) == 0);
std::vector<osmium::NodeRef> refs = {
osmium::NodeRef{1, osmium::Location{1.2, 4.5}},
osmium::NodeRef{6, osmium::Location{3.5, -7.2}},
osmium::NodeRef{2 * buffer_size, osmium::Location{10.2, 64.5}},
osmium::NodeRef{2 * buffer_size + 1, osmium::Location{39.5, -71.2}},
osmium::NodeRef{3 * buffer_size - 1, osmium::Location{-1.2, 54.6}},
osmium::NodeRef{3 * buffer_size, osmium::Location{-171.2, 9.3}},
osmium::NodeRef{3 * buffer_size + 1, osmium::Location{-171.21, 9.26}},
osmium::NodeRef{3 * buffer_size + 2, osmium::Location{-171.22, 9.25}},
osmium::NodeRef{3 * buffer_size + 3, osmium::Location{-171.24, 9.23}},
osmium::NodeRef{3 * buffer_size + 4, osmium::Location{-171.25, 9.22}},
osmium::NodeRef{3 * buffer_size + 5, osmium::Location{-171.26, 9.21}}
};
TSparseIndex sparse_index;
for (const auto& r : refs) {
sparse_index.set(r.ref(), r.location());
}
sparse_index.sort();
sparse_index.dump_as_array(fd);
const dense_file_array dense_index{fd};
const auto max_id_in_refs = std::max_element(refs.begin(), refs.end())->ref();
// Array index should be as large as necessary.
REQUIRE(osmium::file_size(fd) >= max_id_in_refs * sizeof(osmium::Location));
// check beyond largest ID
REQUIRE_THROWS_AS(dense_index.get(max_id_in_refs + 1), osmium::not_found);
// check if written values can be retrieved
for (const auto& r : refs) {
REQUIRE(dense_index.get(r.ref()) == r.location());
}
// check if all other values are invalid locations
size_t invalid_count = 0;
for (osmium::object_id_type id = 0; id <= max_id_in_refs; ++id) {
if (!dense_index.get_noexcept(id).valid()) {
++invalid_count;
}
}
REQUIRE(invalid_count == max_id_in_refs - refs.size() + 1);
}
#ifdef __linux__
using sparse_mmap_array = osmium::index::map::SparseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
TEST_CASE("Dump SparseMmapArray as array and load it as DenseFileArray") {
test_index<sparse_mmap_array>();
}
#else
# pragma message("not running 'SparseMmapArray' test case on this machine")
#endif
using sparse_mem_array = osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
TEST_CASE("Dump SparseMemArray as array and load it as DenseFileArray") {
test_index<sparse_mem_array>();
}
@@ -0,0 +1,160 @@
#include "catch.hpp"
#include <osmium/index/detail/tmpfile.hpp>
#include <osmium/index/map/dense_file_array.hpp>
#include <osmium/index/map/sparse_file_array.hpp>
#include <osmium/index/node_locations_map.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/osm/types.hpp>
#include <osmium/util/file.hpp>
#include <iterator>
TEST_CASE("File based dense index") {
const int fd = osmium::detail::create_tmp_file();
REQUIRE(osmium::file_size(fd) == 0);
const osmium::unsigned_object_id_type id1 = 6;
const osmium::unsigned_object_id_type id2 = 3;
const osmium::Location loc1{1.2, 4.5};
const osmium::Location loc2{3.5, -7.2};
using index_type = osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
constexpr const size_t S = sizeof(index_type::element_type);
{
index_type index{fd};
REQUIRE(index.size() == 0);
REQUIRE_THROWS_AS(index.get( 0), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 1), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 3), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 5), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 6), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 7), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
index.set(id1, loc1);
REQUIRE(index.size() == 7);
index.set(id2, loc2);
REQUIRE(index.size() == 7);
index.sort();
REQUIRE(loc1 == index.get(id1));
REQUIRE(loc2 == index.get(id2));
REQUIRE_THROWS_AS(index.get( 0), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 1), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 5), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 7), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE(index.size() == 7);
REQUIRE(std::distance(index.cbegin(), index.cend()) == 7);
REQUIRE(osmium::file_size(fd) >= (6 * S));
}
{
const index_type index{fd};
REQUIRE(osmium::file_size(fd) >= (6 * S));
REQUIRE(index.size() == 7);
REQUIRE(loc1 == index.get(id1));
REQUIRE(loc2 == index.get(id2));
REQUIRE_THROWS_AS(index.get( 0), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 1), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 5), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 7), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE(index.size() == 7);
REQUIRE(std::distance(index.cbegin(), index.cend()) == 7);
const auto* it = index.cbegin();
REQUIRE(*it++ == osmium::Location{});
REQUIRE(*it++ == osmium::Location{});
REQUIRE(*it++ == osmium::Location{});
REQUIRE(*it++ == loc2);
REQUIRE(*it++ == osmium::Location{});
REQUIRE(*it++ == osmium::Location{});
REQUIRE(*it++ == loc1);
REQUIRE(it++ == index.cend());
}
}
TEST_CASE("File based sparse index") {
const int fd = osmium::detail::create_tmp_file();
REQUIRE(osmium::file_size(fd) == 0);
const osmium::unsigned_object_id_type id1 = 6;
const osmium::unsigned_object_id_type id2 = 3;
const osmium::Location loc1{1.2, 4.5};
const osmium::Location loc2{3.5, -7.2};
using index_type = osmium::index::map::SparseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
constexpr const size_t S = sizeof(index_type::element_type);
{
index_type index{fd};
REQUIRE(index.size() == 0);
REQUIRE_THROWS_AS(index.get( 0), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 1), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 3), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 5), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 6), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 7), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
index.set(id1, loc1);
REQUIRE(index.size() == 1);
index.set(id2, loc2);
REQUIRE(index.size() == 2);
index.sort();
REQUIRE(loc1 == index.get(id1));
REQUIRE(loc2 == index.get(id2));
REQUIRE_THROWS_AS(index.get( 0), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 1), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 5), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 7), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE(index.size() == 2);
REQUIRE(std::distance(index.cbegin(), index.cend()) == 2);
REQUIRE(osmium::file_size(fd) >= (2 * S));
}
{
const index_type index{fd};
REQUIRE(osmium::file_size(fd) >= (2 * S));
REQUIRE(index.size() == 2);
REQUIRE(loc1 == index.get(id1));
REQUIRE(loc2 == index.get(id2));
REQUIRE_THROWS_AS(index.get( 0), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 1), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 5), osmium::not_found);
REQUIRE_THROWS_AS(index.get( 7), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE(index.size() == 2);
REQUIRE(std::distance(index.cbegin(), index.cend()) == 2);
}
}
+254
View File
@@ -0,0 +1,254 @@
#include "catch.hpp"
#include <osmium/index/id_set.hpp>
#include <osmium/osm/types.hpp>
TEST_CASE("Basic functionality of IdSetDense") {
osmium::index::IdSetDense<osmium::unsigned_object_id_type> s;
REQUIRE_FALSE(s.get(17));
REQUIRE_FALSE(s.get(28));
REQUIRE(s.empty());
REQUIRE(s.size() == 0); // NOLINT(readability-container-size-empty)
s.set(17);
REQUIRE(s.get(17));
REQUIRE_FALSE(s.get(28));
REQUIRE_FALSE(s.empty());
REQUIRE(s.size() == 1);
s.set(28);
REQUIRE(s.get(17));
REQUIRE(s.get(28));
REQUIRE_FALSE(s.empty());
REQUIRE(s.size() == 2);
s.set(17);
REQUIRE(s.get(17));
REQUIRE(s.size() == 2);
REQUIRE_FALSE(s.check_and_set(17));
REQUIRE(s.get(17));
REQUIRE(s.size() == 2);
s.unset(17);
REQUIRE_FALSE(s.get(17));
REQUIRE(s.size() == 1);
REQUIRE(s.check_and_set(32));
REQUIRE(s.get(32));
REQUIRE(s.size() == 2);
s.clear();
REQUIRE(s.empty());
}
TEST_CASE("Copying IdSetDense") {
osmium::index::IdSetDense<osmium::unsigned_object_id_type> s1;
osmium::index::IdSetDense<osmium::unsigned_object_id_type> s2;
REQUIRE(s1.empty());
REQUIRE(s2.empty());
s1.set(17);
s1.set(28);
REQUIRE(s1.get(17));
REQUIRE(s1.get(17));
REQUIRE(s1.size() == 2);
s2 = s1;
REQUIRE(s1.get(17));
REQUIRE(s1.get(28));
REQUIRE(s1.size() == 2);
REQUIRE(s2.get(17));
REQUIRE(s2.get(28));
REQUIRE(s2.size() == 2);
const osmium::index::IdSetDense<osmium::unsigned_object_id_type> s3{s1};
REQUIRE(s3.get(17));
REQUIRE(s3.get(28));
REQUIRE(s3.size() == 2);
}
TEST_CASE("Iterating over IdSetDense") {
osmium::index::IdSetDense<osmium::unsigned_object_id_type> s;
s.set(7);
s.set(35);
s.set(35);
s.set(20);
s.set(1ULL << 33U);
s.set(21);
s.set((1ULL << 27U) + 13U);
REQUIRE(s.size() == 6);
auto it = s.begin();
REQUIRE(it != s.end());
REQUIRE(*it == 7);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 20);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 21);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 35);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == (1ULL << 27U) + 13U);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 1ULL << 33U);
++it;
REQUIRE(it == s.end());
}
TEST_CASE("Test with larger Ids") {
osmium::index::IdSetDense<osmium::unsigned_object_id_type> s;
const osmium::unsigned_object_id_type start = 25;
const osmium::unsigned_object_id_type end = 100000000;
const osmium::unsigned_object_id_type step = 123456;
for (osmium::unsigned_object_id_type i = start; i < end; i += step) {
s.set(i);
}
for (osmium::unsigned_object_id_type i = start; i < end; i += step) {
REQUIRE(s.get(i));
REQUIRE_FALSE(s.get(i + 1));
}
}
TEST_CASE("Large gap") {
osmium::index::IdSetDense<osmium::unsigned_object_id_type> s;
s.set(3);
s.set(1U << 30U);
REQUIRE(s.get(1U << 30U));
REQUIRE_FALSE(s.get(1U << 29U));
}
TEST_CASE("Basic functionality of IdSetSmall") {
osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s;
REQUIRE_FALSE(s.get(17));
REQUIRE_FALSE(s.get(28));
REQUIRE(s.empty());
s.set(17);
REQUIRE(s.get(17));
REQUIRE_FALSE(s.get(28));
REQUIRE_FALSE(s.empty());
s.set(28);
REQUIRE(s.get(17));
REQUIRE(s.get(28));
REQUIRE_FALSE(s.empty());
const auto size = s.size();
// Setting the same id as last time doesn't grow the size
s.set(28);
REQUIRE(s.get(17));
REQUIRE(s.get(28));
REQUIRE_FALSE(s.empty());
REQUIRE(size == s.size());
s.clear();
REQUIRE(s.empty());
}
TEST_CASE("Copying IdSetSmall") {
osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s1;
osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s2;
REQUIRE(s1.empty());
REQUIRE(s2.empty());
s1.set(17);
s1.set(28);
REQUIRE(s1.get(17));
REQUIRE(s1.get(17));
REQUIRE(s1.size() == 2);
s2 = s1;
REQUIRE(s1.get(17));
REQUIRE(s1.get(28));
REQUIRE(s1.size() == 2);
REQUIRE(s2.get(17));
REQUIRE(s2.get(28));
REQUIRE(s2.size() == 2);
const osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s3{s1};
REQUIRE(s3.get(17));
REQUIRE(s3.get(28));
REQUIRE(s3.size() == 2);
}
TEST_CASE("Iterating over IdSetSmall") {
osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s;
s.set(7);
s.set(35);
s.set(35);
s.set(20);
s.set(1ULL << 33U);
s.set(21);
s.set((1ULL << 27U) + 13U);
// needs to be called before size() and iterator will work properly
s.sort_unique();
REQUIRE(s.size() == 6);
auto it = s.begin();
REQUIRE(it != s.end());
REQUIRE(*it == 7);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 20);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 21);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 35);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == (1ULL << 27U) + 13U);
++it;
REQUIRE(it != s.end());
REQUIRE(*it == 1ULL << 33U);
++it;
REQUIRE(it == s.end());
}
TEST_CASE("Merge two IdSetSmall") {
osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s1;
osmium::index::IdSetSmall<osmium::unsigned_object_id_type> s2;
s1.set(23);
s1.set(2);
s1.set(7);
s1.set(55);
s1.set(42);
s1.set(7);
s2.set(2);
s2.set(32);
s2.set(8);
s2.set(55);
s2.set(1);
s1.sort_unique();
REQUIRE(s1.size() == 5);
s2.sort_unique();
REQUIRE(s2.size() == 5);
s1.merge_sorted(s2);
REQUIRE(s1.size() == 8);
const auto ids = {1, 2, 7, 8, 23, 32, 42, 55};
REQUIRE(std::equal(s1.cbegin(), s1.cend(), ids.begin()));
}
@@ -0,0 +1,259 @@
#include "catch.hpp"
#include <osmium/index/map/dense_file_array.hpp>
#include <osmium/index/map/dense_mem_array.hpp>
#include <osmium/index/map/dense_mmap_array.hpp>
#include <osmium/index/map/dummy.hpp>
#include <osmium/index/map/flex_mem.hpp>
#include <osmium/index/map/sparse_file_array.hpp>
#include <osmium/index/map/sparse_mem_array.hpp>
#include <osmium/index/map/sparse_mem_map.hpp>
#include <osmium/index/map/sparse_mmap_array.hpp>
#include <osmium/index/node_locations_map.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/osm/types.hpp>
#include <memory>
#include <string>
#include <vector>
static_assert(osmium::index::empty_value<osmium::Location>() == osmium::Location{}, "Empty value for location is wrong");
template <typename TIndex>
void test_func_all(TIndex& index) {
const osmium::unsigned_object_id_type id1 = 12;
const osmium::unsigned_object_id_type id2 = 3;
const osmium::Location loc1{1.2, 4.5};
const osmium::Location loc2{3.5, -7.2};
REQUIRE_THROWS_AS(index.get(id1), osmium::not_found);
index.set(id1, loc1);
index.set(id2, loc2);
index.sort();
REQUIRE_THROWS_AS(index.get(0), osmium::not_found);
REQUIRE_THROWS_AS(index.get(1), osmium::not_found);
REQUIRE_THROWS_AS(index.get(5), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE_THROWS_WITH(index.get(0), "id 0 not found");
REQUIRE_THROWS_WITH(index.get(1), "id 1 not found");
REQUIRE(index.get_noexcept(0) == osmium::Location{});
REQUIRE(index.get_noexcept(1) == osmium::Location{});
REQUIRE(index.get_noexcept(5) == osmium::Location{});
REQUIRE(index.get_noexcept(100) == osmium::Location{});
}
template <typename TIndex>
void test_func_real(TIndex& index) {
const osmium::unsigned_object_id_type id1 = 12;
const osmium::unsigned_object_id_type id2 = 3;
const osmium::Location loc1{1.2, 4.5};
const osmium::Location loc2{3.5, -7.2};
index.set(id1, loc1);
index.set(id2, loc2);
index.sort();
REQUIRE(loc1 == index.get(id1));
REQUIRE(loc2 == index.get(id2));
REQUIRE(loc1 == index.get_noexcept(id1));
REQUIRE(loc2 == index.get_noexcept(id2));
REQUIRE_THROWS_AS(index.get(0), osmium::not_found);
REQUIRE_THROWS_AS(index.get(1), osmium::not_found);
REQUIRE_THROWS_AS(index.get(5), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE(index.get_noexcept(0) == osmium::Location{});
REQUIRE(index.get_noexcept(1) == osmium::Location{});
REQUIRE(index.get_noexcept(5) == osmium::Location{});
REQUIRE(index.get_noexcept(100) == osmium::Location{});
index.clear();
REQUIRE_THROWS_AS(index.get(id1), osmium::not_found);
REQUIRE_THROWS_AS(index.get(id2), osmium::not_found);
REQUIRE_THROWS_AS(index.get(0), osmium::not_found);
REQUIRE_THROWS_AS(index.get(1), osmium::not_found);
REQUIRE_THROWS_AS(index.get(5), osmium::not_found);
REQUIRE_THROWS_AS(index.get(100), osmium::not_found);
REQUIRE(index.get_noexcept(id1) == osmium::Location{});
REQUIRE(index.get_noexcept(id2) == osmium::Location{});
REQUIRE(index.get_noexcept(0) == osmium::Location{});
REQUIRE(index.get_noexcept(1) == osmium::Location{});
REQUIRE(index.get_noexcept(5) == osmium::Location{});
REQUIRE(index.get_noexcept(100) == osmium::Location{});
}
TEST_CASE("Map Id to location: Dummy") {
using index_type = osmium::index::map::Dummy<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
REQUIRE(0 == index1.size());
REQUIRE(0 == index1.used_memory());
test_func_all<index_type>(index1);
REQUIRE(0 == index1.size());
REQUIRE(0 == index1.used_memory());
}
TEST_CASE("Map Id to location: DenseMemArray") {
using index_type = osmium::index::map::DenseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
index1.reserve(1000);
test_func_all<index_type>(index1);
index_type index2;
index2.reserve(1000);
test_func_real<index_type>(index2);
}
#ifdef __linux__
TEST_CASE("Map Id to location: DenseMmapArray") {
using index_type = osmium::index::map::DenseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
test_func_all<index_type>(index1);
index_type index2;
test_func_real<index_type>(index2);
}
#else
# pragma message("not running 'DenseMmapArray' test case on this machine")
#endif
TEST_CASE("Map Id to location: DenseFileArray") {
using index_type = osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
test_func_all<index_type>(index1);
index_type index2;
test_func_real<index_type>(index2);
}
TEST_CASE("Map Id to location: SparseMemMap") {
using index_type = osmium::index::map::SparseMemMap<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
test_func_all<index_type>(index1);
index_type index2;
test_func_real<index_type>(index2);
}
TEST_CASE("Map Id to location: SparseMemArray") {
using index_type = osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
REQUIRE(0 == index1.size());
REQUIRE(0 == index1.used_memory());
test_func_all<index_type>(index1);
REQUIRE(2 == index1.size());
index_type index2;
test_func_real<index_type>(index2);
}
#ifdef __linux__
TEST_CASE("Map Id to location: SparseMmapArray") {
using index_type = osmium::index::map::SparseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
test_func_all<index_type>(index1);
index_type index2;
test_func_real<index_type>(index2);
}
#else
# pragma message("not running 'SparseMmapArray' test case on this machine")
#endif
TEST_CASE("Map Id to location: FlexMem sparse") {
using index_type = osmium::index::map::FlexMem<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1;
test_func_all<index_type>(index1);
index_type index2;
test_func_real<index_type>(index2);
}
TEST_CASE("Map Id to location: FlexMem dense") {
using index_type = osmium::index::map::FlexMem<osmium::unsigned_object_id_type, osmium::Location>;
index_type index1{true};
test_func_all<index_type>(index1);
index_type index2{true};
test_func_real<index_type>(index2);
}
TEST_CASE("Map Id to location: FlexMem switch") {
using index_type = osmium::index::map::FlexMem<osmium::unsigned_object_id_type, osmium::Location>;
const osmium::Location loc1{1.1, 1.2};
const osmium::Location loc2{2.2, -9.4};
index_type index;
REQUIRE(index.size() == 0);
index.set(17, loc1);
index.set(99, loc2);
REQUIRE_FALSE(index.is_dense());
REQUIRE(index.size() == 2);
REQUIRE(index.get_noexcept(0) == osmium::Location{});
REQUIRE(index.get_noexcept(1) == osmium::Location{});
REQUIRE(index.get_noexcept(17) == loc1);
REQUIRE(index.get_noexcept(99) == loc2);
REQUIRE(index.get_noexcept(2000000000) == osmium::Location{});
index.switch_to_dense();
REQUIRE(index.is_dense());
REQUIRE(index.size() >= 2);
REQUIRE(index.get_noexcept(0) == osmium::Location{});
REQUIRE(index.get_noexcept(1) == osmium::Location{});
REQUIRE(index.get_noexcept(17) == loc1);
REQUIRE(index.get_noexcept(99) == loc2);
REQUIRE(index.get_noexcept(2000000000) == osmium::Location{});
}
TEST_CASE("Map Id to location: Dynamic map choice") {
using map_type = osmium::index::map::Map<osmium::unsigned_object_id_type, osmium::Location>;
const auto& map_factory = osmium::index::MapFactory<osmium::unsigned_object_id_type, osmium::Location>::instance();
const std::vector<std::string> map_type_names = map_factory.map_types();
REQUIRE(map_type_names.size() >= 6);
REQUIRE_THROWS_AS(map_factory.create_map(""), osmium::map_factory_error);
REQUIRE_THROWS_AS(map_factory.create_map("does not exist"), osmium::map_factory_error);
REQUIRE_THROWS_WITH(map_factory.create_map(""), "Need non-empty map type name");
REQUIRE_THROWS_WITH(map_factory.create_map("does not exist"), "Support for map type 'does not exist' not compiled into this binary");
for (const auto& map_type_name : map_type_names) {
std::unique_ptr<map_type> index1 = map_factory.create_map(map_type_name);
index1->reserve(1000);
test_func_all<map_type>(*index1);
std::unique_ptr<map_type> index2 = map_factory.create_map(map_type_name);
index2->reserve(1000);
test_func_real<map_type>(*index2);
}
}
+21
View File
@@ -0,0 +1,21 @@
#include "catch.hpp"
#include <osmium/index/nwr_array.hpp>
TEST_CASE("nwr_array") {
osmium::nwr_array<int> a;
a(osmium::item_type::node) = 1;
a(osmium::item_type::way) = 2;
a(osmium::item_type::relation) = 3;
REQUIRE(a(osmium::item_type::node) == 1);
REQUIRE(a(osmium::item_type::way) == 2);
REQUIRE(a(osmium::item_type::relation) == 3);
auto it = a.cbegin();
REQUIRE(*it++ == 1);
REQUIRE(*it++ == 2);
REQUIRE(*it++ == 3);
REQUIRE(it == a.cend());
}
@@ -0,0 +1,84 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/object_pointer_collection.hpp>
#include <osmium/osm/object_comparisons.hpp>
#include <osmium/visitor.hpp>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Create ObjectPointerCollection") {
osmium::memory::Buffer buffer{1024, osmium::memory::Buffer::auto_grow::yes};
osmium::builder::add_node(buffer,
_id(3),
_version(3)
);
osmium::builder::add_node(buffer,
_id(1),
_version(2)
);
osmium::builder::add_node(buffer,
_id(1),
_version(4)
);
osmium::ObjectPointerCollection collection;
REQUIRE(collection.empty());
REQUIRE(collection.size() == 0); // NOLINT(readability-container-size-empty)
osmium::apply(buffer, collection);
REQUIRE_FALSE(collection.empty());
REQUIRE(collection.size() == 3);
auto it = collection.cbegin();
REQUIRE(it->id() == 3);
REQUIRE(it->version() == 3);
++it;
REQUIRE(it->id() == 1);
REQUIRE(it->version() == 2);
++it;
REQUIRE(it->id() == 1);
REQUIRE(it->version() == 4);
++it;
REQUIRE(it == collection.cend());
collection.sort(osmium::object_order_type_id_version{});
REQUIRE(collection.size() == 3);
it = collection.cbegin();
REQUIRE(it->id() == 1);
REQUIRE(it->version() == 2);
++it;
REQUIRE(it->id() == 1);
REQUIRE(it->version() == 4);
++it;
REQUIRE(it->id() == 3);
REQUIRE(it->version() == 3);
++it;
REQUIRE(it == collection.cend());
collection.sort(osmium::object_order_type_id_reverse_version{});
it = collection.cbegin();
REQUIRE(it->id() == 1);
REQUIRE(it->version() == 4);
++it;
REQUIRE(it->id() == 1);
REQUIRE(it->version() == 2);
++it;
REQUIRE(it->id() == 3);
REQUIRE(it->version() == 3);
++it;
REQUIRE(it == collection.cend());
collection.clear();
REQUIRE(collection.empty());
}
@@ -0,0 +1,107 @@
#include "catch.hpp"
#include <osmium/index/relations_map.hpp>
#include <type_traits>
static_assert(!std::is_default_constructible<osmium::index::RelationsMapIndex>::value, "RelationsMapIndex should not be default constructible");
static_assert(!std::is_copy_constructible<osmium::index::RelationsMapIndex>::value, "RelationsMapIndex should not be copy constructible");
static_assert(!std::is_copy_constructible<osmium::index::RelationsMapStash>::value, "RelationsMapStash should not be copy constructible");
static_assert(!std::is_copy_assignable<osmium::index::RelationsMapIndex>::value, "RelationsMapIndex should not be copy assignable");
static_assert(!std::is_copy_assignable<osmium::index::RelationsMapStash>::value, "RelationsMapStash should not be copy assignable");
TEST_CASE("RelationsMapStash lvalue") {
osmium::index::RelationsMapStash stash;
REQUIRE(stash.empty());
REQUIRE(stash.size() == 0); // NOLINT(readability-container-size-empty)
stash.add(1, 2);
stash.add(2, 3);
REQUIRE_FALSE(stash.empty());
REQUIRE(stash.size() == 2);
const auto index = stash.build_member_to_parent_index();
REQUIRE_FALSE(index.empty());
REQUIRE(index.size() == 2);
int count = 0;
index.for_each(1, [&](osmium::unsigned_object_id_type id) {
REQUIRE(id == 2);
++count;
});
REQUIRE(count == 1);
}
osmium::index::RelationsMapIndex func() {
osmium::index::RelationsMapStash stash;
stash.add(1, 2);
stash.add(2, 3);
return stash.build_member_to_parent_index();
}
TEST_CASE("RelationsMapStash rvalue") {
const osmium::index::RelationsMapIndex index{func()};
int count = 0;
index.for_each(2, [&](osmium::unsigned_object_id_type id) {
REQUIRE(id == 3);
++count;
});
REQUIRE(count == 1);
}
TEST_CASE("RelationsMapStash reverse index") {
osmium::index::RelationsMapStash stash;
REQUIRE(stash.empty());
stash.add(1, 2);
stash.add(2, 3);
REQUIRE_FALSE(stash.empty());
REQUIRE(stash.size() == 2);
const auto index = stash.build_parent_to_member_index();
REQUIRE_FALSE(index.empty());
REQUIRE(index.size() == 2);
int count = 0;
index.for_each(2, [&](osmium::unsigned_object_id_type id) {
REQUIRE(id == 1);
++count;
});
index.for_each(3, [&](osmium::unsigned_object_id_type id) {
REQUIRE(id == 2);
++count;
});
REQUIRE(count == 2);
}
TEST_CASE("RelationsMapStash both indexes") {
osmium::index::RelationsMapStash stash;
REQUIRE(stash.empty());
stash.add(1, 2);
stash.add(2, 3);
REQUIRE_FALSE(stash.empty());
REQUIRE(stash.size() == 2);
const auto index = stash.build_indexes();
REQUIRE_FALSE(index.empty());
REQUIRE(index.size() == 2);
int count = 0;
index.member_to_parent().for_each(2, [&](osmium::unsigned_object_id_type id) {
REQUIRE(id == 3);
++count;
});
index.parent_to_member().for_each(2, [&](osmium::unsigned_object_id_type id) {
REQUIRE(id == 1);
++count;
});
REQUIRE(count == 2);
}
Binary file not shown.
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
n1 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.02 y1.02
+22
View File
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<way id="20" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="highway" v="primary"/>
<nd ref="10"/>
<nd ref="11"/>
</way>
<relation id="30" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="test"/>
<member type="node" ref="10" role="none"/>
</relation>
<relation id="31" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="restriction"/>
<member type="way" ref="20" role="from"/>
<member type="node" ref="11" role="via"/>
<member type="way" ref="22" role="to"/>
</relation>
<relation id="32" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="test" v="relinrel"/>
<member type="relation" ref="30" role="none"/>
</relation>
</osm>
Binary file not shown.
+4
View File
@@ -0,0 +1,4 @@
w20 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Thighway=primary Nn10,n11
r30 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttype=test Mn10@none
r31 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttype=restriction Mw20@from,n11@via,w22@to
r32 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttest=relinrel Mr30@none
+22
View File
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<node id="10" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.0"/>
<node id="11" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.1"/>
<node id="12" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.2"/>
<node id="13" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.3"/>
<node id="14" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.4"/>
<relation id="30" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="test"/>
<member type="node" ref="10" role="none"/>
</relation>
<relation id="31" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="restriction"/>
<member type="way" ref="20" role="from"/>
<member type="node" ref="11" role="via"/>
<member type="way" ref="22" role="to"/>
</relation>
<relation id="32" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="test" v="relinrel"/>
<member type="relation" ref="30" role="none"/>
</relation>
</osm>
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
n10 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1 y1
n11 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.1 y1
n12 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.2 y1
n13 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.3 y1
n14 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.4 y1
r30 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttype=test Mn10@none
r31 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttype=restriction Mw20@from,n11@via,w22@to
r32 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttest=relinrel Mr30@none
+13
View File
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<node id="10" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.0"/>
<node id="11" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.1"/>
<node id="12" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.2"/>
<node id="13" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.3"/>
<node id="14" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.4"/>
<way id="20" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="highway" v="primary"/>
<nd ref="10"/>
<nd ref="11"/>
</way>
</osm>
Binary file not shown.
+6
View File
@@ -0,0 +1,6 @@
n10 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1 y1
n11 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.1 y1
n12 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.2 y1
n13 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.3 y1
n14 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.4 y1
w20 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Thighway=primary Nn10,n11
+27
View File
@@ -0,0 +1,27 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<node id="10" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.0"/>
<node id="11" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.1"/>
<node id="12" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.2"/>
<node id="13" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.3"/>
<node id="14" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.4"/>
<way id="20" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="highway" v="primary"/>
<nd ref="10"/>
<nd ref="11"/>
</way>
<relation id="30" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="test"/>
<member type="node" ref="10" role="none"/>
</relation>
<relation id="31" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="restriction"/>
<member type="way" ref="20" role="from"/>
<member type="node" ref="11" role="via"/>
<member type="way" ref="22" role="to"/>
</relation>
<relation id="32" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="test" v="relinrel"/>
<member type="relation" ref="30" role="none"/>
</relation>
</osm>
Binary file not shown.
+9
View File
@@ -0,0 +1,9 @@
n10 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1 y1
n11 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.1 y1
n12 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.2 y1
n13 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.3 y1
n14 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.4 y1
w20 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Thighway=primary Nn10,n11
r30 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttype=test Mn10@none
r31 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttype=restriction Mw20@from,n11@via,w22@to
r32 v1 dV c1 t2014-01-01T00:00:00Z i1 utest Ttest=relinrel Mr30@none
+1
View File
@@ -0,0 +1 @@
n1 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.02 y1.02
+1
View File
@@ -0,0 +1 @@
n1 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.02 y1.02
+4
View File
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<node id="1" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lon="1.02" lat="1.02"/>
</osm>
Binary file not shown.
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
TESTDATA
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="osmium/1.7.1">
<node id="2" lat="50" lon="10.01"/>
</osm>
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="osmium/1.2.1">
<node id="1" version="1" timestamp="2015-09-17T11:52:00Z" uid="1" user="user_1" changeset="1" visible="false"/>
<node id="2" version="1" timestamp="2015-09-17T11:52:00Z" uid="1" user="user_1" changeset="1" visible="true" lat="1" lon="1"/>
</osm>
Binary file not shown.
View File
Whitespace-only changes.
+148
View File
@@ -0,0 +1,148 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/bzip2_compression.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <string>
static void read_from_decompressor(int fd) {
osmium::io::Bzip2Decompressor decomp{fd};
decomp.read();
decomp.close();
}
TEST_CASE("Invalid file descriptor of bzip2-compressed file") {
REQUIRE_THROWS(read_from_decompressor(-1));
}
TEST_CASE("Non-open file descriptor of bzip2-compressed file") {
// 12345 is just a random file descriptor that should not be open
REQUIRE_THROWS(read_from_decompressor(12345));
}
TEST_CASE("Empty bzip2-compressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/empty_file");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
osmium::io::Bzip2Decompressor decomp{fd};
REQUIRE_THROWS_AS(decomp.read(), osmium::bzip2_error);
decomp.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Read bzip2-compressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/data_bzip2.txt.bz2");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
size_t size = 0;
std::string all;
{
osmium::io::Bzip2Decompressor decomp{fd};
for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
size += data.size();
all += data;
}
decomp.close();
}
REQUIRE(size >= 9);
all.resize(8);
REQUIRE("TESTDATA" == all);
REQUIRE(count == count_fds());
}
TEST_CASE("Read bzip2-compressed file without explicit close") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/data_bzip2.txt.bz2");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
size_t size = 0;
std::string all;
{
osmium::io::Bzip2Decompressor decomp{fd};
for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
size += data.size();
all += data;
}
}
REQUIRE(size >= 9);
all.resize(8);
REQUIRE("TESTDATA" == all);
REQUIRE(count == count_fds());
}
TEST_CASE("Corrupted bzip2-compressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/corrupt_data_bzip2.txt.bz2");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
osmium::io::Bzip2Decompressor decomp{fd};
REQUIRE_THROWS_AS(decomp.read(), osmium::bzip2_error);
decomp.close();
REQUIRE(count == count_fds());
}
static void write_to_compressor(int fd) {
osmium::io::Bzip2Compressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
comp.close();
}
TEST_CASE("Compressor: Invalid file descriptor for bzip2-compressed file") {
REQUIRE_THROWS(write_to_compressor(-1));
}
TEST_CASE("Compressor: Non-open file descriptor for bzip2-compressed file") {
// 12345 is just a random file descriptor that should not be open
REQUIRE_THROWS(write_to_compressor(12345));
}
TEST_CASE("Write bzip2-compressed file with explicit close") {
const int count = count_fds();
const std::string output_file = "test_gzip_out.txt.bz2";
const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
REQUIRE(fd > 0);
osmium::io::Bzip2Compressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
comp.close();
REQUIRE(count == count_fds());
REQUIRE(osmium::file_size(output_file) > 10);
}
TEST_CASE("Write bzip2-compressed file with implicit close") {
const int count = count_fds();
const std::string output_file = "test_gzip_out.txt.bz2";
const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
REQUIRE(fd > 0);
{
osmium::io::Bzip2Compressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
}
REQUIRE(count == count_fds());
REQUIRE(osmium::file_size(output_file) > 10);
}
@@ -0,0 +1,22 @@
#include "catch.hpp"
#include <osmium/io/compression.hpp>
TEST_CASE("Create compressor using factory") {
const auto& factory = osmium::io::CompressionFactory::instance();
REQUIRE(factory.create_compressor(osmium::io::file_compression::none, -1, osmium::io::fsync::no));
}
TEST_CASE("Create decompressor using factory") {
const auto& factory = osmium::io::CompressionFactory::instance();
REQUIRE(factory.create_decompressor(osmium::io::file_compression::none, nullptr, 0));
}
TEST_CASE("Compression factory fails on undefined compression") {
const auto& factory = osmium::io::CompressionFactory::instance();
REQUIRE_THROWS_AS(factory.create_compressor(osmium::io::file_compression::gzip, -1, osmium::io::fsync::no),
osmium::unsupported_file_format_error);
REQUIRE_THROWS_WITH(factory.create_compressor(osmium::io::file_compression::gzip, -1, osmium::io::fsync::no),
"Support for compression 'gzip' not compiled into this binary");
}
+312
View File
@@ -0,0 +1,312 @@
#include "catch.hpp"
#include <osmium/io/file.hpp>
#include <iterator>
#include <stdexcept>
TEST_CASE("Default file format") {
const osmium::io::File f;
REQUIRE(osmium::io::file_format::unknown == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("File format when empty (stdin/stdout)") {
const osmium::io::File f{""};
REQUIRE(osmium::io::file_format::unknown == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("File format from dash (stdin/stdout)") {
const osmium::io::File f{"-"};
REQUIRE(osmium::io::file_format::unknown == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("File format from dash with osm.bz2") {
const osmium::io::File f{"-", "osm.bz2"};
REQUIRE(f.filename().empty());
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::bzip2 == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'osm'") {
const osmium::io::File f{"test.osm"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'pbf'") {
const osmium::io::File f{"test.pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'osm.pbf'") {
const osmium::io::File f{"test.osm.pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'opl'") {
const osmium::io::File f{"test.opl"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'osm.opl'") {
const osmium::io::File f{"test.osm.opl"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'osm.gz'") {
const osmium::io::File f{"test.osm.gz"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'opl.bz2'") {
const osmium::io::File f{"test.osm.opl.bz2"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::bzip2 == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'osc.gz'") {
const osmium::io::File f{"test.osc.gz"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'opl.gz'") {
const osmium::io::File f{"test.osh.opl.gz"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Detect file format by suffix 'osh.pbf'") {
const osmium::io::File f{"test.osh.pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm'") {
const osmium::io::File f{"test", "osm"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'pbf'") {
const osmium::io::File f{"test", "pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm.pbf'") {
const osmium::io::File f{"test", "osm.pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'opl'") {
const osmium::io::File f{"test", "opl"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm.opl'") {
const osmium::io::File f{"test", "osm.opl"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm.gz'") {
const osmium::io::File f{"test", "osm.gz"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm.opl.bz2'") {
const osmium::io::File f{"test", "osm.opl.bz2"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::bzip2 == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osc.gz'") {
const osmium::io::File f{"test", "osc.gz"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osh.opl.gz'") {
const osmium::io::File f{"test", "osh.opl.gz"};
REQUIRE(osmium::io::file_format::opl == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("File format by suffix 'blackhole'") {
const osmium::io::File f{"test.blackhole"};
REQUIRE(osmium::io::file_format::blackhole == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'blackhole'") {
const osmium::io::File f{"test", "blackhole"};
REQUIRE(osmium::io::file_format::blackhole == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm.blackhole'") {
const osmium::io::File f{"test", "osm.blackhole"};
REQUIRE(osmium::io::file_format::blackhole == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osm.blackhole.bz2'") {
const osmium::io::File f{"test", "osm.blackhole.bz2"};
REQUIRE(osmium::io::file_format::blackhole == f.format());
REQUIRE(osmium::io::file_compression::bzip2 == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osh.blackhole.gz'") {
const osmium::io::File f{"test", "osh.blackhole.gz"};
REQUIRE(osmium::io::file_format::blackhole == f.format());
REQUIRE(osmium::io::file_compression::gzip == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Override file format by suffix 'osh.pbf'") {
const osmium::io::File f{"test", "osh.pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Format option pbf history") {
const osmium::io::File f{"test", "pbf,history=true"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("Format option pbf foo") {
const osmium::io::File f{"test.osm", "pbf,foo=bar"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE("bar" == f.get("foo"));
f.check();
}
TEST_CASE("Format option xml abc something") {
const osmium::io::File f{"test.bla", "xml,abc,some=thing"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE("true" == f.get("abc"));
REQUIRE("thing" == f.get("some"));
REQUIRE(2 == std::distance(f.begin(), f.end()));
f.check();
}
TEST_CASE("Unknown format 'foo.bar'") {
const osmium::io::File f{"test.foo.bar"};
REQUIRE(osmium::io::file_format::unknown == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("Unknown format 'foo'") {
const osmium::io::File f{"test", "foo"};
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("Unknown format 'osm.foo'") {
const osmium::io::File f{"test", "osm.foo"};
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("Unknown format 'bla=foo'") {
const osmium::io::File f{"test", "bla=foo"};
REQUIRE_THROWS_AS(f.check(), std::runtime_error);
}
TEST_CASE("URL without format") {
const osmium::io::File f{"http://www.example.com/api"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("URL without format and filename") {
const osmium::io::File f{"https://planet.osm.org/pbf/planet-latest.osm.pbf"};
REQUIRE(osmium::io::file_format::pbf == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE_FALSE(f.has_multiple_object_versions());
f.check();
}
TEST_CASE("URL with format") {
const osmium::io::File f{"http://www.example.com/api", "osh"};
REQUIRE(osmium::io::file_format::xml == f.format());
REQUIRE(osmium::io::file_compression::none == f.compression());
REQUIRE(f.has_multiple_object_versions());
f.check();
}
+137
View File
@@ -0,0 +1,137 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/detail/read_write.hpp>
#include <osmium/io/gzip_compression.hpp>
#include <string>
TEST_CASE("Invalid file descriptor of gzip-compressed file") {
REQUIRE_THROWS_AS(osmium::io::GzipDecompressor{-1}, osmium::gzip_error);
}
TEST_CASE("Non-open file descriptor of gzip-compressed file") {
// 12345 is just a random file descriptor that should not be open
osmium::io::GzipDecompressor decomp{12345};
REQUIRE_THROWS_AS(decomp.read(), osmium::gzip_error);
}
TEST_CASE("Empty gzip-compressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/empty_file");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
osmium::io::GzipDecompressor decomp{fd};
REQUIRE(decomp.read().empty());
decomp.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Read gzip-compressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/data_gzip.txt.gz");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
size_t size = 0;
std::string all;
{
osmium::io::GzipDecompressor decomp{fd};
for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
size += data.size();
all += data;
}
decomp.close();
}
REQUIRE(size >= 9);
all.resize(8);
REQUIRE("TESTDATA" == all);
REQUIRE(count == count_fds());
}
TEST_CASE("Read gzip-compressed file without explicit close") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/data_gzip.txt.gz");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
size_t size = 0;
std::string all;
{
osmium::io::GzipDecompressor decomp{fd};
for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
size += data.size();
all += data;
}
}
REQUIRE(size >= 9);
all.resize(8);
REQUIRE("TESTDATA" == all);
REQUIRE(count == count_fds());
}
TEST_CASE("Corrupted gzip-compressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/corrupt_data_gzip.txt.gz");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
osmium::io::GzipDecompressor decomp{fd};
decomp.read();
REQUIRE_THROWS_AS(decomp.close(), osmium::gzip_error);
REQUIRE(count == count_fds());
}
TEST_CASE("Compressor: Invalid file descriptor for gzip-compressed file") {
REQUIRE_THROWS_AS(osmium::io::GzipCompressor(-1, osmium::io::fsync::yes), std::system_error);
}
TEST_CASE("Compressor: Non-open file descriptor for gzip-compressed file") {
// 12345 is just a random file descriptor that should not be open
REQUIRE_THROWS_AS(osmium::io::GzipCompressor(12345, osmium::io::fsync::yes), std::system_error);
}
TEST_CASE("Write gzip-compressed file with explicit close") {
const int count = count_fds();
const std::string output_file = "test_gzip_out.txt.gz";
const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
REQUIRE(fd > 0);
osmium::io::GzipCompressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
comp.close();
REQUIRE(count == count_fds());
REQUIRE(osmium::file_size(output_file) > 10);
}
TEST_CASE("Write gzip-compressed file with implicit close") {
const int count = count_fds();
const std::string output_file = "test_gzip_out.txt.gz";
const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
REQUIRE(fd > 0);
{
osmium::io::GzipCompressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
}
REQUIRE(count == count_fds());
REQUIRE(osmium::file_size(output_file) > 10);
}
+125
View File
@@ -0,0 +1,125 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/compression.hpp>
#include <string>
TEST_CASE("Invalid file descriptor of uncompressed file") {
osmium::io::NoDecompressor decomp{-1};
REQUIRE_THROWS_AS(decomp.read(), std::system_error);
}
TEST_CASE("Non-open file descriptor of uncompressed file") {
// 12345 is just a random file descriptor that should not be open
osmium::io::NoDecompressor decomp{12345};
REQUIRE_THROWS_AS(decomp.read(), std::system_error);
}
TEST_CASE("Empty uncompressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/empty_file");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
osmium::io::NoDecompressor decomp{fd};
REQUIRE(decomp.read().empty());
decomp.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Read uncompressed file") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/data.txt");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
size_t size = 0;
std::string all;
{
osmium::io::NoDecompressor decomp{fd};
for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
size += data.size();
all += data;
}
decomp.close();
}
REQUIRE(size >= 9);
all.resize(8);
REQUIRE("TESTDATA" == all);
REQUIRE(count == count_fds());
}
TEST_CASE("Read uncompressed file without explicit close") {
const int count = count_fds();
const std::string input_file = with_data_dir("t/io/data.txt");
const int fd = osmium::io::detail::open_for_reading(input_file);
REQUIRE(fd > 0);
size_t size = 0;
std::string all;
{
osmium::io::NoDecompressor decomp{fd};
for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
size += data.size();
all += data;
}
}
REQUIRE(size >= 9);
all.resize(8);
REQUIRE("TESTDATA" == all);
REQUIRE(count == count_fds());
}
TEST_CASE("Compressor: Invalid file descriptor for uncompressed file") {
osmium::io::NoCompressor comp{-1, osmium::io::fsync::yes};
REQUIRE_THROWS_AS(comp.write("foo"), std::system_error);
}
TEST_CASE("Compressor: Non-open file descriptor for uncompressed file") {
// 12345 is just a random file descriptor that should not be open
osmium::io::NoCompressor comp{12345, osmium::io::fsync::yes};
REQUIRE_THROWS_AS(comp.write("foo"), std::system_error);
}
TEST_CASE("Write uncompressed file with explicit close") {
const int count = count_fds();
const std::string output_file = "test_uncompressed_out.txt";
const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
REQUIRE(fd > 0);
osmium::io::NoCompressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
comp.close();
REQUIRE(count == count_fds());
REQUIRE(osmium::file_size(output_file) == 3);
}
TEST_CASE("Write uncompressed file with implicit close") {
const int count = count_fds();
const std::string output_file = "test_uncompressed_out.txt";
const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
REQUIRE(fd > 0);
{
osmium::io::NoCompressor comp{fd, osmium::io::fsync::yes};
comp.write("foo");
}
REQUIRE(count == count_fds());
REQUIRE(osmium::file_size(output_file) == 3);
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,27 @@
#include "catch.hpp"
#include <osmium/io/output_iterator.hpp>
#include <osmium/io/writer.hpp>
#include <osmium/io/xml_output.hpp>
TEST_CASE("Output iterator should be copy constructable and incrementable") {
const osmium::io::Header header{};
osmium::io::Writer writer{"test.osm", header, osmium::io::overwrite::allow};
const osmium::io::OutputIterator<osmium::io::Writer> out1{writer};
osmium::io::OutputIterator<osmium::io::Writer> out2{out1};
++out2;
}
TEST_CASE("Output iterator should be copy assignable") {
const osmium::io::Header header{};
osmium::io::Writer writer1{"test1.osm", header, osmium::io::overwrite::allow};
osmium::io::Writer writer2{"test2.osm", header, osmium::io::overwrite::allow};
const osmium::io::OutputIterator<osmium::io::Writer> out1{writer1};
osmium::io::OutputIterator<osmium::io::Writer> out2{writer2};
out2 = out1;
}
+227
View File
@@ -0,0 +1,227 @@
#include "catch.hpp"
#include <osmium/io/detail/string_util.hpp>
#include <iterator>
#include <locale>
#include <stdexcept>
#include <string>
// From C++20 we need to handle unicode literals differently
#ifdef __cpp_char8_t
static const char* u8cast(const char8_t *s) noexcept {
return reinterpret_cast<const char*>(s);
}
#else
static const char* u8cast(const char *s) noexcept {
return s;
}
#endif
TEST_CASE("output formatted with small results") {
std::string out;
osmium::io::detail::append_printf_formatted_string(out, "%d", 17);
REQUIRE(out == "17");
}
TEST_CASE("output formatted with several parameters in format string") {
std::string out;
osmium::io::detail::append_printf_formatted_string(out, "%d %s", 17, "foo");
REQUIRE(out == "17 foo");
}
TEST_CASE("output formatted into string already containing something") {
std::string out{"foo"};
osmium::io::detail::append_printf_formatted_string(out, " %d", 23);
REQUIRE(out == "foo 23");
}
TEST_CASE("output formatted with large results") {
const char* str =
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
std::string out;
#pragma GCC diagnostic push
#if __GNUC__ >= 8
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
osmium::io::detail::append_printf_formatted_string(out, "%s", str);
#pragma GCC diagnostic pop
REQUIRE(out == str);
}
TEST_CASE("UTF8 encoding: append to string") {
std::string out{"1234"};
osmium::io::detail::append_utf8_encoded_string(out, "abc");
REQUIRE(out == "1234abc");
}
TEST_CASE("UTF8 encoding: don't encode alphabetic characters") {
const char* s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string out;
osmium::io::detail::append_utf8_encoded_string(out, s);
REQUIRE(out == s);
}
TEST_CASE("UTF8 encoding: don't encode numeric characters") {
const char* s = "0123456789";
std::string out;
osmium::io::detail::append_utf8_encoded_string(out, s);
REQUIRE(out == s);
}
TEST_CASE("UTF8 encoding: don't encode lots of often used characters characters") {
const char* s = ".-;:_#+";
std::string out;
osmium::io::detail::append_utf8_encoded_string(out, s);
REQUIRE(out == s);
}
TEST_CASE("UTF8 encoding: encode characters that are special in OPL") {
std::string out;
osmium::io::detail::append_utf8_encoded_string(out, " \n,=@");
REQUIRE(out == "%20%%0a%%2c%%3d%%40%");
}
TEST_CASE("UTF8 encoding: encode multibyte character") {
std::string out;
osmium::io::detail::append_utf8_encoded_string(out, u8cast(u8"\u30dc_\U0001d11e_\U0001f6eb"));
REQUIRE(out == "%30dc%_%1d11e%_%1f6eb%");
}
TEST_CASE("html encoding does not encode normal characters") {
const char* s = "abc123,.-";
std::string out;
osmium::io::detail::append_xml_encoded_string(out, s);
REQUIRE(out == s);
}
TEST_CASE("html encoding encodes special XML characters") {
const char* s = "& \" \' < > \n \r \t";
std::string out;
osmium::io::detail::append_xml_encoded_string(out, s);
REQUIRE(out == "&amp; &quot; &apos; &lt; &gt; &#xA; &#xD; &#x9;");
}
TEST_CASE("debug encoding does not encode normal characters") {
const char* s = "abc123,.-";
std::string out;
osmium::io::detail::append_debug_encoded_string(out, s, "[", "]");
REQUIRE(out == s);
}
TEST_CASE("debug encoding encodes some unicode characters") {
const char* s = u8cast(u8"\n_\u30dc_\U0001d11e_\U0001f6eb");
std::string out;
osmium::io::detail::append_debug_encoded_string(out, s, "[", "]");
REQUIRE(out == "[<U+000A>]_[<U+30DC>]_[<U+1D11E>]_[<U+1F6EB>]");
}
TEST_CASE("utf8 encoding of non-printable characters in the first 127 characters") {
const std::locale cloc{"C"};
char s[] = "a\0";
for (char c = 1; c < 0x7f; ++c) {
std::string out;
s[0] = c;
osmium::io::detail::append_utf8_encoded_string(out, s);
if (!std::isprint(c, cloc)) {
REQUIRE(out[0] == '%');
}
}
}
TEST_CASE("debug encoding of non-printable characters in the first 127 characters") {
const std::locale cloc{"C"};
char s[] = "a\0";
for (char c = 1; c < 0x7f; ++c) {
std::string out;
s[0] = c;
osmium::io::detail::append_debug_encoded_string(out, s, "", "");
if (!std::isprint(c, cloc)) {
REQUIRE(out[0] == '<');
}
}
}
TEST_CASE("test codepoint to utf8 encoding") {
const char* s = u8cast(u8"\n_\u01a2_\u30dc_\U0001d11e_\U0001f680");
std::string out;
osmium::io::detail::append_codepoint_as_utf8(0x0aU, std::back_inserter(out)); // 1 utf8 byte
REQUIRE(out.size() == 1);
osmium::io::detail::append_codepoint_as_utf8('_', std::back_inserter(out));
REQUIRE(out.size() == 2);
osmium::io::detail::append_codepoint_as_utf8(0x01a2U, std::back_inserter(out)); // 2 utf8 bytes
REQUIRE(out.size() == 4);
osmium::io::detail::append_codepoint_as_utf8('_', std::back_inserter(out));
REQUIRE(out.size() == 5);
osmium::io::detail::append_codepoint_as_utf8(0x30dcU, std::back_inserter(out)); // 3 utf8 bytes
REQUIRE(out.size() == 8);
osmium::io::detail::append_codepoint_as_utf8('_', std::back_inserter(out));
REQUIRE(out.size() == 9);
osmium::io::detail::append_codepoint_as_utf8(0x1d11eU, std::back_inserter(out)); // 4 utf8 bytes
REQUIRE(out.size() == 13);
osmium::io::detail::append_codepoint_as_utf8('_', std::back_inserter(out));
REQUIRE(out.size() == 14);
osmium::io::detail::append_codepoint_as_utf8(0x1f680U, std::back_inserter(out)); // 4 utf8 bytes
REQUIRE(out.size() == 18);
REQUIRE(out == s);
}
TEST_CASE("test utf8 to codepoint decoding") {
const char* s = u8cast(u8"\n_\u01a2_\u30dc_\U0001d11e_\U0001f680");
const char* it = s;
const char* end = s + std::strlen(s) + 1;
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == 0x0aU);
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == '_');
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == 0x01a2U);
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == '_');
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == 0x30dcU);
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == '_');
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == 0x1d11eU);
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == '_');
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end) == 0x1f680U);
REQUIRE(*it++ == '\0');
REQUIRE(it == end);
}
TEST_CASE("Roundtrip unicode characters") {
char s[4] = {0};
const uint32_t max_code_point = 0x10ffffU;
for (uint32_t cp = 0; cp <= max_code_point; ++cp) {
const auto* const end = osmium::io::detail::append_codepoint_as_utf8(cp, s);
const char* it = s;
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, std::end(s)) == cp);
REQUIRE(end == it);
}
}
TEST_CASE("invalid codepoint") {
const char s[] = {static_cast<char>(0xff), static_cast<char>(0xff), static_cast<char>(0xff), static_cast<char>(0xff)};
const char* it = s;
REQUIRE_THROWS_AS(osmium::io::detail::next_utf8_codepoint(&it, std::end(s)), std::runtime_error);
}
TEST_CASE("incomplete Unicode codepoint") {
const char* s = u8cast(u8"\U0001f680");
const auto* it = s;
const char* end = s + std::strlen(s) + 1;
REQUIRE(osmium::io::detail::next_utf8_codepoint(&it, end - 1) == 0x1f680U);
REQUIRE(std::distance(s, it) == 4);
for (const int i : {0, 1, 2, 3}) {
it = s;
REQUIRE_THROWS_AS(osmium::io::detail::next_utf8_codepoint(&it, s + i), std::out_of_range);
}
}
+52
View File
@@ -0,0 +1,52 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/pbf_input.hpp>
#include <osmium/io/reader.hpp>
#include <osmium/osm/object.hpp>
TEST_CASE("Get supported PBF compression types") {
const auto types = osmium::io::supported_pbf_compression_types();
REQUIRE(types.size() >= 2);
REQUIRE(types[0] == "none");
REQUIRE(types[1] == "zlib");
}
/**
* Osmosis writes PBF with changeset=-1 if its input file did not contain the changeset field.
* The default value of the version field is -1 in the OSM.PBF format.
*
* t/io/data_pbf_version-1.osm.pbf was created using the following steps:
*
* * Convert t/io/data_pbf_version-1.osm to PBF format using Osmium Tool:
* `osmium cat -o tmp.osm.pbf --output-format pbf,add_metadata=no /data_pbf_version-1.osm.pbf`)
* * Convert that file using Osmosis:
* `osmosis --read-pbf file=tmp.osm.pbf --write-pbf file=data_pbf_version-1.osm.pbf compress=none usedense=false`
*/
TEST_CASE("Read PBF file with version=-1 and changeset=-1 (written by Osmosis)") {
const osmium::memory::Buffer buffer = osmium::io::read_file(with_data_dir("t/io/data_pbf_version-1.osm.pbf"));
// one node should be in this buffer
const osmium::OSMObject& object = *(buffer.cbegin<osmium::OSMObject>());
REQUIRE(object.version() == 0);
REQUIRE(object.changeset() == 0);
}
/**
* Osmosis writes PBF with changeset=-1 if its input file did not contain the changeset field.
* The default value of the version field is -1 in the OSM.PBF format.
*
* t/io/data_pbf_version-1.osm.pbf was created using the following steps:
*
* * Convert t/io/data_pbf_version-1.osm to PBF format using Osmium Tool:
* `osmium cat -o tmp.osm.pbf --output-format pbf,add_metadata=no /data_pbf_version-1.osm.pbf`)
* * Convert that file using Osmosis:
* `osmosis --read-pbf file=tmp.osm.pbf --write-pbf file=data_pbf_version-1-densenodes.osm.pbf compress=none usedense=true`
*/
TEST_CASE("Read PBF file with version=-1 and changeset=-1 and DenseNodes (written by Osmosis)") {
const osmium::memory::Buffer buffer = osmium::io::read_file(with_data_dir("t/io/data_pbf_version-1-densenodes.osm.pbf"));
// one node should be in this buffer
const osmium::OSMObject& object = *(buffer.cbegin<osmium::OSMObject>());
REQUIRE(object.version() == 0);
REQUIRE(object.changeset() == 0);
}
+380
View File
@@ -0,0 +1,380 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/handler.hpp>
#include <osmium/io/any_compression.hpp>
#include <osmium/io/any_input.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/visitor.hpp>
#include <iterator>
#include <stdexcept>
#include <vector>
struct CountHandler : public osmium::handler::Handler {
int count = 0;
void node(const osmium::Node& /*node*/) {
++count;
}
}; // class CountHandler
struct ZeroPositionNodeCountHandler : public osmium::handler::Handler {
// number of nodes seen at zero position, or visible with undefined
// location.
int count = 0;
int total_count = 0; // total number of nodes seen
const osmium::Location zero = osmium::Location{static_cast<int32_t>(0), static_cast<int32_t>(0)};
void node(const osmium::Node& node) {
// no nodes in the history file have a zero location, and
// no visible nodes should have an undefined location.
if ((node.location() == zero) ||
(node.visible() && !node.location())) {
++count;
}
++total_count;
}
}; // class ZeroPositionNodeCountHandler
TEST_CASE("Reader can be initialized with file") {
const osmium::io::File file{with_data_dir("t/io/data.osm")};
const int count = count_fds();
osmium::io::Reader reader{file};
osmium::handler::Handler handler;
osmium::apply(reader, handler);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader can be initialized with string") {
const int count = count_fds();
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
osmium::handler::Handler handler;
osmium::apply(reader, handler);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader can be initialized with user-provided pool") {
const int count = count_fds();
osmium::thread::Pool pool{4};
const osmium::io::File file{with_data_dir("t/io/data.osm")};
osmium::io::Reader reader{file, pool};
osmium::handler::Handler handler;
osmium::apply(reader, handler);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should throw after eof") {
const int count = count_fds();
const osmium::io::File file{with_data_dir("t/io/data.osm")};
osmium::io::Reader reader{file};
SECTION("Get header") {
const auto header = reader.header();
REQUIRE(header.get("generator") == "testdata");
REQUIRE_FALSE(reader.eof());
}
REQUIRE_FALSE(reader.eof());
while (const osmium::memory::Buffer buffer = reader.read()) {
}
REQUIRE(reader.eof());
REQUIRE_THROWS_AS(reader.read(), osmium::io_error);
reader.close();
REQUIRE(reader.eof());
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should not hang when apply() is called twice on reader") {
const int count = count_fds();
const osmium::io::File file{with_data_dir("t/io/data.osm")};
osmium::io::Reader reader{file};
osmium::handler::Handler handler;
osmium::apply(reader, handler);
REQUIRE_THROWS_AS(osmium::apply(reader, handler), osmium::io_error);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should work with a buffer with uncompressed data") {
const int count = count_fds();
const int fd = osmium::io::detail::open_for_reading(with_data_dir("t/io/data.osm"));
REQUIRE(fd >= 0);
const size_t buffer_size = 1000;
char buffer[buffer_size];
const auto length = ::read(fd, buffer, buffer_size);
REQUIRE(length > 0);
osmium::io::detail::reliable_close(fd);
const osmium::io::File file{buffer, static_cast<size_t>(length), "osm"};
osmium::io::Reader reader{file};
CountHandler handler;
REQUIRE(handler.count == 0);
osmium::apply(reader, handler);
REQUIRE(handler.count == 1);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should work with a buffer with gzip-compressed data") {
const int count = count_fds();
const int fd = osmium::io::detail::open_for_reading(with_data_dir("t/io/data.osm.gz"));
REQUIRE(fd >= 0);
const size_t buffer_size = 1000;
char buffer[buffer_size];
const auto length = ::read(fd, buffer, buffer_size);
REQUIRE(length > 0);
osmium::io::detail::reliable_close(fd);
const osmium::io::File file{buffer, static_cast<size_t>(length), "osm.gz"};
osmium::io::Reader reader{file};
CountHandler handler;
REQUIRE(handler.count == 0);
osmium::apply(reader, handler);
REQUIRE(handler.count == 1);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should work with a buffer with bzip2-compressed data") {
const int count = count_fds();
const int fd = osmium::io::detail::open_for_reading(with_data_dir("t/io/data.osm.bz2"));
REQUIRE(fd >= 0);
const size_t buffer_size = 1000;
char buffer[buffer_size];
const auto length = ::read(fd, buffer, buffer_size);
REQUIRE(length > 0);
osmium::io::detail::reliable_close(fd);
const osmium::io::File file{buffer, static_cast<size_t>(length), "osm.bz2"};
osmium::io::Reader reader{file};
CountHandler handler;
REQUIRE(handler.count == 0);
osmium::apply(reader, handler);
REQUIRE(handler.count == 1);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should decode zero node positions in history (XML)") {
const int count = count_fds();
osmium::io::Reader reader{with_data_dir("t/io/deleted_nodes.osh"),
osmium::osm_entity_bits::node};
ZeroPositionNodeCountHandler handler;
REQUIRE(handler.count == 0);
REQUIRE(handler.total_count == 0);
osmium::apply(reader, handler);
REQUIRE(handler.count == 0);
REQUIRE(handler.total_count == 2);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should decode zero node positions in history (PBF)") {
const int count = count_fds();
osmium::io::Reader reader{with_data_dir("t/io/deleted_nodes.osh.pbf"),
osmium::osm_entity_bits::node};
ZeroPositionNodeCountHandler handler;
REQUIRE(handler.count == 0);
REQUIRE(handler.total_count == 0);
osmium::apply(reader, handler);
REQUIRE(handler.count == 0);
REQUIRE(handler.total_count == 2);
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should fail with nonexistent file") {
const int count = count_fds();
REQUIRE_THROWS(osmium::io::Reader{with_data_dir("t/io/nonexistent-file.osm")});
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should fail with nonexistent file (gz)") {
const int count = count_fds();
REQUIRE_THROWS(osmium::io::Reader{with_data_dir("t/io/nonexistent-file.osm.gz")});
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should fail with nonexistent file (pbf)") {
const int count = count_fds();
REQUIRE_THROWS(osmium::io::Reader{with_data_dir("t/io/nonexistent-file.osm.pbf")});
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should work when there is an exception in main thread before getting header") {
const int count = count_fds();
try {
const osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
REQUIRE_FALSE(reader.eof());
throw std::runtime_error{"foo"};
} catch (...) {
}
REQUIRE(count == count_fds());
}
TEST_CASE("Reader should work when there is an exception in main thread while reading") {
const int count = count_fds();
try {
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
REQUIRE_FALSE(reader.eof());
auto header = reader.header();
throw std::runtime_error{"foo"};
} catch (...) {
}
REQUIRE(count == count_fds());
}
TEST_CASE("Applying rvalue handler on reader") {
const int count = count_fds();
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
struct NullHandler : public osmium::handler::Handler { };
osmium::apply(reader, NullHandler{});
reader.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Can call read() exactly once on Reader with entity_bits nothing") {
const int count = count_fds();
const osmium::io::File file{with_data_dir("t/io/data.osm")};
osmium::io::Reader reader{file, osmium::osm_entity_bits::nothing};
REQUIRE_FALSE(reader.eof());
SECTION("Get header") {
const auto header = reader.header();
REQUIRE(header.get("generator") == "testdata");
REQUIRE_FALSE(reader.eof());
}
const osmium::memory::Buffer buffer = reader.read();
REQUIRE_FALSE(buffer);
REQUIRE(reader.eof());
REQUIRE_THROWS_AS(reader.read(), osmium::io_error);
reader.close();
REQUIRE(reader.eof());
REQUIRE(count == count_fds());
}
TEST_CASE("Can not read after close") {
const int count = count_fds();
const osmium::io::File file{with_data_dir("t/io/data.osm")};
osmium::io::Reader reader{file};
SECTION("Get header") {
const auto header = reader.header();
REQUIRE(header.get("generator") == "testdata");
REQUIRE_FALSE(reader.eof());
}
REQUIRE_FALSE(reader.eof());
const osmium::memory::Buffer buffer = reader.read();
REQUIRE(buffer);
REQUIRE_FALSE(reader.eof());
reader.close();
REQUIRE(reader.eof());
REQUIRE_THROWS_AS(reader.read(), osmium::io_error);
REQUIRE(count == count_fds());
}
using object_counts = std::array<std::size_t, 3>;
std::vector<object_counts> count_objects_per_buffer(const char* filename, osmium::io::buffers_type btype) {
const osmium::io::File file{with_data_dir(filename)};
osmium::io::Reader reader{file, btype};
std::vector<object_counts> counts;
while (osmium::memory::Buffer buffer = reader.read()) {
const auto rn = buffer.select<osmium::Node>();
const auto rw = buffer.select<osmium::Way>();
const auto rr = buffer.select<osmium::Relation>();
counts.push_back(object_counts{static_cast<std::size_t>(std::distance(rn.begin(), rn.end())),
static_cast<std::size_t>(std::distance(rw.begin(), rw.end())),
static_cast<std::size_t>(std::distance(rr.begin(), rr.end()))});
}
return counts;
}
void check_buffer_counts(const std::string& filename, const std::vector<object_counts>& oc, osmium::io::buffers_type btype) {
for (const auto* suffix : {".osm", ".osm.opl", ".osm.o5m"}) {
const std::string fn = filename + suffix;
const auto counts = count_objects_per_buffer(fn.c_str(), btype);
REQUIRE(counts == oc);
}
}
TEST_CASE("Reader with single object type per buffer") {
check_buffer_counts("t/io/data-n5w1r3", {{5, 1, 3}}, osmium::io::buffers_type::any);
check_buffer_counts("t/io/data-n5w1r3", {{5, 0, 0}, {0, 1, 0}, {0, 0, 3}}, osmium::io::buffers_type::single);
check_buffer_counts("t/io/data-n0w1r3", {{0, 1, 0}, {0, 0, 3}}, osmium::io::buffers_type::single);
check_buffer_counts("t/io/data-n5w0r3", {{5, 0, 0}, {0, 0, 3}}, osmium::io::buffers_type::single);
check_buffer_counts("t/io/data-n5w1r0", {{5, 0, 0}, {0, 1, 0}}, osmium::io::buffers_type::single);
}
@@ -0,0 +1,10 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/reader.hpp>
TEST_CASE("Reader throws on unsupported file format") {
REQUIRE_THROWS_AS(osmium::io::Reader{with_data_dir("t/io/data.osm")}, osmium::unsupported_file_format_error);
}
@@ -0,0 +1,150 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/compression.hpp>
#include <osmium/io/xml_input.hpp>
#include <stdexcept>
#include <string>
#include <utility>
// The MockDecompressor behaves like other Decompressor classes, but "invents"
// OSM data in XML format that can be read. Through a parameter to the
// constructor it can be instructed to throw an exception in specific parts
// of its code. This is then used to test the internals of the Reader.
class MockDecompressor final : public osmium::io::Decompressor {
std::string m_fail_in;
int m_read_count = 0;
public:
explicit MockDecompressor(std::string fail_in) :
m_fail_in(std::move(fail_in)) {
if (m_fail_in == "constructor") {
throw std::runtime_error{"error constructor"};
}
}
MockDecompressor(const MockDecompressor&) = delete;
MockDecompressor& operator=(const MockDecompressor&) = delete;
MockDecompressor(MockDecompressor&&) = delete;
MockDecompressor& operator=(MockDecompressor&&) = delete;
~MockDecompressor() noexcept override = default;
static void add_node(std::string& s, int i) {
s += "<node id='";
s += std::to_string(i);
s += "' version='1' timestamp='2014-01-01T00:00:00Z' uid='1' user='test' changeset='1' lon='1.02' lat='1.02'/>\n";
}
std::string read() override {
std::string buffer;
++m_read_count;
if (m_read_count == 1) {
if (m_fail_in == "first read") {
throw std::runtime_error{"error first read"};
}
buffer += "<?xml version='1.0' encoding='UTF-8'?>\n<osm version='0.6' generator='testdata'>\n";
for (int i = 0; i < 1000; ++i) {
add_node(buffer, i);
}
} else if (m_read_count == 2) {
if (m_fail_in == "second read") {
throw std::runtime_error{"error second read"};
}
for (int i = 1000; i < 2000; ++i) {
add_node(buffer, i);
}
} else if (m_read_count == 3) {
buffer += "</osm>";
}
return buffer;
}
void close() override {
if (m_fail_in == "close") {
throw std::runtime_error{"error close"};
}
}
}; // class MockDecompressor
std::string fail_in;
TEST_CASE("Test Reader using MockDecompressor") {
osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::gzip,
[](int /*unused*/, osmium::io::fsync /*unused*/) { return nullptr; },
[&](int /*unused*/) { return new MockDecompressor(fail_in); },
[](const char* /*unused*/, size_t /*unused*/) { return nullptr; }
);
SECTION("fail in constructor") {
fail_in = "constructor";
try {
const osmium::io::Reader reader{with_data_dir("t/io/data.osm.gz")};
REQUIRE(false);
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error constructor");
}
}
SECTION("fail in first read") {
fail_in = "first read";
try {
osmium::io::Reader reader{with_data_dir("t/io/data.osm.gz")};
reader.read();
REQUIRE(false);
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error first read");
}
}
SECTION("fail in second read") {
fail_in = "second read";
try {
osmium::io::Reader reader{with_data_dir("t/io/data.osm.gz")};
reader.read();
reader.read();
REQUIRE(false);
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error second read");
}
}
SECTION("fail in close") {
fail_in = "close";
try {
osmium::io::Reader reader{with_data_dir("t/io/data.osm.gz")};
reader.read();
reader.read();
reader.read();
reader.close();
REQUIRE(false);
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error close");
}
}
SECTION("not failing") {
fail_in = "not";
osmium::io::Reader reader{with_data_dir("t/io/data.osm.gz")};
reader.read();
reader.close();
REQUIRE(true);
}
}
@@ -0,0 +1,111 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/io/compression.hpp>
#include <osmium/io/detail/input_format.hpp>
#include <osmium/io/detail/queue_util.hpp>
#include <osmium/io/file_format.hpp>
#include <osmium/io/header.hpp>
#include <osmium/io/reader.hpp>
#include <osmium/thread/queue.hpp>
#include <osmium/thread/util.hpp>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
class MockParser : public osmium::io::detail::Parser {
std::string m_fail_in;
public:
MockParser(osmium::io::detail::parser_arguments& args,
std::string fail_in) :
Parser(args),
m_fail_in(std::move(fail_in)) {
}
void run() final {
osmium::thread::set_thread_name("_osmium_mock_in");
if (m_fail_in == "header") {
throw std::runtime_error{"error in header"};
}
set_header_value(osmium::io::Header{});
osmium::memory::Buffer buffer(1000);
osmium::builder::add_node(buffer, osmium::builder::attr::_user("foo"));
send_to_output_queue(std::move(buffer));
if (m_fail_in == "read") {
throw std::runtime_error{"error in read"};
}
}
}; // class MockParser
TEST_CASE("Test Reader using MockParser") {
std::string fail_in;
osmium::io::detail::ParserFactory::instance().register_parser(
osmium::io::file_format::xml,
[&](osmium::io::detail::parser_arguments& args) {
return std::unique_ptr<osmium::io::detail::Parser>(new MockParser(args, fail_in));
});
SECTION("no failure") {
fail_in = "";
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
auto header = reader.header();
REQUIRE(reader.read());
REQUIRE_FALSE(reader.read());
REQUIRE(reader.eof());
reader.close();
}
SECTION("throw in header") {
fail_in = "header";
try {
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
reader.header();
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error in header");
}
}
SECTION("throw in read") {
fail_in = "read";
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
reader.header();
try {
reader.read();
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error in read");
}
reader.close();
}
SECTION("throw in user code") {
fail_in = "";
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
reader.header();
try {
throw std::runtime_error{"error in user code"};
} catch (const std::runtime_error& e) {
REQUIRE(std::string{e.what()} == "error in user code");
}
REQUIRE(reader.read());
REQUIRE_FALSE(reader.read());
REQUIRE(reader.eof());
reader.close();
}
}
+131
View File
@@ -0,0 +1,131 @@
#include "catch.hpp"
#include <osmium/io/detail/string_table.hpp>
#include <osmium/util/misc.hpp>
#include <iterator>
#include <string>
TEST_CASE("Empty StringStore") {
const osmium::io::detail::StringStore ss{100};
REQUIRE(ss.begin() == ss.end());
REQUIRE(ss.get_chunk_size() == 100);
REQUIRE(ss.get_chunk_count() == 1);
}
TEST_CASE("Add zero-length string to StringStore") {
osmium::io::detail::StringStore ss{100};
const char* s1 = ss.add("");
REQUIRE(std::string{s1}.empty());
auto it = ss.begin();
REQUIRE(s1 == *it);
REQUIRE(std::string{*it}.empty());
REQUIRE(++it == ss.end());
REQUIRE(ss.get_chunk_count() == 1);
}
TEST_CASE("Add strings to StringStore") {
osmium::io::detail::StringStore ss{100};
const char* s1 = ss.add("foo");
const char* s2 = ss.add("bar");
REQUIRE(s1 != s2);
REQUIRE(std::string{s1} == "foo");
REQUIRE(std::string{s2} == "bar");
auto it = ss.begin();
REQUIRE(s1 == *it++);
REQUIRE(s2 == *it++);
REQUIRE(it == ss.end());
}
TEST_CASE("Add zero-length string and longer strings to StringStore") {
osmium::io::detail::StringStore ss{100};
ss.add("");
ss.add("xxx");
ss.add("yyyyy");
auto it = ss.begin();
REQUIRE(std::string{*it++}.empty());
REQUIRE(std::string{*it++} == "xxx");
REQUIRE(std::string{*it++} == "yyyyy");
REQUIRE(it == ss.end());
}
TEST_CASE("Add many strings to StringStore") {
for (const char* teststring : {"", "a", "abc", "abcd", "abcde"}) {
osmium::io::detail::StringStore ss{100};
int i = 0;
for (; i < 200; ++i) {
ss.add(teststring);
}
for (const char* s : ss) {
REQUIRE(std::string{s} == teststring);
--i;
}
REQUIRE(i == 0);
REQUIRE(ss.get_chunk_count() > 1);
}
}
TEST_CASE("Empty StringTable") {
const osmium::io::detail::StringTable st;
REQUIRE(st.size() == 1);
REQUIRE(std::next(st.begin()) == st.end());
}
TEST_CASE("Add strings to StringTable") {
osmium::io::detail::StringTable st;
REQUIRE(st.add("foo") == 1);
REQUIRE(st.add("bar") == 2);
REQUIRE(st.add("bar") == 2);
REQUIRE(st.add("baz") == 3);
REQUIRE(st.add("foo") == 1);
REQUIRE(st.size() == 4);
auto it = st.begin();
REQUIRE(std::string{} == *it++);
REQUIRE(std::string{"foo"} == *it++);
REQUIRE(std::string{"bar"} == *it++);
REQUIRE(std::string{"baz"} == *it++);
REQUIRE(it == st.end());
}
TEST_CASE("Add empty string to StringTable") {
osmium::io::detail::StringTable st;
REQUIRE(st.add("") == 1);
REQUIRE(st.size() == 2);
REQUIRE(st.add("") == 1);
REQUIRE(st.size() == 2);
}
TEST_CASE("Lots of strings in string table so chunk overflows") {
osmium::io::detail::StringTable st{100};
REQUIRE(st.size() == 1);
const int n = 1000;
for (int i = 0; i < n; ++i) {
const auto s = std::to_string(i);
st.add(s.c_str());
}
REQUIRE(st.size() == n + 1);
auto it = st.begin();
REQUIRE(std::string{} == *it++);
for (int i = 0; i < n; ++i) {
REQUIRE(osmium::detail::str_to_int<int>(*it++) == i);
}
REQUIRE(it == st.end());
}
+225
View File
@@ -0,0 +1,225 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/any_compression.hpp>
#include <osmium/io/output_iterator.hpp>
#include <osmium/io/xml_input.hpp>
#include <osmium/io/xml_output.hpp>
#include <osmium/memory/buffer.hpp>
#include <algorithm>
#include <iterator>
#include <stdexcept>
#include <string>
#include <utility>
static osmium::memory::Buffer get_buffer() {
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
osmium::memory::Buffer buffer = reader.read();
REQUIRE(buffer);
REQUIRE(buffer.committed() > 0);
return buffer;
}
static osmium::memory::Buffer get_and_check_buffer() {
auto buffer = get_buffer();
const auto num = std::distance(buffer.select<osmium::OSMObject>().cbegin(), buffer.select<osmium::OSMObject>().cend());
REQUIRE(num > 0);
REQUIRE(buffer.select<osmium::OSMObject>().cbegin()->id() == 1);
return buffer;
}
TEST_CASE("Writer: Empty writes") {
const int count = count_fds();
std::string filename;
SECTION("Empty buffer") {
filename = "test-writer-out-empty-buffer.osm";
osmium::io::Writer writer{filename, osmium::io::overwrite::allow};
osmium::memory::Buffer empty_buffer{1024};
writer(std::move(empty_buffer));
writer.close();
}
SECTION("Invalid buffer") {
filename = "test-writer-out-invalid-buffer.osm";
osmium::io::Writer writer{filename, osmium::io::overwrite::allow};
osmium::memory::Buffer invalid_buffer;
writer(std::move(invalid_buffer));
writer.close();
}
osmium::io::Reader reader_check{filename};
const osmium::memory::Buffer buffer_check = reader_check.read();
REQUIRE_FALSE(buffer_check);
REQUIRE(count == count_fds());
}
TEST_CASE("Writer: Successful writes writing buffer") {
const int count = count_fds();
auto buffer = get_buffer();
const auto num = std::distance(buffer.select<osmium::OSMObject>().cbegin(), buffer.select<osmium::OSMObject>().cend());
REQUIRE(num > 0);
REQUIRE(buffer.select<osmium::OSMObject>().cbegin()->id() == 1);
const std::string filename = "test-writer-out-buffer.osm";
osmium::io::Writer writer{filename, osmium::io::Header{}, osmium::io::overwrite::allow};
writer(std::move(buffer));
writer.close();
REQUIRE(count == count_fds());
REQUIRE_THROWS_AS(writer(osmium::memory::Buffer{}), osmium::io_error);
osmium::io::Reader reader_check{filename};
const osmium::memory::Buffer buffer_check = reader_check.read();
REQUIRE(buffer_check);
REQUIRE(buffer_check.committed() > 0);
REQUIRE(buffer_check.select<osmium::OSMObject>().size() == static_cast<std::size_t>(num));
REQUIRE(buffer_check.select<osmium::OSMObject>().cbegin()->id() == 1);
}
TEST_CASE("Writer: Successful writes writing items") {
const int count = count_fds();
auto buffer = get_buffer();
const auto num = std::distance(buffer.select<osmium::OSMObject>().cbegin(), buffer.select<osmium::OSMObject>().cend());
REQUIRE(num > 0);
REQUIRE(buffer.select<osmium::OSMObject>().cbegin()->id() == 1);
const std::string filename = "test-writer-out-item.osm";
osmium::io::Writer writer{filename, osmium::io::overwrite::allow};
for (const auto& item : buffer) {
writer(item);
}
writer.close();
REQUIRE(count == count_fds());
osmium::io::Reader reader_check{filename};
const osmium::memory::Buffer buffer_check = reader_check.read();
REQUIRE(buffer_check);
REQUIRE(buffer_check.committed() > 0);
REQUIRE(buffer_check.select<osmium::OSMObject>().size() == static_cast<std::size_t>(num));
REQUIRE(buffer_check.select<osmium::OSMObject>().cbegin()->id() == 1);
}
TEST_CASE("Writer: Successful writes using output iterator") {
const int count = count_fds();
auto buffer = get_buffer();
const auto num = std::distance(buffer.select<osmium::OSMObject>().cbegin(), buffer.select<osmium::OSMObject>().cend());
REQUIRE(num > 0);
REQUIRE(buffer.select<osmium::OSMObject>().cbegin()->id() == 1);
const std::string filename = "test-writer-out-iterator.osm";
osmium::io::Writer writer{filename, osmium::io::overwrite::allow};
auto it = osmium::io::make_output_iterator(writer);
std::copy(buffer.cbegin(), buffer.cend(), it);
writer.close();
REQUIRE(count == count_fds());
osmium::io::Reader reader_check{filename};
const osmium::memory::Buffer buffer_check = reader_check.read();
REQUIRE(buffer_check);
REQUIRE(buffer_check.committed() > 0);
REQUIRE(buffer_check.select<osmium::OSMObject>().size() == static_cast<std::size_t>(num));
REQUIRE(buffer_check.select<osmium::OSMObject>().cbegin()->id() == 1);
}
TEST_CASE("Writer: Interrupted writer after open") {
const int count = count_fds();
auto buffer = get_and_check_buffer();
bool error = false;
try {
const osmium::io::Writer writer{"test-writer-out-fail1.osm", osmium::io::overwrite::allow};
throw std::runtime_error{"some error"};
} catch (...) {
error = true;
}
REQUIRE(error);
REQUIRE(count == count_fds());
}
TEST_CASE("Writer: Interrupted writer after write") {
const int count = count_fds();
auto buffer = get_and_check_buffer();
bool error = false;
try {
osmium::io::Writer writer{"test-writer-out-fail2.osm", osmium::io::overwrite::allow};
writer(std::move(buffer));
throw std::runtime_error{"some error"};
} catch (...) {
error = true;
}
REQUIRE(error);
REQUIRE(count == count_fds());
}
TEST_CASE("Writer with user-provided pool with default number of threads") {
const int count = count_fds();
auto buffer = get_buffer();
osmium::thread::Pool pool;
osmium::io::Writer writer{"test-writer-pool-with-default-threads.osm", pool, osmium::io::overwrite::allow};
writer(std::move(buffer));
writer.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Writer with user-provided pool with negative number of threads") {
const int count = count_fds();
auto buffer = get_buffer();
osmium::thread::Pool pool{-2};
osmium::io::Writer writer{"test-writer-pool-with-negative-threads.osm", pool, osmium::io::overwrite::allow};
writer(std::move(buffer));
writer.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Writer with user-provided pool with outlier negative number of threads") {
const int count = count_fds();
auto buffer = get_buffer();
osmium::thread::Pool pool{-1000};
osmium::io::Writer writer{"test-writer-pool-with-outlier-negative-threads.osm", osmium::io::overwrite::allow, pool};
writer(std::move(buffer));
writer.close();
REQUIRE(count == count_fds());
}
TEST_CASE("Writer with user-provided pool with outlier positive number of threads") {
const int count = count_fds();
auto buffer = get_buffer();
osmium::thread::Pool pool{1000};
osmium::io::Writer writer{"test-writer-pool-with-outlier-positive-threads.osm", osmium::io::overwrite::allow, pool};
writer(std::move(buffer));
writer.close();
REQUIRE(count == count_fds());
}
@@ -0,0 +1,106 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/compression.hpp>
#include <osmium/io/xml_input.hpp>
#include <osmium/io/xml_output.hpp>
#include <stdexcept>
#include <string>
#include <utility>
class MockCompressor final : public osmium::io::Compressor {
std::string m_fail_in;
public:
explicit MockCompressor(std::string fail_in) :
Compressor(osmium::io::fsync::no),
m_fail_in(std::move(fail_in)) {
if (m_fail_in == "constructor") {
throw std::logic_error{"constructor"};
}
}
MockCompressor(const MockCompressor&) = delete;
MockCompressor& operator=(const MockCompressor&) = delete;
MockCompressor(MockCompressor&&) = delete;
MockCompressor& operator=(MockCompressor&&) = delete;
~MockCompressor() noexcept override = default;
void write(const std::string& /*data*/) override {
if (m_fail_in == "write") {
throw std::logic_error{"write"};
}
}
void close() override {
if (m_fail_in == "close") {
throw std::logic_error{"close"};
}
}
}; // class MockCompressor
std::string fail_in;
TEST_CASE("Write with mock compressor") {
osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::gzip,
[&](int /*unused*/, osmium::io::fsync /*unused*/) { return new MockCompressor(fail_in); },
[](int /*unused*/) { return nullptr; },
[](const char* /*unused*/, size_t /*unused*/) { return nullptr; }
);
osmium::io::Header header;
header.set("generator", "test_writer_with_mock_compression.cpp");
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
osmium::memory::Buffer buffer = reader.read();
REQUIRE(buffer);
REQUIRE(buffer.committed() > 0);
REQUIRE_FALSE(buffer.select<osmium::OSMObject>().empty());
REQUIRE(buffer.select<osmium::OSMObject>().size() > 0); // NOLINT(readability-container-size-empty)
SECTION("fail on construction") {
fail_in = "constructor";
REQUIRE_THROWS_AS([&](){
osmium::io::Writer writer("test-writer-mock-fail-on-construction.osm.gz", header, osmium::io::overwrite::allow);
writer(std::move(buffer));
writer.close();
}(), std::logic_error);
}
SECTION("fail on write") {
fail_in = "write";
REQUIRE_THROWS_AS([&](){
osmium::io::Writer writer("test-writer-mock-fail-on-write.osm.gz", header, osmium::io::overwrite::allow);
writer(std::move(buffer));
writer.close();
}(), std::logic_error);
}
SECTION("fail on close") {
fail_in = "close";
REQUIRE_THROWS_AS([&](){
osmium::io::Writer writer("test-writer-mock-fail-on-close.osm.gz", header, osmium::io::overwrite::allow);
writer(std::move(buffer));
writer.close();
}(), std::logic_error);
}
}
@@ -0,0 +1,106 @@
#include "catch.hpp"
#include "utils.hpp"
#include <osmium/io/compression.hpp>
#include <osmium/io/detail/output_format.hpp>
#include <osmium/io/detail/queue_util.hpp>
#include <osmium/io/writer.hpp>
#include <osmium/io/xml_input.hpp>
#include <stdexcept>
#include <string>
#include <utility>
class MockOutputFormat : public osmium::io::detail::OutputFormat {
std::string m_fail_in;
public:
MockOutputFormat(osmium::thread::Pool& pool, const osmium::io::File& /*file*/, osmium::io::detail::future_string_queue_type& output_queue, std::string fail_in) :
OutputFormat(pool, output_queue),
m_fail_in(std::move(fail_in)) {
}
void write_header(const osmium::io::Header& /*header*/) final {
if (m_fail_in == "header") {
throw std::logic_error{"header"};
}
send_to_output_queue(std::string{"header"});
}
void write_buffer(osmium::memory::Buffer&& /*buffer*/) final {
if (m_fail_in == "write") {
throw std::logic_error{"write"};
}
send_to_output_queue(std::string{"write"});
}
void write_end() final {
if (m_fail_in == "write_end") {
throw std::logic_error{"write_end"};
}
send_to_output_queue(std::string{"end"});
}
}; // class MockOutputFormat
TEST_CASE("Test Writer with MockOutputFormat") {
std::string fail_in;
osmium::io::detail::OutputFormatFactory::instance().register_output_format(
osmium::io::file_format::xml,
[&](osmium::thread::Pool& pool, const osmium::io::File& file, osmium::io::detail::future_string_queue_type& output_queue) {
return new MockOutputFormat{pool, file, output_queue, fail_in};
});
osmium::io::Header header;
header.set("generator", "test_writer_with_mock_encoder.cpp");
osmium::io::Reader reader{with_data_dir("t/io/data.osm")};
osmium::memory::Buffer buffer = reader.read();
REQUIRE(buffer);
REQUIRE(buffer.committed() > 0);
REQUIRE_FALSE(buffer.select<osmium::OSMObject>().empty());
REQUIRE(buffer.select<osmium::OSMObject>().size() > 0); // NOLINT(readability-container-size-empty)
SECTION("error in header") {
fail_in = "header";
REQUIRE_THROWS_AS([&](){
osmium::io::Writer writer("test-writer-mock-fail-on-construction.osm", header, osmium::io::overwrite::allow);
writer(std::move(buffer));
writer.close();
}(), std::logic_error);
}
SECTION("error in write") {
fail_in = "write";
REQUIRE_THROWS_AS([&](){
osmium::io::Writer writer("test-writer-mock-fail-on-construction.osm", header, osmium::io::overwrite::allow);
writer(std::move(buffer));
writer.close();
}(), std::logic_error);
}
SECTION("error in write_end") {
fail_in = "write_end";
REQUIRE_THROWS_AS([&](){
osmium::io::Writer writer("test-writer-mock-fail-on-construction.osm", header, osmium::io::overwrite::allow);
writer(std::move(buffer));
writer.close();
}(), std::logic_error);
}
}
@@ -0,0 +1,131 @@
#include "catch.hpp"
#include <osmium/memory/buffer.hpp>
#include <array>
#include <stdexcept>
TEST_CASE("Buffer basics") {
osmium::memory::Buffer invalid_buffer1;
const osmium::memory::Buffer invalid_buffer2;
osmium::memory::Buffer empty_buffer1{1024};
osmium::memory::Buffer empty_buffer2{2048};
REQUIRE_FALSE(invalid_buffer1);
REQUIRE_FALSE(invalid_buffer2);
REQUIRE(empty_buffer1);
REQUIRE(empty_buffer2);
REQUIRE(invalid_buffer1 == invalid_buffer2);
REQUIRE(invalid_buffer1 != empty_buffer1);
REQUIRE(empty_buffer1 != empty_buffer2);
REQUIRE(invalid_buffer1.capacity() == 0);
REQUIRE(invalid_buffer1.written() == 0);
REQUIRE(invalid_buffer1.committed() == 0);
REQUIRE(invalid_buffer1.clear() == 0);
REQUIRE(empty_buffer1.capacity() == 1024);
REQUIRE(empty_buffer1.written() == 0);
REQUIRE(empty_buffer1.committed() == 0);
REQUIRE(empty_buffer1.is_aligned());
REQUIRE(empty_buffer1.clear() == 0);
REQUIRE(empty_buffer2.capacity() == 2048);
REQUIRE(empty_buffer2.written() == 0);
REQUIRE(empty_buffer2.committed() == 0);
REQUIRE(empty_buffer2.is_aligned());
REQUIRE(empty_buffer2.clear() == 0);
}
TEST_CASE("Buffer with zero size") {
const osmium::memory::Buffer buffer{0};
REQUIRE(buffer.capacity() == 64);
}
TEST_CASE("Buffer with less than minimum size") {
const osmium::memory::Buffer buffer{63};
REQUIRE(buffer.capacity() == 64);
}
TEST_CASE("Buffer with minimum size") {
const osmium::memory::Buffer buffer{64};
REQUIRE(buffer.capacity() == 64);
}
TEST_CASE("Buffer with non-aligned size") {
const osmium::memory::Buffer buffer{65};
REQUIRE(buffer.capacity() > 65);
}
TEST_CASE("Grow a buffer") {
osmium::memory::Buffer buffer{128};
REQUIRE(buffer.capacity() == 128);
buffer.grow(256);
REQUIRE(buffer.capacity() == 256);
buffer.grow(257);
REQUIRE(buffer.capacity() > 256);
REQUIRE(buffer.committed() == 0);
REQUIRE(buffer.written() == 0);
REQUIRE(buffer.is_aligned());
}
TEST_CASE("Reserve space in a non-growing buffer") {
osmium::memory::Buffer buffer{128, osmium::memory::Buffer::auto_grow::no};
REQUIRE(buffer.reserve_space(20) != nullptr);
REQUIRE(buffer.written() == 20);
REQUIRE_THROWS_AS(buffer.reserve_space(1000), osmium::buffer_is_full);
}
TEST_CASE("Reserve space in a growing buffer") {
osmium::memory::Buffer buffer{128, osmium::memory::Buffer::auto_grow::yes};
REQUIRE(buffer.reserve_space(20) != nullptr);
REQUIRE(buffer.written() == 20);
REQUIRE(buffer.reserve_space(1000) != nullptr);
REQUIRE(buffer.written() == 1020);
}
TEST_CASE("Create buffer from existing data with good alignment works") {
std::array<unsigned char, 128> data = {{0}};
const osmium::memory::Buffer buffer{data.data(), data.size()};
REQUIRE(buffer.capacity() == 128);
REQUIRE(buffer.committed() == 128);
}
TEST_CASE("Create buffer from existing data with good alignment and committed value works") {
std::array<unsigned char, 128> data = {{0}};
const osmium::memory::Buffer buffer{data.data(), data.size(), 32};
REQUIRE(buffer.capacity() == 128);
REQUIRE(buffer.committed() == 32);
REQUIRE(buffer.written() == 32);
}
TEST_CASE("Create buffer from existing data with bad alignment fails") {
std::array<unsigned char, 128> data = {{0}};
const auto l1 = [&](){
const osmium::memory::Buffer buffer{data.data(), 127};
};
const auto l2 = [&](){
const osmium::memory::Buffer buffer{data.data(), 127, 120};
};
const auto l3 = [&](){
const osmium::memory::Buffer buffer{data.data(), 128, 127};
};
const auto l4 = [&](){
const osmium::memory::Buffer buffer{data.data(), 32, 128};
};
REQUIRE_THROWS_AS(l1(), std::invalid_argument);
REQUIRE_THROWS_AS(l2(), std::invalid_argument);
REQUIRE_THROWS_AS(l3(), std::invalid_argument);
REQUIRE_THROWS_AS(l4(), std::invalid_argument);
}
+191
View File
@@ -0,0 +1,191 @@
#include "catch.hpp"
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/osm/node.hpp>
#include <algorithm>
#include <iterator>
#include <string>
void check_node_1(const osmium::Node& node) {
REQUIRE(1 == node.id());
REQUIRE(3 == node.version());
REQUIRE(node.visible());
REQUIRE(333 == node.changeset());
REQUIRE(21 == node.uid());
REQUIRE(123 == uint32_t(node.timestamp()));
REQUIRE(osmium::Location(3.5, 4.7) == node.location());
REQUIRE(std::string{"testuser"} == node.user());
for (const osmium::memory::Item& item : node) {
REQUIRE(osmium::item_type::tag_list == item.type());
}
REQUIRE(node.tags().begin() == node.tags().end());
REQUIRE(node.tags().empty());
REQUIRE(0 == std::distance(node.tags().begin(), node.tags().end()));
}
void check_node_2(const osmium::Node& node) {
REQUIRE(2 == node.id());
REQUIRE(3 == node.version());
REQUIRE(node.visible());
REQUIRE(333 == node.changeset());
REQUIRE(21 == node.uid());
REQUIRE(123 == uint32_t(node.timestamp()));
REQUIRE(osmium::Location(3.5, 4.7) == node.location());
REQUIRE(std::string{"testuser"} == node.user());
for (const osmium::memory::Item& item : node) {
REQUIRE(osmium::item_type::tag_list == item.type());
}
REQUIRE_FALSE(node.tags().empty());
REQUIRE(2 == std::distance(node.tags().begin(), node.tags().end()));
int n = 0;
for (const osmium::Tag& tag : node.tags()) {
switch (n) {
case 0:
REQUIRE(std::string("amenity") == tag.key());
REQUIRE(std::string("bank") == tag.value());
break;
case 1:
REQUIRE(std::string("name") == tag.key());
REQUIRE(std::string("OSM Savings") == tag.value());
break;
default:
REQUIRE(false);
}
++n;
}
REQUIRE(2 == n);
}
TEST_CASE("Node in Buffer") {
constexpr size_t buffer_size = 10000;
unsigned char data[buffer_size];
osmium::memory::Buffer buffer{data, buffer_size, 0};
SECTION("Add node to buffer") {
{
// add node 1
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_id(1)
.set_version(3)
.set_visible(true)
.set_changeset(333)
.set_uid(21)
.set_timestamp(123)
.set_location(osmium::Location{3.5, 4.7})
.set_user("testuser");
}
buffer.commit();
{
// add node 2
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_id(2)
.set_version(3)
.set_visible(true)
.set_changeset(333)
.set_uid(21)
.set_timestamp(123)
.set_location(osmium::Location{3.5, 4.7})
.set_user("testuser");
osmium::builder::TagListBuilder tag_builder{node_builder};
tag_builder.add_tag("amenity", "bank");
tag_builder.add_tag("name", "OSM Savings");
}
buffer.commit();
REQUIRE(2 == std::distance(buffer.begin(), buffer.end()));
int item_no = 0;
for (const osmium::memory::Item& item : buffer) {
REQUIRE(osmium::item_type::node == item.type());
const auto& node = static_cast<const osmium::Node&>(item);
switch (item_no) {
case 0:
check_node_1(node);
break;
case 1:
check_node_2(node);
break;
default:
break;
}
++item_no;
}
}
SECTION("Add buffer to another one") {
{
// add node 1
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_id(1)
.set_version(3)
.set_visible(true)
.set_changeset(333)
.set_uid(21)
.set_timestamp(123)
.set_location(osmium::Location{3.5, 4.7})
.set_user("testuser");
}
buffer.commit();
osmium::memory::Buffer buffer2{buffer_size, osmium::memory::Buffer::auto_grow::yes};
buffer2.add_buffer(buffer);
buffer2.commit();
REQUIRE(buffer.committed() == buffer2.committed());
const osmium::Node& node = buffer2.get<osmium::Node>(0);
REQUIRE(node.id() == 1);
REQUIRE(123 == uint32_t(node.timestamp()));
}
SECTION("Use back_inserter on buffer") {
{
// add node 1
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_id(1)
.set_version(3)
.set_visible(true)
.set_changeset(333)
.set_uid(21)
.set_timestamp(123)
.set_location(osmium::Location{3.5, 4.7})
.set_user("testuser");
}
buffer.commit();
osmium::memory::Buffer buffer2{buffer_size, osmium::memory::Buffer::auto_grow::yes};
std::copy(buffer.begin(), buffer.end(), std::back_inserter(buffer2));
REQUIRE(buffer.committed() == buffer2.committed());
const osmium::Node& node = buffer2.get<osmium::Node>(0);
REQUIRE(node.id() == 1);
REQUIRE(123 == uint32_t(node.timestamp()));
}
}
@@ -0,0 +1,208 @@
#include "catch.hpp"
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/osm/node.hpp>
#include <iterator>
struct CallbackClass {
int count = 0;
void moving_in_buffer(size_t old_offset, size_t new_offset) {
REQUIRE(old_offset > new_offset);
++count;
}
}; // struct CallbackClass
TEST_CASE("Purge data from empty buffer") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 0);
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 0);
REQUIRE(buffer.committed() == 0);
}
TEST_CASE("Purge buffer with one object but nothing to delete") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
}
buffer.commit();
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
const size_t committed = buffer.committed();
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 0);
REQUIRE(committed == buffer.committed());
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
}
TEST_CASE("Purge buffer with one object which gets deleted") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
node_builder.set_removed(true);
}
buffer.commit();
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 0);
REQUIRE(buffer.committed() == 0);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 0);
}
TEST_CASE("Purge buffer without callback") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
node_builder.set_removed(true);
}
buffer.commit();
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
buffer.purge_removed();
REQUIRE(buffer.committed() == 0);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 0);
}
TEST_CASE("Purge buffer with two objects, first gets deleted") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
node_builder.set_removed(true);
}
buffer.commit();
const size_t size1 = buffer.committed();
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
}
buffer.commit();
const size_t size2 = buffer.committed() - size1;
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 2);
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 1);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
REQUIRE(buffer.committed() == size2);
}
TEST_CASE("Purge buffer with two objects, second gets deleted") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser_longer_name");
}
buffer.commit();
const size_t size1 = buffer.committed();
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
node_builder.set_removed(true);
}
buffer.commit();
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 2);
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 0);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
REQUIRE(buffer.committed() == size1);
}
TEST_CASE("Purge buffer with three objects, middle one gets deleted") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser_longer_name");
}
buffer.commit();
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
node_builder.set_removed(true);
}
buffer.commit();
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("sn");
}
buffer.commit();
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 3);
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 1);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 2);
}
TEST_CASE("Purge buffer with three objects, all get deleted") {
constexpr const size_t buffer_size = 10000;
osmium::memory::Buffer buffer{buffer_size};
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser_longer_name");
node_builder.set_removed(true);
}
buffer.commit();
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("testuser");
node_builder.set_removed(true);
}
buffer.commit();
{
osmium::builder::NodeBuilder node_builder{buffer};
node_builder.set_user("sn");
node_builder.set_removed(true);
}
buffer.commit();
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 3);
CallbackClass callback;
buffer.purge_removed(&callback);
REQUIRE(callback.count == 0);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 0);
}
@@ -0,0 +1,101 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/memory/callback_buffer.hpp>
#include <iterator>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Callback buffer") {
osmium::memory::CallbackBuffer cb;
REQUIRE(cb.buffer().committed() == 0);
osmium::builder::add_node(cb.buffer(), _id(1));
osmium::builder::add_node(cb.buffer(), _id(2));
osmium::builder::add_node(cb.buffer(), _id(3));
auto c = cb.buffer().committed();
REQUIRE(c > 0);
REQUIRE(std::distance(cb.buffer().begin(), cb.buffer().end()) == 3);
auto buffer = cb.read();
REQUIRE(cb.buffer().committed() == 0);
REQUIRE(buffer.committed() == c);
REQUIRE(std::distance(cb.buffer().begin(), cb.buffer().end()) == 0);
// no callback defined, so nothing will happen
cb.flush();
}
TEST_CASE("Callback buffer with callback triggering every time") {
int run = 0;
osmium::memory::CallbackBuffer cb{[&](osmium::memory::Buffer&& buffer){
REQUIRE(buffer.committed() > 0);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 1);
++run;
}, 1000, 10};
osmium::builder::add_node(cb.buffer(), _id(1));
REQUIRE(cb.buffer().committed() > 10);
cb.possibly_flush();
osmium::builder::add_node(cb.buffer(), _id(2));
REQUIRE(cb.buffer().committed() > 10);
cb.possibly_flush();
osmium::builder::add_node(cb.buffer(), _id(3));
REQUIRE(cb.buffer().committed() > 10);
cb.possibly_flush();
REQUIRE(run == 3);
REQUIRE(std::distance(cb.buffer().begin(), cb.buffer().end()) == 0);
}
TEST_CASE("Callback buffer with callback triggering sometimes") {
int run = 0;
osmium::memory::CallbackBuffer cb{[&](osmium::memory::Buffer&& buffer){
REQUIRE(buffer.committed() > 0);
++run;
}, 1000, 100};
osmium::builder::add_node(cb.buffer(), _id(1));
REQUIRE(cb.buffer().committed() < 100);
cb.possibly_flush();
osmium::builder::add_node(cb.buffer(), _id(2));
cb.possibly_flush();
osmium::builder::add_node(cb.buffer(), _id(3));
cb.possibly_flush();
REQUIRE(run < 3);
}
TEST_CASE("Callback buffer with callback set later") {
int run = 0;
osmium::memory::CallbackBuffer cb{1000, 10};
osmium::builder::add_node(cb.buffer(), _id(1));
cb.possibly_flush();
osmium::builder::add_node(cb.buffer(), _id(2));
cb.possibly_flush();
osmium::builder::add_node(cb.buffer(), _id(3));
cb.set_callback([&](osmium::memory::Buffer&& buffer){
REQUIRE(buffer.committed() > 0);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 3);
++run;
});
REQUIRE(std::distance(cb.buffer().begin(), cb.buffer().end()) == 3);
cb.possibly_flush();
REQUIRE(std::distance(cb.buffer().begin(), cb.buffer().end()) == 0);
REQUIRE(run == 1);
}
+29
View File
@@ -0,0 +1,29 @@
#include "catch.hpp"
#include <osmium/memory/item.hpp>
TEST_CASE("padded length") {
REQUIRE(osmium::memory::padded_length(0) == 0);
REQUIRE(osmium::memory::padded_length(1) == 8);
REQUIRE(osmium::memory::padded_length(2) == 8);
REQUIRE(osmium::memory::padded_length(7) == 8);
REQUIRE(osmium::memory::padded_length(8) == 8);
REQUIRE(osmium::memory::padded_length(9) == 16);
REQUIRE(osmium::memory::padded_length(2147483647UL) == 2147483648UL);
REQUIRE(osmium::memory::padded_length(2147483648UL) == 2147483648UL);
REQUIRE(osmium::memory::padded_length(2147483650UL) == 2147483656UL);
// The following checks only make sense on a 64 bit system (with
// sizeof(size_t) == 8), because the numbers are too large for 32 bit.
// The casts to size_t do nothing on a 64 bit system, on a 32 bit system
// they bring the numbers into the right range and everything still works.
REQUIRE(osmium::memory::padded_length(static_cast<std::size_t>(4294967295ULL)) == static_cast<std::size_t>(4294967296ULL));
REQUIRE(osmium::memory::padded_length(static_cast<std::size_t>(4294967296ULL)) == static_cast<std::size_t>(4294967296ULL));
REQUIRE(osmium::memory::padded_length(static_cast<std::size_t>(4294967297ULL)) == static_cast<std::size_t>(4294967304ULL));
REQUIRE(osmium::memory::padded_length(static_cast<std::size_t>(7999999999ULL)) == static_cast<std::size_t>(8000000000ULL));
REQUIRE(osmium::memory::padded_length(static_cast<std::size_t>(8000000000ULL)) == static_cast<std::size_t>(8000000000ULL));
REQUIRE(osmium::memory::padded_length(static_cast<std::size_t>(8000000001ULL)) == static_cast<std::size_t>(8000000008ULL));
}
@@ -0,0 +1,71 @@
#include "catch.hpp"
#include <osmium/memory/item_iterator.hpp>
#include <osmium/osm.hpp>
static_assert(osmium::memory::detail::type_is_compatible<osmium::Node>(osmium::item_type::node), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Way>(osmium::item_type::node), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Relation>(osmium::item_type::node), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Area>(osmium::item_type::node), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMObject>(osmium::item_type::node), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Changeset>(osmium::item_type::node), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMEntity>(osmium::item_type::node), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::memory::Item>(osmium::item_type::node), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Node>(osmium::item_type::way), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::Way>(osmium::item_type::way), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Relation>(osmium::item_type::way), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Area>(osmium::item_type::way), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMObject>(osmium::item_type::way), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Changeset>(osmium::item_type::way), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMEntity>(osmium::item_type::way), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::memory::Item>(osmium::item_type::way), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Node>(osmium::item_type::relation), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Way>(osmium::item_type::relation), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::Relation>(osmium::item_type::relation), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Area>(osmium::item_type::relation), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMObject>(osmium::item_type::relation), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Changeset>(osmium::item_type::relation), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMEntity>(osmium::item_type::relation), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::memory::Item>(osmium::item_type::relation), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Node>(osmium::item_type::area), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Way>(osmium::item_type::area), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Relation>(osmium::item_type::area), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::Area>(osmium::item_type::area), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMObject>(osmium::item_type::area), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Changeset>(osmium::item_type::area), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMEntity>(osmium::item_type::area), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::memory::Item>(osmium::item_type::area), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Node>(osmium::item_type::changeset), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Way>(osmium::item_type::changeset), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Relation>(osmium::item_type::changeset), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::Area>(osmium::item_type::changeset), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::OSMObject>(osmium::item_type::changeset), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::Changeset>(osmium::item_type::changeset), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OSMEntity>(osmium::item_type::changeset), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::memory::Item>(osmium::item_type::changeset), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::OuterRing>(osmium::item_type::outer_ring), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::InnerRing>(osmium::item_type::inner_ring), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::WayNodeList>(osmium::item_type::way_node_list), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::OuterRing>(osmium::item_type::inner_ring), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::InnerRing>(osmium::item_type::outer_ring), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::WayNodeList>(osmium::item_type::outer_ring), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::TagList>(osmium::item_type::tag_list), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::InnerRing>(osmium::item_type::inner_ring), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::TagList>(osmium::item_type::inner_ring), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::InnerRing>(osmium::item_type::tag_list), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::RelationMemberList>(osmium::item_type::relation_member_list), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::RelationMemberList>(osmium::item_type::relation_member_list_with_full_members), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::RelationMemberList>(osmium::item_type::tag_list), "");
static_assert(osmium::memory::detail::type_is_compatible<osmium::ChangesetDiscussion>(osmium::item_type::changeset_discussion), "");
static_assert(!osmium::memory::detail::type_is_compatible<osmium::ChangesetDiscussion>(osmium::item_type::relation_member_list), "");
+86
View File
@@ -0,0 +1,86 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/osm/area.hpp>
#include <osmium/osm/crc.hpp>
#include <string>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Build area") {
osmium::memory::Buffer buffer(10000);
osmium::builder::add_area(buffer,
_id(17),
_version(3),
_visible(),
_cid(333),
_uid(21),
_timestamp(static_cast<std::time_t>(123)),
_user("foo"),
_tag("landuse", "forest"),
_tag("name", "Sherwood Forest"),
_outer_ring({
{1, {3.2, 4.2}},
{2, {3.5, 4.7}},
{3, {3.6, 4.9}},
{1, {3.2, 4.2}}
}),
_inner_ring({
{5, {1.0, 1.0}},
{6, {8.0, 1.0}},
{7, {8.0, 8.0}},
{8, {1.0, 8.0}},
{5, {1.0, 1.0}}
})
);
const osmium::Area& area = buffer.get<osmium::Area>(0);
REQUIRE(17 == area.id());
REQUIRE(3 == area.version());
REQUIRE(area.visible());
REQUIRE(333 == area.changeset());
REQUIRE(21 == area.uid());
REQUIRE(std::string("foo") == area.user());
REQUIRE(123 == uint32_t(area.timestamp()));
REQUIRE(2 == area.tags().size());
int inner = 0;
int outer = 0;
for (const auto& subitem : area) {
switch (subitem.type()) {
case osmium::item_type::outer_ring: {
const auto& ring = static_cast<const osmium::OuterRing&>(subitem);
REQUIRE(ring.size() == 4);
++outer;
}
break;
case osmium::item_type::inner_ring: {
const auto& ring = static_cast<const osmium::OuterRing&>(subitem);
REQUIRE(ring.size() == 5);
++inner;
}
break;
default:
break;
}
}
REQUIRE(outer == 1);
REQUIRE(inner == 1);
osmium::CRC<crc_type> crc32;
crc32.update(area);
REQUIRE(crc32().checksum() == 0x2b2b7fa0);
const osmium::Box envelope = area.envelope();
REQUIRE(envelope.bottom_left().lon() == Approx(3.2));
REQUIRE(envelope.bottom_left().lat() == Approx(4.2));
REQUIRE(envelope.top_right().lon() == Approx(3.6));
REQUIRE(envelope.top_right().lat() == Approx(4.9));
}
+176
View File
@@ -0,0 +1,176 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/geom/relations.hpp>
#include <osmium/osm/box.hpp>
#include <osmium/osm/crc.hpp>
#include <sstream>
TEST_CASE("Default constructor creates invalid box") {
const osmium::Box b{};
REQUIRE_FALSE(b);
REQUIRE_FALSE(b.bottom_left());
REQUIRE_FALSE(b.top_right());
REQUIRE_THROWS_AS(b.size(), osmium::invalid_location);
}
TEST_CASE("Extend box with undefined") {
osmium::Box b{};
REQUIRE_FALSE(b);
b.extend(osmium::Location{});
REQUIRE_FALSE(b);
REQUIRE_FALSE(b.bottom_left());
REQUIRE_FALSE(b.top_right());
}
TEST_CASE("Extend box with invalid") {
osmium::Box b{};
REQUIRE_FALSE(b);
b.extend(osmium::Location{200.0, 100.0});
REQUIRE_FALSE(b);
REQUIRE_FALSE(b.bottom_left());
REQUIRE_FALSE(b.top_right());
}
TEST_CASE("Extend box with valid") {
osmium::Box b{};
const osmium::Location loc1{1.2, 3.4};
b.extend(loc1);
REQUIRE(!!b);
REQUIRE(!!b.bottom_left());
REQUIRE(!!b.top_right());
REQUIRE(b.contains(loc1));
const osmium::Location loc2{3.4, 4.5};
const osmium::Location loc3{5.6, 7.8};
b.extend(loc2);
b.extend(loc3);
REQUIRE(b.bottom_left() == osmium::Location(1.2, 3.4));
REQUIRE(b.top_right() == osmium::Location(5.6, 7.8));
// extend with undefined doesn't change anything
b.extend(osmium::Location{});
REQUIRE(b.bottom_left() == osmium::Location(1.2, 3.4));
REQUIRE(b.top_right() == osmium::Location(5.6, 7.8));
REQUIRE(b.contains(loc1));
REQUIRE(b.contains(loc2));
REQUIRE(b.contains(loc3));
osmium::CRC<crc_type> crc32;
crc32.update(b);
REQUIRE(crc32().checksum() == 0xd381a838);
}
TEST_CASE("Output of defined Box") {
osmium::Box b{};
b.extend(osmium::Location{1.2, 3.4});
b.extend(osmium::Location{5.6, 7.8});
std::stringstream out;
out << b;
REQUIRE(out.str() == "(1.2,3.4,5.6,7.8)");
REQUIRE(b.size() == Approx(19.36).epsilon(0.000001));
}
TEST_CASE("Output of undefined Box") {
const osmium::Box b{};
std::stringstream out;
out << b;
REQUIRE(out.str() == "(undefined)");
}
TEST_CASE("Output of undefined Box (bottom left)") {
osmium::Box b{};
b.top_right() = osmium::Location(1.2, 3.4);
std::stringstream out;
out << b;
REQUIRE(out.str() == "(undefined)");
}
TEST_CASE("Output of undefined Box (top right)") {
osmium::Box b{};
b.bottom_left() = osmium::Location(1.2, 3.4);
std::stringstream out;
out << b;
REQUIRE(out.str() == "(undefined)");
}
TEST_CASE("Create box from locations") {
const osmium::Box b{osmium::Location{1.23, 2.34}, osmium::Location{3.45, 4.56}};
REQUIRE(!!b);
REQUIRE(b.bottom_left() == (osmium::Location{1.23, 2.34}));
REQUIRE(b.top_right() == (osmium::Location{3.45, 4.56}));
REQUIRE(b.top() == Approx(4.56));
REQUIRE(b.bottom() == Approx(2.34));
REQUIRE(b.left() == Approx(1.23));
REQUIRE(b.right() == Approx(3.45));
}
TEST_CASE("Create box from doubles") {
const osmium::Box b{1.23, 2.34, 3.45, 4.56};
REQUIRE(!!b);
REQUIRE(b.bottom_left() == (osmium::Location{1.23, 2.34}));
REQUIRE(b.top_right() == (osmium::Location{3.45, 4.56}));
}
TEST_CASE("Relationship between boxes: contains") {
osmium::Box outer{};
outer.extend(osmium::Location{1, 1});
outer.extend(osmium::Location{10, 10});
osmium::Box inner{};
inner.extend(osmium::Location{2, 2});
inner.extend(osmium::Location{4, 4});
osmium::Box overlap{};
overlap.extend(osmium::Location{3, 3});
overlap.extend(osmium::Location{5, 5});
REQUIRE( osmium::geom::contains(inner, outer));
REQUIRE_FALSE(osmium::geom::contains(outer, inner));
REQUIRE_FALSE(osmium::geom::contains(overlap, inner));
REQUIRE_FALSE(osmium::geom::contains(inner, overlap));
}
TEST_CASE("Relationship between boxes: overlaps") {
osmium::Box outer{};
outer.extend(osmium::Location{1, 1});
outer.extend(osmium::Location{10, 10});
osmium::Box inner{};
inner.extend(osmium::Location{2, 2});
inner.extend(osmium::Location{4, 4});
osmium::Box overlap{};
overlap.extend(osmium::Location{3, 3});
overlap.extend(osmium::Location{5, 5});
const osmium::Box outside{};
overlap.extend(osmium::Location{30, 30});
overlap.extend(osmium::Location{50, 50});
REQUIRE(osmium::geom::overlaps(inner, outer));
REQUIRE(osmium::geom::overlaps(outer, inner));
REQUIRE(osmium::geom::overlaps(overlap, inner));
REQUIRE(osmium::geom::overlaps(inner, overlap));
REQUIRE_FALSE(osmium::geom::overlaps(outside, inner));
REQUIRE_FALSE(osmium::geom::overlaps(inner, outside));
}
+190
View File
@@ -0,0 +1,190 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/osm/changeset.hpp>
#include <osmium/osm/crc.hpp>
#include <string>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
constexpr const std::size_t test_buffer_size = 1024UL * 10UL;
TEST_CASE("Build changeset") {
osmium::memory::Buffer buffer{test_buffer_size};
osmium::builder::add_changeset(buffer,
_cid(42),
_created_at(static_cast<std::time_t>(100)),
_closed_at(static_cast<std::time_t>(200)),
_num_changes(7),
_num_comments(3),
_uid(9),
_user("user"),
_tag("comment", "foo")
);
const osmium::Changeset& cs1 = buffer.get<osmium::Changeset>(0);
REQUIRE(42 == cs1.id());
REQUIRE(9 == cs1.uid());
REQUIRE(7 == cs1.num_changes());
REQUIRE(3 == cs1.num_comments());
REQUIRE(cs1.closed());
REQUIRE(osmium::Timestamp{100} == cs1.created_at());
REQUIRE(osmium::Timestamp{200} == cs1.closed_at());
REQUIRE(1 == cs1.tags().size());
REQUIRE(std::string("user") == cs1.user());
osmium::CRC<crc_type> crc32;
crc32.update(cs1);
REQUIRE(crc32().checksum() == 0x502e8c0e);
const auto pos = osmium::builder::add_changeset(buffer,
_cid(43),
_created_at(static_cast<std::time_t>(120)),
_num_changes(21),
_num_comments(0),
_uid(9),
_user("user"),
_tag("comment", "foo"),
_tag("foo", "bar"),
_comment({static_cast<std::time_t>(300), 10, "user2", "foo"}),
_comments({{static_cast<std::time_t>(400), 9, "user", "bar"}})
);
const osmium::Changeset& cs2 = buffer.get<osmium::Changeset>(pos);
REQUIRE(43 == cs2.id());
REQUIRE(9 == cs2.uid());
REQUIRE(21 == cs2.num_changes());
REQUIRE(0 == cs2.num_comments());
REQUIRE_FALSE(cs2.closed());
REQUIRE(osmium::Timestamp{120} == cs2.created_at());
REQUIRE(osmium::Timestamp{} == cs2.closed_at());
REQUIRE(2 == cs2.tags().size());
REQUIRE(std::string("user") == cs2.user());
REQUIRE(cs1 != cs2);
REQUIRE(cs1 < cs2);
REQUIRE(cs1 <= cs2);
REQUIRE_FALSE(cs1 > cs2);
REQUIRE_FALSE(cs1 >= cs2);
auto cit = cs2.discussion().begin();
REQUIRE(cit != cs2.discussion().end());
REQUIRE(cit->date() == osmium::Timestamp{300});
REQUIRE(cit->uid() == 10);
REQUIRE(std::string("user2") == cit->user());
REQUIRE(std::string("foo") == cit->text());
REQUIRE(++cit != cs2.discussion().end());
REQUIRE(cit->date() == osmium::Timestamp{400});
REQUIRE(cit->uid() == 9);
REQUIRE(std::string("user") == cit->user());
REQUIRE(std::string("bar") == cit->text());
REQUIRE(++cit == cs2.discussion().end());
}
TEST_CASE("Create changeset without helper") {
osmium::memory::Buffer buffer{test_buffer_size};
{
osmium::builder::ChangesetBuilder builder{buffer};
builder.set_id(42)
.set_created_at(100)
.set_closed_at(200)
.set_num_changes(7)
.set_num_comments(2)
.set_uid(9)
.set_user("user");
{
osmium::builder::TagListBuilder tl_builder{builder};
tl_builder.add_tag("key1", "val1");
tl_builder.add_tag("key2", "val2");
}
osmium::builder::ChangesetDiscussionBuilder disc_builder{builder};
disc_builder.add_comment(osmium::Timestamp{300}, 10, "user2");
disc_builder.add_comment_text("foo");
disc_builder.add_comment(osmium::Timestamp{400}, 9, "user");
disc_builder.add_comment_text("bar");
}
const auto& cs = buffer.get<osmium::Changeset>(buffer.commit());
REQUIRE(42 == cs.id());
REQUIRE(9 == cs.uid());
REQUIRE(7 == cs.num_changes());
REQUIRE(2 == cs.num_comments());
REQUIRE(cs.closed());
REQUIRE(osmium::Timestamp{100} == cs.created_at());
REQUIRE(osmium::Timestamp{200} == cs.closed_at());
REQUIRE(2 == cs.tags().size());
REQUIRE(std::string("user") == cs.user());
auto cit = cs.discussion().begin();
REQUIRE(cit != cs.discussion().end());
REQUIRE(cit->date() == osmium::Timestamp{300});
REQUIRE(cit->uid() == 10);
REQUIRE(std::string("user2") == cit->user());
REQUIRE(std::string("foo") == cit->text());
REQUIRE(++cit != cs.discussion().end());
REQUIRE(cit->date() == osmium::Timestamp{400});
REQUIRE(cit->uid() == 9);
REQUIRE(std::string("user") == cit->user());
REQUIRE(std::string("bar") == cit->text());
REQUIRE(++cit == cs.discussion().end());
}
TEST_CASE("Change changeset") {
osmium::memory::Buffer buffer{test_buffer_size};
osmium::builder::add_changeset(buffer,
_cid(42),
_created_at(static_cast<std::time_t>(100)),
_closed_at(static_cast<std::time_t>(200)),
_num_changes(7),
_num_comments(3),
_uid(9),
_user("user"),
_tag("comment", "foo")
);
auto& cs = buffer.get<osmium::Changeset>(0);
cs.set_id(12);
cs.set_created_at(static_cast<std::time_t>(200));
cs.set_closed_at(static_cast<std::time_t>(300));
cs.set_num_changes(3);
cs.set_num_comments(4);
cs.set_uid(10);
cs.clear_user();
REQUIRE(12 == cs.id());
REQUIRE(10 == cs.uid());
REQUIRE(3 == cs.num_changes());
REQUIRE(4 == cs.num_comments());
REQUIRE(cs.closed());
REQUIRE(osmium::Timestamp{200} == cs.created_at());
REQUIRE(osmium::Timestamp{300} == cs.closed_at());
REQUIRE(1 == cs.tags().size());
REQUIRE(std::string("") == cs.user());
REQUIRE(cs.tags().size() == 1);
auto it = cs.tags().begin();
REQUIRE(it->key() == std::string{"comment"});
REQUIRE(it->value() == std::string{"foo"});
++it;
REQUIRE(it == cs.tags().end());
}
+78
View File
@@ -0,0 +1,78 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/osm/crc.hpp>
TEST_CASE("CRC of bool") {
osmium::CRC<crc_type> crc32;
crc32.update_bool(true);
crc32.update_bool(false);
REQUIRE(crc32().checksum() == 0x58c223be);
}
TEST_CASE("CRC of char") {
osmium::CRC<crc_type> crc32;
crc32.update_int8('x');
crc32.update_int8('y');
REQUIRE(crc32().checksum() == 0x8fe62899);
}
TEST_CASE("CRC of int16") {
osmium::CRC<crc_type> crc32;
crc32.update_int16(0x0123U);
crc32.update_int16(0x1234U);
REQUIRE(crc32().checksum() == 0xda923744);
}
TEST_CASE("CRC of int32") {
osmium::CRC<crc_type> crc32;
crc32.update_int32(0x01234567UL);
crc32.update_int32(0x12345678UL);
REQUIRE(crc32().checksum() == 0x9b4e2af3);
}
TEST_CASE("CRC of int64") {
osmium::CRC<crc_type> crc32;
crc32.update_int64(0x0123456789abcdefULL);
crc32.update_int64(0x123456789abcdef0ULL);
REQUIRE(crc32().checksum() == 0x6d8b7267);
}
TEST_CASE("CRC of string") {
osmium::CRC<crc_type> crc32;
const char* str = "foobar";
crc32.update_string(str);
REQUIRE(crc32().checksum() == 0x9ef61f95);
}
TEST_CASE("CRC of Timestamp") {
osmium::CRC<crc_type> crc32;
const osmium::Timestamp t{"2015-07-12T13:10:46Z"};
crc32.update(t);
REQUIRE(crc32().checksum() == 0x58a29d7);
}
TEST_CASE("CRC of Location") {
osmium::CRC<crc_type> crc32;
const osmium::Location loc{3.46, 2.001};
crc32.update(loc);
REQUIRE(crc32().checksum() == 0xddee042c);
}
+62
View File
@@ -0,0 +1,62 @@
#include "catch.hpp"
#include <osmium/osm/entity_bits.hpp>
static_assert((osmium::osm_entity_bits::node
|osmium::osm_entity_bits::way
|osmium::osm_entity_bits::relation)
== osmium::osm_entity_bits::nwr, "entity_bits nwr failed");
static_assert((osmium::osm_entity_bits::node
|osmium::osm_entity_bits::way
|osmium::osm_entity_bits::relation
|osmium::osm_entity_bits::area)
== osmium::osm_entity_bits::nwra, "entity_bits nwra failed");
static_assert((osmium::osm_entity_bits::nwra
|osmium::osm_entity_bits::changeset)
== osmium::osm_entity_bits::all, "entity_bits all failed");
static_assert((osmium::osm_entity_bits::all
&osmium::osm_entity_bits::node)
== osmium::osm_entity_bits::node, "entity_bits node failed");
static_assert((~osmium::osm_entity_bits::all) == osmium::osm_entity_bits::nothing, "entity_bits nothing is the inverse of all");
static_assert((~osmium::osm_entity_bits::nothing) == osmium::osm_entity_bits::all, "entity_bits all is the inverse of nothing");
static_assert((~osmium::osm_entity_bits::changeset) == osmium::osm_entity_bits::nwra, "entity_bits nwra is the inverse of changeset");
TEST_CASE("Bitwise 'and' and 'or' on entity bits") {
osmium::osm_entity_bits::type entities = osmium::osm_entity_bits::node | osmium::osm_entity_bits::way;
REQUIRE(entities == (osmium::osm_entity_bits::node | osmium::osm_entity_bits::way));
entities |= osmium::osm_entity_bits::relation;
REQUIRE((entities & osmium::osm_entity_bits::object));
entities |= osmium::osm_entity_bits::area;
REQUIRE(entities == osmium::osm_entity_bits::object);
REQUIRE_FALSE((entities & osmium::osm_entity_bits::changeset));
entities &= osmium::osm_entity_bits::node;
REQUIRE((entities & osmium::osm_entity_bits::node));
REQUIRE_FALSE((entities & osmium::osm_entity_bits::way));
REQUIRE(entities == osmium::osm_entity_bits::node);
}
TEST_CASE("Bitwise 'not' on entity bits") {
REQUIRE(~osmium::osm_entity_bits::all == osmium::osm_entity_bits::nothing);
REQUIRE(~osmium::osm_entity_bits::nothing == osmium::osm_entity_bits::all);
REQUIRE(~osmium::osm_entity_bits::node == (osmium::osm_entity_bits::way | osmium::osm_entity_bits::relation | osmium::osm_entity_bits::area | osmium::osm_entity_bits::changeset));
REQUIRE(~osmium::osm_entity_bits::nwr == (osmium::osm_entity_bits::area | osmium::osm_entity_bits::changeset));
REQUIRE(~osmium::osm_entity_bits::nwra == osmium::osm_entity_bits::changeset);
}
TEST_CASE("Converting item types to entity bits") {
REQUIRE(osmium::osm_entity_bits::nothing == osmium::osm_entity_bits::from_item_type(osmium::item_type::undefined));
REQUIRE(osmium::osm_entity_bits::node == osmium::osm_entity_bits::from_item_type(osmium::item_type::node));
REQUIRE(osmium::osm_entity_bits::way == osmium::osm_entity_bits::from_item_type(osmium::item_type::way));
REQUIRE(osmium::osm_entity_bits::relation == osmium::osm_entity_bits::from_item_type(osmium::item_type::relation));
REQUIRE(osmium::osm_entity_bits::changeset == osmium::osm_entity_bits::from_item_type(osmium::item_type::changeset));
REQUIRE(osmium::osm_entity_bits::area == osmium::osm_entity_bits::from_item_type(osmium::item_type::area));
}
+432
View File
@@ -0,0 +1,432 @@
#include "catch.hpp"
#include <osmium/osm/location.hpp>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
// fails on MSVC and doesn't really matter
// static_assert(std::is_literal_type<osmium::Location>::value, "osmium::Location not literal type");
TEST_CASE("Location instantiation with default parameters") {
const osmium::Location loc;
REQUIRE_FALSE(loc);
REQUIRE_FALSE(loc.is_defined());
REQUIRE(loc.is_undefined());
REQUIRE_THROWS_AS(loc.lon(), osmium::invalid_location);
REQUIRE_THROWS_AS(loc.lat(), osmium::invalid_location);
}
TEST_CASE("Location instantiation with double parameters") {
const osmium::Location loc1{1.2, 4.5};
REQUIRE(bool(loc1));
REQUIRE(loc1.is_defined());
REQUIRE_FALSE(loc1.is_undefined());
REQUIRE(12000000 == loc1.x());
REQUIRE(45000000 == loc1.y());
REQUIRE(1.2 == Approx(loc1.lon()));
REQUIRE(4.5 == Approx(loc1.lat()));
const osmium::Location loc2{loc1};
REQUIRE(4.5 == Approx(loc2.lat()));
const osmium::Location loc3 = loc1;
REQUIRE(4.5 == Approx(loc3.lat()));
}
TEST_CASE("Location instantiation with double parameters constructor with universal initializer") {
const osmium::Location loc{2.2, 3.3};
REQUIRE(2.2 == Approx(loc.lon()));
REQUIRE(3.3 == Approx(loc.lat()));
}
TEST_CASE("Location instantiation with double parameters constructor with initializer list") {
const osmium::Location loc({4.4, 5.5});
REQUIRE(4.4 == Approx(loc.lon()));
REQUIRE(5.5 == Approx(loc.lat()));
}
TEST_CASE("Location instantiation with double parameters operator equal") {
const osmium::Location loc = {5.5, 6.6};
REQUIRE(5.5 == Approx(loc.lon()));
REQUIRE(6.6 == Approx(loc.lat()));
}
TEST_CASE("Location equality") {
const osmium::Location loc1{1.2, 4.5};
const osmium::Location loc2{1.2, 4.5};
const osmium::Location loc3{1.5, 1.5};
REQUIRE(loc1 == loc2);
REQUIRE(loc1 != loc3);
}
TEST_CASE("Location order") {
REQUIRE(osmium::Location(-1.2, 10.0) < osmium::Location(1.2, 10.0));
REQUIRE(osmium::Location(1.2, 10.0) > osmium::Location(-1.2, 10.0));
REQUIRE(osmium::Location(10.2, 20.0) < osmium::Location(11.2, 20.2));
REQUIRE(osmium::Location(10.2, 20.2) < osmium::Location(11.2, 20.0));
REQUIRE(osmium::Location(11.2, 20.2) > osmium::Location(10.2, 20.0));
}
TEST_CASE("Location validity") {
REQUIRE(osmium::Location(0.0, 0.0).valid());
REQUIRE(osmium::Location(1.2, 4.5).valid());
REQUIRE(osmium::Location(-1.2, 4.5).valid());
REQUIRE(osmium::Location(-180.0, -90.0).valid());
REQUIRE(osmium::Location(180.0, -90.0).valid());
REQUIRE(osmium::Location(-180.0, 90.0).valid());
REQUIRE(osmium::Location(180.0, 90.0).valid());
REQUIRE_FALSE(osmium::Location(200.0, 4.5).valid());
REQUIRE_FALSE(osmium::Location(-1.2, -100.0).valid());
REQUIRE_FALSE(osmium::Location(-180.0, 90.005).valid());
}
TEST_CASE("Location output to iterator comma separator") {
char buffer[100];
const osmium::Location loc{-3.2, 47.3};
*loc.as_string(buffer, ',') = 0;
REQUIRE(std::string("-3.2,47.3") == buffer);
}
TEST_CASE("Location output to iterator space separator") {
char buffer[100];
const osmium::Location loc{0.0, 7.0};
*loc.as_string(buffer, ' ') = 0;
REQUIRE(std::string("0 7") == buffer);
}
TEST_CASE("Location output to iterator check precision") {
char buffer[100];
const osmium::Location loc{-179.9999999, -90.0};
*loc.as_string(buffer, ' ') = 0;
REQUIRE(std::string("-179.9999999 -90") == buffer);
}
TEST_CASE("Location output to iterator undefined location") {
char buffer[100];
const osmium::Location loc;
REQUIRE_THROWS_AS(loc.as_string(buffer, ','), osmium::invalid_location);
}
TEST_CASE("Location output to string comman separator") {
std::string s;
const osmium::Location loc{-3.2, 47.3};
loc.as_string(std::back_inserter(s), ',');
REQUIRE(s == "-3.2,47.3");
}
TEST_CASE("Location output to string space separator") {
std::string s;
const osmium::Location loc{0.0, 7.0};
loc.as_string(std::back_inserter(s), ' ');
REQUIRE(s == "0 7");
}
TEST_CASE("Location output to string check precision") {
std::string s;
const osmium::Location loc{-179.9999999, -90.0};
loc.as_string(std::back_inserter(s), ' ');
REQUIRE(s == "-179.9999999 -90");
}
TEST_CASE("Location output to string undefined location") {
std::string s;
const osmium::Location loc;
REQUIRE_THROWS_AS(loc.as_string(std::back_inserter(s), ','), osmium::invalid_location);
}
TEST_CASE("Location output defined") {
const osmium::Location loc{-3.20, 47.30};
std::stringstream out;
out << loc;
REQUIRE(out.str() == "(-3.2,47.3)");
}
TEST_CASE("Location output undefined") {
const osmium::Location loc;
std::stringstream out;
out << loc;
REQUIRE(out.str() == "(undefined,undefined)");
}
TEST_CASE("Location hash") {
if (sizeof(size_t) == 8) {
REQUIRE(std::hash<osmium::Location>{}({0, 0}) == 0);
REQUIRE(std::hash<osmium::Location>{}({0, 1}) == 1);
const int64_t a = std::hash<osmium::Location>{}({1, 0});
REQUIRE(a == 0x100000000);
const int64_t b = std::hash<osmium::Location>{}({1, 1});
REQUIRE(b == 0x100000001);
} else {
REQUIRE(std::hash<osmium::Location>{}({0, 0}) == 0);
REQUIRE(std::hash<osmium::Location>{}({0, 1}) == 1);
REQUIRE(std::hash<osmium::Location>{}({1, 0}) == 1);
REQUIRE(std::hash<osmium::Location>{}({1, 1}) == 0);
}
}
void C(const char* s, int32_t v, const char* r = "") {
std::string strm{"-"};
strm += s;
REQUIRE(std::atof(strm.c_str() + 1) == Approx( v / 10000000.0).scale(1.0)); // NOLINT(cert-err34-c)
REQUIRE(std::atof(strm.c_str() ) == Approx(-v / 10000000.0).scale(1.0)); // NOLINT(cert-err34-c)
const char* x = strm.c_str() + 1;
const char** data = &x;
REQUIRE(osmium::detail::string_to_location_coordinate(data) == v);
REQUIRE(std::string{*data} == r);
x = strm.c_str();
data = &x;
REQUIRE(osmium::detail::string_to_location_coordinate(data) == -v);
REQUIRE(std::string{*data} == r);
}
void F(const char* s) {
std::string strm{"-"};
strm += s;
const char* x = strm.c_str();
const char** data = &x;
REQUIRE_THROWS_AS(osmium::detail::string_to_location_coordinate(data), osmium::invalid_location);
++x;
data = &x;
REQUIRE_THROWS_AS(osmium::detail::string_to_location_coordinate(data), osmium::invalid_location);
}
TEST_CASE("Parsing coordinates from strings") {
F("x");
F(".");
F(".e2");
F("--");
F("");
F(" ");
F(" 123");
C("123 ", 1230000000, " ");
C("123x", 1230000000, "x");
C("1.2x", 12000000, "x");
C("0", 0);
C("1", 10000000);
C("2", 20000000);
C("9", 90000000);
C("10", 100000000);
C("11", 110000000);
C("90", 900000000);
C("100", 1000000000);
C("101", 1010000000);
C("00", 0);
C("01", 10000000);
C("001", 10000000);
C("0001", 10000000);
F("1234");
F("1234.");
F("12345678901234567890");
F("1.1234568111111111111111111111111111111");
F("112.34568111111111111111111111111111111");
C("0.", 0);
C(".0", 0);
C("0.0", 0);
C("1.", 10000000);
C("1.0", 10000000);
C("1.2", 12000000);
C("0.1", 1000000);
C(".1", 1000000);
C("0.01", 100000);
C(".01", 100000);
C("0.001", 10000);
C("0.0001", 1000);
C("0.00001", 100);
C("0.000001", 10);
C("0.0000001", 1);
C("1.1234567", 11234567);
C("1.12345670", 11234567);
C("1.12345674", 11234567);
C("1.123456751", 11234568);
C("1.12345679", 11234568);
C("1.12345680", 11234568);
C("1.12345681", 11234568);
C("180.0000000", 1800000000);
C("180.0000001", 1800000001);
C("179.9999999", 1799999999);
C("179.99999999", 1800000000);
C("200.123", 2001230000);
C("214.7483647", 2147483647);
C("8.109E-4" , 8109);
C("8.1090E-4" , 8109);
C("8.10909E-4" , 8109);
C("8.109095E-4" , 8109);
C("8.1090959E-4" , 8109);
C("8.10909598E-4" , 8109);
C("8.109095988E-4" , 8109);
C("8.1090959887E-4" , 8109);
C("8.10909598870E-4" , 8109);
C("8.109095988709E-4" , 8109);
C("8.1090959887098E-4" , 8109);
C("8.10909598870983E-4" , 8109);
C("8.109095988709837E-4" , 8109);
C("81.09095988709837E-4" , 81091);
C("810.9095988709837E-4" , 810910);
C(".8109095988709837E-4" , 811);
C(".08109095988709837E-4" , 81);
C("1e2", 1000000000);
C("1e1", 100000000);
C("1e0", 10000000);
C("1e-1", 1000000);
C("1e-2", 100000);
C("1e-3", 10000);
C("1e-4", 1000);
C("1e-5", 100);
C("1e-6", 10);
C("1e-7", 1);
C("1.0e2", 1000000000);
C("1.e2", 1000000000);
C("1.1e1", 110000000);
C("0.1e1", 10000000);
C(".1e1", 10000000);
C("1.2e0", 12000000);
C("1.9e-1", 1900000);
C("2.0e-2", 200000);
C("2.1e-3", 21000);
C("9.0e-4", 9000);
C("9.1e-5", 910);
C("1.0e-6", 10);
C("1.0e-7", 1);
C("1.4e-7", 1);
C("1.5e-7", 2);
C("1.9e-7", 2);
C("0.5e-7", 1);
C("0.1e-7", 0);
C("0.0e-7", 0);
C("1.9e-8", 0);
C("1.9e-9", 0);
C("1.9e-10", 0);
F("e");
F(" e");
F(" 1.1e2");
F("1.0e3");
F("5e4");
F("5.0e2");
F("3e2");
F("1e");
F("1e-");
F("1e1234567");
F("0.5e");
F("1e10");
C("1e2 ", 1000000000, " ");
C("1.1e2 ", 1100000000, " ");
C("1.1e2x", 1100000000, "x");
C("1.1e2:", 1100000000, ":");
}
TEST_CASE("Parsing min coordinate from string") {
const char* minval = "-214.7483648";
const char** data = &minval;
REQUIRE(osmium::detail::string_to_location_coordinate(data) == std::numeric_limits<int32_t>::min());
}
TEST_CASE("Writing zero coordinate into string") {
std::string buffer;
osmium::detail::append_location_coordinate_to_string(std::back_inserter(buffer), 0);
REQUIRE(buffer == "0");
}
void CW(int32_t v, const char* s) {
std::string buffer;
osmium::detail::append_location_coordinate_to_string(std::back_inserter(buffer), v);
REQUIRE(buffer == s);
buffer.clear();
osmium::detail::append_location_coordinate_to_string(std::back_inserter(buffer), -v);
REQUIRE(buffer[0] == '-');
REQUIRE_FALSE(std::strcmp(buffer.c_str() + 1, s));
}
TEST_CASE("Writing coordinate into string") {
CW( 10000000, "1");
CW( 90000000, "9");
CW( 100000000, "10");
CW(1000000000, "100");
CW(2000000000, "200");
CW( 1000000, "0.1");
CW( 100000, "0.01");
CW( 10000, "0.001");
CW( 1000, "0.0001");
CW( 100, "0.00001");
CW( 10, "0.000001");
CW( 1, "0.0000001");
CW( 1230000, "0.123");
CW( 9999999, "0.9999999");
CW( 40101010, "4.010101");
CW( 494561234, "49.4561234");
CW(1799999999, "179.9999999");
CW(2147483647, "214.7483647");
}
TEST_CASE("Writing min coordinate into string") {
std::string buffer;
osmium::detail::append_location_coordinate_to_string(std::back_inserter(buffer), std::numeric_limits<int32_t>::min());
REQUIRE(buffer == "-214.7483648");
}
TEST_CASE("set lon/lat from string") {
osmium::Location loc;
REQUIRE(loc.is_undefined());
REQUIRE_FALSE(loc.is_defined());
REQUIRE_FALSE(loc.valid());
loc.set_lon("1.2");
REQUIRE_FALSE(loc.is_undefined());
REQUIRE(loc.is_defined());
REQUIRE_FALSE(loc.valid());
loc.set_lat("3.4");
REQUIRE_FALSE(loc.is_undefined());
REQUIRE(loc.is_defined());
REQUIRE(loc.valid());
REQUIRE(loc.lon() == Approx(1.2));
REQUIRE(loc.lat() == Approx(3.4));
}
TEST_CASE("set lon/lat from string with trailing characters") {
osmium::Location loc;
REQUIRE_THROWS_AS(loc.set_lon("1.2x"), osmium::invalid_location);
REQUIRE_THROWS_AS(loc.set_lat("3.4e1 "), osmium::invalid_location);
}
TEST_CASE("set lon/lat from string with trailing characters using partial") {
osmium::Location loc;
const char* x = "1.2x";
const char* y = "3.4 ";
loc.set_lon_partial(&x);
loc.set_lat_partial(&y);
REQUIRE(loc.lon() == Approx(1.2));
REQUIRE(loc.lat() == Approx(3.4));
REQUIRE(*x == 'x');
REQUIRE(*y == ' ');
}
+175
View File
@@ -0,0 +1,175 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/metadata_options.hpp>
#include <osmium/osm/object.hpp>
#include <stdexcept>
TEST_CASE("Metadata options: default") {
const osmium::metadata_options m{};
REQUIRE_FALSE(m.none());
REQUIRE(m.any());
REQUIRE(m.all());
REQUIRE(m.version());
REQUIRE(m.timestamp());
REQUIRE(m.changeset());
REQUIRE(m.uid());
REQUIRE(m.user());
}
TEST_CASE("Metadata options: false") {
const osmium::metadata_options m{"false"};
REQUIRE(m.none());
REQUIRE_FALSE(m.any());
REQUIRE_FALSE(m.all());
REQUIRE_FALSE(m.version());
REQUIRE_FALSE(m.timestamp());
REQUIRE_FALSE(m.changeset());
REQUIRE_FALSE(m.uid());
REQUIRE_FALSE(m.user());
}
TEST_CASE("Metadata options: none") {
const osmium::metadata_options m{"none"};
REQUIRE(m.none());
REQUIRE_FALSE(m.any());
REQUIRE_FALSE(m.all());
REQUIRE_FALSE(m.version());
REQUIRE_FALSE(m.timestamp());
REQUIRE_FALSE(m.changeset());
REQUIRE_FALSE(m.uid());
REQUIRE_FALSE(m.user());
}
TEST_CASE("Metadata options: true") {
const osmium::metadata_options m{"true"};
REQUIRE_FALSE(m.none());
REQUIRE(m.any());
REQUIRE(m.all());
REQUIRE(m.version());
REQUIRE(m.timestamp());
REQUIRE(m.changeset());
REQUIRE(m.uid());
REQUIRE(m.user());
}
TEST_CASE("Metadata options: all") {
const osmium::metadata_options m{"all"};
REQUIRE_FALSE(m.none());
REQUIRE(m.any());
REQUIRE(m.all());
REQUIRE(m.version());
REQUIRE(m.timestamp());
REQUIRE(m.changeset());
REQUIRE(m.uid());
REQUIRE(m.user());
}
TEST_CASE("Metadata options: version,changeset") {
const osmium::metadata_options m{"version+changeset"};
REQUIRE_FALSE(m.none());
REQUIRE(m.any());
REQUIRE_FALSE(m.all());
REQUIRE(m.version());
REQUIRE_FALSE(m.timestamp());
REQUIRE(m.changeset());
REQUIRE_FALSE(m.uid());
REQUIRE_FALSE(m.user());
}
TEST_CASE("Metadata options: timestamp,uid,user") {
const osmium::metadata_options m{"timestamp+uid+user"};
REQUIRE_FALSE(m.none());
REQUIRE(m.any());
REQUIRE_FALSE(m.all());
REQUIRE_FALSE(m.version());
REQUIRE(m.timestamp());
REQUIRE_FALSE(m.changeset());
REQUIRE(m.uid());
REQUIRE(m.user());
}
TEST_CASE("Metadata options: fail") {
REQUIRE_THROWS_AS(osmium::metadata_options{"timestamp+foo"}, std::invalid_argument);
}
TEST_CASE("Metdata options: constructor using OSMObject") {
osmium::memory::Buffer buffer{10UL * 1024UL};
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
SECTION("only version") {
const osmium::OSMObject& obj = buffer.get<osmium::OSMObject>(osmium::builder::add_node(buffer,
_id(1),
_version(2)));
const osmium::metadata_options options = osmium::detect_available_metadata(obj);
REQUIRE_FALSE(options.user());
REQUIRE_FALSE(options.uid());
REQUIRE_FALSE(options.changeset());
REQUIRE_FALSE(options.timestamp());
REQUIRE(options.version());
}
SECTION("full") {
const osmium::OSMObject& obj = buffer.get<osmium::OSMObject>(osmium::builder::add_node(buffer,
_id(1),
_version(2),
_timestamp("2018-01-01T23:00:00Z"),
_cid(30),
_uid(8),
_user("foo")));
const osmium::metadata_options options = osmium::detect_available_metadata(obj);
REQUIRE(options.all());
}
SECTION("changeset+timestamp+version") {
const osmium::OSMObject& obj = buffer.get<osmium::OSMObject>(osmium::builder::add_node(buffer,
_id(1),
_version(2),
_timestamp("2018-01-01T23:00:00Z"),
_cid(30)));
const osmium::metadata_options options = osmium::detect_available_metadata(obj);
REQUIRE(options.version());
REQUIRE(options.timestamp());
REQUIRE(options.changeset());
REQUIRE_FALSE(options.user());
REQUIRE_FALSE(options.uid());
}
}
TEST_CASE("Metdata options: string representation should be valid 'version+changeset'") {
const osmium::metadata_options options{"version+changeset"};
REQUIRE(options.to_string() == "version+changeset");
}
TEST_CASE("Metdata options: string representation should be valid 'version+uid+user'") {
const osmium::metadata_options options{"version+uid+user"};
REQUIRE(options.to_string() == "version+uid+user");
}
TEST_CASE("Metdata options: string representation should be valid 'version+timestamp'") {
const osmium::metadata_options options{"version+timestamp"};
REQUIRE(options.to_string() == "version+timestamp");
}
TEST_CASE("Metdata options: string representation should be valid 'timestamp+version (different order'") {
const osmium::metadata_options options{"timestamp+version"};
REQUIRE(options.to_string() == "version+timestamp");
}
TEST_CASE("Metdata options: string representation should be valid 'none'") {
const osmium::metadata_options options{"none"};
REQUIRE(options.to_string() == "none");
}
TEST_CASE("Metdata options: string representation should be valid 'all (short)'") {
const osmium::metadata_options options{"all"};
REQUIRE(options.to_string() == "all");
}
TEST_CASE("Metdata options: string representation should be valid 'all (long)'") {
const osmium::metadata_options options{"user+uid+version+timestamp+changeset"};
REQUIRE(options.to_string() == "all");
}
+194
View File
@@ -0,0 +1,194 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/osm/crc.hpp>
#include <osmium/osm/node.hpp>
#include <string>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Build node") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_node(buffer,
_id(17),
_version(3),
_visible(true),
_cid(333),
_uid(21),
_timestamp(static_cast<std::time_t>(123)),
_user("foo"),
_tag("amenity", "pub"),
_tag("name", "OSM BAR"),
_location(3.5, 4.7)
);
auto& node = buffer.get<osmium::Node>(0);
REQUIRE(osmium::item_type::node == node.type());
REQUIRE(node.type_is_in(osmium::osm_entity_bits::node));
REQUIRE(node.type_is_in(osmium::osm_entity_bits::nwr));
REQUIRE(17L == node.id());
REQUIRE(17UL == node.positive_id());
REQUIRE(3 == node.version());
REQUIRE(node.visible());
REQUIRE_FALSE(node.deleted());
REQUIRE(333 == node.changeset());
REQUIRE(21 == node.uid());
REQUIRE(std::string{"foo"} == node.user());
REQUIRE(123 == uint32_t(node.timestamp()));
REQUIRE(osmium::Location(3.5, 4.7) == node.location());
REQUIRE(2 == node.tags().size());
osmium::CRC<crc_type> crc32;
crc32.update(node);
REQUIRE(crc32().checksum() == 0x7dc553f9);
node.set_visible(false);
REQUIRE_FALSE(node.visible());
REQUIRE(node.deleted());
node.remove_tags();
REQUIRE(node.tags().empty());
}
TEST_CASE("default values for node attributes") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_node(buffer, _id(0));
const osmium::Node& node = buffer.get<osmium::Node>(0);
REQUIRE(0L == node.id());
REQUIRE(0UL == node.positive_id());
REQUIRE(0 == node.version());
REQUIRE(node.visible());
REQUIRE(0 == node.changeset());
REQUIRE(0 == node.uid());
REQUIRE(std::string{} == node.user());
REQUIRE(0 == uint32_t(node.timestamp()));
REQUIRE(osmium::Location() == node.location());
REQUIRE(node.tags().empty());
}
TEST_CASE("set node attributes from strings") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_node(buffer, _id(0));
auto& node = buffer.get<osmium::Node>(0);
node.set_id("-17")
.set_version("3")
.set_visible("true")
.set_changeset("333")
.set_timestamp("2014-03-17T16:23:08Z")
.set_uid("21");
REQUIRE(-17L == node.id());
REQUIRE(17UL == node.positive_id());
REQUIRE(3 == node.version());
REQUIRE(node.visible());
REQUIRE(333 == node.changeset());
REQUIRE(std::string{"2014-03-17T16:23:08Z"} == node.timestamp().to_iso());
REQUIRE(21 == node.uid());
}
TEST_CASE("set node attributes from strings using set_attribute()") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_node(buffer, _id(0));
auto& node = buffer.get<osmium::Node>(0);
node.set_attribute("id", "-17")
.set_attribute("version", "3")
.set_attribute("visible", "true")
.set_attribute("changeset", "333")
.set_attribute("timestamp", "2014-03-17T16:23:08Z")
.set_attribute("uid", "21");
REQUIRE(-17L == node.id());
REQUIRE(17UL == node.positive_id());
REQUIRE(3 == node.version());
REQUIRE(node.visible());
REQUIRE(333 == node.changeset());
REQUIRE(std::string{"2014-03-17T16:23:08Z"} == node.timestamp().to_iso());
REQUIRE(21 == node.uid());
}
TEST_CASE("Setting attributes from bad data on strings should fail") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_node(buffer, _id(0));
auto& node = buffer.get<osmium::Node>(0);
REQUIRE_THROWS(node.set_id("bar"));
REQUIRE_THROWS(node.set_id("123x"));
REQUIRE_THROWS(node.set_version("123x"));
REQUIRE_THROWS(node.set_visible("foo"));
REQUIRE_THROWS(node.set_changeset("123x"));
REQUIRE_THROWS(node.set_changeset("NULL"));
REQUIRE_THROWS(node.set_timestamp("2014-03-17T16:23:08Zx"));
REQUIRE_THROWS(node.set_timestamp("2014-03-17T16:23:99Z"));
REQUIRE_THROWS(node.set_uid("123x"));
REQUIRE_THROWS(node.set_uid("anonymous"));
}
TEST_CASE("set large id") {
osmium::memory::Buffer buffer{10000};
const int64_t id = 3000000000L;
osmium::builder::add_node(buffer, _id(id));
auto& node = buffer.get<osmium::Node>(0);
REQUIRE(id == node.id());
REQUIRE(static_cast<osmium::unsigned_object_id_type>(id) == node.positive_id());
node.set_id(-id);
REQUIRE(-id == node.id());
REQUIRE(static_cast<osmium::unsigned_object_id_type>(id) == node.positive_id());
}
TEST_CASE("set tags on node") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_node(buffer,
_user("foo"),
_tag("amenity", "pub"),
_tag("name", "OSM BAR")
);
const osmium::Node& node = buffer.get<osmium::Node>(0);
REQUIRE(nullptr == node.tags().get_value_by_key("fail"));
REQUIRE(std::string{"pub"} == node.tags().get_value_by_key("amenity"));
REQUIRE(std::string{"pub"} == node.get_value_by_key("amenity"));
REQUIRE(std::string{"default"} == node.tags().get_value_by_key("fail", "default"));
REQUIRE(std::string{"pub"} == node.tags().get_value_by_key("amenity", "default"));
REQUIRE(std::string{"pub"} == node.get_value_by_key("amenity", "default"));
}
TEST_CASE("Setting diff flags on node") {
osmium::memory::Buffer buffer{1000};
osmium::builder::add_node(buffer, _id(17));
auto& node = buffer.get<osmium::Node>(0);
REQUIRE(node.diff() == osmium::diff_indicator_type::none);
REQUIRE(node.diff_as_char() == '*');
node.set_diff(osmium::diff_indicator_type::left);
REQUIRE(node.diff() == osmium::diff_indicator_type::left);
REQUIRE(node.diff_as_char() == '-');
node.set_diff(osmium::diff_indicator_type::right);
REQUIRE(node.diff() == osmium::diff_indicator_type::right);
REQUIRE(node.diff_as_char() == '+');
node.set_diff(osmium::diff_indicator_type::both);
REQUIRE(node.diff() == osmium::diff_indicator_type::both);
REQUIRE(node.diff_as_char() == ' ');
}
+126
View File
@@ -0,0 +1,126 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/osm/node_ref.hpp>
#include <osmium/osm/node_ref_list.hpp>
TEST_CASE("Default construct a NodeRef") {
const osmium::NodeRef node_ref;
REQUIRE(node_ref.ref() == 0);
REQUIRE(node_ref.location() == osmium::Location{});
}
TEST_CASE("Construct a NodeRef with an id") {
const osmium::NodeRef node_ref{7};
REQUIRE(node_ref.ref() == 7);
}
TEST_CASE("Equality comparison fo NodeRefs") {
const osmium::NodeRef node_ref1{7, {1.2, 3.4}};
const osmium::NodeRef node_ref2{7, {1.4, 3.1}};
const osmium::NodeRef node_ref3{9, {1.2, 3.4}};
REQUIRE(node_ref1 == node_ref2);
REQUIRE(node_ref1 != node_ref3);
REQUIRE_FALSE(osmium::location_equal()(node_ref1, node_ref2));
REQUIRE_FALSE(osmium::location_equal()(node_ref2, node_ref3));
REQUIRE( osmium::location_equal()(node_ref1, node_ref3));
}
TEST_CASE("Set location on a NodeRef") {
osmium::NodeRef node_ref{7};
REQUIRE_FALSE(node_ref.location().valid());
REQUIRE(node_ref.location() == osmium::Location());
node_ref.set_location(osmium::Location(13.5, -7.2));
REQUIRE(node_ref.location().lon() == 13.5);
REQUIRE(node_ref.location().valid());
}
TEST_CASE("Ordering of NodeRefs") {
const osmium::NodeRef node_ref1{1, {1.0, 3.0}};
const osmium::NodeRef node_ref2{2, {1.4, 2.9}};
const osmium::NodeRef node_ref3{3, {1.2, 3.0}};
const osmium::NodeRef node_ref4{4, {1.2, 3.3}};
REQUIRE(node_ref1 < node_ref2);
REQUIRE(node_ref2 < node_ref3);
REQUIRE(node_ref1 < node_ref3);
REQUIRE(node_ref1 >= node_ref1);
REQUIRE(osmium::location_less()(node_ref1, node_ref2));
REQUIRE_FALSE(osmium::location_less()(node_ref2, node_ref3));
REQUIRE(osmium::location_less()(node_ref1, node_ref3));
REQUIRE(osmium::location_less()(node_ref3, node_ref4));
REQUIRE_FALSE(osmium::location_less()(node_ref1, node_ref1));
}
TEST_CASE("WayNodeList") {
osmium::memory::Buffer buffer{1024};
SECTION("Empty list") {
{
const osmium::builder::WayNodeListBuilder builder{buffer};
}
REQUIRE(buffer.commit() == 0);
REQUIRE(buffer.committed() > 0);
const osmium::WayNodeList& nrl = buffer.get<osmium::WayNodeList>(0);
REQUIRE(nrl.empty());
REQUIRE(nrl.size() == 0); // NOLINT(readability-container-size-empty)
}
SECTION("Change a WayNodeList") {
osmium::builder::add_way_node_list(buffer, osmium::builder::attr::_nodes({
{1, {0.0, 0.0}},
{2, {0.0, 1.0}},
{3, {1.0, 1.0}}
}));
auto& nrl = buffer.get<osmium::WayNodeList>(0);
REQUIRE(nrl.size() == 3);
REQUIRE(nrl[1].location() == osmium::Location(0.0, 1.0));
nrl[1].set_location(osmium::Location{13.5, -7.2});
REQUIRE(nrl[1].location() == osmium::Location(13.5, -7.2));
}
SECTION("Small area") {
osmium::builder::add_way_node_list(buffer, osmium::builder::attr::_nodes({
{1, {0.0, 0.0}},
{2, {0.0, 1.0}},
{3, {1.0, 1.0}},
{4, {1.0, 0.0}},
{1, {0.0, 0.0}},
}));
const osmium::WayNodeList& nrl = buffer.get<osmium::WayNodeList>(0);
REQUIRE_FALSE(nrl.empty());
REQUIRE(nrl.size() == 5);
REQUIRE(nrl.is_closed());
REQUIRE(nrl.ends_have_same_id());
REQUIRE(nrl.ends_have_same_location());
const osmium::Box envelope = nrl.envelope();
REQUIRE(envelope.bottom_left().lon() == Approx(0));
REQUIRE(envelope.bottom_left().lat() == Approx(0));
REQUIRE(envelope.top_right().lon() == Approx(1));
REQUIRE(envelope.top_right().lat() == Approx(1));
}
SECTION("Not an area") {
osmium::builder::add_way_node_list(buffer, osmium::builder::attr::_nodes({
{1, {0.0, 0.0}},
{2, {1.0, 0.0}},
{1, {0.0, 0.0}},
}));
const osmium::WayNodeList& nrl = buffer.get<osmium::WayNodeList>(0);
REQUIRE_FALSE(nrl.empty());
REQUIRE(nrl.size() == 3);
REQUIRE(nrl.is_closed());
REQUIRE(nrl.ends_have_same_id());
REQUIRE(nrl.ends_have_same_location());
}
}
@@ -0,0 +1,227 @@
#include "catch.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/osm.hpp>
#include <osmium/osm/object_comparisons.hpp>
#include <algorithm>
#include <functional>
#include <vector>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
constexpr const std::size_t test_buffer_size = 1024UL * 10UL;
TEST_CASE("Object ID comparisons") {
const osmium::object_id_type a = 0;
const osmium::object_id_type b = -1;
const osmium::object_id_type c = -10;
const osmium::object_id_type d = -11;
const osmium::object_id_type e = 1;
const osmium::object_id_type f = 11;
const osmium::object_id_type g = 12;
REQUIRE_FALSE(osmium::id_order{}(a, a));
REQUIRE(osmium::id_order{}(a, b));
REQUIRE(osmium::id_order{}(a, c));
REQUIRE(osmium::id_order{}(a, d));
REQUIRE(osmium::id_order{}(a, e));
REQUIRE(osmium::id_order{}(a, f));
REQUIRE(osmium::id_order{}(a, g));
REQUIRE_FALSE(osmium::id_order{}(b, a));
REQUIRE_FALSE(osmium::id_order{}(b, b));
REQUIRE(osmium::id_order{}(b, c));
REQUIRE(osmium::id_order{}(b, d));
REQUIRE(osmium::id_order{}(b, e));
REQUIRE(osmium::id_order{}(b, f));
REQUIRE(osmium::id_order{}(b, g));
REQUIRE_FALSE(osmium::id_order{}(c, a));
REQUIRE_FALSE(osmium::id_order{}(c, b));
REQUIRE_FALSE(osmium::id_order{}(c, c));
REQUIRE(osmium::id_order{}(c, d));
REQUIRE(osmium::id_order{}(c, e));
REQUIRE(osmium::id_order{}(c, f));
REQUIRE(osmium::id_order{}(c, g));
REQUIRE_FALSE(osmium::id_order{}(d, a));
REQUIRE_FALSE(osmium::id_order{}(d, b));
REQUIRE_FALSE(osmium::id_order{}(d, c));
REQUIRE_FALSE(osmium::id_order{}(d, d));
REQUIRE(osmium::id_order{}(d, e));
REQUIRE(osmium::id_order{}(d, f));
REQUIRE(osmium::id_order{}(d, g));
REQUIRE_FALSE(osmium::id_order{}(e, a));
REQUIRE_FALSE(osmium::id_order{}(e, b));
REQUIRE_FALSE(osmium::id_order{}(e, c));
REQUIRE_FALSE(osmium::id_order{}(e, d));
REQUIRE_FALSE(osmium::id_order{}(e, e));
REQUIRE(osmium::id_order{}(e, f));
REQUIRE(osmium::id_order{}(e, g));
REQUIRE_FALSE(osmium::id_order{}(f, a));
REQUIRE_FALSE(osmium::id_order{}(f, b));
REQUIRE_FALSE(osmium::id_order{}(f, c));
REQUIRE_FALSE(osmium::id_order{}(f, d));
REQUIRE_FALSE(osmium::id_order{}(f, e));
REQUIRE_FALSE(osmium::id_order{}(f, f));
REQUIRE(osmium::id_order{}(f, g));
REQUIRE_FALSE(osmium::id_order{}(g, a));
REQUIRE_FALSE(osmium::id_order{}(g, b));
REQUIRE_FALSE(osmium::id_order{}(g, c));
REQUIRE_FALSE(osmium::id_order{}(g, d));
REQUIRE_FALSE(osmium::id_order{}(g, e));
REQUIRE_FALSE(osmium::id_order{}(g, f));
REQUIRE_FALSE(osmium::id_order{}(g, g));
}
TEST_CASE("Object ID comparisons - the other way around") {
const osmium::object_id_type a = 0;
const osmium::object_id_type b = -1;
const osmium::object_id_type c = -10;
const osmium::object_id_type d = -11;
const osmium::object_id_type e = 1;
const osmium::object_id_type f = 11;
const osmium::object_id_type g = 12;
REQUIRE_FALSE(osmium::id_order{}(a, a));
REQUIRE_FALSE(osmium::id_order{}(b, a));
REQUIRE_FALSE(osmium::id_order{}(c, a));
REQUIRE_FALSE(osmium::id_order{}(d, a));
REQUIRE_FALSE(osmium::id_order{}(e, a));
REQUIRE_FALSE(osmium::id_order{}(f, a));
REQUIRE_FALSE(osmium::id_order{}(g, a));
REQUIRE(osmium::id_order{}(a, b));
REQUIRE_FALSE(osmium::id_order{}(b, b));
REQUIRE_FALSE(osmium::id_order{}(c, b));
REQUIRE_FALSE(osmium::id_order{}(d, b));
REQUIRE_FALSE(osmium::id_order{}(e, b));
REQUIRE_FALSE(osmium::id_order{}(f, b));
REQUIRE_FALSE(osmium::id_order{}(g, b));
REQUIRE(osmium::id_order{}(a, c));
REQUIRE(osmium::id_order{}(b, c));
REQUIRE_FALSE(osmium::id_order{}(c, c));
REQUIRE_FALSE(osmium::id_order{}(d, c));
REQUIRE_FALSE(osmium::id_order{}(e, c));
REQUIRE_FALSE(osmium::id_order{}(f, c));
REQUIRE_FALSE(osmium::id_order{}(g, c));
REQUIRE(osmium::id_order{}(a, d));
REQUIRE(osmium::id_order{}(b, d));
REQUIRE(osmium::id_order{}(c, d));
REQUIRE_FALSE(osmium::id_order{}(d, d));
REQUIRE_FALSE(osmium::id_order{}(e, d));
REQUIRE_FALSE(osmium::id_order{}(f, d));
REQUIRE_FALSE(osmium::id_order{}(g, d));
REQUIRE(osmium::id_order{}(a, e));
REQUIRE(osmium::id_order{}(b, e));
REQUIRE(osmium::id_order{}(c, e));
REQUIRE(osmium::id_order{}(d, e));
REQUIRE_FALSE(osmium::id_order{}(e, e));
REQUIRE_FALSE(osmium::id_order{}(f, e));
REQUIRE_FALSE(osmium::id_order{}(g, e));
REQUIRE(osmium::id_order{}(a, f));
REQUIRE(osmium::id_order{}(b, f));
REQUIRE(osmium::id_order{}(c, f));
REQUIRE(osmium::id_order{}(d, f));
REQUIRE(osmium::id_order{}(e, f));
REQUIRE_FALSE(osmium::id_order{}(f, f));
REQUIRE_FALSE(osmium::id_order{}(g, f));
REQUIRE(osmium::id_order{}(a, g));
REQUIRE(osmium::id_order{}(b, g));
REQUIRE(osmium::id_order{}(c, g));
REQUIRE(osmium::id_order{}(d, g));
REQUIRE(osmium::id_order{}(e, g));
REQUIRE(osmium::id_order{}(f, g));
REQUIRE_FALSE(osmium::id_order{}(g, g));
}
TEST_CASE("Node comparisons") {
osmium::memory::Buffer buffer{test_buffer_size};
std::vector<std::reference_wrapper<osmium::Node>> nodes;
SECTION("nodes are ordered by id, version, and timestamp") {
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 0), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 10), _version(2), _timestamp("2016-01-01T00:01:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 10), _version(3), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 12), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 12), _version(2), _timestamp("2016-01-01T00:01:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 15), _version(1), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(10000000000LL), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
REQUIRE(std::is_sorted(nodes.cbegin(), nodes.cend()));
}
SECTION("equal nodes are not different") {
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(1), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(1), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
REQUIRE(nodes[0] == nodes[1]);
REQUIRE_FALSE(nodes[0] < nodes[1]);
REQUIRE_FALSE(nodes[0] > nodes[1]);
}
SECTION("IDs are ordered by sign and then absolute value") {
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 0))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( -1))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(-10))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 10))));
REQUIRE(std::is_sorted(nodes.cbegin(), nodes.cend()));
}
SECTION("reverse version ordering") {
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 0), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 1), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 10), _version(3), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 10), _version(2), _timestamp("2016-01-01T00:01:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 12), _version(2), _timestamp("2016-01-01T00:01:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 12), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id( 15), _version(1), _timestamp("2016-01-01T00:00:00Z"))));
nodes.emplace_back(buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(10000000000LL), _version(2), _timestamp("2016-01-01T00:00:00Z"))));
REQUIRE(std::is_sorted(nodes.cbegin(), nodes.cend(), osmium::object_order_type_id_reverse_version{}));
}
}
TEST_CASE("Object comparisons: types are ordered nodes, then ways, then relations") {
osmium::memory::Buffer buffer{test_buffer_size};
std::vector<std::reference_wrapper<osmium::OSMObject>> objects;
objects.emplace_back(buffer.get<osmium::Node>( osmium::builder::add_node( buffer, _id(3))));
objects.emplace_back(buffer.get<osmium::Way>( osmium::builder::add_way( buffer, _id(2))));
objects.emplace_back(buffer.get<osmium::Relation>(osmium::builder::add_relation(buffer, _id(1))));
REQUIRE(std::is_sorted(objects.cbegin(), objects.cend()));
}
TEST_CASE("Object comparisons with partially missing timestamp") {
osmium::memory::Buffer buffer{test_buffer_size};
const osmium::OSMObject& obj1 = buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(3), _version(2), _timestamp(("2016-01-01T00:00:00Z"))));
const osmium::OSMObject& obj2 = buffer.get<osmium::Node>(osmium::builder::add_node(buffer, _id(3), _version(2)));
SECTION("OSMObject::operator<") {
REQUIRE_FALSE(obj1 < obj2);
REQUIRE_FALSE(obj1 > obj2);
REQUIRE(obj1 == obj2);
}
SECTION("object_order_type_id_reverse_version") {
const osmium::object_order_type_id_reverse_version comp{};
REQUIRE_FALSE(comp(obj1, obj2));
REQUIRE_FALSE(comp(obj2, obj1));
}
}
+77
View File
@@ -0,0 +1,77 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/osm/crc.hpp>
#include <osmium/osm/relation.hpp>
#include <string>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Build relation") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_relation(buffer,
_id(17),
_version(3),
_visible(),
_cid(333),
_uid(21),
_timestamp(static_cast<std::time_t>(123)),
_user("foo"),
_tag("type", "multipolygon"),
_tag("name", "Sherwood Forest"),
_member(osmium::item_type::way, 1, "inner"),
_member(osmium::item_type::way, 2, ""),
_member(osmium::item_type::way, 3, "outer")
);
const osmium::Relation& relation = buffer.get<osmium::Relation>(0);
REQUIRE(17 == relation.id());
REQUIRE(3 == relation.version());
REQUIRE(relation.visible());
REQUIRE(333 == relation.changeset());
REQUIRE(21 == relation.uid());
REQUIRE(std::string("foo") == relation.user());
REQUIRE(123 == uint32_t(relation.timestamp()));
REQUIRE(2 == relation.tags().size());
REQUIRE(3 == relation.members().size());
int n=1;
for (const auto& member : relation.members()) {
REQUIRE(osmium::item_type::way == member.type());
REQUIRE(n == member.ref());
switch (n) {
case 1:
REQUIRE(std::string("inner") == member.role());
break;
case 2:
REQUIRE(std::string("") == member.role());
break;
case 3:
REQUIRE(std::string("outer") == member.role());
break;
default:
REQUIRE(false);
}
++n;
}
osmium::CRC<crc_type> crc32;
crc32.update(relation);
REQUIRE(crc32().checksum() == 0x2c2352e);
}
TEST_CASE("Member role too long") {
osmium::memory::Buffer buffer{10000};
osmium::builder::RelationMemberListBuilder builder{buffer};
const char role[2000] = "";
builder.add_member(osmium::item_type::node, 1, role, 1024);
REQUIRE_THROWS(builder.add_member(osmium::item_type::node, 1, role, 1025));
}
+192
View File
@@ -0,0 +1,192 @@
#include "catch.hpp"
#include <osmium/osm/timestamp.hpp>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
TEST_CASE("Timestamp can be default initialized to invalid value") {
const osmium::Timestamp t{};
REQUIRE(0 == uint32_t(t));
REQUIRE(t.to_iso().empty());
REQUIRE(t.to_iso_all() == "1970-01-01T00:00:00Z");
REQUIRE_FALSE(t.valid());
}
TEST_CASE("Timestamp invalid value is zero") {
const osmium::Timestamp t{static_cast<time_t>(0)};
REQUIRE(0 == uint32_t(t));
REQUIRE(t.to_iso().empty());
REQUIRE(t.to_iso_all() == "1970-01-01T00:00:00Z");
REQUIRE_FALSE(t.valid());
}
TEST_CASE("Timestamp can be initialized from time_t") {
const osmium::Timestamp t{static_cast<time_t>(1)};
REQUIRE(1 == uint32_t(t));
REQUIRE("1970-01-01T00:00:01Z" == t.to_iso());
REQUIRE(t.valid());
}
TEST_CASE("Timestamp can be initialized from const char*") {
const osmium::Timestamp t{"2000-01-01T00:00:00Z"};
REQUIRE("2000-01-01T00:00:00Z" == t.to_iso());
REQUIRE(t.valid());
}
TEST_CASE("Timestamp can be initialized from string") {
const std::string s = "2000-01-01T00:00:00Z";
const osmium::Timestamp t{s};
REQUIRE("2000-01-01T00:00:00Z" == t.to_iso());
REQUIRE(t.valid());
}
TEST_CASE("Timestamp throws if initialized from bad string") {
REQUIRE_THROWS_AS(osmium::Timestamp("x"), std::invalid_argument);
}
TEST_CASE("Timestamp can be explicitly cast to time_t") {
const osmium::Timestamp t{4242};
const time_t x = t.seconds_since_epoch();
REQUIRE(x == 4242);
}
TEST_CASE("Timestamp uint32_t can be initialized from Timestamp") {
const osmium::Timestamp t{4242};
const uint32_t x { t };
REQUIRE(x == 4242);
}
TEST_CASE("Timestamps can be compared") {
const osmium::Timestamp t1{10};
const osmium::Timestamp t2{50};
REQUIRE(t1 < t2);
REQUIRE(t1 > osmium::start_of_time());
REQUIRE(t2 > osmium::start_of_time());
REQUIRE(t1 < osmium::end_of_time());
REQUIRE(t2 < osmium::end_of_time());
}
TEST_CASE("Timestamp can be written to stream") {
const osmium::Timestamp t{1};
std::stringstream ss;
ss << t;
REQUIRE("1970-01-01T00:00:01Z" == ss.str());
}
void test_int2_to_string(int value, const char* ref) {
std::string s;
osmium::detail::add_2digit_int_to_string(value, s);
REQUIRE(s == ref);
}
void test_int4_to_string(int value, const char* ref) {
std::string s;
osmium::detail::add_4digit_int_to_string(value, s);
REQUIRE(s == ref);
}
TEST_CASE("Write two digit numbers") {
test_int2_to_string( 0, "00");
test_int2_to_string( 1, "01");
test_int2_to_string( 2, "02");
test_int2_to_string( 3, "03");
test_int2_to_string( 4, "04");
test_int2_to_string( 9, "09");
test_int2_to_string(10, "10");
test_int2_to_string(20, "20");
test_int2_to_string(29, "29");
test_int2_to_string(99, "99");
}
TEST_CASE("Write four digit numbers") {
test_int4_to_string(1000, "1000");
test_int4_to_string(1001, "1001");
test_int4_to_string(2018, "2018");
test_int4_to_string(9999, "9999");
}
TEST_CASE("Valid timestamps") {
const std::vector<std::string> test_cases = {
"1970-01-01T00:00:01Z",
"2000-01-01T00:00:00Z",
"2006-12-31T23:59:59Z",
"2030-12-31T23:59:59Z",
"2016-02-28T23:59:59Z",
"2016-03-31T23:59:59Z"
};
for (const auto& tc : test_cases) {
const osmium::Timestamp t{tc};
REQUIRE(tc == t.to_iso());
REQUIRE(tc == t.to_iso_all());
}
}
TEST_CASE("Valid timestamps with fractional seconds") {
const std::vector<std::string> test_cases = {
"2016-03-31T23:59:59.123Z",
"2016-03-31T23:59:59,4Z",
"2016-03-31T23:59:59,000000000000000001Z",
"2016-03-31T23:59:59.99Z"
};
for (const auto& tc : test_cases) {
const osmium::Timestamp t{tc};
REQUIRE("2016-03-31T23:59:59Z" == t.to_iso());
REQUIRE("2016-03-31T23:59:59Z" == t.to_iso_all());
}
}
TEST_CASE("Timestamp parsing leaves pointer after timestamp") {
const std::vector<std::string> test_cases = {
"2016-03-31T23:59:59Z#",
"2016-03-31T23:59:59.123Z#",
"2016-03-31T23:59:59,4Z#",
"2016-03-31T23:59:59,000000000000000001Z#",
"2016-03-31T23:59:59.99Z#"
};
for (const auto& tc : test_cases) {
const char *s = tc.data();
const char **str = &s;
auto const timestamp = osmium::detail::parse_timestamp(str);
REQUIRE(**str == '#');
REQUIRE(osmium::Timestamp(timestamp).to_iso() == "2016-03-31T23:59:59Z");
}
}
TEST_CASE("Invalid timestamps") {
REQUIRE_THROWS_AS(osmium::Timestamp{""}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"x"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"xxxxxxxxxxxxxxxxxxxx"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01x00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01T00:00:00x"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000x01-01T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01x01T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01T00x00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01T00:00x00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"0000-00-01T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-00-01T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-00T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01T24:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01T00:60:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-01T00:00:61Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-01-32T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-02-30T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-32T00:00:00Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-01T00:00:00"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-01T00:00:00,Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-01T00:00:00,xZ"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-01T00:00:00,123mZ"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-01T00:00:00.@Z"}, std::invalid_argument);
REQUIRE_THROWS_AS(osmium::Timestamp{"2000-03-01T00:00:00.@"}, std::invalid_argument);
}
@@ -0,0 +1,115 @@
#include "catch.hpp"
#include <osmium/osm/types.hpp>
#include <osmium/osm/types_from_string.hpp>
#include <stdexcept>
TEST_CASE("set ID from string") {
REQUIRE(osmium::string_to_object_id("0") == 0);
REQUIRE(osmium::string_to_object_id("17") == 17);
REQUIRE(osmium::string_to_object_id("-17") == -17);
REQUIRE(osmium::string_to_object_id("01") == 1);
REQUIRE_THROWS_AS(osmium::string_to_object_id(""), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id(" "), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id(" 22"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("x"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("0x1"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("12a"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("12345678901234567890"), std::range_error);
}
TEST_CASE("set type and ID from string") {
const auto n17 = osmium::string_to_object_id("n17", osmium::osm_entity_bits::nwr);
REQUIRE(n17.first == osmium::item_type::node);
REQUIRE(n17.second == 17);
const auto w42 = osmium::string_to_object_id("w42", osmium::osm_entity_bits::nwr);
REQUIRE(w42.first == osmium::item_type::way);
REQUIRE(w42.second == 42);
const auto r_2 = osmium::string_to_object_id("r-2", osmium::osm_entity_bits::nwr);
REQUIRE(r_2.first == osmium::item_type::relation);
REQUIRE(r_2.second == -2);
const auto d3 = osmium::string_to_object_id("3", osmium::osm_entity_bits::nwr);
REQUIRE(d3.first == osmium::item_type::undefined);
REQUIRE(d3.second == 3);
const auto u3 = osmium::string_to_object_id("3", osmium::osm_entity_bits::nwr, osmium::item_type::undefined);
REQUIRE(u3.first == osmium::item_type::undefined);
REQUIRE(u3.second == 3);
const auto n3 = osmium::string_to_object_id("3", osmium::osm_entity_bits::nwr, osmium::item_type::node);
REQUIRE(n3.first == osmium::item_type::node);
REQUIRE(n3.second == 3);
REQUIRE_THROWS_AS(osmium::string_to_object_id("", osmium::osm_entity_bits::nwr), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("n", osmium::osm_entity_bits::nwr), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("x3", osmium::osm_entity_bits::nwr), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("nx3", osmium::osm_entity_bits::nwr), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("n3", osmium::osm_entity_bits::way), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_id("n3a", osmium::osm_entity_bits::nwr), std::range_error);
}
TEST_CASE("set object version from string") {
REQUIRE(osmium::string_to_object_version("0") == 0);
REQUIRE(osmium::string_to_object_version("1") == 1);
REQUIRE(osmium::string_to_object_version("-1") == 0);
REQUIRE(osmium::string_to_object_version("4294967294") == 4294967294);
REQUIRE_THROWS_AS(osmium::string_to_object_version("-2"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_version(""), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_version(" "), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_version(" 22"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_version("x"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_version("4294967295"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_object_version("999999999999"), std::range_error);
}
TEST_CASE("set changeset id from string") {
REQUIRE(osmium::string_to_changeset_id("0") == 0);
REQUIRE(osmium::string_to_changeset_id("1") == 1);
REQUIRE(osmium::string_to_changeset_id("-1") == 0);
REQUIRE(osmium::string_to_changeset_id("4294967294") == 4294967294);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id("-2"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id(""), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id(" "), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id(" 22"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id("x"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id("4294967295"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_changeset_id("999999999999"), std::range_error);
}
TEST_CASE("set uid from string") {
REQUIRE(osmium::string_to_uid("0") == 0);
REQUIRE(osmium::string_to_uid("1") == 1);
REQUIRE(osmium::string_to_uid("-1") == 0);
REQUIRE(osmium::string_to_uid("4294967294") == 4294967294);
REQUIRE_THROWS_AS(osmium::string_to_uid("-2"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_uid(""), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_uid(" "), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_uid(" 22"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_uid("x"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_uid("4294967295"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_uid("999999999999"), std::range_error);
}
TEST_CASE("set num changes from string") {
REQUIRE(osmium::string_to_num_changes("0") == 0);
REQUIRE(osmium::string_to_num_changes("1") == 1);
REQUIRE(osmium::string_to_num_changes("-1") == 0);
REQUIRE(osmium::string_to_num_changes("4294967294") == 4294967294);
REQUIRE_THROWS_AS(osmium::string_to_num_changes("-2"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_num_changes(""), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_num_changes(" "), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_num_changes(" 22"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_num_changes("x"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_num_changes("4294967295"), std::range_error);
REQUIRE_THROWS_AS(osmium::string_to_num_changes("999999999999"), std::range_error);
}
+107
View File
@@ -0,0 +1,107 @@
#include "catch.hpp"
#include "test_crc.hpp"
#include <osmium/builder/attr.hpp>
#include <osmium/builder/osm_object_builder.hpp>
#include <osmium/osm/crc.hpp>
#include <osmium/osm/way.hpp>
#include <string>
using namespace osmium::builder::attr; // NOLINT(google-build-using-namespace)
TEST_CASE("Build way") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_way(buffer,
_id(17),
_version(3),
_visible(true),
_cid(333),
_uid(21),
_timestamp(static_cast<std::time_t>(123)),
_user("foo"),
_tag("highway", "residential"),
_tag("name", "High Street"),
_nodes({1, 3, 2})
);
auto& way = buffer.get<osmium::Way>(0);
REQUIRE(osmium::item_type::way == way.type());
REQUIRE(way.type_is_in(osmium::osm_entity_bits::way));
REQUIRE(way.type_is_in(osmium::osm_entity_bits::node | osmium::osm_entity_bits::way));
REQUIRE(17 == way.id());
REQUIRE(3 == way.version());
REQUIRE(way.visible());
REQUIRE(333 == way.changeset());
REQUIRE(21 == way.uid());
REQUIRE(std::string("foo") == way.user());
REQUIRE(123 == uint32_t(way.timestamp()));
REQUIRE(2 == way.tags().size());
REQUIRE(3 == way.nodes().size());
REQUIRE(1 == way.nodes()[0].ref());
REQUIRE(3 == way.nodes()[1].ref());
REQUIRE(2 == way.nodes()[2].ref());
REQUIRE_FALSE(way.is_closed());
osmium::CRC<crc_type> crc32;
crc32.update(way);
REQUIRE(crc32().checksum() == 0x65f6ba91);
way.remove_tags();
REQUIRE(way.tags().empty());
REQUIRE(3 == way.nodes().size());
}
TEST_CASE("build closed way") {
osmium::memory::Buffer buffer{10000};
osmium::builder::add_way(buffer,
_tag("highway", "residential"),
_tag("name", "High Street"),
_nodes({1, 3, 1})
);
const osmium::Way& way = buffer.get<osmium::Way>(0);
REQUIRE(way.is_closed());
}
TEST_CASE("build way with helpers") {
osmium::memory::Buffer buffer{10000};
{
osmium::builder::WayBuilder builder(buffer);
builder.set_user("username");
builder.add_tags({
{"amenity", "restaurant"},
{"name", "Zum goldenen Schwanen"}
});
builder.add_node_refs({
{22, {3.5, 4.7}},
{67, {4.1, 2.2}}
});
}
buffer.commit();
const osmium::Way& way = buffer.get<osmium::Way>(0);
REQUIRE(std::string("username") == way.user());
REQUIRE(2 == way.tags().size());
REQUIRE(std::string("amenity") == way.tags().begin()->key());
REQUIRE(std::string("Zum goldenen Schwanen") == way.tags()["name"]);
REQUIRE(2 == way.nodes().size());
REQUIRE(22 == way.nodes()[0].ref());
REQUIRE(4.1 == Approx(way.nodes()[1].location().lon()));
const osmium::Box envelope = way.envelope();
REQUIRE(envelope.bottom_left().lon() == Approx(3.5));
REQUIRE(envelope.bottom_left().lat() == Approx(2.2));
REQUIRE(envelope.top_right().lon() == Approx(4.1));
REQUIRE(envelope.top_right().lat() == Approx(4.7));
}
+32
View File
@@ -0,0 +1,32 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<node id="10" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.0"/>
<node id="11" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.1"/>
<node id="12" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.2"/>
<node id="13" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.3"/>
<node id="14" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.4"/>
<way id="20" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="highway" v="primary"/>
<nd ref="10"/>
<nd ref="11"/>
</way>
<way id="21" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="highway" v="residential"/>
<nd ref="11"/>
<nd ref="12"/>
</way>
<relation id="30" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="test"/>
<member type="node" ref="10" role="none"/>
</relation>
<relation id="31" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="restriction"/>
<member type="way" ref="20" role="from"/>
<member type="node" ref="11" role="via"/>
<member type="way" ref="22" role="to"/>
</relation>
<relation id="32" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="test" v="relinrel"/>
<member type="relation" ref="30" role="none"/>
</relation>
</osm>
+17
View File
@@ -0,0 +1,17 @@
<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="testdata" upload="false">
<node id="10" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.0"/>
<node id="11" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.1"/>
<node id="12" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1" lat="1.0" lon="1.1"/>
<relation id="30" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="test"/>
<member type="node" ref="10" role="none"/>
<member type="node" ref="10" role="none"/>
<member type="node" ref="11" role="none"/>
</relation>
<relation id="31" version="1" timestamp="2014-01-01T00:00:00Z" uid="1" user="test" changeset="1">
<tag k="type" v="test"/>
<member type="node" ref="10" role="none"/>
<member type="node" ref="12" role="none"/>
</relation>
</osm>
Loaded 100 of 1312 files, more files were not shown because too many files have changed in this diff. Show more