mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-03 02:58:47 -06:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3eecf55c46 | ||
|
|
25ea1eb064 | ||
|
|
e9dbf10433 |
@@ -39,12 +39,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hsl_roundtrip() {
|
||||
for c in [Rgba::rgb(10, 200, 30), Rgba::rgb(255, 0, 0), Rgba::rgb(128, 128, 128), Rgba::rgb(21, 96, 130)] {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
let d = Rgba::from_hsl(h, s, l, 255);
|
||||
for color in [Rgba::rgb(10, 200, 30), Rgba::rgb(255, 0, 0), Rgba::rgb(128, 128, 128), Rgba::rgb(21, 96, 130)] {
|
||||
let (hue, saturation, lightness) = color.to_hsl();
|
||||
let converted = Rgba::from_hsl(hue, saturation, lightness, 255);
|
||||
assert!(
|
||||
(d.r as i32 - c.r as i32).abs() <= 1 && (d.g as i32 - c.g as i32).abs() <= 1 && (d.b as i32 - c.b as i32).abs() <= 1,
|
||||
"{c:?} {d:?}"
|
||||
(i32::from(converted.r) - i32::from(color.r)).abs() <= 1
|
||||
&& (i32::from(converted.g) - i32::from(color.g)).abs() <= 1
|
||||
&& (i32::from(converted.b) - i32::from(color.b)).abs() <= 1,
|
||||
"{color:?} {converted:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+15
-39
@@ -376,15 +376,18 @@ Then wave"
|
||||
);
|
||||
}
|
||||
|
||||
/// 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<u8> {
|
||||
let text: String = (0..n).map(|i| if i % 2 == 0 { 'a' } else { '\u{0B}' }).collect();
|
||||
/// 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();
|
||||
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
|
||||
}
|
||||
@@ -395,39 +398,12 @@ fn file_with_character_runs(n: usize) -> Vec<u8> {
|
||||
);
|
||||
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);
|
||||
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");
|
||||
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);
|
||||
}
|
||||
|
||||
/// A slide naming a master that doesn't exist falls back to the first main master, every time. The
|
||||
|
||||
Reference in New Issue
Block a user