From e1c10d27f7d093aaa176add2fc7d1d3bfe23b161 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Sun, 23 Feb 2020 18:57:13 +0100 Subject: [PATCH] improve code coverage of `(U)Float` --- src/types/float.rs | 25 +++++++++++++++++++++ src/types/ufloat.rs | 54 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/types/float.rs b/src/types/float.rs index e14b9c5..d03621d 100644 --- a/src/types/float.rs +++ b/src/types/float.rs @@ -177,6 +177,7 @@ impl ::core::hash::Hash for Float { #[cfg(test)] mod tests { use super::*; + use core::hash::{Hash, Hasher}; use pretty_assertions::assert_eq; #[test] @@ -202,6 +203,29 @@ mod tests { ); } + #[test] + fn test_hash() { + let mut hasher_left = std::collections::hash_map::DefaultHasher::new(); + let mut hasher_right = std::collections::hash_map::DefaultHasher::new(); + + assert_eq!( + Float::new(0.0).hash(&mut hasher_left), + Float::new(-0.0).hash(&mut hasher_right) + ); + + assert_eq!(hasher_left.finish(), hasher_right.finish()); + + let mut hasher_left = std::collections::hash_map::DefaultHasher::new(); + let mut hasher_right = std::collections::hash_map::DefaultHasher::new(); + + assert_eq!( + Float::new(1.0).hash(&mut hasher_left), + Float::new(1.0).hash(&mut hasher_right) + ); + + assert_eq!(hasher_left.finish(), hasher_right.finish()); + } + #[test] fn test_eq() { struct _AssertEq @@ -213,6 +237,7 @@ mod tests { fn test_partial_eq() { assert_eq!(Float::new(1.0).eq(&Float::new(1.0)), true); assert_eq!(Float::new(1.0).eq(&Float::new(33.3)), false); + assert_eq!(Float::new(1.1), 1.1); } #[test] diff --git a/src/types/ufloat.rs b/src/types/ufloat.rs index 4678d89..eb18e00 100644 --- a/src/types/ufloat.rs +++ b/src/types/ufloat.rs @@ -188,6 +188,7 @@ impl ::core::hash::Hash for UFloat { #[cfg(test)] mod tests { use super::*; + use core::hash::{Hash, Hasher}; use pretty_assertions::assert_eq; #[test] @@ -213,6 +214,49 @@ mod tests { assert!(UFloat::from_str("-inf").is_err()); } + #[test] + fn test_hash() { + let mut hasher_left = std::collections::hash_map::DefaultHasher::new(); + let mut hasher_right = std::collections::hash_map::DefaultHasher::new(); + + assert_eq!( + UFloat::new(1.0).hash(&mut hasher_left), + UFloat::new(1.0).hash(&mut hasher_right) + ); + + assert_eq!(hasher_left.finish(), hasher_right.finish()); + } + + #[test] + fn test_ord() { + assert_eq!(UFloat::new(1.1).cmp(&UFloat::new(1.1)), Ordering::Equal); + assert_eq!(UFloat::new(1.1).cmp(&UFloat::new(2.1)), Ordering::Less); + assert_eq!(UFloat::new(1.1).cmp(&UFloat::new(0.1)), Ordering::Greater); + } + + #[test] + fn test_partial_ord() { + assert_eq!( + UFloat::new(1.1).partial_cmp(&UFloat::new(1.1)), + Some(Ordering::Equal) + ); + assert_eq!( + UFloat::new(1.1).partial_cmp(&UFloat::new(2.1)), + Some(Ordering::Less) + ); + assert_eq!( + UFloat::new(1.1).partial_cmp(&UFloat::new(0.1)), + Some(Ordering::Greater) + ); + } + + #[test] + fn test_partial_eq() { + assert_eq!(UFloat::new(1.0).eq(&UFloat::new(1.0)), true); + assert_eq!(UFloat::new(1.0).eq(&UFloat::new(33.3)), false); + assert_eq!(UFloat::new(1.1), 1.1); + } + #[test] #[should_panic = "float must be positive: `-1.1`"] fn test_new_negative() { UFloat::new(-1.1); } @@ -233,11 +277,6 @@ mod tests { #[should_panic = "float must not be `NaN`"] fn test_new_nan() { UFloat::new(::core::f32::NAN); } - #[test] - fn test_partial_eq() { - assert_eq!(UFloat::new(1.1), 1.1); - } - #[test] fn test_as_f32() { assert_eq!(UFloat::new(1.1).as_f32(), 1.1_f32); @@ -253,7 +292,10 @@ mod tests { fn test_try_from() { assert_eq!(UFloat::try_from(1.1_f32).unwrap(), UFloat::new(1.1)); - assert!(UFloat::try_from(-1.1_f32).is_err()); + assert_eq!( + UFloat::try_from(-1.1_f32), + Err(Error::custom("float must be positive: `-1.1`")) + ); assert!(UFloat::try_from(::core::f32::INFINITY).is_err()); assert!(UFloat::try_from(::core::f32::NAN).is_err()); assert!(UFloat::try_from(::core::f32::NEG_INFINITY).is_err());