Skip to content

Commit 241068c

Browse files
committed
Move test buffer class into header so we can reuse it
1 parent aee1be1 commit 241068c

File tree

2 files changed

+72
-49
lines changed

2 files changed

+72
-49
lines changed

tests/common-buffer.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef OSM2PGSQL_TESTS_COMMON_BUFFER_HPP
2+
#define OSM2PGSQL_TESTS_COMMON_BUFFER_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2020 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include "format.hpp"
14+
#include "osmtypes.hpp"
15+
16+
#include <osmium/memory/buffer.hpp>
17+
#include <osmium/opl.hpp>
18+
#include <osmium/osm.hpp>
19+
20+
/**
21+
* Wrapper around an Osmium buffer to create test objects in with some
22+
* convenience.
23+
*/
24+
class test_buffer_t
25+
{
26+
public:
27+
osmium::memory::Buffer const &buffer() const noexcept { return m_buffer; }
28+
29+
osmium::Node const &add_node(std::string const &data)
30+
{
31+
return m_buffer.get<osmium::Node>(add_opl(data));
32+
}
33+
34+
osmium::Way &add_way(std::string const &data)
35+
{
36+
return m_buffer.get<osmium::Way>(add_opl(data));
37+
}
38+
39+
osmium::Way &add_way(osmid_t wid, idlist_t const &ids)
40+
{
41+
assert(!ids.empty());
42+
std::string nodes;
43+
44+
for (auto const id : ids) {
45+
nodes += "n{},"_format(id);
46+
}
47+
48+
nodes.resize(nodes.size() - 1);
49+
50+
return add_way("w{} N{}"_format(wid, nodes));
51+
}
52+
53+
osmium::Relation const &add_relation(std::string const &data)
54+
{
55+
return m_buffer.get<osmium::Relation>(add_opl(data));
56+
}
57+
58+
private:
59+
std::size_t add_opl(std::string const &data)
60+
{
61+
auto const offset = m_buffer.committed();
62+
osmium::opl_parse(data.c_str(), m_buffer);
63+
return offset;
64+
}
65+
66+
osmium::memory::Buffer m_buffer{4096,
67+
osmium::memory::Buffer::auto_grow::yes};
68+
};
69+
70+
#endif // OSM2PGSQL_TESTS_COMMON_BUFFER_HPP

tests/test-middle.cpp

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
#include <algorithm>
1313
#include <cassert>
1414

15-
#include <osmium/opl.hpp>
1615
#include <osmium/osm/crc.hpp>
1716
#include <osmium/osm/crc_zlib.hpp>
1817

1918
#include "dependency-manager.hpp"
2019
#include "middle-pgsql.hpp"
2120
#include "middle-ram.hpp"
2221

22+
#include "common-buffer.hpp"
2323
#include "common-cleanup.hpp"
2424
#include "common-options.hpp"
2525
#include "common-pg.hpp"
@@ -28,59 +28,12 @@ static testing::pg::tempdb_t db;
2828

2929
namespace {
3030

31-
/**
32-
* Wrapper around an Osmium buffer to create test objects in with some
33-
* convenience.
34-
*/
35-
class test_buffer_t
36-
{
37-
public:
38-
osmium::Node const &add_node(std::string const &data)
39-
{
40-
return m_buffer.get<osmium::Node>(add_opl(data));
41-
}
42-
43-
osmium::Way &add_way(std::string const &data)
44-
{
45-
return m_buffer.get<osmium::Way>(add_opl(data));
46-
}
47-
48-
osmium::Way &add_way(osmid_t wid, idlist_t const &ids)
49-
{
50-
assert(!ids.empty());
51-
std::string nodes;
52-
53-
for (auto const id : ids) {
54-
nodes += "n{},"_format(id);
55-
}
56-
57-
nodes.resize(nodes.size() - 1);
58-
59-
return add_way("w{} N{}"_format(wid, nodes));
60-
}
61-
62-
osmium::Relation const &add_relation(std::string const &data)
63-
{
64-
return m_buffer.get<osmium::Relation>(add_opl(data));
65-
}
66-
67-
private:
68-
std::size_t add_opl(std::string const &data)
69-
{
70-
auto const offset = m_buffer.committed();
71-
osmium::opl_parse(data.c_str(), m_buffer);
72-
return offset;
73-
}
74-
75-
osmium::memory::Buffer m_buffer{4096,
76-
osmium::memory::Buffer::auto_grow::yes};
77-
};
78-
7931
void expect_location(osmium::Location loc, osmium::Node const &expected)
8032
{
8133
CHECK(loc.lat() == Approx(expected.location().lat()));
8234
CHECK(loc.lon() == Approx(expected.location().lon()));
8335
}
36+
8437
} // namespace
8538

8639
struct options_slim_default

0 commit comments

Comments
 (0)