First Commit
This commit is contained in:
commit
8a6c7106e3
1312 files changed
+425396
No files matched your search
Binary file not shown.
Binary file not shown.
+1
@@ -0,0 +1 @@
|
||||
n1 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.02 y1.02
|
||||
+22
@@ -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.
@@ -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
@@ -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.
@@ -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
@@ -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.
@@ -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
@@ -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.
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
n1 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.02 y1.02
|
||||
+1
@@ -0,0 +1 @@
|
||||
n1 v1 dV c1 t2014-01-01T00:00:00Z i1 utest T x1.02 y1.02
|
||||
+4
@@ -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>
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
+1
@@ -0,0 +1 @@
|
||||
TESTDATA
|
||||
Binary file not shown.
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.
@@ -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.
Whitespace-only changes.
+148
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+1309
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;
|
||||
}
|
||||
|
||||
@@ -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 == "& " ' < > 
 
 	");
|
||||
}
|
||||
|
||||
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
@@ -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
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user