Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / maps / tile_build / src / pyramid_part7.rs

pyramid_part7.rs203 lines · 8.1 KiBRaw
1
2 #[test]
3 fn two_streaming_runs_are_byte_identical() {
4 let features = mixed_fixture();
5 let opts = Options::new("l", 10, 12);
6 let a = assert_identical("det_a", &features, &opts, &StreamLimits::default());
7 let b = assert_identical("det_b", &features, &opts, &StreamLimits::default());
8 assert_eq!(a, b, "determinism is a regression surface here");
9 }
10
11 #[test]
12 fn the_streaming_producer_refuses_the_same_bad_options() {
13 let scratch = Scratch::new("badopts");
14 let features = vec![line(&[(0.0, 0.0), (1.0, 1.0)], vec![])];
15 let limits = StreamLimits::default();
16 for opts in [
17 Options::new("l", 12, 10),
18 Options { extent: 0, ..Options::new("l", 10, 12) },
19 ] {
20 let mut src = SliceSource::new(&features);
21 assert!(
22 build_archive_to(
23 scratch.0.join("out.pmtiles"),
24 &scratch.0,
25 &opts,
26 &mut src,
27 &limits
28 )
29 .is_err(),
30 "bad options must be refused by both producers"
31 );
32 }
33 }
34
35 /// `--buckets 1` cannot narrow a range, so an over-budget bucket must fail fast
36 /// rather than copy itself to disk once per level up to the depth cap.
37 #[test]
38 fn a_single_bucket_partition_fails_fast_instead_of_recursing() {
39 let scratch = Scratch::new("onebucket");
40 let features = mixed_fixture();
41 let opts = Options::new("l", 10, 10);
42 let limits = StreamLimits {
43 buckets: 1,
44 bucket_budget_bytes: 1,
45 max_repartition_depth: DEFAULT_MAX_REPARTITION_DEPTH,
46 };
47 let mut src = SliceSource::new(&features);
48 let e = build_archive_to(
49 scratch.0.join("out.pmtiles"),
50 &scratch.0,
51 &opts,
52 &mut src,
53 &limits,
54 )
55 .expect_err("a one-bucket partition cannot split, so it must error");
56 assert!(
57 e.to_string().contains("cannot be split further"),
58 "the error must say why: {e}"
59 );
60 }
61
62 /// `zoom_base(max_zoom + 1)` overflows above z30, which only the streaming path
63 /// computes. A message beats a panic.
64 #[test]
65 fn a_zoom_beyond_the_tile_id_range_is_refused() {
66 let scratch = Scratch::new("deepzoom");
67 let features = vec![line(&[(0.0, 0.0), (1.0, 1.0)], vec![])];
68 // Refused before any zoom runs, so this costs nothing: a z30 bucket pass over a
69 // one-degree line would touch millions of tiles.
70 let opts = Options::new("l", 31, 31);
71 let mut src = SliceSource::new(&features);
72 let e = build_archive_to(
73 scratch.0.join("out.pmtiles"),
74 &scratch.0,
75 &opts,
76 &mut src,
77 &StreamLimits::default(),
78 )
79 .expect_err("z31 must be refused");
80 assert!(e.to_string().contains("above 30"), "{e}");
81 }
82
83 /// The same normalized file forced through the sequential path, so a test can pin the
84 /// chunked reader against it. Everything delegates; `chunks` is left at its `None`
85 /// default, which is the whole point.
86 struct SerialOnly(NormalizedSource);
87
88 impl FeatureSource for SerialOnly {
89 fn rewind(&mut self) -> Result<()> {
90 self.0.rewind()
91 }
92 fn next(&mut self) -> Result<Option<Feature>> {
93 self.0.next()
94 }
95 fn len(&self) -> u64 {
96 self.0.len()
97 }
98 fn bounds(&self) -> Option<geom::Rect> {
99 self.0.bounds()
100 }
101 fn geom_kind(&self) -> Option<GeomKind> {
102 self.0.geom_kind()
103 }
104 }
105
106 /// The chunked reader is the production read path, so it must produce exactly what the
107 /// sequential one does, at every thread count.
108 ///
109 /// `tight_fixture` is 201 features against `NORM_CHUNK_FEATURES` of 64, so the index
110 /// spans four chunks and ends mid-chunk — the shape that catches an off-by-one in the
111 /// chunk-to-`seq` mapping. The tight budget matters more: it makes the drop policy
112 /// run, so a perturbed `seq` changes which features SURVIVE rather than only which
113 /// bucket they land in. Without it a wrong `seq` would still sort out and the test
114 /// would pass while the mapping was broken.
115 ///
116 /// Checked by mutation: dropping the chunk base (`first = 0`, so `seq` collides
117 /// across chunks) fails this. Adding a constant to it does NOT, and should not —
118 /// `seq` is only ever a tie-break, so a uniform shift preserves every comparison.
119 #[test]
120 fn the_chunked_reader_matches_the_sequential_one() {
121 let features = tight_fixture();
122 let mut opts = Options::new("l", 11, 11);
123 opts.max_tile_bytes = 400;
124 let limits = StreamLimits::default();
125
126 let held = Scratch::new("chunked_src");
127 let norm = held.0.join("features.bin");
128 let mut w = crate::spill::NormalizedWriter::create(&norm).unwrap();
129 for f in &features {
130 w.push(&f.geometry, &f.props).unwrap();
131 }
132 let summary = w.finish().unwrap();
133
134 // The fixture has to span several chunks and end mid-chunk, or this degenerates
135 // into the single-chunk case and asserts nothing about the mapping.
136 assert!(
137 summary.chunk_count() > 1,
138 "fixture must span several chunks, got {}",
139 summary.chunk_count()
140 );
141 assert_ne!(
142 summary.count % crate::spill::NORM_CHUNK_FEATURES,
143 0,
144 "fixture must end mid-chunk to exercise the partial tail"
145 );
146
147 let serial = {
148 let scratch = Scratch::new("chunked_serial");
149 let out = scratch.0.join("out.pmtiles");
150 let mut src = SerialOnly(NormalizedSource::open(&norm, summary.clone()).unwrap());
151 assert!(src.chunks().is_none(), "this must take the serial path");
152 let report = build_archive_to(&out, &scratch.0, &opts, &mut src, &limits).unwrap();
153 (std::fs::read(&out).unwrap(), report)
154 };
155
156 for n in [1, 2, 3, 32] {
157 par::set_threads(n);
158 let scratch = Scratch::new(&format!("chunked_{n}"));
159 let out = scratch.0.join("out.pmtiles");
160 let mut src = NormalizedSource::open(&norm, summary.clone()).unwrap();
161 assert!(src.chunks().is_some(), "this must take the chunked path");
162 let report = build_archive_to(&out, &scratch.0, &opts, &mut src, &limits).unwrap();
163 assert_eq!(
164 report, serial.1,
165 "{n} threads: the chunked reader changed the report"
166 );
167 assert!(
168 std::fs::read(&out).unwrap() == serial.0,
169 "{n} threads: the chunked reader perturbed the archive"
170 );
171 }
172 par::clear_threads();
173 }
174
175 /// The production source is a file, and it must yield exactly what the slice-backed
176 /// one does -- otherwise the byte-identity tests above are testing a path nothing
177 /// ships.
178 #[test]
179 fn the_normalized_source_produces_the_same_archive_as_the_slice_one() {
180 let scratch = Scratch::new("normsrc");
181 let features = mixed_fixture();
182 let opts = Options::new("l", 10, 12);
183 let limits = StreamLimits::default();
184
185 let (want, want_report) = build_archive(&features, &opts).unwrap();
186
187 let norm = scratch.0.join("features.bin");
188 let mut w = crate::spill::NormalizedWriter::create(&norm).unwrap();
189 for f in &features {
190 w.push(&f.geometry, &f.props).unwrap();
191 }
192 let summary = w.finish().unwrap();
193 assert_eq!(summary.count, features.len() as u64);
194
195 let out = scratch.0.join("out.pmtiles");
196 let mut src = NormalizedSource::open(&norm, summary).unwrap();
197 let report = build_archive_to(&out, &scratch.0, &opts, &mut src, &limits).unwrap();
198 assert_eq!(report, want_report);
199 assert!(
200 std::fs::read(&out).unwrap() == want,
201 "the file-backed source must produce the same bytes"
202 );
203 }