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

Modern-Apps.git / scripts / maps / mamaps_build / src / tilespill_part4.rs

tilespill_part4.rs107 lines · 4.8 KiBRaw
1#[cfg(test)]
2mod tests_extra {
3 use super::tests::{drain, every_layer, feature, tmp};
4 use super::*;
5 use tilecodec::mamaps::body::{GEOM_LINE, WINDING_OUTER};
6
7 /// Byte-identity, at the level the format can state it: what a reader yields does not depend on
8 /// how much it buffers. This is what pins "the window size is not observable".
9 #[test]
10 fn a_chunk_reads_the_same_however_the_window_is_sized() {
11 let spill = ChunkSpill::create(tmp("windows")).expect("create");
12 let mut map: BTreeMap<(u64, u8), ChunkEntry> = every_layer().into_iter().collect();
13 // One entry far larger than the smallest window, so at least one read has to be the
14 // oversize path rather than the buffered one.
15 let mut big = ChunkEntry::new(4);
16 big.layer.features = vec![feature(1, 1, GEOM_LINE, 0, 0, 1)];
17 big.layer.parts =
18 vec![Part { coord_start: 0, point_count: 40_000, winding: WINDING_OUTER }];
19 // The coordinates themselves are arbitrary; only the point count matters here. Kept
20 // inside `i16`'s range rather than cast from the index, which wraps to `i16::MIN` at
21 // 32 768 and then overflows on the negation.
22 big.layer.coords = (0..40_000)
23 .map(|i| {
24 let v = (i % 30_000) as i16;
25 (v, -v)
26 })
27 .collect();
28 map.insert((6, 4), big);
29 let at = spill.write_chunk(map).expect("write");
30
31 let want = drain(&spill, &at, 1 << 20);
32 for window in [1usize, 24, 25, 64, 1024, 1 << 14, 1 << 16, 1 << 22] {
33 assert_eq!(drain(&spill, &at, window), want, "window {window} yielded something else");
34 }
35 }
36
37 #[test]
38 fn the_scratch_file_is_removed_on_drop() {
39 let path = tmp("dropped");
40 {
41 let spill = ChunkSpill::create(&path).expect("create");
42 spill.write_chunk(every_layer().into_iter().collect()).expect("write");
43 // Unlinked at creation on Unix (a killed build leaves nothing); named until drop
44 // elsewhere.
45 assert_eq!(path.exists(), cfg!(not(unix)));
46 }
47 assert!(!path.exists(), "the scratch file outlived its spill");
48 }
49
50 /// The error path too: a spill dropped while a write is failing must not strand the file.
51 #[test]
52 fn the_scratch_file_is_removed_when_a_chunk_is_refused() {
53 let path = tmp("refused");
54 {
55 let spill = ChunkSpill::create(&path).expect("create");
56 // Keyed on one layer, carrying another.
57 let mut map: BTreeMap<(u64, u8), ChunkEntry> = BTreeMap::new();
58 map.insert((1, 2), ChunkEntry::new(3));
59 assert!(spill.write_chunk(map).is_err(), "a mislabelled entry was accepted");
60 }
61 assert!(!path.exists(), "the scratch file outlived a failed write");
62 }
63
64 #[test]
65 fn an_empty_chunk_is_zero_bytes_and_reads_as_nothing() {
66 let spill = ChunkSpill::create(tmp("empty")).expect("create");
67 let at = spill.write_chunk(BTreeMap::new()).expect("write");
68 assert_eq!(at, ChunkRef { at: 0, len: 0, entries: 0 });
69 assert!(drain(&spill, &at, 1 << 16).is_empty());
70 spill.check_books().expect("the books balance");
71 }
72
73 #[test]
74 fn the_books_catch_a_chunk_that_was_never_read() {
75 let spill = ChunkSpill::create(tmp("unread")).expect("create");
76 spill.write_chunk(every_layer().into_iter().collect()).expect("write");
77 assert!(spill.check_books().is_err(), "an undrained spill balanced");
78 }
79
80 #[test]
81 fn the_read_window_is_a_budget_divided_by_the_stream_count() {
82 // `window_for` rather than `read_window_bytes`, so a test elsewhere holding the override
83 // cannot make this one assert something else.
84 assert_eq!(window_for(0), MAX_WINDOW, "no streams clamps up, not to zero");
85 assert_eq!(window_for(1), MAX_WINDOW);
86 // A north-america z14: the budget still binds, well clear of both clamps.
87 let na = window_for(5_700);
88 assert_eq!(na, READ_BUDGET / 5_700);
89 assert!((MIN_WINDOW..=MAX_WINDOW).contains(&na));
90 // A planet z14: the floor takes over, so the total is `streams * MIN_WINDOW`.
91 assert_eq!(window_for(27_000), MIN_WINDOW);
92 assert_eq!(window_for(10_000_000), MIN_WINDOW);
93 for streams in [1usize, 2, 100, 5_700, 27_000, 10_000_000] {
94 let window = window_for(streams);
95 assert!(
96 (MIN_WINDOW..=MAX_WINDOW).contains(&window),
97 "{streams} streams gave a {window}-byte window"
98 );
99 }
100 // The planet ceiling: streams x floor must stay a small fraction of a
101 // constrained box's commit headroom (~50 GB here), not half of it.
102 assert!(
103 27_000u64 * MIN_WINDOW as u64 <= 512 << 20,
104 "a planet z14's merge cursors must fit in ~512 MB"
105 );
106 }
107}