#include "catch.hpp" #include #include #include #include #include #include #include TEST_CASE("File based index") { const int fd = osmium::detail::create_tmp_file(); REQUIRE(osmium::util::file_size(fd) == 0); const osmium::unsigned_object_id_type id1 = 6; const osmium::unsigned_object_id_type id2 = 3; const osmium::Location loc1(1.2, 4.5); const osmium::Location loc2(3.5, -7.2); SECTION("dense index") { using index_type = osmium::index::map::DenseFileArray; constexpr const size_t S = sizeof(index_type::element_type); { index_type index{fd}; REQUIRE(index.size() == 0); REQUIRE_THROWS_AS(index.get( 0), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 1), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 3), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 5), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 6), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 7), const osmium::not_found&); REQUIRE_THROWS_AS(index.get(100), const osmium::not_found&); index.set(id1, loc1); REQUIRE(index.size() == 7); index.set(id2, loc2); REQUIRE(index.size() == 7); index.sort(); REQUIRE(loc1 == index.get(id1)); REQUIRE(loc2 == index.get(id2)); REQUIRE_THROWS_AS(index.get( 0), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 1), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 5), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 7), const osmium::not_found&); REQUIRE_THROWS_AS(index.get(100), const osmium::not_found&); REQUIRE(index.size() == 7); REQUIRE(std::distance(index.cbegin(), index.cend()) == 7); REQUIRE(osmium::util::file_size(fd) >= (6 * S)); } { index_type index{fd}; REQUIRE(osmium::util::file_size(fd) >= (6 * S)); REQUIRE(index.size() == 7); REQUIRE(loc1 == index.get(id1)); REQUIRE(loc2 == index.get(id2)); REQUIRE_THROWS_AS(index.get( 0), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 1), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 5), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 7), const osmium::not_found&); REQUIRE_THROWS_AS(index.get(100), const osmium::not_found&); REQUIRE(index.size() == 7); REQUIRE(std::distance(index.cbegin(), index.cend()) == 7); auto it = index.cbegin(); REQUIRE(*it++ == osmium::Location{}); REQUIRE(*it++ == osmium::Location{}); REQUIRE(*it++ == osmium::Location{}); REQUIRE(*it++ == loc2); REQUIRE(*it++ == osmium::Location{}); REQUIRE(*it++ == osmium::Location{}); REQUIRE(*it++ == loc1); REQUIRE(it++ == index.cend()); } } SECTION("sparse index") { using index_type = osmium::index::map::SparseFileArray; constexpr const size_t S = sizeof(index_type::element_type); { index_type index{fd}; REQUIRE(index.size() == 0); REQUIRE_THROWS_AS(index.get( 0), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 1), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 3), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 5), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 6), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 7), const osmium::not_found&); REQUIRE_THROWS_AS(index.get(100), const osmium::not_found&); index.set(id1, loc1); REQUIRE(index.size() == 1); index.set(id2, loc2); REQUIRE(index.size() == 2); index.sort(); REQUIRE(loc1 == index.get(id1)); REQUIRE(loc2 == index.get(id2)); REQUIRE_THROWS_AS(index.get( 0), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 1), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 5), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 7), const osmium::not_found&); REQUIRE_THROWS_AS(index.get(100), const osmium::not_found&); REQUIRE(index.size() == 2); REQUIRE(std::distance(index.cbegin(), index.cend()) == 2); REQUIRE(osmium::util::file_size(fd) >= (2 * S)); } { index_type index{fd}; REQUIRE(osmium::util::file_size(fd) >= (2 * S)); REQUIRE(index.size() == 2); REQUIRE(loc1 == index.get(id1)); REQUIRE(loc2 == index.get(id2)); REQUIRE_THROWS_AS(index.get( 0), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 1), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 5), const osmium::not_found&); REQUIRE_THROWS_AS(index.get( 7), const osmium::not_found&); REQUIRE_THROWS_AS(index.get(100), const osmium::not_found&); REQUIRE(index.size() == 2); REQUIRE(std::distance(index.cbegin(), index.cend()) == 2); } } }