Skip to content

Commit 1d6092c

Browse files
committed
Extend tests to check that a few node/way/relation are correctly read
1 parent 94c063a commit 1d6092c

File tree

1 file changed

+233
-1
lines changed

1 file changed

+233
-1
lines changed

tests/reader.rs

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use osmpbfreader;
1+
use osmpbfreader::objects::{Node, NodeId, OsmId, Ref, Relation, RelationId, Tags, Way, WayId};
22
use std::fs::File;
33

44
#[derive(Debug, Default, PartialEq)]
@@ -29,30 +29,208 @@ const PBF_STATS_HIGHWAY: Stats = Stats {
2929
sum_relations_ids: 0,
3030
};
3131

32+
fn check_items() -> (Vec<Node>, Vec<Way>, Vec<Relation>) {
33+
let nodes: Vec<Node> = vec![
34+
Node {
35+
id: NodeId(1724610081),
36+
tags: Tags::new(),
37+
decimicro_lat: (10.2871538 * 1e7) as i32,
38+
decimicro_lon: (-109.2143679 * 1e7) as i32,
39+
},
40+
Node {
41+
id: NodeId(1724610082),
42+
tags: Tags::new(),
43+
decimicro_lat: (10.2872043 * 1e7) as i32,
44+
decimicro_lon: (-109.2157633 * 1e7) as i32,
45+
},
46+
Node {
47+
id: NodeId(2710971331),
48+
tags: {
49+
let mut tags = Tags::new();
50+
tags.insert("historic".into(), "wreck".into());
51+
tags.insert("seamark:type".into(), "wreck".into());
52+
tags
53+
},
54+
decimicro_lat: (10.2873955 * 1e7) as i32,
55+
decimicro_lon: (-109.2123449 * 1e7) as i32,
56+
},
57+
Node {
58+
id: NodeId(12710481252),
59+
tags: Tags::new(),
60+
decimicro_lat: (10.2925456 * 1e7) as i32,
61+
decimicro_lon: (-109.2212479 * 1e7) as i32,
62+
},
63+
];
64+
let ways: Vec<Way> = vec![
65+
Way {
66+
id: WayId(633118718),
67+
tags: {
68+
let mut tags = Tags::new();
69+
tags.insert("natural".into(), "wood".into());
70+
tags.insert("trees".into(), "coconut_palms".into());
71+
tags
72+
},
73+
nodes: vec![
74+
NodeId(5976744399),
75+
NodeId(5976744400),
76+
NodeId(5976744401),
77+
NodeId(5976744402),
78+
NodeId(5976744399),
79+
],
80+
},
81+
Way {
82+
id: WayId(1253640658),
83+
tags: {
84+
let mut tags = Tags::new();
85+
tags.insert("natural".into(), "wood".into());
86+
tags
87+
},
88+
nodes: vec![
89+
NodeId(11653090668),
90+
NodeId(11653090669),
91+
NodeId(11653090670),
92+
NodeId(11653090668),
93+
],
94+
},
95+
Way {
96+
id: WayId(1372754726),
97+
tags: {
98+
let mut tags = Tags::new();
99+
tags.insert("aeroway".into(), "taxiway".into());
100+
tags
101+
},
102+
nodes: vec![
103+
NodeId(12710481236),
104+
NodeId(12710481237),
105+
NodeId(12710481238),
106+
NodeId(12710481239),
107+
],
108+
},
109+
];
110+
let relations: Vec<Relation> = vec![
111+
Relation {
112+
id: RelationId(5883537),
113+
tags: {
114+
let mut tags = Tags::new();
115+
tags.insert("natural".into(), "beach".into());
116+
tags.insert("type".into(), "multipolygon".into());
117+
tags
118+
},
119+
refs: vec![
120+
Ref {
121+
member: OsmId::Way(WayId(392464137)),
122+
role: "inner".into(),
123+
},
124+
Ref {
125+
member: OsmId::Way(WayId(160415456)),
126+
role: "outer".into(),
127+
},
128+
],
129+
},
130+
Relation {
131+
id: RelationId(8803113),
132+
tags: {
133+
let mut tags = Tags::new();
134+
tags.insert("place".into(), "islet".into());
135+
tags.insert("type".into(), "multipolygon".into());
136+
tags
137+
},
138+
refs: vec![Ref {
139+
member: OsmId::Way(WayId(633118720)),
140+
role: "outer".into(),
141+
}],
142+
},
143+
];
144+
145+
(nodes, ways, relations)
146+
}
147+
148+
fn check_items_highway() -> (Vec<Node>, Vec<Way>, Vec<Relation>) {
149+
let nodes: Vec<Node> = vec![
150+
Node {
151+
id: NodeId(12710481223),
152+
tags: Tags::new(),
153+
decimicro_lat: (10.2901053 * 1e7) as i32,
154+
decimicro_lon: (-109.2103612 * 1e7) as i32,
155+
},
156+
Node {
157+
id: NodeId(12710481226),
158+
tags: Tags::new(),
159+
decimicro_lat: (10.2897482 * 1e7) as i32,
160+
decimicro_lon: (-109.2101919 * 1e7) as i32,
161+
},
162+
];
163+
let ways: Vec<Way> = vec![Way {
164+
id: WayId(1372754721),
165+
tags: {
166+
let mut tags = Tags::new();
167+
tags.insert("highway".into(), "residential".into());
168+
tags.insert("name".into(), "Clipperton St".into());
169+
tags
170+
},
171+
nodes: vec![
172+
NodeId(12710481223),
173+
NodeId(12710481226),
174+
NodeId(12710481233),
175+
NodeId(12710481224),
176+
NodeId(12710481225),
177+
],
178+
}];
179+
let relations: Vec<Relation> = vec![];
180+
181+
(nodes, ways, relations)
182+
}
183+
32184
#[test]
33185
fn read_pbf() {
34186
let file = File::open(PBF).unwrap();
35187
let mut pbf = osmpbfreader::OsmPbfReader::new(file);
36188
let mut stats = Stats::default();
37189

190+
let (exp_nodes, exp_ways, exp_relations) = check_items();
191+
let mut num_found_nodes = 0;
192+
let mut num_found_ways = 0;
193+
let mut num_found_relations = 0;
194+
38195
for obj in pbf.iter() {
39196
let obj = obj.unwrap();
40197
match obj {
41198
osmpbfreader::OsmObj::Node(n) => {
42199
stats.num_nodes += 1;
43200
stats.sum_nodes_ids += n.id.0;
201+
for exp_node in &exp_nodes {
202+
if exp_node.id.0 == n.id.0 {
203+
num_found_nodes += 1;
204+
assert_eq!(&n, exp_node);
205+
}
206+
}
44207
}
45208
osmpbfreader::OsmObj::Way(w) => {
46209
stats.num_ways += 1;
47210
stats.sum_ways_ids += w.id.0;
211+
for exp_way in &exp_ways {
212+
if exp_way.id.0 == w.id.0 {
213+
num_found_ways += 1;
214+
assert_eq!(&w, exp_way);
215+
}
216+
}
48217
}
49218
osmpbfreader::OsmObj::Relation(r) => {
50219
stats.num_relations += 1;
51220
stats.sum_relations_ids += r.id.0;
221+
for exp_relation in &exp_relations {
222+
if exp_relation.id.0 == r.id.0 {
223+
num_found_relations += 1;
224+
assert_eq!(&r, exp_relation);
225+
}
226+
}
52227
}
53228
}
54229
}
55230
assert_eq!(stats, PBF_STATS_ALL);
231+
assert_eq!(num_found_nodes, exp_nodes.len());
232+
assert_eq!(num_found_ways, exp_ways.len());
233+
assert_eq!(num_found_relations, exp_relations.len());
56234
}
57235

58236
#[test]
@@ -61,24 +239,50 @@ fn read_pbf_par_iter() {
61239
let mut pbf = osmpbfreader::OsmPbfReader::new(file);
62240
let mut stats = Stats::default();
63241

242+
let (exp_nodes, exp_ways, exp_relations) = check_items();
243+
let mut num_found_nodes = 0;
244+
let mut num_found_ways = 0;
245+
let mut num_found_relations = 0;
246+
64247
for obj in pbf.par_iter() {
65248
let obj = obj.unwrap();
66249
match obj {
67250
osmpbfreader::OsmObj::Node(n) => {
68251
stats.num_nodes += 1;
69252
stats.sum_nodes_ids += n.id.0;
253+
for exp_node in &exp_nodes {
254+
if exp_node.id.0 == n.id.0 {
255+
num_found_nodes += 1;
256+
assert_eq!(&n, exp_node);
257+
}
258+
}
70259
}
71260
osmpbfreader::OsmObj::Way(w) => {
72261
stats.num_ways += 1;
73262
stats.sum_ways_ids += w.id.0;
263+
for exp_way in &exp_ways {
264+
if exp_way.id.0 == w.id.0 {
265+
num_found_ways += 1;
266+
assert_eq!(&w, exp_way);
267+
}
268+
}
74269
}
75270
osmpbfreader::OsmObj::Relation(r) => {
76271
stats.num_relations += 1;
77272
stats.sum_relations_ids += r.id.0;
273+
for exp_relation in &exp_relations {
274+
if exp_relation.id.0 == r.id.0 {
275+
num_found_relations += 1;
276+
assert_eq!(&r, exp_relation);
277+
}
278+
}
78279
}
79280
}
80281
}
81282
assert_eq!(stats, PBF_STATS_ALL);
283+
assert_eq!(num_found_nodes, exp_nodes.len());
284+
assert_eq!(num_found_ways, exp_ways.len());
285+
assert_eq!(num_found_relations, exp_relations.len());
82286
}
83287

84288
#[test]
@@ -88,6 +292,11 @@ fn read_pbf_highway() {
88292
let mut stats = Stats::default();
89293
let mut sum_ids = 0;
90294

295+
let (exp_nodes, exp_ways, exp_relations) = check_items_highway();
296+
let mut num_found_nodes = 0;
297+
let mut num_found_ways = 0;
298+
let mut num_found_relations = 0;
299+
91300
let objs = pbf
92301
.get_objs_and_deps(|obj| obj.is_way() && obj.tags().contains_key("highway"))
93302
.unwrap();
@@ -97,14 +306,34 @@ fn read_pbf_highway() {
97306
osmpbfreader::OsmObj::Node(n) => {
98307
stats.num_nodes += 1;
99308
stats.sum_nodes_ids += n.id.0;
309+
for exp_node in &exp_nodes {
310+
if exp_node.id.0 == n.id.0 {
311+
num_found_nodes += 1;
312+
assert_eq!(n, exp_node);
313+
}
314+
}
100315
}
101316
osmpbfreader::OsmObj::Way(w) => {
102317
stats.num_ways += 1;
103318
stats.sum_ways_ids += w.id.0;
319+
assert!(w.tags.contains_key("highway"));
320+
for exp_way in &exp_ways {
321+
if exp_way.id.0 == w.id.0 {
322+
num_found_ways += 1;
323+
assert_eq!(w, exp_way);
324+
}
325+
}
104326
}
105327
osmpbfreader::OsmObj::Relation(r) => {
106328
stats.num_relations += 1;
107329
stats.sum_relations_ids += r.id.0;
330+
assert!(r.tags.contains_key("highway"));
331+
for exp_relation in &exp_relations {
332+
if exp_relation.id.0 == r.id.0 {
333+
num_found_relations += 1;
334+
assert_eq!(r, exp_relation);
335+
}
336+
}
108337
}
109338
}
110339
}
@@ -113,4 +342,7 @@ fn read_pbf_highway() {
113342
+ PBF_STATS_HIGHWAY.sum_ways_ids
114343
+ PBF_STATS_HIGHWAY.sum_relations_ids;
115344
assert_eq!(sum_ids, exp_sum_ids);
345+
assert_eq!(num_found_nodes, exp_nodes.len());
346+
assert_eq!(num_found_ways, exp_ways.len());
347+
assert_eq!(num_found_relations, exp_relations.len());
116348
}

0 commit comments

Comments
 (0)