1 #include "catch.hpp"
2 
3 #include <osmium/index/detail/tmpfile.hpp>
4 #include <osmium/index/map/dense_file_array.hpp>
5 #include <osmium/index/map/dense_mem_array.hpp>
6 #include <osmium/index/map/dense_mmap_array.hpp>
7 #include <osmium/index/map/sparse_file_array.hpp>
8 #include <osmium/index/map/sparse_mem_array.hpp>
9 #include <osmium/index/map/sparse_mmap_array.hpp>
10 #include <osmium/osm/location.hpp>
11 #include <osmium/osm/types.hpp>
12 
13 using dense_file_array = osmium::index::map::DenseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
14 using sparse_file_array = osmium::index::map::SparseFileArray<osmium::unsigned_object_id_type, osmium::Location>;
15 
16 template <class TMemoryIndex, class TFileIndex>
test_index(std::function<void (TMemoryIndex &,const int)> dump_method)17 void test_index(std::function<void(TMemoryIndex&, const int)> dump_method) {
18     const int fd = osmium::detail::create_tmp_file();
19     REQUIRE(osmium::file_size(fd) == 0);
20     const osmium::unsigned_object_id_type id1 = 12;
21     const osmium::unsigned_object_id_type id2 = 3;
22     const osmium::unsigned_object_id_type id3 = 7;
23     const osmium::Location loc1{1.2, 4.5};
24     const osmium::Location loc2{3.5, -7.2};
25     const osmium::Location loc3{-12.7, 14.5};
26 
27     TMemoryIndex index;
28     index.set(id1, loc1);
29     index.set(id2, loc2);
30     index.set(id3, loc3);
31 
32     // implementation of TMemoryIndex::sort should be empty if it is a dense index
33     index.sort();
34     dump_method(index, fd);
35 
36     REQUIRE(osmium::file_size(fd) >= (3 * sizeof(typename TFileIndex::element_type)));
37 
38     // load index from file
39     TFileIndex file_index{fd};
40 
41     // test retrievals
42     REQUIRE(loc1 == file_index.get(id1));
43     REQUIRE(loc2 == file_index.get(id2));
44     REQUIRE(loc3 == file_index.get(id3));
45     REQUIRE_THROWS_AS(file_index.get(5), osmium::not_found);
46     REQUIRE_THROWS_AS(file_index.get(6), osmium::not_found);
47     REQUIRE_THROWS_AS(file_index.get(200), osmium::not_found);
48 }
49 
50 #ifdef __linux__
51 using dense_mmap_array = osmium::index::map::DenseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
52 
53 TEST_CASE("Dump DenseMmapArray, load as DenseFileArray") {
__anonee7d283e0102(dense_mmap_array& index, const int fd) 54     auto dump_method = [](dense_mmap_array& index, const int fd) { index.dump_as_array(fd);};
55     test_index<dense_mmap_array, dense_file_array>(dump_method);
56 }
57 #else
58 # pragma message("not running 'DenseMmapArray' test case on this machine")
59 #endif
60 
61 using dense_mem_array = osmium::index::map::DenseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
62 
63 TEST_CASE("Dump DenseMemArray, load as DenseFileArray") {
__anonee7d283e0202(dense_mem_array& index, const int fd) 64     auto dump_method = [](dense_mem_array& index, const int fd) { index.dump_as_array(fd);};
65     test_index<dense_mem_array, dense_file_array>(dump_method);
66 }
67 
68 #ifdef __linux__
69 using sparse_mmap_array = osmium::index::map::SparseMmapArray<osmium::unsigned_object_id_type, osmium::Location>;
70 
71 TEST_CASE("Dump SparseMmapArray, load as SparseFileArray") {
__anonee7d283e0302(sparse_mmap_array& index, const int fd) 72     auto dump_method = [](sparse_mmap_array& index, const int fd) { index.dump_as_list(fd);};
73     test_index<sparse_mmap_array, sparse_file_array>(dump_method);
74 }
75 #else
76 # pragma message("not running 'SparseMmapArray' test case on this machine")
77 #endif
78 
79 using sparse_mem_array = osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location>;
80 
81 TEST_CASE("Dump SparseMemArray, load as SparseFileArray") {
__anonee7d283e0402(sparse_mem_array& index, const int fd) 82     auto dump_method = [](sparse_mem_array& index, const int fd) { index.dump_as_list(fd);};
83     test_index<sparse_mem_array, sparse_file_array>(dump_method);
84 }
85