[librsvg: 1/6] float_eq_cairo: add an assertion macro
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/6] float_eq_cairo: add an assertion macro
- Date: Tue, 15 May 2018 15:08:37 +0000 (UTC)
commit 569a15812a948150a7694f75ca1b2003f0bb94b6
Author: Paolo Borelli <pborelli gnome org>
Date: Mon May 14 20:12:21 2018 +0200
float_eq_cairo: add an assertion macro
rsvg_internals/src/float_eq_cairo.rs | 30 +++++++++++++++++++++++++++++-
rsvg_internals/src/lib.rs | 4 +++-
2 files changed, 32 insertions(+), 2 deletions(-)
---
diff --git a/rsvg_internals/src/float_eq_cairo.rs b/rsvg_internals/src/float_eq_cairo.rs
index 09bfc8d9..09351149 100644
--- a/rsvg_internals/src/float_eq_cairo.rs
+++ b/rsvg_internals/src/float_eq_cairo.rs
@@ -1,6 +1,7 @@
use float_cmp::ApproxEq;
-// The following are copied from cairo/src/{cairo-fixed-private.h, cairo-fixed-type-private.h}
+// The following are copied from cairo/src/{cairo-fixed-private.h,
+// cairo-fixed-type-private.h}
const CAIRO_FIXED_FRAC_BITS: u64 = 8;
const CAIRO_MAGIC_NUMBER_FIXED: f64 = (1u64 << (52 - CAIRO_FIXED_FRAC_BITS)) as f64 * 1.5;
@@ -62,6 +63,22 @@ impl ApproxEqCairo for f64 {
}
}
+// Macro for usage in unit tests
+#[macro_export]
+macro_rules! assert_approx_eq_cairo {
+ ($left:expr, $right:expr) => ({
+ match (&$left, &$right) {
+ (l, r) => {
+ if !l.approx_eq_cairo(r) {
+ panic!(r#"assertion failed: `(left == right)`
+ left: `{:?}`,
+ right: `{:?}`"#, l, r)
+ }
+ }
+ }
+ });
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -118,4 +135,15 @@ mod tests {
assert!(9_007_199_254_740_992.0.approx_eq_cairo(&9_007_199_254_740_994.0));
assert!(!9_007_199_254_740_992.0.approx_eq_cairo(&9_007_199_254_740_996.0));
}
+
+ #[test]
+ fn assert_approx_eq_cairo_should_not_panic() {
+ assert_approx_eq_cairo!(42_f64, 42_f64);
+ }
+
+ #[test]
+ #[should_panic]
+ fn assert_approx_eq_cairo_should_panic() {
+ assert_approx_eq_cairo!(3_f64, 42_f64);
+ }
}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 2333e32e..85194dcc 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -153,6 +153,9 @@ pub use viewbox::RsvgViewBox;
#[macro_use]
mod coord_units;
+#[macro_use]
+mod float_eq_cairo;
+
#[macro_use]
mod property_macros;
@@ -166,7 +169,6 @@ mod cond;
mod draw;
mod drawing_ctx;
mod error;
-mod float_eq_cairo;
mod gradient;
mod handle;
mod image;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]