Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / chains_part2.rs
chains_part2.rs221 lines · 8.8 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | |
| 5 | /// A straight line of nodes 0.001 degrees apart, well inside the i16 delta |
| 6 | /// range, so nothing in these tests is interpolated. |
| 7 | fn coords(n: usize) -> Vec<geom::Pt> { |
| 8 | (0..n) |
| 9 | .map(|i| (370_000_000 + (i as i32) * 10_000, -1_220_000_000)) |
| 10 | .collect() |
| 11 | } |
| 12 | |
| 13 | #[test] |
| 14 | fn a_run_is_split_to_fit_the_geometry_budget() { |
| 15 | // Nodes 0.02 degrees apart: about 7 encoded points per segment, so ~36 |
| 16 | // segments fill one edge's 256-point budget. |
| 17 | let far: Vec<geom::Pt> = (0..81) |
| 18 | .map(|i| (370_000_000 + i * 200_000, -1_220_000_000)) |
| 19 | .collect(); |
| 20 | let path: Vec<u32> = (0..81).collect(); |
| 21 | let runs = split_runs(&path, &far); |
| 22 | assert!(runs.len() >= 3, "{runs:?}"); |
| 23 | // The runs must tile the path, sharing their boundary nodes. |
| 24 | assert_eq!(runs[0].0, 0); |
| 25 | assert_eq!(runs.last().unwrap().1, 80); |
| 26 | for w in runs.windows(2) { |
| 27 | assert_eq!(w[0].1, w[1].0, "runs must share a node"); |
| 28 | } |
| 29 | // Every run must be encodable, or be a bare chord the reader can infer. |
| 30 | for (lo, hi) in &runs { |
| 31 | let pts: Vec<geom::Pt> = path[*lo..=*hi].iter().map(|n| far[*n as usize]).collect(); |
| 32 | assert!(pts.len() == 2 || geom::fits(&pts), "{} points", pts.len()); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn a_single_unencodable_segment_becomes_its_own_run() { |
| 38 | // 30 degrees apart: one segment needs ~9155 encoded points, far past the |
| 39 | // ceiling, so it must be emitted alone and left without geometry. |
| 40 | let far = vec![(100_000_000, 0), (400_000_000, 0), (700_000_000, 0)]; |
| 41 | let runs = split_runs(&[0, 1, 2], &far); |
| 42 | assert_eq!(runs, vec![(0, 1), (1, 2)]); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn a_short_run_is_never_split() { |
| 47 | let c = coords(5); |
| 48 | assert_eq!(split_runs(&[0, 1, 2, 3, 4], &c), vec![(0, 4)]); |
| 49 | assert_eq!(split_runs(&[0, 1], &c), vec![(0, 1)]); |
| 50 | } |
| 51 | |
| 52 | fn chain(pts_start: u64, pts_len: u32) -> Chain { |
| 53 | Chain { |
| 54 | pts_start, |
| 55 | pts_len, |
| 56 | dist_mm: 4_000, |
| 57 | name_offset: 17, |
| 58 | type_: 7, |
| 59 | speed_limit: 48, |
| 60 | oneway: true, |
| 61 | fwd_lane_off: 3, |
| 62 | fwd_lane_count: 2, |
| 63 | bwd_lane_off: 9, |
| 64 | bwd_lane_count: 1, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn spill_dir(tag: &str) -> std::path::PathBuf { |
| 69 | let dir = std::env::temp_dir().join(format!("osm_ingest_{tag}")); |
| 70 | let _ = std::fs::remove_dir_all(&dir); |
| 71 | std::fs::create_dir_all(&dir).unwrap(); |
| 72 | dir |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn the_spill_round_trips_chains_and_resolves_points_to_coordinates() { |
| 77 | let dir = spill_dir("spill_round_trip"); |
| 78 | let all = coords(6); |
| 79 | // Two chains tiling six points, laid out contiguously as the chain pass |
| 80 | // produces them. |
| 81 | let dense: Vec<u32> = vec![0, 1, 2, 3, 4, 5]; |
| 82 | let src = vec![chain(0, 4), chain(4, 2)]; |
| 83 | |
| 84 | let spill = Spill::new(&dir); |
| 85 | spill.write(&src, &dense, &all).unwrap(); |
| 86 | assert_eq!(spill.chain_count().unwrap(), 2); |
| 87 | |
| 88 | let (back, pts) = spill.read_all().unwrap(); |
| 89 | assert_eq!(pts, all, "points come back as coordinates, in order"); |
| 90 | assert_eq!(back.len(), 2); |
| 91 | // The endpoints are node ids, which coordinates alone could not recover. |
| 92 | assert_eq!((back[0].first, back[0].last), (0, 3)); |
| 93 | assert_eq!((back[1].first, back[1].last), (4, 5)); |
| 94 | for (a, b) in src.iter().zip(&back) { |
| 95 | assert_eq!(b.pts_start, a.pts_start); |
| 96 | assert_eq!(b.pts_len, a.pts_len); |
| 97 | assert_eq!(b.dist_mm, a.dist_mm); |
| 98 | assert_eq!(b.name_offset, a.name_offset); |
| 99 | assert_eq!(b.type_, a.type_); |
| 100 | assert_eq!(b.speed_limit, a.speed_limit); |
| 101 | assert_eq!(b.oneway, a.oneway); |
| 102 | assert_eq!((b.fwd_lane_off, b.fwd_lane_count), (a.fwd_lane_off, a.fwd_lane_count)); |
| 103 | assert_eq!((b.bwd_lane_off, b.bwd_lane_count), (a.bwd_lane_off, a.bwd_lane_count)); |
| 104 | } |
| 105 | // Each chain's polyline is addressable without reading the ones before it, |
| 106 | // which is what the fixed-size header buys. |
| 107 | assert_eq!(back[1].pts(&pts), &all[4..6]); |
| 108 | |
| 109 | spill.remove(); |
| 110 | assert!(spill.chain_count().is_err(), "the spill is gone after remove"); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn two_spills_of_the_same_chains_are_byte_identical() { |
| 115 | let all = coords(6); |
| 116 | let dense: Vec<u32> = vec![0, 1, 2, 3, 4, 5]; |
| 117 | let src = vec![chain(0, 4), chain(4, 2)]; |
| 118 | let mut written: Vec<(Vec<u8>, Vec<u8>)> = Vec::new(); |
| 119 | for tag in ["spill_det_a", "spill_det_b"] { |
| 120 | let dir = spill_dir(tag); |
| 121 | let spill = Spill::new(&dir); |
| 122 | spill.write(&src, &dense, &all).unwrap(); |
| 123 | written.push(( |
| 124 | std::fs::read(&spill.hdr).unwrap(), |
| 125 | std::fs::read(&spill.pts).unwrap(), |
| 126 | )); |
| 127 | } |
| 128 | assert_eq!(written[0].0, written[1].0, "chains.hdr differs between runs"); |
| 129 | assert_eq!(written[0].1, written[1].1, "chains.pts differs between runs"); |
| 130 | // The record stride is what lets a later pass seek to chain k, so it is |
| 131 | // part of the format and not an implementation detail. |
| 132 | assert_eq!(written[0].0.len() as u64, 2 * CHAIN_REC_BYTES); |
| 133 | assert_eq!(written[0].1.len() as u64, 6 * CHAIN_PT_BYTES); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn streaming_the_spill_matches_writing_it_in_one_go() { |
| 138 | // The chain pass streams into the spill from its sink so it never holds the |
| 139 | // chain set; the reference path still writes it in one call. Those two must |
| 140 | // produce the same bytes, or the two collapse paths would not be comparable |
| 141 | // and the round-count byte-identity test would be checking the wrong thing. |
| 142 | let all = coords(9); |
| 143 | let dense: Vec<u32> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8]; |
| 144 | let src = vec![chain(0, 4), chain(4, 2), chain(6, 3)]; |
| 145 | |
| 146 | let batch = Spill::new(&spill_dir("spill_batch")); |
| 147 | batch.write(&src, &dense, &all).unwrap(); |
| 148 | |
| 149 | let streamed = Spill::new(&spill_dir("spill_stream")); |
| 150 | let mut w = streamed.writer().unwrap(); |
| 151 | for c in &src { |
| 152 | let lo = c.pts_start as usize; |
| 153 | w.push(c, &dense[lo..lo + c.pts_len as usize], &all).unwrap(); |
| 154 | } |
| 155 | assert_eq!(w.finish().unwrap(), 3); |
| 156 | |
| 157 | assert_eq!( |
| 158 | std::fs::read(&batch.hdr).unwrap(), |
| 159 | std::fs::read(&streamed.hdr).unwrap(), |
| 160 | "chains.hdr differs between the batch and streaming writers" |
| 161 | ); |
| 162 | assert_eq!( |
| 163 | std::fs::read(&batch.pts).unwrap(), |
| 164 | std::fs::read(&streamed.pts).unwrap(), |
| 165 | "chains.pts differs between the batch and streaming writers" |
| 166 | ); |
| 167 | assert_eq!(streamed.chain_count().unwrap(), 3); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn splitting_the_two_files_across_directories_changes_no_bytes() { |
| 172 | // The header and the points can live on different filesystems, because one |
| 173 | // is streamed and the other is seeked. Which directory each lands in must |
| 174 | // not affect what is in them. |
| 175 | let all = coords(6); |
| 176 | let dense: Vec<u32> = vec![0, 1, 2, 3, 4, 5]; |
| 177 | let src = vec![chain(0, 4), chain(4, 2)]; |
| 178 | |
| 179 | let together = Spill::new(&spill_dir("spill_together")); |
| 180 | together.write(&src, &dense, &all).unwrap(); |
| 181 | |
| 182 | let hdr_dir = spill_dir("spill_apart_hdr"); |
| 183 | let pts_dir = spill_dir("spill_apart_pts"); |
| 184 | let apart = Spill::split(&hdr_dir, &pts_dir); |
| 185 | apart.write(&src, &dense, &all).unwrap(); |
| 186 | |
| 187 | assert_eq!( |
| 188 | std::fs::read(&together.hdr).unwrap(), |
| 189 | std::fs::read(&apart.hdr).unwrap() |
| 190 | ); |
| 191 | assert_eq!( |
| 192 | std::fs::read(&together.pts).unwrap(), |
| 193 | std::fs::read(&apart.pts).unwrap() |
| 194 | ); |
| 195 | // Each file really is under its own directory, and the reader finds both. |
| 196 | assert!(apart.hdr.starts_with(&hdr_dir)); |
| 197 | assert!(apart.pts.starts_with(&pts_dir)); |
| 198 | let (chains, pts) = apart.read_all().unwrap(); |
| 199 | assert_eq!(chains.len(), 2); |
| 200 | assert_eq!(pts, all); |
| 201 | |
| 202 | apart.remove(); |
| 203 | assert!(!apart.hdr.exists() && !apart.pts.exists()); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn a_header_that_is_not_a_whole_number_of_records_is_rejected() { |
| 208 | let dir = spill_dir("spill_ragged"); |
| 209 | let all = coords(2); |
| 210 | let spill = Spill::new(&dir); |
| 211 | spill.write(&[chain(0, 2)], &[0, 1], &all).unwrap(); |
| 212 | // Truncating mid-record must fail loudly: read_all would otherwise decode a |
| 213 | // partial record as a chain full of zeros, which is a structurally valid |
| 214 | // chain pointing at node 0. |
| 215 | let mut bytes = std::fs::read(&spill.hdr).unwrap(); |
| 216 | bytes.truncate(bytes.len() - 1); |
| 217 | std::fs::write(&spill.hdr, &bytes).unwrap(); |
| 218 | assert!(spill.chain_count().is_err()); |
| 219 | assert!(spill.read_all().is_err()); |
| 220 | } |
| 221 | } |