Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / graph_build_part7.rs
graph_build_part7.rs495 lines · 22.7 KiBRaw
| 1 | #[test] |
| 2 | fn synthetic_extract_produces_the_documented_layout() { |
| 3 | let (pbf_path, dir) = testpbf::write_sample("graph_build"); |
| 4 | let stats = build(&pbf_path, &dir).unwrap(); |
| 5 | let o = read_outputs(&dir); |
| 6 | |
| 7 | // Nodes 1-4 come from the residential way, node 5 is the bus stop; the |
| 8 | // cafe node (6) is on no routable way and carries no stop tag, so it is |
| 9 | // not part of the graph at all. Node 3 is then collapsed: it has exactly |
| 10 | // two neighbours (2 and 4) reached by two Main St segments that agree on |
| 11 | // everything, so no route ever chooses anything there. |
| 12 | assert_eq!(stats.raw_node_count, 5); |
| 13 | assert_eq!(stats.node_count, 4); |
| 14 | assert_eq!(local_of(&o, 3), None, "node 3 should have been collapsed"); |
| 15 | for id in [1, 2, 4, testpbf::STOP_NODE_ID] { |
| 16 | assert!(local_of(&o, id).is_some(), "node {id} should have survived"); |
| 17 | } |
| 18 | assert_eq!(o.meta, { |
| 19 | let mut want = Vec::new(); |
| 20 | want.extend(0x4752_414Du32.to_le_bytes()); |
| 21 | want.extend(GRAPH_VERSION.to_le_bytes()); |
| 22 | want.extend(4u64.to_le_bytes()); |
| 23 | want.extend(stats.edge_count.to_le_bytes()); |
| 24 | want.extend(stats.escape_count.to_le_bytes()); |
| 25 | want.extend(stats.named_edges.to_le_bytes()); |
| 26 | want |
| 27 | }); |
| 28 | |
| 29 | // nodes.bin holds node_count + 1 12-byte records (inflated at the |
| 30 | // test boundary — `read_outputs` unpacks the v7 ZBLK section, so this |
| 31 | // still pins the record count, not the container). |
| 32 | assert_eq!(o.nodes.len(), 12 * 5); |
| 33 | // edges.bin is the blocked record array plus the escape/name tail. |
| 34 | // The blocked section must unpack to exactly the pushed records; the |
| 35 | // tail decodes through the version-aware `Edges` mirror below (escape |
| 36 | // totals, name offsets, content), which is the coverage the old |
| 37 | // exact-length assert provided. |
| 38 | let e = Edges::of(&o); |
| 39 | assert_eq!(e.records_len(), 7 * 7, "7 pushed 7-byte records"); |
| 40 | assert_eq!(stats.escape_count, 0, "no fixture edge is long enough to escape"); |
| 41 | // Main St runs 1-2 and 2-4 in both directions; the service road and the two |
| 42 | // synthetic stop connectors carry no name. |
| 43 | assert_eq!(stats.named_edges, 4); |
| 44 | let edge_count = edge_count(&o); assert_eq!(edge_count, stats.edge_count); |
| 45 | // Uncompacted this was 3 bidirectional segments + 1 oneway = 7 directed |
| 46 | // edges. Collapsing node 3 merges two of the Main St pairs into one, so |
| 47 | // 2 (1-2) + 2 (2-4 via 3) + 1 (service) + 2 synthetic stop edges. |
| 48 | assert_eq!(stats.raw_edge_count, 7); |
| 49 | assert_eq!(edge_count, 2 + 2 + 1 + 2); |
| 50 | |
| 51 | // The sentinel's edge_ptr is the edge count, and edge_ptr is monotonic. |
| 52 | assert_eq!(node_at(&o, 4).2, edge_count); |
| 53 | assert_eq!(node_at(&o, 0).2, 0); |
| 54 | for i in 0..4 { |
| 55 | assert!(node_at(&o, i).2 <= node_at(&o, i + 1).2); |
| 56 | } |
| 57 | |
| 58 | // Nodes are Morton-ordered. |
| 59 | let keys: Vec<u64> = (0..4) |
| 60 | .map(|i| { |
| 61 | let (lat, lon, _) = node_at(&o, i); |
| 62 | spatial_from_e7(lat, lon) |
| 63 | }) |
| 64 | .collect(); |
| 65 | assert!(keys.windows(2).all(|w| w[0] <= w[1]), "{keys:?}"); |
| 66 | |
| 67 | // Every edge's source range agrees with the nodes.bin pointers, and the |
| 68 | // whole edge set matches the fixture's geometry. |
| 69 | // (source lat/lon, target lat/lon, dist_mm, type, speed_limit, name) |
| 70 | type SeenEdge = (i32, i32, i32, i32, u32, u8, u8, String); |
| 71 | let mut seen: Vec<SeenEdge> = Vec::new(); |
| 72 | for lid in 0..4usize { |
| 73 | let (slat, slon, start) = node_at(&o, lid); |
| 74 | let end = node_at(&o, lid + 1).2; |
| 75 | for e in start..end { |
| 76 | let (target, dist, name_off, type_, speed) = edge_at(&o, e as usize); |
| 77 | let (tlat, tlon, _) = node_at(&o, target as usize); |
| 78 | let name = if name_off == NO_NAME { |
| 79 | String::new() |
| 80 | } else { |
| 81 | name_at(&o, name_off) |
| 82 | }; |
| 83 | // The road class must be recoverable after masking the flag off; |
| 84 | // `geometry.rs::ROAD_TYPE_MASK` is the device's half of this. |
| 85 | seen.push((slat, slon, tlat, tlon, dist, type_ & !REVERSE_GEOMETRY_FLAG, speed, name)); |
| 86 | } |
| 87 | } |
| 88 | assert_eq!(seen.len(), edge_count as usize); |
| 89 | |
| 90 | // Main St: 2 segments each way now, type 7 (residential), no maxspeed. |
| 91 | let main: Vec<_> = seen.iter().filter(|e| e.7 == "Main St").collect(); |
| 92 | assert_eq!(main.len(), 4); |
| 93 | assert!(main.iter().all(|e| e.5 == 7 && e.6 == 0)); |
| 94 | // The service way is a oneway from node 4 to node 2, 30 mph -> 48 km/h. |
| 95 | let service: Vec<_> = seen.iter().filter(|e| e.5 == 8).collect(); |
| 96 | assert_eq!(service.len(), 1); |
| 97 | assert_eq!(service[0].6, 48); |
| 98 | assert_eq!((service[0].0, service[0].1), (370_030_000, -1_220_000_000)); |
| 99 | assert_eq!((service[0].2, service[0].3), (370_010_000, -1_220_000_000)); |
| 100 | // The bus stop was reconnected both ways with the synthetic type/speed. |
| 101 | let synth: Vec<_> = seen.iter().filter(|e| e.5 == 12).collect(); |
| 102 | assert_eq!(synth.len(), 2); |
| 103 | assert!(synth.iter().all(|e| e.6 == 5 && e.7.is_empty())); |
| 104 | assert_eq!(stats.reconnected_stops, 1); |
| 105 | assert_eq!(stats.stops_already_connected, 0); |
| 106 | assert_eq!(stats.stops_unreachable, 0); |
| 107 | assert_eq!(stats.lcc_size, 3); |
| 108 | |
| 109 | // The collapsed pair carries the merged distance: node 2 -> node 4 is |
| 110 | // two 10_000e7-unit hops, so twice the 1-2 edge's length. |
| 111 | let n1 = local_of(&o, 1).unwrap(); |
| 112 | let n2 = local_of(&o, 2).unwrap(); |
| 113 | let n4 = local_of(&o, 4).unwrap(); |
| 114 | let dist_of = |from: u32, to: u32, ty: u8| -> u32 { |
| 115 | let (_, _, s) = node_at(&o, from as usize); |
| 116 | let e = node_at(&o, from as usize + 1).2; |
| 117 | (s..e) |
| 118 | .filter_map(|k| { |
| 119 | let (t, d, _, raw, _) = edge_at(&o, k as usize); |
| 120 | (t == to && raw & !REVERSE_GEOMETRY_FLAG == ty).then_some(d) |
| 121 | }) |
| 122 | .next() |
| 123 | .unwrap() |
| 124 | }; |
| 125 | let short = dist_of(n1, n2, 7); |
| 126 | let merged = dist_of(n2, n4, 7); |
| 127 | assert!( |
| 128 | merged.abs_diff(short * 2) <= 2, |
| 129 | "merged {merged} should be about twice {short}" |
| 130 | ); |
| 131 | |
| 132 | // road_names.bin holds each unique string once. "Main St" is the only way |
| 133 | // name; "Test Stop" is the bus stop's code. "Plaza" belongs to an |
| 134 | // unrouted area and must not appear. |
| 135 | assert_eq!(o.names, b"Main St\0Test Stop\0".to_vec()); |
| 136 | assert_eq!(stats.unique_names, 2); |
| 137 | assert_eq!(stats.name_bytes, 18); |
| 138 | |
| 139 | // lanes.bin: [u32 n][(edge_idx, off) x (n + 1)][u16 blob]. Only Main St's |
| 140 | // forward direction carries turn:lanes, and collapsing must not lose them: |
| 141 | // the 1-2 edge plus the merged 2-4 edge. Every other edge is simply absent |
| 142 | // from the index rather than owning an empty range. |
| 143 | let (index, blob) = lane_index(&o); |
| 144 | assert_eq!(index.len(), 3, "two lane-bearing edges plus the sentinel"); |
| 145 | assert!( |
| 146 | index[..2].windows(2).all(|w| w[0].0 < w[1].0), |
| 147 | "index entries must ascend by edge index: {index:?}" |
| 148 | ); |
| 149 | assert_eq!(index[2].0, u32::MAX, "the sentinel is not a real edge index"); |
| 150 | assert_eq!(index[2].1 as usize, blob.len() * 2, "the sentinel is the blob length"); |
| 151 | assert_eq!(o.lanes.len(), 4 + 3 * 8 + blob.len() * 2); |
| 152 | // Two forward Main St edges now instead of three, two lane masks each. |
| 153 | assert_eq!(blob, [[LANE_LEFT, LANE_THROUGH]; 2].concat()); |
| 154 | // Both real entries resolve, which with two of them is the first and the |
| 155 | // last the binary search can land on. |
| 156 | for (edge_idx, _) in &index[..2] { |
| 157 | assert_eq!( |
| 158 | lane_masks(&o, u64::from(*edge_idx)), |
| 159 | Some(vec![LANE_LEFT, LANE_THROUGH]) |
| 160 | ); |
| 161 | } |
| 162 | // Every edge not in the index reads as "no lanes", which is the state |
| 163 | // `routing.rs`'s topology fallback already handles. |
| 164 | let listed: Vec<u32> = index[..2].iter().map(|(e, _)| *e).collect(); |
| 165 | for k in 0..edge_count { |
| 166 | if !listed.contains(&(k as u32)) { |
| 167 | assert_eq!(lane_masks(&o, k), None, "edge {k} should carry no lanes"); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn intermediate_bin_reproduces_the_collapsed_geometry() { |
| 174 | let (pbf_path, dir) = testpbf::write_sample("graph_inter"); |
| 175 | let stats = build(&pbf_path, &dir).unwrap(); |
| 176 | let o = read_outputs(&dir); |
| 177 | let edge_count = edge_count(&o); |
| 178 | |
| 179 | // Offsets reassemble monotonically from both levels, start at zero and end |
| 180 | // at the blob length. The coarse entry for a block is the offset of that |
| 181 | // block's first geometry edge, so every within-block value at g % 32 == 0 |
| 182 | // is zero. `G` itself is the trailer's last 8 bytes. |
| 183 | let t = inter(&o); |
| 184 | assert_eq!(t.geometry_edges, stats.geometry_edges); |
| 185 | let offs: Vec<u64> = (0..=t.geometry_edges).map(|g| t.offset(g)).collect(); |
| 186 | assert_eq!(offs[0], 0); |
| 187 | assert!(offs.windows(2).all(|w| w[0] <= w[1]), "{offs:?}"); |
| 188 | assert_eq!( |
| 189 | *offs.last().unwrap(), |
| 190 | t.blob_bytes as u64, |
| 191 | "last offset must be the blob length" |
| 192 | ); |
| 193 | for g in (0..=t.geometry_edges).step_by(INTERMEDIATE_BLOCK as usize) { |
| 194 | let at = t.coarse_at + (g / INTERMEDIATE_BLOCK) as usize * 8; |
| 195 | let coarse = u64::from_le_bytes(o.inter[at..at + 8].try_into().unwrap()); |
| 196 | assert_eq!(coarse, offs[g as usize], "coarse entry for the block at {g}"); |
| 197 | } |
| 198 | // The presence bitmap and its rank index have to agree: an edge's rank is |
| 199 | // the number of storing edges below it, and the total is `G`. |
| 200 | let mut seen = 0u64; |
| 201 | for k in 0..edge_count { |
| 202 | assert_eq!(t.rank(k), seen, "rank at edge {k}"); |
| 203 | if t.present(k) { |
| 204 | seen += 1; |
| 205 | } |
| 206 | } |
| 207 | assert_eq!(seen, t.geometry_edges); |
| 208 | assert_eq!(stats.intermediate_bytes, o.inter.len() as u64); |
| 209 | |
| 210 | // Every stored polyline must start at its edge's source and end at its |
| 211 | // target, and obey the encoding limits. |
| 212 | let mut with_geometry = 0u64; |
| 213 | for k in 0..edge_count { |
| 214 | let (target, _, _, _, _) = edge_at(&o, k as usize); |
| 215 | let u = (0..node_count(&o) as u32) |
| 216 | .rev() |
| 217 | .find(|i| node_at(&o, *i as usize).2 <= k) |
| 218 | .unwrap(); |
| 219 | let Some((pts, _)) = edge_coords(&o, k) else { |
| 220 | continue; |
| 221 | }; |
| 222 | with_geometry += 1; |
| 223 | assert!(pts.len() <= geom::MAX_POINTS as usize); |
| 224 | let (slat, slon, _) = node_at(&o, u as usize); |
| 225 | let (tlat, tlon, _) = node_at(&o, target as usize); |
| 226 | assert_eq!(pts[0], (slat, slon), "edge {k} geometry starts off its source"); |
| 227 | assert_eq!( |
| 228 | pts[pts.len() - 1], |
| 229 | (tlat, tlon), |
| 230 | "edge {k} geometry ends off its target" |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | // The two Main St edges across the collapsed node must pass through the |
| 235 | // node that was removed. That is the whole claim of compaction: the |
| 236 | // polyline is unchanged, only the node is gone. |
| 237 | let (_, lat3, lon3) = testpbf::NODES.iter().find(|n| n.0 == 3).copied().unwrap(); |
| 238 | let through: Vec<_> = (0..edge_count) |
| 239 | .filter_map(|k| edge_coords(&o, k)) |
| 240 | .filter(|(pts, _)| pts.contains(&(lat3, lon3))) |
| 241 | .collect(); |
| 242 | assert_eq!(through.len(), 2, "both directions must retain node 3's vertex"); |
| 243 | for (pts, _) in &through { |
| 244 | assert_eq!(pts.len(), 3); |
| 245 | } |
| 246 | assert_eq!(with_geometry, 2, "both directions of the merged pair resolve geometry"); |
| 247 | // Only one polyline is stored: the other direction defers to it. That is |
| 248 | // what `REVERSE_GEOMETRY_FLAG` buys, and it halves the blob. |
| 249 | assert_eq!(stats.geometry_edges, 1); |
| 250 | assert_eq!(stats.reversed_edges, 1); |
| 251 | let flagged: Vec<u64> = (0..edge_count) |
| 252 | .filter(|k| edge_at(&o, *k as usize).3 & REVERSE_GEOMETRY_FLAG != 0) |
| 253 | .collect(); |
| 254 | assert_eq!(flagged.len(), 1); |
| 255 | // The flagged edge must be resolvable, which is only true because node 2 |
| 256 | // reaches node 4 exactly once. Node 4 reaches node 2 twice (the merged |
| 257 | // Main St chain and the oneway service road), so had the chain been |
| 258 | // oriented the other way the flag would have had to be suppressed. |
| 259 | assert!(edge_coords(&o, flagged[0]).is_some()); |
| 260 | } |
| 261 | |
| 262 | /// A CSR over `kept` nodes from a list of directed edges. |
| 263 | fn csr_of(kept: u32, edges: &[(u32, u32)]) -> Csr { |
| 264 | let mut edge_ptr = vec![0u64; kept as usize + 1]; |
| 265 | for (s, _) in edges { |
| 266 | edge_ptr[*s as usize + 1] += 1; |
| 267 | } |
| 268 | for i in 0..kept as usize { |
| 269 | edge_ptr[i + 1] += edge_ptr[i]; |
| 270 | } |
| 271 | let mut targets = vec![0u32; edges.len()]; |
| 272 | let mut cursor = edge_ptr.clone(); |
| 273 | for (s, t) in edges { |
| 274 | targets[cursor[*s as usize] as usize] = *t; |
| 275 | cursor[*s as usize] += 1; |
| 276 | } |
| 277 | for v in 0..kept as usize { |
| 278 | targets[edge_ptr[v] as usize..edge_ptr[v + 1] as usize].sort_unstable(); |
| 279 | } |
| 280 | Csr { edge_ptr, targets } |
| 281 | } |
| 282 | |
| 283 | #[test] |
| 284 | fn a_twin_is_unique_only_without_parallel_edges() { |
| 285 | let csr = csr_of(3, &[(0, 1), (1, 0), (1, 2), (2, 1), (2, 1)]); |
| 286 | // 1 -> 0 has exactly one twin (0 -> 1). |
| 287 | assert!(twin_is_unique(&csr, &[], 1, 0)); |
| 288 | // 1 -> 2's twin side holds two parallel 2 -> 1 edges. |
| 289 | assert!(!twin_is_unique(&csr, &[], 1, 2)); |
| 290 | // 2 -> 1's twin side has one 1 -> 2 edge. |
| 291 | assert!(twin_is_unique(&csr, &[], 2, 1)); |
| 292 | // An absent twin is not unique either. |
| 293 | assert!(!twin_is_unique(&csr, &[], 1, 1)); |
| 294 | } |
| 295 | |
| 296 | #[test] |
| 297 | fn a_synthetic_connector_counts_towards_twin_uniqueness() { |
| 298 | // The reader cannot tell a synthetic connector from a road, so a reversed |
| 299 | // edge whose twin side also holds a connector back to its source would |
| 300 | // resolve to whichever the scan met first. The connectors are not in the |
| 301 | // CSR, so they have to be counted separately. |
| 302 | let csr = csr_of(3, &[(0, 1), (1, 0)]); |
| 303 | let synth = [ |
| 304 | Synth { |
| 305 | source: 1, |
| 306 | target: 0, |
| 307 | dist_mm: 5, |
| 308 | }, |
| 309 | Synth { |
| 310 | source: 2, |
| 311 | target: 0, |
| 312 | dist_mm: 5, |
| 313 | }, |
| 314 | ]; |
| 315 | // 0 -> 1's twin side now has the real 1 -> 0 *and* a connector 1 -> 0. |
| 316 | assert!(!twin_is_unique(&csr, &synth, 0, 1)); |
| 317 | // 1 -> 0's twin side has only the real 0 -> 1; the connector points the |
| 318 | // other way and must not be miscounted. |
| 319 | assert!(twin_is_unique(&csr, &synth, 1, 0)); |
| 320 | // A node reachable only by a connector has exactly one edge back. |
| 321 | assert!(twin_is_unique(&csr, &synth, 0, 2)); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn a_way_ref_to_a_node_absent_from_the_file_costs_only_its_own_pairs() { |
| 326 | let (pbf_path, dir) = testpbf::write_dangling_sample("graph_dangling"); |
| 327 | let stats = build(&pbf_path, &dir).unwrap(); |
| 328 | let o = read_outputs(&dir); |
| 329 | |
| 330 | // The dangling ref is marked in the node bitset — marks come from way |
| 331 | // refs — but no blob ever supplies its coordinates, so it must not reach |
| 332 | // the graph at all. |
| 333 | assert_eq!(stats.raw_node_count, 3); |
| 334 | assert_eq!(node_count(&o), 2); |
| 335 | |
| 336 | // Every node in nodes.bin is one the file really defined. A phantom slot |
| 337 | // would read (0, 0), which is a plausible-looking point in the Atlantic |
| 338 | // and would corrupt distances and Morton keys without failing anything. |
| 339 | // Node 3 is defined but has no incident segment (both pairs touching 999 |
| 340 | // fail to resolve), so it is dropped with the phantom: no edge addresses |
| 341 | // it and no route can reach it. |
| 342 | let mut written: Vec<(i32, i32)> = (0..2) |
| 343 | .map(|i| { |
| 344 | let (lat, lon, _) = node_at(&o, i); |
| 345 | (lat, lon) |
| 346 | }) |
| 347 | .collect(); |
| 348 | written.sort(); |
| 349 | let mut want: Vec<(i32, i32)> = testpbf::DANGLING_NODES[..2].iter().map(|n| (n.1, n.2)).collect(); |
| 350 | want.sort(); |
| 351 | assert_eq!(written, want); |
| 352 | |
| 353 | // The way is [1, 2, 999, 3], so the 1-2 pair resolves and both pairs |
| 354 | // touching 999 do not: one bidirectional edge. |
| 355 | assert_eq!(stats.raw_edge_count, 2); |
| 356 | assert_eq!(stats.edge_count, 2); |
| 357 | assert_eq!(stats.lcc_size, 2); |
| 358 | for k in 0..stats.edge_count { |
| 359 | let (target, _, _, type_, _) = edge_at(&o, k as usize); |
| 360 | assert!((target as usize) < 2, "edge {k} targets a node that does not exist"); |
| 361 | assert_eq!(type_ & !REVERSE_GEOMETRY_FLAG, 7); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn a_dem_bakes_a_per_node_elevation_in_final_id_order() { |
| 367 | // A whole-world DEM at zoom 0 (one grid covers every coordinate), so every fixture node is |
| 368 | // guaranteed on coverage. A `dim x dim` ramp gives elevation that varies with position, so |
| 369 | // the baked value is a real interpolation rather than a constant. |
| 370 | let (pbf_path, dir) = testpbf::write_sample("graph_dem_bake"); |
| 371 | let dem_path = dir.join("world.mdem"); |
| 372 | let dim: u16 = 4; |
| 373 | let mut bytes = Vec::new(); |
| 374 | bytes.extend_from_slice(b"MDEM"); |
| 375 | bytes.push(1); // version |
| 376 | bytes.push(0); // out_zoom 0: a single tile covers the world |
| 377 | bytes.extend_from_slice(&dim.to_le_bytes()); |
| 378 | bytes.extend_from_slice(&1u32.to_le_bytes()); // one tile |
| 379 | bytes.extend_from_slice(&0u64.to_le_bytes()); // tile_id(0, 0, 0) == 0 |
| 380 | for row in 0..dim { |
| 381 | for col in 0..dim { |
| 382 | // 32768 bias + a ramp, so metres = row * 100 + col * 10 across the grid. |
| 383 | let sample = 32768u16 + row * 100 + col * 10; |
| 384 | bytes.extend_from_slice(&sample.to_le_bytes()); |
| 385 | } |
| 386 | } |
| 387 | std::fs::write(&dem_path, &bytes).unwrap(); |
| 388 | |
| 389 | build_with( |
| 390 | &pbf_path, |
| 391 | &dir, |
| 392 | Options { |
| 393 | dem: Some(dem_path.clone()), |
| 394 | ..Default::default() |
| 395 | }, |
| 396 | ) |
| 397 | .unwrap(); |
| 398 | |
| 399 | // elevation.bin is `i16[node_count]` in final-node-id order, parallel to nodes.bin |
| 400 | // (blocked under v7 — inflate at the boundary like `read_outputs` does). |
| 401 | let nodes_raw = std::fs::read(dir.join("nodes.bin")).unwrap(); |
| 402 | let nodes = unpack_or_raw(&nodes_raw); |
| 403 | let elev_raw = std::fs::read(dir.join("elevation.bin")).unwrap(); |
| 404 | let elev = unpack_or_raw(&elev_raw); |
| 405 | let node_count = nodes.len() / 12 - 1; // one trailing sentinel node |
| 406 | assert_eq!(elev.len(), node_count * 2, "one i16 per real node"); |
| 407 | |
| 408 | // Round-trip: each baked metre equals the DEM sampled at that node's own coordinate. |
| 409 | let dem = crate::dem::Dem::load(&dem_path).unwrap(); |
| 410 | let mut any_nonzero = false; |
| 411 | for i in 0..node_count { |
| 412 | let lat_e7 = i32::from_le_bytes(nodes[i * 12..i * 12 + 4].try_into().unwrap()); |
| 413 | let lon_e7 = i32::from_le_bytes(nodes[i * 12 + 4..i * 12 + 8].try_into().unwrap()); |
| 414 | let want = dem |
| 415 | .sample_metres(f64::from(lon_e7) * 1e-7, f64::from(lat_e7) * 1e-7) |
| 416 | .unwrap_or(0); |
| 417 | let got = i16::from_le_bytes([elev[i * 2], elev[i * 2 + 1]]); |
| 418 | assert_eq!(got, want, "node {i} baked elevation"); |
| 419 | any_nonzero |= got != 0; |
| 420 | } |
| 421 | assert!(any_nonzero, "the ramp should give at least one node a non-zero elevation"); |
| 422 | } |
| 423 | |
| 424 | #[test] |
| 425 | fn a_build_without_a_dem_omits_elevation_bin() { |
| 426 | let (pbf_path, dir) = testpbf::write_sample("graph_no_dem"); |
| 427 | build(&pbf_path, &dir).unwrap(); |
| 428 | assert!( |
| 429 | !dir.join("elevation.bin").exists(), |
| 430 | "no --dem means no elevation.bin, and the graph is still valid", |
| 431 | ); |
| 432 | } |
| 433 | |
| 434 | #[test] |
| 435 | fn two_runs_are_byte_identical() { |
| 436 | let (pbf_path, dir_a) = testpbf::write_sample("graph_det_a"); |
| 437 | let (_, dir_b) = testpbf::write_sample("graph_det_b"); |
| 438 | build(&pbf_path, &dir_a).unwrap(); |
| 439 | build(&pbf_path, &dir_b).unwrap(); |
| 440 | for f in [ |
| 441 | "metadata.bin", |
| 442 | "nodes.bin", |
| 443 | "edges.bin", |
| 444 | "lanes.bin", |
| 445 | "road_names.bin", |
| 446 | "intermediate.bin", |
| 447 | ] { |
| 448 | assert_eq!( |
| 449 | std::fs::read(dir_a.join(f)).unwrap(), |
| 450 | std::fs::read(dir_b.join(f)).unwrap(), |
| 451 | "{f} differs between runs" |
| 452 | ); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /// The gate for threading the graph build: all six outputs must be byte-identical |
| 457 | /// at every thread count. |
| 458 | /// |
| 459 | /// `nodes.bin` is the one that would move first. Final node ids come from the |
| 460 | /// spatial sort, and every offset in `edges.bin`, `lanes.bin` and |
| 461 | /// `intermediate.bin` is expressed in terms of them — so a sort whose order is not |
| 462 | /// total would not corrupt one file, it would silently renumber the whole pack. |
| 463 | /// Multiple rounds are covered too, because `write_graph` partitions by source and |
| 464 | /// each round writes its own slice of `intermediate.bin`. |
| 465 | #[test] |
| 466 | fn the_thread_count_changes_no_graph_bytes() { |
| 467 | for rounds in [1u32, 3] { |
| 468 | let (pbf_path, base_dir) = testpbf::write_sample(&format!("graph_threads_{rounds}_1")); |
| 469 | let run = |n: usize, dir: &Path| { |
| 470 | crate::par::set_threads(n); |
| 471 | build_with( |
| 472 | &pbf_path, |
| 473 | dir, |
| 474 | Options { |
| 475 | rounds, |
| 476 | ..Default::default() |
| 477 | }, |
| 478 | ) |
| 479 | .unwrap(); |
| 480 | read_outputs(dir) |
| 481 | }; |
| 482 | let base = run(1, &base_dir); |
| 483 | for n in [2, 3, 32] { |
| 484 | let (_, dir) = testpbf::write_sample(&format!("graph_threads_{rounds}_{n}")); |
| 485 | let got = run(n, &dir); |
| 486 | assert_eq!(got.meta, base.meta, "{rounds} round(s), {n} threads: metadata.bin"); |
| 487 | assert_eq!(got.nodes, base.nodes, "{rounds} round(s), {n} threads: nodes.bin"); |
| 488 | assert_eq!(got.edges, base.edges, "{rounds} round(s), {n} threads: edges.bin"); |
| 489 | assert_eq!(got.lanes, base.lanes, "{rounds} round(s), {n} threads: lanes.bin"); |
| 490 | assert_eq!(got.names, base.names, "{rounds} round(s), {n} threads: road_names.bin"); |
| 491 | assert_eq!(got.inter, base.inter, "{rounds} round(s), {n} threads: intermediate.bin"); |
| 492 | } |
| 493 | } |
| 494 | crate::par::clear_threads(); |
| 495 | } |