[librsvg: 28/53] Allow to tune the test tolerance using RSVG_TEST_TOLERANCE
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 28/53] Allow to tune the test tolerance using RSVG_TEST_TOLERANCE
- Date: Fri, 23 Oct 2020 02:19:07 +0000 (UTC)
commit 0f5ff14f26fe4254da274b76f4ab82aa81ce1390
Author: Sven Neumann <sven svenfoo org>
Date: Fri Oct 16 22:09:43 2020 +0200
Allow to tune the test tolerance using RSVG_TEST_TOLERANCE
This code is still duplicated in two places.
librsvg_crate/tests/standalone/utils.rs | 61 ++++++++++++++++++++++++-------
tests/src/reference.rs | 65 ++++++++++++++++++++++++++-------
2 files changed, 99 insertions(+), 27 deletions(-)
---
diff --git a/librsvg_crate/tests/standalone/utils.rs b/librsvg_crate/tests/standalone/utils.rs
index 0ba9ef63..b71aaa27 100644
--- a/librsvg_crate/tests/standalone/utils.rs
+++ b/librsvg_crate/tests/standalone/utils.rs
@@ -7,12 +7,14 @@ use librsvg::{CairoRenderer, Loader, LoadingError, RenderingError, SvgHandle};
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
-use rsvg_internals::{compare_surfaces, BufferDiff};
+use rsvg_internals::{compare_surfaces, BufferDiff, Diff};
+use std::convert::TryFrom;
use std::env;
use std::fs::{self, File};
use std::io::BufReader;
use std::path::PathBuf;
+use std::sync::Once;
pub fn load_svg(input: &'static [u8]) -> Result<SvgHandle, LoadingError> {
let bytes = glib::Bytes::from_static(input);
@@ -45,6 +47,35 @@ pub fn render_document<F: FnOnce(&cairo::Context)>(
res.and_then(|_| Ok(SharedImageSurface::wrap(output, SurfaceType::SRgb)?))
}
+fn tolerable_difference() -> u8 {
+ static mut TOLERANCE: u8 = 2;
+
+ static ONCE: Once = Once::new();
+ ONCE.call_once(|| unsafe {
+ if let Ok(str) = env::var("RSVG_TEST_TOLERANCE") {
+ let value: usize = str.parse().unwrap();
+ TOLERANCE = u8::try_from(value).unwrap();
+ }
+ });
+
+ unsafe { TOLERANCE }
+}
+
+trait Deviation {
+ fn distinguishable(&self) -> bool;
+ fn inacceptable(&self) -> bool;
+}
+
+impl Deviation for Diff {
+ fn distinguishable(&self) -> bool {
+ self.max_diff > 2
+ }
+
+ fn inacceptable(&self) -> bool {
+ self.max_diff > tolerable_difference()
+ }
+}
+
pub fn output_dir() -> PathBuf {
let path = PathBuf::from(
env::var_os("OUT_DIR")
@@ -92,7 +123,7 @@ pub fn compare_to_surface(
) {
let output_path = output_dir().join(&format!("{}-out.png", output_base_name));
- println!("output:\t{}", output_path.to_string_lossy());
+ println!("output: {}", output_path.to_string_lossy());
let mut output_file = File::create(output_path).unwrap();
output_surf
@@ -106,27 +137,31 @@ pub fn compare_to_surface(
evaluate_diff(&diff, output_base_name);
}
-const MAX_DIFF: u8 = 2;
-
fn evaluate_diff(diff: &BufferDiff, output_base_name: &str) {
match diff {
BufferDiff::DifferentSizes => unreachable!("surfaces should be of the same size"),
BufferDiff::Diff(diff) => {
- let surf = diff.surface.clone().into_image_surface().unwrap();
- let diff_path = output_dir().join(&format!("{}-diff.png", output_base_name));
- println!("diff:\t{}", diff_path.to_string_lossy());
-
- let mut output_file = File::create(diff_path).unwrap();
- surf.write_to_png(&mut output_file).unwrap();
-
- if diff.num_pixels_changed != 0 && diff.max_diff > MAX_DIFF {
+ if diff.distinguishable() {
println!(
"{}: {} pixels changed with maximum difference of {}",
output_base_name, diff.num_pixels_changed, diff.max_diff,
);
- panic!("surfaces are too different");
+ save_diff(diff, output_base_name);
+
+ if diff.inacceptable() {
+ panic!("surfaces are too different");
+ }
}
}
}
+
+ fn save_diff(diff: &Diff, output_base_name: &str) {
+ let surf = diff.surface.clone().into_image_surface().unwrap();
+ let diff_path = output_dir().join(&format!("{}-diff.png", output_base_name));
+ println!("diff: {}", diff_path.to_string_lossy());
+
+ let mut output_file = File::create(diff_path).unwrap();
+ surf.write_to_png(&mut output_file).unwrap();
+ }
}
diff --git a/tests/src/reference.rs b/tests/src/reference.rs
index b47f19eb..3d2759e1 100644
--- a/tests/src/reference.rs
+++ b/tests/src/reference.rs
@@ -49,7 +49,9 @@ fn reference_test(name: &str) {
.read_path(&path)
.unwrap_or_else(|e| panic!("could not load: {}", e));
- let renderer = CairoRenderer::new(&handle).test_mode().with_dpi(TEST_SUITE_DPI, TEST_SUITE_DPI);
+ let renderer = CairoRenderer::new(&handle)
+ .test_mode()
+ .with_dpi(TEST_SUITE_DPI, TEST_SUITE_DPI);
let (width, height) = image_size(renderer.intrinsic_dimensions(), TEST_SUITE_DPI);
let surface = cairo::ImageSurface::create(
@@ -197,9 +199,40 @@ fn normalize(l: &Length, dpi: f64) -> f64 {
// FIXME: see how to share this code
mod duplicated_from_librsvg_crate {
use super::*;
- use rsvg_internals::{compare_surfaces, BufferDiff};
+ use rsvg_internals::{compare_surfaces, BufferDiff, Diff};
+ use std::convert::TryFrom;
use std::env;
use std::fs;
+ use std::sync::Once;
+
+ fn tolerable_difference() -> u8 {
+ static mut TOLERANCE: u8 = 2;
+
+ static ONCE: Once = Once::new();
+ ONCE.call_once(|| unsafe {
+ if let Ok(str) = env::var("RSVG_TEST_TOLERANCE") {
+ let value: usize = str.parse().unwrap();
+ TOLERANCE = u8::try_from(value).unwrap();
+ }
+ });
+
+ unsafe { TOLERANCE }
+ }
+
+ trait Deviation {
+ fn distinguishable(&self) -> bool;
+ fn inacceptable(&self) -> bool;
+ }
+
+ impl Deviation for Diff {
+ fn distinguishable(&self) -> bool {
+ self.max_diff > 2
+ }
+
+ fn inacceptable(&self) -> bool {
+ self.max_diff > tolerable_difference()
+ }
+ }
fn output_dir() -> PathBuf {
let path = PathBuf::from(
@@ -234,7 +267,7 @@ mod duplicated_from_librsvg_crate {
output_base_name: &str,
) {
let output_path = output_dir().join(&format!("{}-out.png", output_base_name));
- println!("output:\t{}", output_path.to_string_lossy());
+ println!("output: {}", output_path.to_string_lossy());
let mut output_file = File::create(output_path).unwrap();
output_surf
@@ -248,30 +281,34 @@ mod duplicated_from_librsvg_crate {
evaluate_diff(&diff, output_base_name);
}
- const MAX_DIFF: u8 = 2;
-
fn evaluate_diff(diff: &BufferDiff, output_base_name: &str) {
match diff {
BufferDiff::DifferentSizes => unreachable!("surfaces should be of the same size"),
BufferDiff::Diff(diff) => {
- let surf = diff.surface.clone().into_image_surface().unwrap();
- let diff_path = output_dir().join(&format!("{}-diff.png", output_base_name));
- println!("diff:\t{}", diff_path.to_string_lossy());
-
- let mut output_file = File::create(diff_path).unwrap();
- surf.write_to_png(&mut output_file).unwrap();
-
- if diff.num_pixels_changed != 0 && diff.max_diff > MAX_DIFF {
+ if diff.distinguishable() {
println!(
"{}: {} pixels changed with maximum difference of {}",
output_base_name, diff.num_pixels_changed, diff.max_diff,
);
- panic!("surfaces are too different");
+ save_diff(diff, output_base_name);
+
+ if diff.inacceptable() {
+ panic!("surfaces are too different");
+ }
}
}
}
}
+
+ fn save_diff(diff: &Diff, output_base_name: &str) {
+ let surf = diff.surface.clone().into_image_surface().unwrap();
+ let diff_path = output_dir().join(&format!("{}-diff.png", output_base_name));
+ println!("diff: {}", diff_path.to_string_lossy());
+
+ let mut output_file = File::create(diff_path).unwrap();
+ surf.write_to_png(&mut output_file).unwrap();
+ }
}
#[test_resources("tests/fixtures/reftests/*.svg")]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]