diff --git a/crates/ppt/src/tests.rs b/crates/ppt/src/tests.rs index 26d03dca9..2e84f3b1a 100644 --- a/crates/ppt/src/tests.rs +++ b/crates/ppt/src/tests.rs @@ -376,18 +376,15 @@ Then wave" ); } -/// Text with very many formatting runs is laid into runs in about linear time. Each lookup used to -/// scan the run starts from the beginning, so the cost grew with runs²: in a debug build these 640k -/// one-character runs (about 6 MB of file) took 75 s that way, and take about 2 s with binary search. -#[test] -fn many_character_runs_are_not_quadratic() { - const N: usize = 640_000; - let text: String = (0..N).map(|i| if i % 2 == 0 { 'a' } else { '\u{0B}' }).collect(); +/// A text box holding `n` one-character formatting runs (a character and a line break, alternating), +/// as a legacy .ppt file. +fn file_with_character_runs(n: usize) -> Vec { + let text: String = (0..n).map(|i| if i % 2 == 0 { 'a' } else { '\u{0B}' }).collect(); let mut style = vec![]; - style.extend_from_slice(&(N as u32 + 1).to_le_bytes()); + style.extend_from_slice(&(n as u32 + 1).to_le_bytes()); style.extend_from_slice(&0u16.to_le_bytes()); style.extend_from_slice(&0u32.to_le_bytes()); // a paragraph exception with no fields - for _ in 0..=N { + for _ in 0..=n { style.extend_from_slice(&1u32.to_le_bytes()); style.extend_from_slice(&0u32.to_le_bytes()); // a character exception with no fields } @@ -398,12 +395,39 @@ fn many_character_runs_are_not_quadratic() { ); let slide = container(rt::SLIDE, 0, &[slide_atom(1, MASTER_ID, 0x7), drawing(None, &[shape])]); let (cu, doc, pics) = streams_of(0xE391_C05F, 1, slide, None); - let bytes = crate::cfb::build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)]); - let t = std::time::Instant::now(); - let p = crate::import(&bytes).unwrap(); - assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed()); - let runs = &p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs[0].runs; - assert_eq!(runs.iter().filter(|r| r.kind == RunKind::Break).count(), N / 2); + crate::cfb::build(&[("Current User", &cu), ("PowerPoint Document", &doc), ("Pictures", &pics)]) +} + +/// Text with very many formatting runs is laid into runs in about linear time. Each lookup used to +/// scan the run starts from the beginning, so the cost grew with runs²: 640k one-character runs +/// (about 6 MB of file) took 75 s that way in a debug build, and about 2 s with binary search. +/// +/// The test compares two sizes in the same process instead of timing one against a fixed limit, so +/// a slow or busy host (the FreeBSD CI VM took 20 to 31 s for the 640k file, mostly in the kernel, +/// where a quiet one takes 2 s) slows both alike. Four times the runs take about four times as long +/// when the work is linear and sixteen times as long when it is quadratic. Each size is the best of +/// three imports, so a stall in one of them doesn't decide the result. +#[test] +fn many_character_runs_are_not_quadratic() { + const SMALL: usize = 40_000; + const LARGE: usize = 4 * SMALL; + let best_of_three = |n: usize| { + let bytes = file_with_character_runs(n); + let mut best = std::time::Duration::MAX; + let mut breaks = 0; + for _ in 0..3 { + let t = std::time::Instant::now(); + let p = crate::import(&bytes).unwrap(); + best = best.min(t.elapsed()); + let runs = &p.slides[0].shapes[0].text.as_ref().unwrap().paragraphs[0].runs; + breaks = runs.iter().filter(|r| r.kind == RunKind::Break).count(); + } + (best, breaks) + }; + let (small, small_breaks) = best_of_three(SMALL); + let (large, large_breaks) = best_of_three(LARGE); + assert_eq!((small_breaks, large_breaks), (SMALL / 2, LARGE / 2)); + assert!(large < small * 10, "{LARGE} runs took {large:?}, {SMALL} runs took {small:?}: more than 10x for 4x the runs"); } /// A slide naming a master that doesn't exist falls back to the first main master, every time. The