gatling/tests/window_plan.rs
//! The one read-window plan every decoder shares: segment and read-window
//! sizes from the input size S (if known) and the cores N.
use gatling::window_plan::{Plan, windows};
const MB: u64 = 1 << 20;
fn lgz() -> Plan {
Plan {
min_seg: MB,
max_seg: 50 * MB,
k: 3,
}
}
/// (S, N) → (segment, read_window, first windows)
#[test]
fn the_table() {
let p = lgz();
// 20 MB on 8 cores: 20 ≤ 2 × 8 × 1 MB? no (16 MB) → segment = clamp(20/24) = 1 MB
let w: Vec<u64> = windows(Some(20 * MB), 8, p).collect();
assert_eq!(p.segment(Some(20 * MB), 8), MB);
assert_eq!(w.iter().sum::<u64>(), 20 * MB);
assert_eq!(w[0], 8 * MB, "first window = N × min_seg");
// 237 MB on 12 cores: segment = 237/36 ≈ 6.6 MB, read window = 12 × that
let seg = p.segment(Some(237 * MB), 12);
assert_eq!(seg, 237 * MB / 36);
let w: Vec<u64> = windows(Some(237 * MB), 12, p).collect();
assert_eq!(w[0], 12 * MB);
assert_eq!(w[1], 24 * MB, "ramp doubles");
assert!(w.iter().all(|&x| x <= 12 * seg));
assert_eq!(w.iter().sum::<u64>(), 237 * MB);
// 400 MB on 32 cores: segment = 400/96 ≈ 4.2 MB
assert_eq!(p.segment(Some(400 * MB), 32), 400 * MB / 96);
let w: Vec<u64> = windows(Some(400 * MB), 32, p).collect();
assert_eq!(w[0], 32 * MB);
assert_eq!(w.iter().sum::<u64>(), 400 * MB);
// 100 GB on 32 cores: segment hits max_seg (50 MB), read window 1.6 GB
assert_eq!(p.segment(Some(100 << 30), 32), 50 * MB);
let w: Vec<u64> = windows(Some(100 << 30), 32, p).take(12).collect();
assert_eq!(w[0], 32 * MB);
assert_eq!(*w.last().unwrap(), 32 * 50 * MB, "steady at N × max_seg");
// unknown S (stdin): ramp from N × min_seg, doubling up to N × max_seg
let w: Vec<u64> = windows(None, 12, p).take(10).collect();
assert_eq!(w[0], 12 * MB);
assert_eq!(w[1], 24 * MB);
assert_eq!(*w.last().unwrap(), 12 * 50 * MB);
assert!(w.windows(2).all(|x| x[1] >= x[0]));
}
#[test]
fn small_inputs_are_read_whole() {
let p = lgz();
// S ≤ 2 × N × min_seg: one window, no ramp
let w: Vec<u64> = windows(Some(15 * MB), 8, p).collect();
assert_eq!(w, vec![15 * MB]);
let w: Vec<u64> = windows(Some(3), 8, p).collect();
assert_eq!(w, vec![3]);
assert_eq!(windows(Some(0), 8, p).count(), 0);
}