osrm-backend/test/data-tests/testdata-xml.cpp

463 lines
13 KiB
C++
Raw Normal View History

/* The code in this file is released into the Public Domain. */
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <string>
#include <osmium/io/xml_input.hpp>
#include <osmium/io/gzip_compression.hpp>
#include <osmium/visitor.hpp>
std::string filename(const char* test_id, const char* suffix = "osm") {
const char* testdir = getenv("TESTDIR");
if (!testdir) {
std::cerr << "You have to set TESTDIR environment variable before running testdata-xml\n";
exit(2);
}
std::string f;
f += testdir;
f += "/";
f += test_id;
f += "/data.";
f += suffix;
return f;
}
struct header_buffer_type {
osmium::io::Header header;
osmium::memory::Buffer buffer;
};
// =============================================
// The following helper functions are used to call different parts of the
// Osmium internals used to read and parse XML files. This way those parts
// can be tested individually. These function can not be used in normal
// operations, because they make certain assumptions, for instance that
// file contents fit into small buffers.
std::string read_file(const char* test_id) {
int fd = osmium::io::detail::open_for_reading(filename(test_id));
assert(fd >= 0);
std::string input(10000, '\0');
Squashed 'third_party/libosmium/' changes from 6522da5..f074d94 f074d94 Use shorter path names in Doxygen doc. 5117d5a Make Doxygen-generated HTML docs work better on small screens. 41607ac Appveyor config: Use lowercase command names everywhere. Filter 7z output. 9e77a39 Remove debug output. 3416c4b Revert "Comment out test to see if there are dependencies between tests." fa49dde Comment out test to see if there are dependencies between tests. 5add75d Fix typo in appveyor conf: nul. 53b3778 Use different file names for tests. 1091c0b Fix Windows munmap() to return the same as the posix one. 14fdc4e Revert "CMake: Remove unneeded build type setting." 0270087 Workaround for MSVC. 56c2120 Another try... 8e96a6f Trying to fix test on Windows... 11a64c2 CMake workaround: Set CMAKE_CONFIGURATION_TYPES before project(). As per http://www.cmake.org/pipermail/cmake/2012-January/048856.html 3847e7a CMake: Remove unneeded build type setting. a1bcaf1 CMake: Make CMAKE_CONFIGURATION_TYPES a cache variable. ddab3e3 Create test file in test_typed_mmap in current dir instead of tmp. ce1d3bd Build "RelWithDebInfo" on appveyor instead of "Release". b6db34f Add README showing source of FindGem.cmake. 2c86c55 Better error message and some comments in CMake for sparsetable check. b16a5a3 Fix ctest on Windows. 2203bc8 Fix FindGem.cmake. 5d1f81d Another try to fix travis build. ddd3f5e Hopefully fix problem where travis doesn't find osm-testdata. 0e673ea More debug output to find travis problem. eb01107 Updated data-tests README. 8a971c8 Remove special case of the multipolygon test in travis/appveyor config. dce792c CMake: Check dependencies of multipolygon test and run as CMake script. b967677 Add tests for thread pool. 400e9b3 Bugfix: Handle exception in one pool threads properly. a1ba489 More detailed error reporting from zlib. 95cf621 Disable SparseMemTable if sparsetable size_type is smaller than 8. 7b601b5 Add missing overload to cast_with_assert() function. 60a7d86 Giving up on trying to remove LNK4221 warning. 35ed5df Try another way to get rid of MSV warning... f9c7d92 Disable a warning about changed behaviour on MSVC. c3c6b2d CTest: Only output failed tests. 9c99996 Try setting option in a different way for getting rid of LNK4221 on MSVC. 4ac563c Another ssize_t fix for Windows. 11db84f Fix mmap for windows. Convert macro to function. 95d8f75 Fix test on Windows (which doesn't have ssize_t). df51aa4 Do not write huge files in one system call. 9c4f772 Change mmap() implementation for Windows. 5be817c Rewrote function to work on Windows without warnings. ea84f73 Fix warnings on Windows. 572d692 Fixed the static_cast_with_assert function and added tests. 89ef86b Use SparseMemArray instead of SparseMemTable in examples. git-subtree-dir: third_party/libosmium git-subtree-split: f074d949a5585a81578d682035f2163de971beb3
2015-03-04 06:50:42 -05:00
auto n = ::read(fd, reinterpret_cast<unsigned char*>(const_cast<char*>(input.data())), 10000);
assert(n >= 0);
input.resize(static_cast<std::string::size_type>(n));
close(fd);
return input;
}
std::string read_gz_file(const char* test_id, const char* suffix) {
int fd = osmium::io::detail::open_for_reading(filename(test_id, suffix));
assert(fd >= 0);
osmium::io::GzipDecompressor gzip_decompressor(fd);
std::string input = gzip_decompressor.read();
gzip_decompressor.close();
return input;
}
header_buffer_type parse_xml(std::string input) {
osmium::thread::Queue<std::string> input_queue;
osmium::thread::Queue<osmium::memory::Buffer> output_queue;
std::promise<osmium::io::Header> header_promise;
std::atomic<bool> done {false};
input_queue.push(input);
input_queue.push(std::string()); // EOF marker
osmium::io::detail::XMLParser parser(input_queue, output_queue, header_promise, osmium::osm_entity_bits::all, done);
parser();
header_buffer_type result;
result.header = header_promise.get_future().get();
output_queue.wait_and_pop(result.buffer);
if (result.buffer) {
osmium::memory::Buffer buffer;
output_queue.wait_and_pop(buffer);
assert(!buffer);
}
return result;
}
header_buffer_type read_xml(const char* test_id) {
std::string input = read_file(test_id);
return parse_xml(input);
}
// =============================================
TEST_CASE("Reading OSM XML 100") {
SECTION("Direct") {
header_buffer_type r = read_xml("100-correct_but_no_data");
REQUIRE(r.header.get("generator") == "testdata");
REQUIRE(0 == r.buffer.committed());
REQUIRE(! r.buffer);
}
SECTION("Using Reader") {
osmium::io::Reader reader(filename("100-correct_but_no_data"));
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
osmium::memory::Buffer buffer = reader.read();
REQUIRE(0 == buffer.committed());
REQUIRE(! buffer);
reader.close();
}
SECTION("Using Reader asking for header only") {
osmium::io::Reader reader(filename("100-correct_but_no_data"), osmium::osm_entity_bits::nothing);
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
reader.close();
}
}
// =============================================
TEST_CASE("Reading OSM XML 101") {
SECTION("Direct") {
REQUIRE_THROWS_AS(read_xml("101-missing_version"), osmium::format_version_error);
try {
read_xml("101-missing_version");
} catch (osmium::format_version_error& e) {
REQUIRE(e.version.empty());
}
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("101-missing_version"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::format_version_error);
}
}
// =============================================
TEST_CASE("Reading OSM XML 102") {
SECTION("Direct") {
REQUIRE_THROWS_AS(read_xml("102-wrong_version"), osmium::format_version_error);
try {
read_xml("102-wrong_version");
} catch (osmium::format_version_error& e) {
REQUIRE(e.version == "0.1");
}
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("102-wrong_version"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::format_version_error);
}
}
// =============================================
TEST_CASE("Reading OSM XML 103") {
SECTION("Direct") {
REQUIRE_THROWS_AS(read_xml("103-old_version"), osmium::format_version_error);
try {
read_xml("103-old_version");
} catch (osmium::format_version_error& e) {
REQUIRE(e.version == "0.5");
}
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("103-old_version"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::format_version_error);
}
}
// =============================================
TEST_CASE("Reading OSM XML 104") {
SECTION("Direct") {
REQUIRE_THROWS_AS(read_xml("104-empty_file"), osmium::xml_error);
try {
read_xml("104-empty_file");
} catch (osmium::xml_error& e) {
REQUIRE(e.line == 1);
REQUIRE(e.column == 0);
}
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("104-empty_file"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::xml_error);
}
}
// =============================================
TEST_CASE("Reading OSM XML 105") {
SECTION("Direct") {
REQUIRE_THROWS_AS(read_xml("105-incomplete_xml_file"), osmium::xml_error);
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("105-incomplete_xml_file"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::xml_error);
}
}
// =============================================
TEST_CASE("Reading OSM XML 120") {
SECTION("Direct") {
std::string data = read_gz_file("120-correct_gzip_file_without_data", "osm.gz");
REQUIRE(data.size() == 102);
header_buffer_type r = parse_xml(data);
REQUIRE(r.header.get("generator") == "testdata");
REQUIRE(0 == r.buffer.committed());
REQUIRE(! r.buffer);
}
SECTION("Using Reader") {
osmium::io::Reader reader(filename("120-correct_gzip_file_without_data", "osm.gz"));
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
osmium::memory::Buffer buffer = reader.read();
REQUIRE(0 == buffer.committed());
REQUIRE(! buffer);
reader.close();
}
}
// =============================================
TEST_CASE("Reading OSM XML 121") {
SECTION("Direct") {
REQUIRE_THROWS_AS( {
read_gz_file("121-truncated_gzip_file", "osm.gz");
}, osmium::gzip_error);
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("121-truncated_gzip_file", "osm.gz"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::gzip_error);
}
Squashed 'third_party/libosmium/' changes from f074d94..8bcd4ea 8bcd4ea Add explicit cast operator from osmium::Timestamp to uint32_t. 0b24814 Fixed the full_queue_sleep_duration const (again). 5e19dd2 Try different workaround for windows... df5d6c9 Fix build error on Windows. 1553adf Workaround for missing support for u8 string literals on Windows. aa5e44a Do not build benchmarks in Appveyor to speed up build. 2b22b31 Remove warning generated by assert by casting to largest type. 148c5e3 Fix inclusion of our own css file into doxygen documentation. 13bce6f Split out test thats fails on Windows and do not run it on appveyor. ca04757 Make CMake add_unit_test() function more flexible. 6c04a63 Tell CMake to output json file with compile commands. c0dd848 Fix indentation in some tests. dfa7e4b Formatting: Consistently use spaces around equal signs. 08fe6db Add change log, release version 2.1.0. 667192e Add XML tests. 28acfc7 Make sorting the PBF stringtables optional. 8184781 Fix PBFInputFormat. Use member variable instead of static variable. 2b48945 Fix comment. 6d37054 Rename m_done to m_quit_input_thread to clarify what it is for. bc23083 Fix race condition in PBF reader. 7fc380e Add various docs, noexcepts, asserts, and tests. aeaf4d7 Not a good idea to how overbroad patterns in .gitignore. c1ef2f9 Bugfix: Multipolygon collector was accessing non-existent NodeRef. 0ef9a13 Add noexcept and docs to some functions in NodeRefList class. da4d764 Workaround for Doxygen bug. e67d5d9 use absolute paths to osmium in YouCompleteMe configuration 38abeac remove template parameter from NodeRefList c47adf0 Add check that osm xml file has <osm> or <osmChange> as top-level element. 5e4af90 Updated version number to 2.0.0. 5b2bc3e Workaround in cmake test for sparsehash size. git-subtree-dir: third_party/libosmium git-subtree-split: 8bcd4ea771696812bbb08ebc58e3ee22d0538070
2015-04-13 09:44:38 -04:00
}
// =============================================
TEST_CASE("Reading OSM XML 122") {
SECTION("Direct") {
REQUIRE_THROWS_AS( {
read_xml("122-no_osm_element");
}, osmium::xml_error);
}
SECTION("Using Reader") {
REQUIRE_THROWS_AS({
osmium::io::Reader reader(filename("122-no_osm_element"));
osmium::io::Header header = reader.header();
osmium::memory::Buffer buffer = reader.read();
reader.close();
}, osmium::xml_error);
}
}
// =============================================
TEST_CASE("Reading OSM XML 140") {
SECTION("Using Reader") {
osmium::io::Reader reader(filename("140-unicode"));
osmium::memory::Buffer buffer = reader.read();
reader.close();
int count = 0;
for (auto it = buffer.begin<osmium::Node>(); it != buffer.end<osmium::Node>(); ++it) {
++count;
REQUIRE(it->id() == count);
const osmium::TagList& t = it->tags();
const char* uc = t["unicode_char"];
auto len = atoi(t["unicode_utf8_length"]);
REQUIRE(len == strlen(uc));
REQUIRE(!strcmp(uc, t["unicode_xml"]));
// workaround for missing support for u8 string literals on Windows
#if !defined(_MSC_VER)
switch (count) {
case 1:
REQUIRE(!strcmp(uc, u8"a"));
break;
case 2:
REQUIRE(!strcmp(uc, u8"\u00e4"));
break;
case 3:
REQUIRE(!strcmp(uc, u8"\u30dc"));
break;
case 4:
REQUIRE(!strcmp(uc, u8"\U0001d11e"));
break;
case 5:
REQUIRE(!strcmp(uc, u8"\U0001f6eb"));
break;
default:
REQUIRE(false); // should not be here
}
#endif
Squashed 'third_party/libosmium/' changes from f074d94..8bcd4ea 8bcd4ea Add explicit cast operator from osmium::Timestamp to uint32_t. 0b24814 Fixed the full_queue_sleep_duration const (again). 5e19dd2 Try different workaround for windows... df5d6c9 Fix build error on Windows. 1553adf Workaround for missing support for u8 string literals on Windows. aa5e44a Do not build benchmarks in Appveyor to speed up build. 2b22b31 Remove warning generated by assert by casting to largest type. 148c5e3 Fix inclusion of our own css file into doxygen documentation. 13bce6f Split out test thats fails on Windows and do not run it on appveyor. ca04757 Make CMake add_unit_test() function more flexible. 6c04a63 Tell CMake to output json file with compile commands. c0dd848 Fix indentation in some tests. dfa7e4b Formatting: Consistently use spaces around equal signs. 08fe6db Add change log, release version 2.1.0. 667192e Add XML tests. 28acfc7 Make sorting the PBF stringtables optional. 8184781 Fix PBFInputFormat. Use member variable instead of static variable. 2b48945 Fix comment. 6d37054 Rename m_done to m_quit_input_thread to clarify what it is for. bc23083 Fix race condition in PBF reader. 7fc380e Add various docs, noexcepts, asserts, and tests. aeaf4d7 Not a good idea to how overbroad patterns in .gitignore. c1ef2f9 Bugfix: Multipolygon collector was accessing non-existent NodeRef. 0ef9a13 Add noexcept and docs to some functions in NodeRefList class. da4d764 Workaround for Doxygen bug. e67d5d9 use absolute paths to osmium in YouCompleteMe configuration 38abeac remove template parameter from NodeRefList c47adf0 Add check that osm xml file has <osm> or <osmChange> as top-level element. 5e4af90 Updated version number to 2.0.0. 5b2bc3e Workaround in cmake test for sparsehash size. git-subtree-dir: third_party/libosmium git-subtree-split: 8bcd4ea771696812bbb08ebc58e3ee22d0538070
2015-04-13 09:44:38 -04:00
}
REQUIRE(count == 5);
}
}
// =============================================
TEST_CASE("Reading OSM XML 141") {
SECTION("Using Reader") {
osmium::io::Reader reader(filename("141-entities"));
osmium::memory::Buffer buffer = reader.read();
reader.close();
REQUIRE(buffer.committed() > 0);
REQUIRE(buffer.get<osmium::memory::Item>(0).type() == osmium::item_type::node);
const osmium::Node& node = buffer.get<osmium::Node>(0);
const osmium::TagList& tags = node.tags();
REQUIRE(!strcmp(tags["less-than"], "<"));
REQUIRE(!strcmp(tags["greater-than"], ">"));
REQUIRE(!strcmp(tags["apostrophe"], "'"));
REQUIRE(!strcmp(tags["ampersand"], "&"));
REQUIRE(!strcmp(tags["quote"], "\""));
}
}
Squashed 'third_party/libosmium/' changes from f074d94..8bcd4ea 8bcd4ea Add explicit cast operator from osmium::Timestamp to uint32_t. 0b24814 Fixed the full_queue_sleep_duration const (again). 5e19dd2 Try different workaround for windows... df5d6c9 Fix build error on Windows. 1553adf Workaround for missing support for u8 string literals on Windows. aa5e44a Do not build benchmarks in Appveyor to speed up build. 2b22b31 Remove warning generated by assert by casting to largest type. 148c5e3 Fix inclusion of our own css file into doxygen documentation. 13bce6f Split out test thats fails on Windows and do not run it on appveyor. ca04757 Make CMake add_unit_test() function more flexible. 6c04a63 Tell CMake to output json file with compile commands. c0dd848 Fix indentation in some tests. dfa7e4b Formatting: Consistently use spaces around equal signs. 08fe6db Add change log, release version 2.1.0. 667192e Add XML tests. 28acfc7 Make sorting the PBF stringtables optional. 8184781 Fix PBFInputFormat. Use member variable instead of static variable. 2b48945 Fix comment. 6d37054 Rename m_done to m_quit_input_thread to clarify what it is for. bc23083 Fix race condition in PBF reader. 7fc380e Add various docs, noexcepts, asserts, and tests. aeaf4d7 Not a good idea to how overbroad patterns in .gitignore. c1ef2f9 Bugfix: Multipolygon collector was accessing non-existent NodeRef. 0ef9a13 Add noexcept and docs to some functions in NodeRefList class. da4d764 Workaround for Doxygen bug. e67d5d9 use absolute paths to osmium in YouCompleteMe configuration 38abeac remove template parameter from NodeRefList c47adf0 Add check that osm xml file has <osm> or <osmChange> as top-level element. 5e4af90 Updated version number to 2.0.0. 5b2bc3e Workaround in cmake test for sparsehash size. git-subtree-dir: third_party/libosmium git-subtree-split: 8bcd4ea771696812bbb08ebc58e3ee22d0538070
2015-04-13 09:44:38 -04:00
// =============================================
TEST_CASE("Reading OSM XML 200") {
SECTION("Direct") {
header_buffer_type r = read_xml("200-nodes");
REQUIRE(r.header.get("generator") == "testdata");
REQUIRE(r.buffer.committed() > 0);
REQUIRE(r.buffer.get<osmium::memory::Item>(0).type() == osmium::item_type::node);
REQUIRE(r.buffer.get<osmium::Node>(0).id() == 36966060);
REQUIRE(std::distance(r.buffer.begin(), r.buffer.end()) == 3);
}
SECTION("Using Reader") {
osmium::io::Reader reader(filename("200-nodes"));
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
osmium::memory::Buffer buffer = reader.read();
REQUIRE(buffer.committed() > 0);
REQUIRE(buffer.get<osmium::memory::Item>(0).type() == osmium::item_type::node);
REQUIRE(buffer.get<osmium::Node>(0).id() == 36966060);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 3);
reader.close();
}
SECTION("Using Reader asking for nodes") {
osmium::io::Reader reader(filename("200-nodes"), osmium::osm_entity_bits::node);
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
osmium::memory::Buffer buffer = reader.read();
REQUIRE(buffer.committed() > 0);
REQUIRE(buffer.get<osmium::memory::Item>(0).type() == osmium::item_type::node);
REQUIRE(buffer.get<osmium::Node>(0).id() == 36966060);
REQUIRE(std::distance(buffer.begin(), buffer.end()) == 3);
reader.close();
}
SECTION("Using Reader asking for header only") {
osmium::io::Reader reader(filename("200-nodes"), osmium::osm_entity_bits::nothing);
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
osmium::memory::Buffer buffer = reader.read();
REQUIRE(0 == buffer.committed());
REQUIRE(! buffer);
reader.close();
}
SECTION("Using Reader asking for ways") {
osmium::io::Reader reader(filename("200-nodes"), osmium::osm_entity_bits::way);
osmium::io::Header header = reader.header();
REQUIRE(header.get("generator") == "testdata");
osmium::memory::Buffer buffer = reader.read();
REQUIRE(0 == buffer.committed());
REQUIRE(! buffer);
reader.close();
}
}