1 #include "catch.hpp" 2 3 #include <osmium/io/file.hpp> 4 5 #include <iterator> 6 #include <stdexcept> 7 8 TEST_CASE("Default file format") { 9 const osmium::io::File f; 10 REQUIRE(osmium::io::file_format::unknown == f.format()); 11 REQUIRE(osmium::io::file_compression::none == f.compression()); 12 REQUIRE_FALSE(f.has_multiple_object_versions()); 13 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 14 } 15 16 TEST_CASE("File format when empty (stdin/stdout)") { 17 const osmium::io::File f{""}; 18 REQUIRE(osmium::io::file_format::unknown == f.format()); 19 REQUIRE(osmium::io::file_compression::none == f.compression()); 20 REQUIRE_FALSE(f.has_multiple_object_versions()); 21 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 22 } 23 24 TEST_CASE("File format from dash (stdin/stdout)") { 25 const osmium::io::File f{"-"}; 26 REQUIRE(osmium::io::file_format::unknown == f.format()); 27 REQUIRE(osmium::io::file_compression::none == f.compression()); 28 REQUIRE_FALSE(f.has_multiple_object_versions()); 29 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 30 } 31 32 TEST_CASE("File format from dash with osm.bz2") { 33 const osmium::io::File f{"-", "osm.bz2"}; 34 REQUIRE(f.filename().empty()); 35 REQUIRE(osmium::io::file_format::xml == f.format()); 36 REQUIRE(osmium::io::file_compression::bzip2 == f.compression()); 37 REQUIRE_FALSE(f.has_multiple_object_versions()); 38 f.check(); 39 } 40 41 TEST_CASE("Detect file format by suffix 'osm'") { 42 const osmium::io::File f{"test.osm"}; 43 REQUIRE(osmium::io::file_format::xml == f.format()); 44 REQUIRE(osmium::io::file_compression::none == f.compression()); 45 REQUIRE_FALSE(f.has_multiple_object_versions()); 46 f.check(); 47 } 48 49 TEST_CASE("Detect file format by suffix 'pbf'") { 50 const osmium::io::File f{"test.pbf"}; 51 REQUIRE(osmium::io::file_format::pbf == f.format()); 52 REQUIRE(osmium::io::file_compression::none == f.compression()); 53 REQUIRE_FALSE(f.has_multiple_object_versions()); 54 f.check(); 55 } 56 57 TEST_CASE("Detect file format by suffix 'osm.pbf'") { 58 const osmium::io::File f{"test.osm.pbf"}; 59 REQUIRE(osmium::io::file_format::pbf == f.format()); 60 REQUIRE(osmium::io::file_compression::none == f.compression()); 61 REQUIRE_FALSE(f.has_multiple_object_versions()); 62 f.check(); 63 } 64 65 TEST_CASE("Detect file format by suffix 'opl'") { 66 const osmium::io::File f{"test.opl"}; 67 REQUIRE(osmium::io::file_format::opl == f.format()); 68 REQUIRE(osmium::io::file_compression::none == f.compression()); 69 REQUIRE_FALSE(f.has_multiple_object_versions()); 70 f.check(); 71 } 72 73 TEST_CASE("Detect file format by suffix 'osm.opl'") { 74 const osmium::io::File f{"test.osm.opl"}; 75 REQUIRE(osmium::io::file_format::opl == f.format()); 76 REQUIRE(osmium::io::file_compression::none == f.compression()); 77 REQUIRE_FALSE(f.has_multiple_object_versions()); 78 f.check(); 79 } 80 81 TEST_CASE("Detect file format by suffix 'osm.gz'") { 82 const osmium::io::File f{"test.osm.gz"}; 83 REQUIRE(osmium::io::file_format::xml == f.format()); 84 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 85 REQUIRE_FALSE(f.has_multiple_object_versions()); 86 f.check(); 87 } 88 89 TEST_CASE("Detect file format by suffix 'opl.bz2'") { 90 const osmium::io::File f{"test.osm.opl.bz2"}; 91 REQUIRE(osmium::io::file_format::opl == f.format()); 92 REQUIRE(osmium::io::file_compression::bzip2 == f.compression()); 93 REQUIRE_FALSE(f.has_multiple_object_versions()); 94 f.check(); 95 } 96 97 TEST_CASE("Detect file format by suffix 'osc.gz'") { 98 const osmium::io::File f{"test.osc.gz"}; 99 REQUIRE(osmium::io::file_format::xml == f.format()); 100 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 101 REQUIRE(f.has_multiple_object_versions()); 102 f.check(); 103 } 104 105 TEST_CASE("Detect file format by suffix 'opl.gz'") { 106 const osmium::io::File f{"test.osh.opl.gz"}; 107 REQUIRE(osmium::io::file_format::opl == f.format()); 108 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 109 REQUIRE(f.has_multiple_object_versions()); 110 f.check(); 111 } 112 113 TEST_CASE("Detect file format by suffix 'osh.pbf'") { 114 const osmium::io::File f{"test.osh.pbf"}; 115 REQUIRE(osmium::io::file_format::pbf == f.format()); 116 REQUIRE(osmium::io::file_compression::none == f.compression()); 117 REQUIRE(f.has_multiple_object_versions()); 118 f.check(); 119 } 120 121 TEST_CASE("Override file format by suffix 'osm'") { 122 const osmium::io::File f{"test", "osm"}; 123 REQUIRE(osmium::io::file_format::xml == f.format()); 124 REQUIRE(osmium::io::file_compression::none == f.compression()); 125 REQUIRE_FALSE(f.has_multiple_object_versions()); 126 f.check(); 127 } 128 129 TEST_CASE("Override file format by suffix 'pbf'") { 130 const osmium::io::File f{"test", "pbf"}; 131 REQUIRE(osmium::io::file_format::pbf == f.format()); 132 REQUIRE(osmium::io::file_compression::none == f.compression()); 133 REQUIRE_FALSE(f.has_multiple_object_versions()); 134 f.check(); 135 } 136 137 TEST_CASE("Override file format by suffix 'osm.pbf'") { 138 const osmium::io::File f{"test", "osm.pbf"}; 139 REQUIRE(osmium::io::file_format::pbf == f.format()); 140 REQUIRE(osmium::io::file_compression::none == f.compression()); 141 REQUIRE_FALSE(f.has_multiple_object_versions()); 142 f.check(); 143 } 144 145 TEST_CASE("Override file format by suffix 'opl'") { 146 const osmium::io::File f{"test", "opl"}; 147 REQUIRE(osmium::io::file_format::opl == f.format()); 148 REQUIRE(osmium::io::file_compression::none == f.compression()); 149 REQUIRE_FALSE(f.has_multiple_object_versions()); 150 f.check(); 151 } 152 153 TEST_CASE("Override file format by suffix 'osm.opl'") { 154 const osmium::io::File f{"test", "osm.opl"}; 155 REQUIRE(osmium::io::file_format::opl == f.format()); 156 REQUIRE(osmium::io::file_compression::none == f.compression()); 157 REQUIRE_FALSE(f.has_multiple_object_versions()); 158 f.check(); 159 } 160 161 TEST_CASE("Override file format by suffix 'osm.gz'") { 162 const osmium::io::File f{"test", "osm.gz"}; 163 REQUIRE(osmium::io::file_format::xml == f.format()); 164 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 165 REQUIRE_FALSE(f.has_multiple_object_versions()); 166 f.check(); 167 } 168 169 TEST_CASE("Override file format by suffix 'osm.opl.bz2'") { 170 const osmium::io::File f{"test", "osm.opl.bz2"}; 171 REQUIRE(osmium::io::file_format::opl == f.format()); 172 REQUIRE(osmium::io::file_compression::bzip2 == f.compression()); 173 REQUIRE_FALSE(f.has_multiple_object_versions()); 174 f.check(); 175 } 176 177 TEST_CASE("Override file format by suffix 'osc.gz'") { 178 const osmium::io::File f{"test", "osc.gz"}; 179 REQUIRE(osmium::io::file_format::xml == f.format()); 180 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 181 REQUIRE(f.has_multiple_object_versions()); 182 f.check(); 183 } 184 185 TEST_CASE("Override file format by suffix 'osh.opl.gz'") { 186 const osmium::io::File f{"test", "osh.opl.gz"}; 187 REQUIRE(osmium::io::file_format::opl == f.format()); 188 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 189 REQUIRE(f.has_multiple_object_versions()); 190 f.check(); 191 } 192 193 TEST_CASE("File format by suffix 'blackhole'") { 194 const osmium::io::File f{"test.blackhole"}; 195 REQUIRE(osmium::io::file_format::blackhole == f.format()); 196 REQUIRE(osmium::io::file_compression::none == f.compression()); 197 REQUIRE_FALSE(f.has_multiple_object_versions()); 198 f.check(); 199 } 200 201 TEST_CASE("Override file format by suffix 'blackhole'") { 202 const osmium::io::File f{"test", "blackhole"}; 203 REQUIRE(osmium::io::file_format::blackhole == f.format()); 204 REQUIRE(osmium::io::file_compression::none == f.compression()); 205 REQUIRE_FALSE(f.has_multiple_object_versions()); 206 f.check(); 207 } 208 209 TEST_CASE("Override file format by suffix 'osm.blackhole'") { 210 const osmium::io::File f{"test", "osm.blackhole"}; 211 REQUIRE(osmium::io::file_format::blackhole == f.format()); 212 REQUIRE(osmium::io::file_compression::none == f.compression()); 213 REQUIRE_FALSE(f.has_multiple_object_versions()); 214 f.check(); 215 } 216 217 TEST_CASE("Override file format by suffix 'osm.blackhole.bz2'") { 218 const osmium::io::File f{"test", "osm.blackhole.bz2"}; 219 REQUIRE(osmium::io::file_format::blackhole == f.format()); 220 REQUIRE(osmium::io::file_compression::bzip2 == f.compression()); 221 REQUIRE_FALSE(f.has_multiple_object_versions()); 222 f.check(); 223 } 224 225 TEST_CASE("Override file format by suffix 'osh.blackhole.gz'") { 226 const osmium::io::File f{"test", "osh.blackhole.gz"}; 227 REQUIRE(osmium::io::file_format::blackhole == f.format()); 228 REQUIRE(osmium::io::file_compression::gzip == f.compression()); 229 REQUIRE(f.has_multiple_object_versions()); 230 f.check(); 231 } 232 233 TEST_CASE("Override file format by suffix 'osh.pbf'") { 234 const osmium::io::File f{"test", "osh.pbf"}; 235 REQUIRE(osmium::io::file_format::pbf == f.format()); 236 REQUIRE(osmium::io::file_compression::none == f.compression()); 237 REQUIRE(f.has_multiple_object_versions()); 238 f.check(); 239 } 240 241 TEST_CASE("Format option pbf history") { 242 const osmium::io::File f{"test", "pbf,history=true"}; 243 REQUIRE(osmium::io::file_format::pbf == f.format()); 244 REQUIRE(osmium::io::file_compression::none == f.compression()); 245 REQUIRE(f.has_multiple_object_versions()); 246 f.check(); 247 } 248 249 TEST_CASE("Format option pbf foo") { 250 const osmium::io::File f{"test.osm", "pbf,foo=bar"}; 251 REQUIRE(osmium::io::file_format::pbf == f.format()); 252 REQUIRE(osmium::io::file_compression::none == f.compression()); 253 REQUIRE("bar" == f.get("foo")); 254 f.check(); 255 } 256 257 TEST_CASE("Format option xml abc something") { 258 const osmium::io::File f{"test.bla", "xml,abc,some=thing"}; 259 REQUIRE(osmium::io::file_format::xml == f.format()); 260 REQUIRE(osmium::io::file_compression::none == f.compression()); 261 REQUIRE("true" == f.get("abc")); 262 REQUIRE("thing" == f.get("some")); 263 REQUIRE(2 == std::distance(f.begin(), f.end())); 264 f.check(); 265 } 266 267 TEST_CASE("Unknown format 'foo.bar'") { 268 const osmium::io::File f{"test.foo.bar"}; 269 REQUIRE(osmium::io::file_format::unknown == f.format()); 270 REQUIRE(osmium::io::file_compression::none == f.compression()); 271 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 272 } 273 274 TEST_CASE("Unknown format 'foo'") { 275 const osmium::io::File f{"test", "foo"}; 276 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 277 } 278 279 TEST_CASE("Unknown format 'osm.foo'") { 280 const osmium::io::File f{"test", "osm.foo"}; 281 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 282 } 283 284 TEST_CASE("Unknown format 'bla=foo'") { 285 const osmium::io::File f{"test", "bla=foo"}; 286 REQUIRE_THROWS_AS(f.check(), std::runtime_error); 287 } 288 289 TEST_CASE("URL without format") { 290 const osmium::io::File f{"http://www.example.com/api"}; 291 REQUIRE(osmium::io::file_format::xml == f.format()); 292 REQUIRE(osmium::io::file_compression::none == f.compression()); 293 REQUIRE_FALSE(f.has_multiple_object_versions()); 294 f.check(); 295 } 296 297 TEST_CASE("URL without format and filename") { 298 const osmium::io::File f{"https://planet.osm.org/pbf/planet-latest.osm.pbf"}; 299 REQUIRE(osmium::io::file_format::pbf == f.format()); 300 REQUIRE(osmium::io::file_compression::none == f.compression()); 301 REQUIRE_FALSE(f.has_multiple_object_versions()); 302 f.check(); 303 } 304 305 TEST_CASE("URL with format") { 306 const osmium::io::File f{"http://www.example.com/api", "osh"}; 307 REQUIRE(osmium::io::file_format::xml == f.format()); 308 REQUIRE(osmium::io::file_compression::none == f.compression()); 309 REQUIRE(f.has_multiple_object_versions()); 310 f.check(); 311 } 312 313