Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / traffic_extra.rs
traffic_extra.rs231 lines · 10.1 KiBRaw
| 1 | //! Tests for [`super::traffic`], kept beside the schema rules they pin. |
| 2 | #[cfg(test)] |
| 3 | pub(crate) mod tests { |
| 4 | use super::super::traffic::*; |
| 5 | use crate::store::Sink; |
| 6 | use tilecodec::mamaps::dict::LAYER_TRAFFIC; |
| 7 | |
| 8 | /// One directed edge of a synthetic graph. |
| 9 | pub(crate) struct EdgeSpec { |
| 10 | pub source: u32, |
| 11 | pub target: u32, |
| 12 | pub type_: u8, |
| 13 | /// Interior points as `(d_lat, d_lon)` deltas, chained from the source. An empty list is a |
| 14 | /// straight chord, which is how the presence bitmap says "this edge stores no geometry". |
| 15 | pub interior: Vec<(i16, i16)>, |
| 16 | } |
| 17 | |
| 18 | pub(crate) struct GraphFixture { |
| 19 | pub dir: std::path::PathBuf, |
| 20 | } |
| 21 | impl Drop for GraphFixture { |
| 22 | fn drop(&mut self) { |
| 23 | let _ = std::fs::remove_dir_all(&self.dir); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// Write a real v6 graph to a fresh temp directory, byte for byte as the contract specifies. |
| 28 | /// |
| 29 | /// `pub(crate)` because [`crate::schema::junction`] reads the same four files and tests against |
| 30 | /// the same layout; a second writer would be a second chance to disagree with `osm_ingest`. |
| 31 | /// |
| 32 | /// `edges` must be grouped by `source` ascending, which is what makes `nodes.bin`'s `edge_ptr` |
| 33 | /// a CSR row pointer. `lanes` is `(edge_idx, masks)` ascending, and writes no `lanes.bin` at |
| 34 | /// all when empty — which is the common shape of a real graph. |
| 35 | pub(crate) fn write_graph( |
| 36 | tag: &str, |
| 37 | coords: &[(i32, i32)], |
| 38 | edges: &[EdgeSpec], |
| 39 | lanes: &[(u32, Vec<u16>)], |
| 40 | ) -> GraphFixture { |
| 41 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 42 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 43 | let dir = std::env::temp_dir().join(format!( |
| 44 | "mamaps_{tag}_{}_{}", |
| 45 | std::process::id(), |
| 46 | NEXT.fetch_add(1, Ordering::Relaxed), |
| 47 | )); |
| 48 | std::fs::create_dir_all(&dir).expect("mkdir"); |
| 49 | |
| 50 | let node_count = coords.len() as u64; |
| 51 | let edge_count = edges.len() as u64; |
| 52 | assert!( |
| 53 | edges.windows(2).all(|w| w[0].source <= w[1].source), |
| 54 | "a CSR row pointer needs the edges grouped by source", |
| 55 | ); |
| 56 | |
| 57 | // metadata.bin (40 bytes). |
| 58 | let mut meta = Vec::new(); |
| 59 | meta.extend_from_slice(&MARG_MAGIC.to_le_bytes()); |
| 60 | // v6: the fixture hand-encodes raw record tables, so it stamps the |
| 61 | // version those bytes are. |
| 62 | meta.extend_from_slice(&GRAPH_VERSION_MIN.to_le_bytes()); |
| 63 | meta.extend_from_slice(&node_count.to_le_bytes()); |
| 64 | meta.extend_from_slice(&edge_count.to_le_bytes()); |
| 65 | meta.extend_from_slice(&0u64.to_le_bytes()); // escape_count |
| 66 | meta.extend_from_slice(&0u64.to_le_bytes()); // named_edges |
| 67 | std::fs::write(dir.join("metadata.bin"), &meta).expect("meta"); |
| 68 | |
| 69 | // nodes.bin: NodeRec[node_count + 1], 12 bytes each. `edge_ptr` is the index of the first |
| 70 | // edge leaving each node, and the sentinel holds `edge_count`. |
| 71 | let mut nodes = Vec::new(); |
| 72 | for (n, (lat, lon)) in coords.iter().enumerate() { |
| 73 | let first = edges.iter().position(|e| e.source as usize >= n).unwrap_or(edges.len()); |
| 74 | nodes.extend_from_slice(&lat.to_le_bytes()); |
| 75 | nodes.extend_from_slice(&lon.to_le_bytes()); |
| 76 | nodes.extend_from_slice(&(first as u32).to_le_bytes()); |
| 77 | } |
| 78 | nodes.extend_from_slice(&0i32.to_le_bytes()); |
| 79 | nodes.extend_from_slice(&0i32.to_le_bytes()); |
| 80 | nodes.extend_from_slice(&(edge_count as u32).to_le_bytes()); |
| 81 | std::fs::write(dir.join("nodes.bin"), &nodes).expect("nodes"); |
| 82 | |
| 83 | // edges.bin: EdgeRec[edge_count], 7 bytes: i16 target_delta, u24 dist, u8 type_, u8 speed. |
| 84 | let mut raw = Vec::new(); |
| 85 | for e in edges { |
| 86 | let delta = (i64::from(e.target) - i64::from(e.source)) as i16; |
| 87 | raw.extend_from_slice(&delta.to_le_bytes()); |
| 88 | // dist: a plausible non-escape value. |
| 89 | raw.extend_from_slice(&[0x10, 0x00, 0x00]); |
| 90 | raw.push(e.type_); |
| 91 | raw.push(50); // speed_limit |
| 92 | } |
| 93 | // Pad the EdgeRec section to an 8-byte boundary, then the all-zero escape block index. |
| 94 | while raw.len() % 8 != 0 { |
| 95 | raw.push(0); |
| 96 | } |
| 97 | for _ in 0..(edge_count.div_ceil(ESCAPE_BLOCK) + 1) { |
| 98 | raw.extend_from_slice(&0u32.to_le_bytes()); |
| 99 | } |
| 100 | std::fs::write(dir.join("edges.bin"), &raw).expect("edges"); |
| 101 | |
| 102 | // intermediate.bin: the interior-point blob, then the presence bitmap, the coarse offsets |
| 103 | // and the per-geometry-edge within-block offsets, then the geometry-edge count. |
| 104 | let mut blob = Vec::new(); |
| 105 | let mut within = Vec::new(); |
| 106 | let mut present = vec![0u8; edge_count.div_ceil(8).max(1) as usize]; |
| 107 | let mut g_edges = 0u64; |
| 108 | for (idx, e) in edges.iter().enumerate() { |
| 109 | if e.interior.is_empty() { |
| 110 | continue; |
| 111 | } |
| 112 | present[idx / 8] |= 1 << (idx % 8); |
| 113 | within.extend_from_slice(&(blob.len() as u16).to_le_bytes()); |
| 114 | for (d_lat, d_lon) in &e.interior { |
| 115 | blob.extend_from_slice(&d_lat.to_le_bytes()); |
| 116 | blob.extend_from_slice(&d_lon.to_le_bytes()); |
| 117 | } |
| 118 | g_edges += 1; |
| 119 | } |
| 120 | within.extend_from_slice(&(blob.len() as u16).to_le_bytes()); |
| 121 | |
| 122 | let mut inter = blob; |
| 123 | // One rank word per RANK_BLOCK_BYTES of presence, all zero: every fixture here is well |
| 124 | // under 512 edges, so the rank of the first block is 0 and the rest is a popcount scan. |
| 125 | for _ in 0..(edge_count.div_ceil(512) + 1) { |
| 126 | inter.extend_from_slice(&0u64.to_le_bytes()); |
| 127 | } |
| 128 | inter.extend_from_slice(&present); |
| 129 | for _ in 0..(g_edges.div_ceil(INTERMEDIATE_BLOCK) + 1) { |
| 130 | inter.extend_from_slice(&0u64.to_le_bytes()); |
| 131 | } |
| 132 | inter.extend_from_slice(&within); |
| 133 | inter.extend_from_slice(&g_edges.to_le_bytes()); |
| 134 | std::fs::write(dir.join("intermediate.bin"), &inter).expect("inter"); |
| 135 | |
| 136 | // lanes.bin: [u32 n][(u32 edge_idx, u32 blob_off) x (n + 1)][u16 blob]. Sparse, so a graph |
| 137 | // with no tagged turn lanes carries no file at all. |
| 138 | if !lanes.is_empty() { |
| 139 | let mut index = Vec::new(); |
| 140 | let mut masks = Vec::new(); |
| 141 | index.extend_from_slice(&(lanes.len() as u32).to_le_bytes()); |
| 142 | for (idx, lane_masks) in lanes { |
| 143 | index.extend_from_slice(&idx.to_le_bytes()); |
| 144 | index.extend_from_slice(&(masks.len() as u32).to_le_bytes()); |
| 145 | for m in lane_masks { |
| 146 | masks.extend_from_slice(&m.to_le_bytes()); |
| 147 | } |
| 148 | } |
| 149 | // The trailing sentinel exists only to give the last edge an end offset. |
| 150 | index.extend_from_slice(&u32::MAX.to_le_bytes()); |
| 151 | index.extend_from_slice(&(masks.len() as u32).to_le_bytes()); |
| 152 | index.extend_from_slice(&masks); |
| 153 | std::fs::write(dir.join("lanes.bin"), &index).expect("lanes"); |
| 154 | } |
| 155 | |
| 156 | GraphFixture { dir } |
| 157 | } |
| 158 | |
| 159 | /// The traffic layer's own graph: four nodes in a square. Edges: |
| 160 | /// * 0: node0 -> node1, one-way, curved (one interior point). Emitted: 2 segments. |
| 161 | /// * 1: node1 -> node2, straight two-way (canonical, `1 < 2`). Emitted: 1 segment. |
| 162 | /// * 2: node2 -> node1, straight two-way (non-canonical twin of edge 1). Skipped. |
| 163 | /// * 3: node2 -> node3, pedestrian (type 10). Skipped (not drivable). |
| 164 | fn write_fixture() -> GraphFixture { |
| 165 | let coords: [(i32, i32); 4] = [ |
| 166 | (35_000_000, -120_000_000), |
| 167 | (35_000_000, -119_990_000), |
| 168 | (35_010_000, -119_990_000), |
| 169 | (35_010_000, -120_000_000), |
| 170 | ]; |
| 171 | let edge = |source, target, type_, interior: &[(i16, i16)]| EdgeSpec { |
| 172 | source, |
| 173 | target, |
| 174 | type_, |
| 175 | interior: interior.to_vec(), |
| 176 | }; |
| 177 | write_graph( |
| 178 | "traffic", |
| 179 | &coords, |
| 180 | &[ |
| 181 | // An interior point roughly midway, curving off the straight chord. |
| 182 | edge(0, 1, 1, &[(3_000, -5_000)]), |
| 183 | edge(1, 2, 7, &[]), |
| 184 | edge(2, 1, 7, &[]), |
| 185 | edge(2, 3, 10, &[]), |
| 186 | ], |
| 187 | &[], |
| 188 | ) |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn the_component_id_packing_round_trips() { |
| 193 | for (edge, seg) in [(0u64, 0u32), (1, 0), (5, 254), (1_000_000, 42)] { |
| 194 | let id = pack_component_id(edge, seg); |
| 195 | assert_eq!(unpack_component_id(id), (edge, seg)); |
| 196 | } |
| 197 | // The contract's worked example. |
| 198 | assert_eq!(pack_component_id(5, 3), (5 << 16) | 3); |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn a_synthetic_graph_streams_the_expected_segments() { |
| 203 | let fixture = write_fixture(); |
| 204 | let spill = fixture.dir.join("features.tmp"); |
| 205 | let mut sink = Sink::create(&spill).expect("sink"); |
| 206 | let emitted = stream_graph(&fixture.dir, &mut sink).expect("stream"); |
| 207 | let store = sink.finish(&spill).expect("finish"); |
| 208 | |
| 209 | // Edge 0 (curved, 3 vertices) -> 2 segments; edge 1 (straight canonical) -> 1 segment. |
| 210 | // Edge 2 is the non-canonical twin of edge 1 (skipped); edge 3 is pedestrian (skipped). |
| 211 | assert_eq!(emitted, 3, "two segments from the curved edge, one from the straight two-way"); |
| 212 | assert_eq!(store.len(), 3); |
| 213 | |
| 214 | // Read every feature back and confirm each id unpacks to a valid (big_edge_id, seg_index). |
| 215 | let mut reader = store.reader().expect("reader"); |
| 216 | let mut ids = Vec::new(); |
| 217 | while let Some(feature) = reader.next().expect("read") { |
| 218 | assert_eq!(feature.class.layer, LAYER_TRAFFIC); |
| 219 | assert_eq!(feature.class.min_zoom, MIN_ZOOM); |
| 220 | let (edge, seg) = unpack_component_id(feature.id); |
| 221 | assert!(edge < 4, "edge {edge} is past the graph"); |
| 222 | assert!(seg <= 254, "seg_index {seg} does not fit the contract"); |
| 223 | ids.push((edge, seg)); |
| 224 | } |
| 225 | // The curved edge 0 contributes segments 0 and 1; the straight edge 1 contributes segment 0. |
| 226 | assert!(ids.contains(&(0, 0)), "{ids:?}"); |
| 227 | assert!(ids.contains(&(0, 1)), "{ids:?}"); |
| 228 | assert!(ids.contains(&(1, 0)), "{ids:?}"); |
| 229 | let _ = std::fs::remove_file(&spill); |
| 230 | } |
| 231 | } |