From 56cbf96fe2b3b82d34b2e69a49bcb7b0c0267f6a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 26 Jul 2021 00:08:08 +0200 Subject: [PATCH] Move incremental test into separate function --- src/layout/tree.rs | 41 ++++++++++----------- tests/typeset.rs | 88 ++++++++++++++++++++++++++-------------------- 2 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/layout/tree.rs b/src/layout/tree.rs index 258f1cccc..77b01ad28 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -50,6 +50,15 @@ pub struct LayoutNode { } impl LayoutNode { + /// Create a new instance from any node that satisifies the required bounds. + #[cfg(not(feature = "layout-cache"))] + pub fn new(node: T) -> Self + where + T: Layout + Debug + Clone + Eq + PartialEq + 'static, + { + Self { node: Box::new(node) } + } + /// Create a new instance from any node that satisifies the required bounds. #[cfg(feature = "layout-cache")] pub fn new(node: T) -> Self @@ -65,15 +74,6 @@ impl LayoutNode { Self { node: Box::new(node), hash } } - - /// Create a new instance from any node that satisifies the required bounds. - #[cfg(not(feature = "layout-cache"))] - pub fn new(node: T) -> Self - where - T: Layout + Debug + Clone + Eq + PartialEq + 'static, - { - Self { node: Box::new(node) } - } } impl Layout for LayoutNode { @@ -82,20 +82,17 @@ impl Layout for LayoutNode { ctx: &mut LayoutContext, regions: &Regions, ) -> Vec>> { - #[cfg(feature = "layout-cache")] - { - ctx.level += 1; - let frames = ctx.layouts.get(self.hash, regions.clone()).unwrap_or_else(|| { - let frames = self.node.layout(ctx, regions); - ctx.layouts.insert(self.hash, frames.clone(), ctx.level - 1); - frames - }); - ctx.level -= 1; - frames - } - #[cfg(not(feature = "layout-cache"))] - self.node.layout(ctx, regions) + return self.node.layout(ctx, regions); + + #[cfg(feature = "layout-cache")] + ctx.layouts.get(self.hash, regions.clone()).unwrap_or_else(|| { + ctx.level += 1; + let frames = self.node.layout(ctx, regions); + ctx.level -= 1; + ctx.layouts.insert(self.hash, frames.clone(), ctx.level); + frames + }) } } diff --git a/tests/typeset.rs b/tests/typeset.rs index 6b2258bf7..59eb008f9 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -16,7 +16,7 @@ use typst::eval::{eval, Scope, Value}; use typst::exec::{exec, State}; use typst::geom::{self, Length, PathElement, Point, Sides, Size}; use typst::image::ImageId; -use typst::layout::{layout, Element, Frame, Geometry, Paint, Text}; +use typst::layout::{layout, Element, Frame, Geometry, LayoutTree, Paint, Text}; use typst::loading::{FileId, FsLoader}; use typst::parse::{parse, LineMap, Scanner}; use typst::syntax::{Location, Pos}; @@ -256,6 +256,7 @@ fn test_part( diags.extend(tree.diags); let mut ok = true; + for panic in panics.borrow().iter() { let line = map.location(panic.pos).unwrap().line; println!(" Assertion failed in line {} ❌", lines + line); @@ -290,44 +291,7 @@ fn test_part( } #[cfg(feature = "layout-cache")] - { - let reference = ctx.layouts.clone(); - for level in 0 .. reference.levels() { - ctx.layouts = reference.clone(); - ctx.layouts.retain(|x| x == level); - if ctx.layouts.is_empty() { - continue; - } - - ctx.layouts.turnaround(); - - let cached = layout(ctx, &tree.output); - let misses = ctx - .layouts - .entries() - .filter(|e| e.level() == level && !e.hit() && e.age() == 2) - .count(); - - if misses > 0 { - ok = false; - println!( - " Recompilation had {} cache misses on level {} (Subtest {}) ❌", - misses, level, i - ); - } - - if cached != frames { - ok = false; - println!( - " Recompilation of subtest {} differs from clean pass ❌", - i - ); - } - } - - ctx.layouts = reference; - ctx.layouts.turnaround(); - } + (ok &= test_incremental(ctx, i, &tree.output, &frames)); if !compare_ref { frames.clear(); @@ -336,6 +300,52 @@ fn test_part( (ok, compare_ref, frames) } +#[cfg(feature = "layout-cache")] +fn test_incremental( + ctx: &mut Context, + i: usize, + tree: &LayoutTree, + frames: &[Rc], +) -> bool { + let mut ok = true; + + let reference = ctx.layouts.clone(); + for level in 0 .. reference.levels() { + ctx.layouts = reference.clone(); + ctx.layouts.retain(|x| x == level); + if ctx.layouts.is_empty() { + continue; + } + + ctx.layouts.turnaround(); + + let cached = layout(ctx, tree); + let misses = ctx + .layouts + .entries() + .filter(|e| e.level() == level && !e.hit() && e.age() == 2) + .count(); + + if misses > 0 { + println!( + " Subtest {} relayout had {} cache misses on level {} ❌", + i, misses, level + ); + ok = false; + } + + if cached != frames { + println!(" Subtest {} relayout differs from clean pass ❌", i); + ok = false; + } + } + + ctx.layouts = reference; + ctx.layouts.turnaround(); + + ok +} + fn parse_metadata(src: &str, map: &LineMap) -> (Option, DiagSet) { let mut diags = DiagSet::new(); let mut compare_ref = None;