/
test_formatter.cpp
313 lines (268 loc) · 9.61 KB
/
test_formatter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* SPDX-License-Identifier: GPL-2.0-only
*
* This file is part of openstreetmap-cgimap (https://github.com/zerebubuth/openstreetmap-cgimap/).
*
* Copyright (C) 2009-2023 by the CGImap developer community.
* For a full list of authors see the git log.
*/
#include "cgimap/output_formatter.hpp"
#include "test_formatter.hpp"
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
namespace {
bool equal_tags(const tags_t &a, const tags_t &b) {
using vec_tags_t = std::vector<std::pair<std::string, std::string> >;
if (a.size() != b.size()) { return false; }
vec_tags_t sorted_a(a.begin(), a.end());
vec_tags_t sorted_b(b.begin(), b.end());
std::sort(sorted_a.begin(), sorted_a.end());
std::sort(sorted_b.begin(), sorted_b.end());
return std::equal(sorted_a.begin(), sorted_a.end(), sorted_b.begin());
}
bool equal_nodes(const nodes_t &a, const nodes_t &b) {
if (a.size() != b.size()) { return false; }
std::vector<osm_nwr_id_t> a_vec(a.begin(), a.end());
std::vector<osm_nwr_id_t> b_vec(b.begin(), b.end());
return std::equal(a_vec.begin(), a_vec.end(), b_vec.begin());
}
bool equal_members(const members_t &a, const members_t &b) {
if (a.size() != b.size()) { return false; }
std::vector<member_info> a_vec(a.begin(), a.end());
std::vector<member_info> b_vec(b.begin(), b.end());
const size_t n = a_vec.size();
for (size_t i = 0; i < n; ++i) {
if ((a_vec[i].type != b_vec[i].type)
|| (a_vec[i].ref != b_vec[i].ref)
|| (a_vec[i].role != b_vec[i].role)) {
return false;
}
}
return true;
}
} // anonymous namespace
test_formatter::node_t::node_t(const element_info &elem_, double lon_, double lat_,
const tags_t &tags_)
: elem(elem_), lon(lon_), lat(lat_), tags(tags_) {}
bool test_formatter::node_t::operator==(const node_t &other) const {
#define CMP(sym) { if ((sym) != other. sym) { return false; } }
CMP(elem.id);
CMP(elem.version);
CMP(elem.changeset);
CMP(elem.timestamp);
CMP(elem.uid);
CMP(elem.display_name);
CMP(elem.visible);
CMP(lon);
CMP(lat);
#undef CMP
return equal_tags(tags, other.tags);
}
test_formatter::way_t::way_t(const element_info &elem_, const nodes_t &nodes_,
const tags_t &tags_)
: elem(elem_), nodes(nodes_), tags(tags_) {}
bool test_formatter::way_t::operator==(const way_t &other) const {
#define CMP(sym) { if ((sym) != other. sym) { return false; } }
CMP(elem.id);
CMP(elem.version);
CMP(elem.changeset);
CMP(elem.timestamp);
CMP(elem.uid);
CMP(elem.display_name);
CMP(elem.visible);
#undef CMP
return equal_tags(tags, other.tags) && equal_nodes(nodes, other.nodes);
}
test_formatter::relation_t::relation_t(const element_info &elem_,
const members_t &members_,
const tags_t &tags_)
: elem(elem_), members(members_), tags(tags_) {}
bool test_formatter::relation_t::operator==(const relation_t &other) const {
#define CMP(sym) { if ((sym) != other. sym) { return false; } }
CMP(elem.id);
CMP(elem.version);
CMP(elem.changeset);
CMP(elem.timestamp);
CMP(elem.uid);
CMP(elem.display_name);
CMP(elem.visible);
#undef CMP
return equal_tags(tags, other.tags) && equal_members(members, other.members);
}
test_formatter::changeset_t::changeset_t(const changeset_info &info,
const tags_t &tags,
bool include_comments,
const comments_t &comments,
const std::chrono::system_clock::time_point &time)
: m_info(info)
, m_tags(tags)
, m_include_comments(include_comments)
, m_comments(comments)
, m_time(time) {
}
bool test_formatter::changeset_t::operator==(const changeset_t &other) const {
#define CMP(sym) { if (!(sym == other.sym)) { return false; } }
CMP(m_info.id);
CMP(m_info.created_at);
CMP(m_info.closed_at);
CMP(m_info.uid);
CMP(m_info.display_name);
CMP(m_info.bounding_box);
CMP(m_info.num_changes);
CMP(m_info.comments_count);
CMP(m_tags.size());
CMP(m_include_comments);
if (m_include_comments) {
CMP(m_comments.size());
if (!std::equal(m_comments.begin(), m_comments.end(), other.m_comments.begin())) {
return false;
}
}
CMP(m_time);
#undef CMP
return std::equal(m_tags.begin(), m_tags.end(), other.m_tags.begin());
}
mime::type test_formatter::mime_type() const {
throw std::runtime_error("Unimplemented");
}
void test_formatter::start_document(
const std::string &generator, const std::string &root_name) {
}
void test_formatter::end_document() {
}
void test_formatter::write_bounds(const bbox &bounds) {
}
void test_formatter::start_element_type(element_type type) {
}
void test_formatter::end_element_type(element_type type) {
}
void test_formatter::start_action(action_type type) {
}
void test_formatter::end_action(action_type type) {
}
void test_formatter::write_node(const element_info &elem, double lon, double lat,
const tags_t &tags) {
m_nodes.push_back(node_t(elem, lon, lat, tags));
}
void test_formatter::write_way(const element_info &elem, const nodes_t &nodes,
const tags_t &tags) {
m_ways.push_back(way_t(elem, nodes, tags));
}
void test_formatter::write_relation(const element_info &elem,
const members_t &members, const tags_t &tags) {
m_relations.push_back(relation_t(elem, members, tags));
}
void test_formatter::write_changeset(const changeset_info &elem, const tags_t &tags,
bool include_comments, const comments_t &comments,
const std::chrono::system_clock::time_point &time) {
m_changesets.push_back(changeset_t(elem, tags, include_comments, comments, time));
}
void test_formatter::write_diffresult_create_modify(const element_type elem,
const osm_nwr_signed_id_t old_id,
const osm_nwr_id_t new_id,
const osm_version_t new_version) {
}
void test_formatter::write_diffresult_delete(const element_type elem,
const osm_nwr_signed_id_t old_id) {
}
void test_formatter::flush() {}
void test_formatter::error(const std::exception &e) {
throw e;
}
void test_formatter::error(const std::string &str) {
throw std::runtime_error(str);
}
std::ostream &operator<<(std::ostream &out, const element_info &elem) {
out << "element_info("
<< "id=" << elem.id << ", "
<< "version=" << elem.version << ", "
<< "changeset=" << elem.changeset << ", "
<< "timestamp=" << elem.timestamp << ", "
<< "uid=" << elem.uid.value_or(0) << ", "
<< "display_name=" << elem.display_name.value_or("") << ", "
<< "visible=" << elem.visible << ")";
return out;
}
std::ostream &operator<<(std::ostream &out, const test_formatter::node_t &n) {
out << "node(" << n.elem << ", "
<< "lon=" << n.lon << ", "
<< "lat=" << n.lat << ", "
<< "tags{";
for (const tags_t::value_type &v : n.tags) {
out << "\"" << v.first << "\" => \"" << v.second << "\", ";
}
out << "})";
return out;
}
std::ostream &operator<<(std::ostream &out, const bbox &b) {
out << "bbox("
<< b.minlon << ", "
<< b.minlat << ", "
<< b.maxlon << ", "
<< b.maxlat << ")";
return out;
}
std::ostream &operator<<(std::ostream &out, const test_formatter::changeset_t &c) {
out << "changeset(changeset_info("
<< "id=" << c.m_info.id << ", "
<< "created_at=\"" << c.m_info.created_at << "\", "
<< "closed_at=\"" << c.m_info.closed_at << "\", "
<< "uid=" << *c.m_info.uid << ", "
<< "display_name=\"" << c.m_info.display_name.value_or("") << "\", "
<< "bounding_box=" << c.m_info.bounding_box.value_or(bbox{}) << ", "
<< "num_changes=" << c.m_info.num_changes << ", "
<< "comments_count=" << c.m_info.comments_count << "), "
<< "tags{";
for (const tags_t::value_type &v : c.m_tags) {
out << "\"" << v.first << "\" => \"" << v.second << "\", ";
}
out << "}, "
<< "include_comments=" << c.m_include_comments << ", "
<< "comments[";
for (const comments_t::value_type &v : c.m_comments) {
out << "comment(id=" << v.id << ", "
<< "author_id=" << v.author_id << ", "
<< "body=\"" << v.body << "\", "
<< "created_at=\"" << v.created_at << "\", "
<< "author_display_name=\"" << v.author_display_name << "\"), ";
}
std::time_t t = std::chrono::system_clock::to_time_t(c.m_time);
out << "], "
<< "time=" << std::put_time( std::gmtime( &t ), "%FT%T%z")
<< ")";
return out;
}
std::ostream &operator<<(std::ostream &out, const test_formatter::way_t &w) {
out << "way(" << w.elem << ", "
<< "[";
for (const nodes_t::value_type &v : w.nodes) {
out << v << ", ";
}
out << "], {";
for (const tags_t::value_type &v : w.tags) {
out << "\"" << v.first << "\" => \"" << v.second << "\", ";
}
out << "})";
return out;
}
std::ostream &operator<<(std::ostream &out, const member_info &m) {
out << "member_info(type=" << element_type_name(m.type) << ", "
<< "ref=" << m.ref << ", "
<< "role=\"" << m.role << "\")";
return out;
}
std::ostream &operator<<(std::ostream &out, const test_formatter::relation_t &r) {
out << "relation(" << r.elem << ", "
<< "[";
for (const member_info &m : r.members) {
out << m << ", ";
}
out << "], {";
for (const tags_t::value_type &v : r.tags) {
out << "\"" << v.first << "\" => \"" << v.second << "\", ";
}
out << "})";
return out;
}