[librsvg: 3/5] Move the remaining test functions to rsvg_internals
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 3/5] Move the remaining test functions to rsvg_internals
- Date: Sat, 24 Oct 2020 01:17:54 +0000 (UTC)
commit dec2138ff0f58ceb9811a9ff72f2525e1ed9d828
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Oct 23 19:37:04 2020 -0500
Move the remaining test functions to rsvg_internals
Now there is no more duplicated code for tests.
rsvg_internals/src/test_utils.rs | 35 +++++++++-
tests/src/reference.rs | 140 +--------------------------------------
2 files changed, 35 insertions(+), 140 deletions(-)
---
diff --git a/rsvg_internals/src/test_utils.rs b/rsvg_internals/src/test_utils.rs
index d47dc730..6318a1aa 100644
--- a/rsvg_internals/src/test_utils.rs
+++ b/rsvg_internals/src/test_utils.rs
@@ -1,9 +1,10 @@
-use crate::surface_utils::shared_surface::SharedImageSurface;
use crate::surface_utils::compare_surfaces::{compare_surfaces, BufferDiff, Diff};
+use crate::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
use std::convert::TryFrom;
use std::env;
use std::fs::{self, File};
+use std::io::BufReader;
use std::path::PathBuf;
use std::sync::Once;
@@ -50,6 +51,38 @@ pub fn output_dir() -> PathBuf {
path
}
+// FIXME: proper errors?
+fn load_png_as_argb(path: &PathBuf) -> Result<cairo::ImageSurface, ()> {
+ let file = File::open(path).map_err(|_| ())?;
+
+ let mut reference_file = BufReader::new(file);
+
+ let png = cairo::ImageSurface::create_from_png(&mut reference_file).map_err(|_| ())?;
+ let argb =
+ cairo::ImageSurface::create(cairo::Format::ARgb32, png.get_width(), png.get_height())
+ .map_err(|_| ())?;
+
+ {
+ // convert to ARGB; the PNG may come as Rgb24
+ let cr = cairo::Context::new(&argb);
+ cr.set_source_surface(&png, 0.0, 0.0);
+ cr.paint();
+ }
+
+ Ok(argb)
+}
+
+pub fn compare_to_file(
+ output_surf: &SharedImageSurface,
+ output_base_name: &str,
+ reference_path: &PathBuf,
+) {
+ let png = load_png_as_argb(reference_path).unwrap();
+ let reference_surf = SharedImageSurface::wrap(png, SurfaceType::SRgb).unwrap();
+
+ compare_to_surface(output_surf, &reference_surf, output_base_name);
+}
+
pub fn compare_to_surface(
output_surf: &SharedImageSurface,
reference_surf: &SharedImageSurface,
diff --git a/tests/src/reference.rs b/tests/src/reference.rs
index cf961b15..856d3285 100644
--- a/tests/src/reference.rs
+++ b/tests/src/reference.rs
@@ -12,14 +12,11 @@ use test_generator::test_resources;
use cairo;
use librsvg::{CairoRenderer, IntrinsicDimensions, Length, Loader};
use rsvg_internals::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
-use std::fs::File;
-use std::io::BufReader;
+use rsvg_internals::test_utils::compare_to_file;
use std::path::PathBuf;
use crate::utils::{fixture_path, setup_font_map, setup_language};
-use self::duplicated_from_librsvg_crate::compare_to_file;
-
// The original reference images from the SVG1.1 test suite are at 72 DPI.
const TEST_SUITE_DPI: f64 = 72.0;
@@ -199,141 +196,6 @@ 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, 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()
- .expect("Can not parse RSVG_TEST_TOLERANCE as a number");
- TOLERANCE =
- u8::try_from(value).expect("RSVG_TEST_TOLERANCE should be between 0 and 255");
- }
- });
-
- 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(
- env::var_os("OUT_DIR")
- .expect(r#"OUT_DIR is not set, please set it or run under "cargo test""#),
- );
-
- fs::create_dir_all(&path).expect("could not create output directory for tests");
-
- path
- }
-
- // FIXME: proper errors?
- fn load_png_as_argb(path: &PathBuf) -> Result<cairo::ImageSurface, ()> {
- let file = File::open(path).map_err(|_| ())?;
-
- let mut reference_file = BufReader::new(file);
-
- let png = cairo::ImageSurface::create_from_png(&mut reference_file).map_err(|_| ())?;
- let argb =
- cairo::ImageSurface::create(cairo::Format::ARgb32, png.get_width(), png.get_height())
- .map_err(|_| ())?;
-
- {
- // convert to ARGB; the PNG may come as Rgb24
- let cr = cairo::Context::new(&argb);
- cr.set_source_surface(&png, 0.0, 0.0);
- cr.paint();
- }
-
- Ok(argb)
- }
-
- // FIXME: this is slightly different from librsvg_crate
- pub fn compare_to_file(
- output_surf: &SharedImageSurface,
- output_base_name: &str,
- reference_path: &PathBuf,
- ) {
- let png = load_png_as_argb(reference_path).unwrap();
- let reference_surf = SharedImageSurface::wrap(png, SurfaceType::SRgb).unwrap();
-
- compare_to_surface(output_surf, &reference_surf, output_base_name);
- }
-
- fn compare_to_surface(
- output_surf: &SharedImageSurface,
- reference_surf: &SharedImageSurface,
- output_base_name: &str,
- ) {
- let output_path = output_dir().join(&format!("{}-out.png", output_base_name));
- println!("output: {}", output_path.to_string_lossy());
-
- let mut output_file = File::create(output_path).unwrap();
- output_surf
- .clone()
- .into_image_surface()
- .unwrap()
- .write_to_png(&mut output_file)
- .unwrap();
-
- let diff = compare_surfaces(output_surf, reference_surf).unwrap();
- evaluate_diff(&diff, output_base_name);
- }
-
- fn evaluate_diff(diff: &BufferDiff, output_base_name: &str) {
- match diff {
- BufferDiff::DifferentSizes => unreachable!("surfaces should be of the same size"),
-
- BufferDiff::Diff(diff) => {
- if diff.distinguishable() {
- println!(
- "{}: {} pixels changed with maximum difference of {}",
- output_base_name, diff.num_pixels_changed, diff.max_diff,
- );
- 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")]
fn reftests(name: &str) {
reference_test(name);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]