From 22697f0c0c858bc013ec5c7d8b3ea50f000246dd Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 13 Oct 2020 12:34:11 +0200 Subject: =?UTF-8?q?Simple=20regression=20testing=20with=20file-based=20com?= =?UTF-8?q?parisons=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/README.md | 7 ++++++ tests/coma.typ | 25 --------------------- tests/ref/coma.png | Bin 0 -> 125591 bytes tests/typ/coma.typ | 25 +++++++++++++++++++++ tests/typeset.rs | 63 +++++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 tests/README.md delete mode 100644 tests/coma.typ create mode 100644 tests/ref/coma.png create mode 100644 tests/typ/coma.typ (limited to 'tests') diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..3ae908af --- /dev/null +++ b/tests/README.md @@ -0,0 +1,7 @@ +# Tests + +- `typ`: Input files +- `pdf`: PDF files produced by tests +- `png`: PNG files produced by tests +- `ref`: Reference images which the PNGs are compared to byte-wise to determine + whether the test passed or failed diff --git a/tests/coma.typ b/tests/coma.typ deleted file mode 100644 index 839335b7..00000000 --- a/tests/coma.typ +++ /dev/null @@ -1,25 +0,0 @@ -[page: width=450pt, height=300pt, margins=1cm] - -[box][ - *Technische Universität Berlin* \ - *Fakultät II, Institut for Mathematik* \ - Sekretariat MA \ - Dr. Max Mustermann \ - Ola Nordmann, John Doe -] -[align: right >> box][*WiSe 2019/2020* \ Woche 3] - -[v: 6mm] - -[align: center][ - #### 3. Übungsblatt Computerorientierte Mathematik II [v: 2mm] - *Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) [v: 2mm] - *Alle Antworten sind zu beweisen.* -] - -*1. Aufgabe* [align: right][(1 + 1 + 2 Punkte)] - -Ein _Binärbaum_ ist ein Wurzelbaum, in dem jeder Knoten ≤ 2 Kinder hat. -Die Tiefe eines Knotens _v_ ist die Länge des eindeutigen Weges von der Wurzel -zu _v_, und die Höhe von _v_ ist die Länge eines längsten (absteigenden) Weges -von _v_ zu einem Blatt. Die Höhe des Baumes ist die Höhe der Wurzel. diff --git a/tests/ref/coma.png b/tests/ref/coma.png new file mode 100644 index 00000000..642759a8 Binary files /dev/null and b/tests/ref/coma.png differ diff --git a/tests/typ/coma.typ b/tests/typ/coma.typ new file mode 100644 index 00000000..839335b7 --- /dev/null +++ b/tests/typ/coma.typ @@ -0,0 +1,25 @@ +[page: width=450pt, height=300pt, margins=1cm] + +[box][ + *Technische Universität Berlin* \ + *Fakultät II, Institut for Mathematik* \ + Sekretariat MA \ + Dr. Max Mustermann \ + Ola Nordmann, John Doe +] +[align: right >> box][*WiSe 2019/2020* \ Woche 3] + +[v: 6mm] + +[align: center][ + #### 3. Übungsblatt Computerorientierte Mathematik II [v: 2mm] + *Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) [v: 2mm] + *Alle Antworten sind zu beweisen.* +] + +*1. Aufgabe* [align: right][(1 + 1 + 2 Punkte)] + +Ein _Binärbaum_ ist ein Wurzelbaum, in dem jeder Knoten ≤ 2 Kinder hat. +Die Tiefe eines Knotens _v_ ist die Länge des eindeutigen Weges von der Wurzel +zu _v_, und die Höhe von _v_ ist die Länge eines längsten (absteigenden) Weges +von _v_ zu einem Blatt. Die Höhe des Baumes ist die Höhe der Wurzel. diff --git a/tests/typeset.rs b/tests/typeset.rs index c2deca6a..b3080a52 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -7,6 +7,7 @@ use std::path::Path; use std::rc::Rc; use fontdock::fs::{FsIndex, FsSource}; +use memmap::Mmap; use raqote::{DrawTarget, PathBuilder, SolidSource, Source, Transform, Vector}; use ttf_parser::OutlineBuilder; @@ -20,9 +21,11 @@ use typst::parse::LineMap; use typst::shaping::Shaped; use typst::typeset; -const TEST_DIR: &str = "tests"; -const OUT_DIR: &str = "tests/out"; const FONT_DIR: &str = "fonts"; +const TYP_DIR: &str = "tests/typ"; +const PDF_DIR: &str = "tests/pdf"; +const PNG_DIR: &str = "tests/png"; +const REF_DIR: &str = "tests/ref"; const BLACK: SolidSource = SolidSource { r: 0, g: 0, b: 0, a: 255 }; const WHITE: SolidSource = SolidSource { r: 255, g: 255, b: 255, a: 255 }; @@ -31,16 +34,19 @@ fn main() { let filter = TestFilter::new(env::args().skip(1)); let mut filtered = Vec::new(); - for entry in fs::read_dir(TEST_DIR).unwrap() { - let path = entry.unwrap().path(); - if path.extension() != Some(OsStr::new("typ")) { + for entry in fs::read_dir(TYP_DIR).unwrap() { + let src_path = entry.unwrap().path(); + if src_path.extension() != Some(OsStr::new("typ")) { continue; } - let name = path.file_stem().unwrap().to_string_lossy().to_string(); + let name = src_path.file_stem().unwrap().to_string_lossy().to_string(); + let pdf_path = Path::new(PDF_DIR).join(&name).with_extension("pdf"); + let png_path = Path::new(PNG_DIR).join(&name).with_extension("png"); + let ref_path = Path::new(REF_DIR).join(&name).with_extension("png"); + if filter.matches(&name) { - let src = fs::read_to_string(&path).unwrap(); - filtered.push((name, path, src)); + filtered.push((name, src_path, pdf_path, png_path, ref_path)); } } @@ -53,7 +59,8 @@ fn main() { println!("Running {} tests", len); } - fs::create_dir_all(OUT_DIR).unwrap(); + fs::create_dir_all(PDF_DIR).unwrap(); + fs::create_dir_all(PNG_DIR).unwrap(); let mut index = FsIndex::new(); index.search_dir(FONT_DIR); @@ -64,14 +71,40 @@ fn main() { descriptors, ))); - for (name, path, src) in filtered { - test(&name, &src, &path, &loader) + let mut ok = true; + + for (name, src_path, pdf_path, png_path, ref_path) in filtered { + print!("Testing {}.", name); + test(&src_path, &pdf_path, &png_path, &loader); + + let png_file = File::open(&png_path).unwrap(); + let ref_file = match File::open(&ref_path) { + Ok(file) => file, + Err(_) => { + println!(" Failed to open reference image. ❌"); + ok = false; + continue; + } + }; + + let a = unsafe { Mmap::map(&png_file).unwrap() }; + let b = unsafe { Mmap::map(&ref_file).unwrap() }; + + if *a != *b { + println!(" Does not match reference image. ❌"); + ok = false; + } else { + println!(" Okay. ✔"); + } } -} -fn test(name: &str, src: &str, src_path: &Path, loader: &SharedFontLoader) { - println!("Testing {}.", name); + if !ok { + std::process::exit(1); + } +} +fn test(src_path: &Path, pdf_path: &Path, png_path: &Path, loader: &SharedFontLoader) { + let src = fs::read_to_string(src_path).unwrap(); let state = State::default(); let Pass { output: layouts, @@ -99,11 +132,9 @@ fn test(name: &str, src: &str, src_path: &Path, loader: &SharedFontLoader) { let loader = loader.borrow(); - let png_path = format!("{}/{}.png", OUT_DIR, name); let surface = render(&layouts, &loader, 3.0); surface.write_png(png_path).unwrap(); - let pdf_path = format!("{}/{}.pdf", OUT_DIR, name); let file = BufWriter::new(File::create(pdf_path).unwrap()); pdf::export(&layouts, &loader, file).unwrap(); } -- cgit v1.2.3