[librsvg] (#358) - Use assert_approx_eq_cairo in tests



commit 3a660806e581d00a73965700262f617d2132828f
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Oct 11 09:53:04 2018 -0500

    (#358) - Use assert_approx_eq_cairo in tests
    
    Otherwise, the tests fail on i386 due to its conversions from 80-bit
    extended precision.
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/issues/358

 rsvg_internals/src/aspect_ratio.rs |  45 ++++++++------
 rsvg_internals/src/gradient.rs     |  12 +++-
 rsvg_internals/src/transform.rs    | 123 ++++++++++++++++++++-----------------
 3 files changed, 103 insertions(+), 77 deletions(-)
---
diff --git a/rsvg_internals/src/aspect_ratio.rs b/rsvg_internals/src/aspect_ratio.rs
index baad541f..a3def588 100644
--- a/rsvg_internals/src/aspect_ratio.rs
+++ b/rsvg_internals/src/aspect_ratio.rs
@@ -216,6 +216,7 @@ impl Parse for AspectRatio {
 #[cfg(test)]
 mod tests {
     use super::*;
+    use float_eq_cairo::ApproxEqCairo;
 
     #[test]
     fn parsing_invalid_strings_yields_error() {
@@ -300,79 +301,85 @@ mod tests {
         );
     }
 
+    fn assert_quadruples_equal(a: &(f64, f64, f64, f64), b: &(f64, f64, f64, f64)) {
+        assert_approx_eq_cairo!(a.0, b.0);
+        assert_approx_eq_cairo!(a.1, b.1);
+        assert_approx_eq_cairo!(a.2, b.2);
+        assert_approx_eq_cairo!(a.3, b.3);
+    }
+
     #[test]
     fn aligns() {
         let foo = AspectRatio::parse_str("xMinYMin meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(0.0, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMinYMin slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, 0.0, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, 0.0, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMinYMid meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(0.0, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMinYMid slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-
-        assert_eq!(foo, (0.0, -49.5, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, -49.5, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMinYMax meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(0.0, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMinYMax slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, -99.0, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, -99.0, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMidYMin meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (4.95, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(4.95, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMin slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, 0.0, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, 0.0, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMidYMid meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (4.95, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(4.95, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMid slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, -49.5, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, -49.5, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMidYMax meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (4.95, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(4.95, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMidYMax slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, -99.0, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, -99.0, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMaxYMin meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (9.9, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(9.9, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMin slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, 0.0, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, 0.0, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMaxYMid meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (9.9, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(9.9, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMid slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, -49.5, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, -49.5, 10.0, 100.0));
 
         let foo = AspectRatio::parse_str("xMaxYMax meet", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (9.9, 0.0, 0.1, 1.0));
+        assert_quadruples_equal(&foo, &(9.9, 0.0, 0.1, 1.0));
 
         let foo = AspectRatio::parse_str("xMaxYMax slice", ()).unwrap();
         let foo = foo.compute(1.0, 10.0, 0.0, 0.0, 10.0, 1.0);
-        assert_eq!(foo, (0.0, -99.0, 10.0, 100.0));
+        assert_quadruples_equal(&foo, &(0.0, -99.0, 10.0, 100.0));
     }
 }
diff --git a/rsvg_internals/src/gradient.rs b/rsvg_internals/src/gradient.rs
index abc24ca2..36b30e63 100644
--- a/rsvg_internals/src/gradient.rs
+++ b/rsvg_internals/src/gradient.rs
@@ -708,6 +708,7 @@ pub fn gradient_resolve_fallbacks_and_set_pattern(
 #[cfg(test)]
 mod tests {
     use super::*;
+    use float_eq_cairo::ApproxEqCairo;
 
     #[test]
     fn parses_spread_method() {
@@ -723,16 +724,21 @@ mod tests {
         assert!(SpreadMethod::parse_str("foobar", ()).is_err());
     }
 
+    fn assert_tuples_equal(a: &(f64, f64), b: &(f64, f64)) {
+        assert_approx_eq_cairo!(a.0, b.0);
+        assert_approx_eq_cairo!(a.1, b.1);
+    }
+
     #[test]
     fn fixes_focus_point() {
         // inside the circle
-        assert_eq!(fix_focus_point(1.0, 1.0, 2.0, 1.0, 3.0), (1.0, 1.0));
+        assert_tuples_equal(&fix_focus_point(1.0, 1.0, 2.0, 1.0, 3.0), &(1.0, 1.0));
 
         // on the edge
-        assert_eq!(fix_focus_point(1.0, 1.0, 2.0, 1.0, 2.0), (1.0, 1.0));
+        assert_tuples_equal(&fix_focus_point(1.0, 1.0, 2.0, 1.0, 2.0), &(1.0, 1.0));
 
         // outside the circle
-        assert_eq!(fix_focus_point(1.0, 1.0, 3.0, 1.0, 1.0), (2.0, 1.0));
+        assert_tuples_equal(&fix_focus_point(1.0, 1.0, 3.0, 1.0, 1.0), &(2.0, 1.0));
     }
 
     #[test]
diff --git a/rsvg_internals/src/transform.rs b/rsvg_internals/src/transform.rs
index 0ddbd60d..9bbb6692 100644
--- a/rsvg_internals/src/transform.rs
+++ b/rsvg_internals/src/transform.rs
@@ -247,11 +247,24 @@ fn make_rotation_matrix(angle_degrees: f64, tx: f64, ty: f64) -> cairo::Matrix {
 #[cfg(test)]
 mod tests {
     use super::*;
+    use float_cmp::ApproxEq;
+    use std::f64;
 
     fn parse_transform(s: &str) -> Result<cairo::Matrix, ValueErrorKind> {
         cairo::Matrix::parse_str(s, ())
     }
 
+    fn assert_matrix_eq(a: &cairo::Matrix, b: &cairo::Matrix) {
+        let epsilon = 8.0 * f64::EPSILON; // kind of arbitrary, but allow for some sloppiness
+
+        assert!(a.xx.approx_eq(&b.xx, epsilon, 1));
+        assert!(a.yx.approx_eq(&b.yx, epsilon, 1));
+        assert!(a.xy.approx_eq(&b.xy, epsilon, 1));
+        assert!(a.yy.approx_eq(&b.yy, epsilon, 1));
+        assert!(a.x0.approx_eq(&b.x0, epsilon, 1));
+        assert!(a.y0.approx_eq(&b.y0, epsilon, 1));
+    }
+
     #[test]
     fn parses_valid_transform() {
         let t = cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, 20.0, 30.0);
@@ -259,9 +272,9 @@ mod tests {
         let r = make_rotation_matrix(30.0, 10.0, 10.0);
 
         let a = cairo::Matrix::multiply(&s, &t);
-        assert_eq!(
-            parse_transform("translate(20, 30), scale (10) rotate (30 10 10)").unwrap(),
-            cairo::Matrix::multiply(&r, &a)
+        assert_matrix_eq(
+            &parse_transform("translate(20, 30), scale (10) rotate (30 10 10)").unwrap(),
+            &cairo::Matrix::multiply(&r, &a),
         );
     }
 
@@ -312,71 +325,71 @@ mod tests {
 
     #[test]
     fn parses_matrix() {
-        assert_eq!(
-            parse_transform("matrix (1 2 3 4 5 6)").unwrap(),
-            cairo::Matrix::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
+        assert_matrix_eq(
+            &parse_transform("matrix (1 2 3 4 5 6)").unwrap(),
+            &cairo::Matrix::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0),
         );
 
-        assert_eq!(
-            parse_transform("matrix(1,2,3,4 5 6)").unwrap(),
-            cairo::Matrix::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
+        assert_matrix_eq(
+            &parse_transform("matrix(1,2,3,4 5 6)").unwrap(),
+            &cairo::Matrix::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0),
         );
 
-        assert_eq!(
-            parse_transform("matrix (1,2.25,-3.25e2,4 5 6)").unwrap(),
-            cairo::Matrix::new(1.0, 2.25, -325.0, 4.0, 5.0, 6.0)
+        assert_matrix_eq(
+            &parse_transform("matrix (1,2.25,-3.25e2,4 5 6)").unwrap(),
+            &cairo::Matrix::new(1.0, 2.25, -325.0, 4.0, 5.0, 6.0),
         );
     }
 
     #[test]
     fn parses_translate() {
-        assert_eq!(
-            parse_transform("translate(-1 -2)").unwrap(),
-            cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, -1.0, -2.0)
+        assert_matrix_eq(
+            &parse_transform("translate(-1 -2)").unwrap(),
+            &cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, -1.0, -2.0),
         );
 
-        assert_eq!(
-            parse_transform("translate(-1, -2)").unwrap(),
-            cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, -1.0, -2.0)
+        assert_matrix_eq(
+            &parse_transform("translate(-1, -2)").unwrap(),
+            &cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, -1.0, -2.0),
         );
 
-        assert_eq!(
-            parse_transform("translate(-1)").unwrap(),
-            cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, -1.0, 0.0)
+        assert_matrix_eq(
+            &parse_transform("translate(-1)").unwrap(),
+            &cairo::Matrix::new(1.0, 0.0, 0.0, 1.0, -1.0, 0.0),
         );
     }
 
     #[test]
     fn parses_scale() {
-        assert_eq!(
-            parse_transform("scale (-1)").unwrap(),
-            cairo::Matrix::new(-1.0, 0.0, 0.0, -1.0, 0.0, 0.0)
+        assert_matrix_eq(
+            &parse_transform("scale (-1)").unwrap(),
+            &cairo::Matrix::new(-1.0, 0.0, 0.0, -1.0, 0.0, 0.0),
         );
 
-        assert_eq!(
-            parse_transform("scale(-1 -2)").unwrap(),
-            cairo::Matrix::new(-1.0, 0.0, 0.0, -2.0, 0.0, 0.0)
+        assert_matrix_eq(
+            &parse_transform("scale(-1 -2)").unwrap(),
+            &cairo::Matrix::new(-1.0, 0.0, 0.0, -2.0, 0.0, 0.0),
         );
 
-        assert_eq!(
-            parse_transform("scale(-1, -2)").unwrap(),
-            cairo::Matrix::new(-1.0, 0.0, 0.0, -2.0, 0.0, 0.0)
+        assert_matrix_eq(
+            &parse_transform("scale(-1, -2)").unwrap(),
+            &cairo::Matrix::new(-1.0, 0.0, 0.0, -2.0, 0.0, 0.0),
         );
     }
 
     #[test]
     fn parses_rotate() {
-        assert_eq!(
-            parse_transform("rotate (30)").unwrap(),
-            make_rotation_matrix(30.0, 0.0, 0.0)
+        assert_matrix_eq(
+            &parse_transform("rotate (30)").unwrap(),
+            &make_rotation_matrix(30.0, 0.0, 0.0),
         );
-        assert_eq!(
-            parse_transform("rotate (30,-1,-2)").unwrap(),
-            make_rotation_matrix(30.0, -1.0, -2.0)
+        assert_matrix_eq(
+            &parse_transform("rotate (30,-1,-2)").unwrap(),
+            &make_rotation_matrix(30.0, -1.0, -2.0),
         );
-        assert_eq!(
-            parse_transform("rotate(30, -1, -2)").unwrap(),
-            make_rotation_matrix(30.0, -1.0, -2.0)
+        assert_matrix_eq(
+            &parse_transform("rotate(30, -1, -2)").unwrap(),
+            &make_rotation_matrix(30.0, -1.0, -2.0),
         );
     }
 
@@ -394,17 +407,17 @@ mod tests {
 
     #[test]
     fn parses_skew_x() {
-        assert_eq!(
-            parse_transform("skewX (30)").unwrap(),
-            make_skew_x_matrix(30.0)
+        assert_matrix_eq(
+            &parse_transform("skewX (30)").unwrap(),
+            &make_skew_x_matrix(30.0),
         );
     }
 
     #[test]
     fn parses_skew_y() {
-        assert_eq!(
-            parse_transform("skewY (30)").unwrap(),
-            make_skew_y_matrix(30.0)
+        assert_matrix_eq(
+            &parse_transform("skewY (30)").unwrap(),
+            &make_skew_y_matrix(30.0),
         );
     }
 
@@ -414,25 +427,25 @@ mod tests {
         let s = cairo::Matrix::new(10.0, 0.0, 0.0, 10.0, 0.0, 0.0);
         let r = make_rotation_matrix(30.0, 10.0, 10.0);
 
-        assert_eq!(
-            parse_transform("scale(10)rotate(30, 10, 10)").unwrap(),
-            cairo::Matrix::multiply(&r, &s)
+        assert_matrix_eq(
+            &parse_transform("scale(10)rotate(30, 10, 10)").unwrap(),
+            &cairo::Matrix::multiply(&r, &s),
         );
 
-        assert_eq!(
-            parse_transform("translate(20, 30), scale (10)").unwrap(),
-            cairo::Matrix::multiply(&s, &t)
+        assert_matrix_eq(
+            &parse_transform("translate(20, 30), scale (10)").unwrap(),
+            &cairo::Matrix::multiply(&s, &t),
         );
 
         let a = cairo::Matrix::multiply(&s, &t);
-        assert_eq!(
-            parse_transform("translate(20, 30), scale (10) rotate (30 10 10)").unwrap(),
-            cairo::Matrix::multiply(&r, &a)
+        assert_matrix_eq(
+            &parse_transform("translate(20, 30), scale (10) rotate (30 10 10)").unwrap(),
+            &cairo::Matrix::multiply(&r, &a),
         );
     }
 
     #[test]
     fn parses_empty() {
-        assert_eq!(parse_transform("").unwrap(), cairo::Matrix::identity());
+        assert_matrix_eq(&parse_transform("").unwrap(), &cairo::Matrix::identity());
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]