From 33b8f37231f4b54cb4f1e50ae3a74483abc01dad Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 14 Dec 2020 13:44:46 +0000 Subject: [PATCH] Address review: use assert_almost_eq macro --- README.md | 2 +- rand_distr/src/cauchy.rs | 3 +-- rand_distr/src/normal.rs | 2 +- rand_distr/src/pareto.rs | 6 +++--- rand_distr/tests/value_stability.rs | 30 ++++++++++++++++------------- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 3ca09b008b7..214d9ed41e0 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ greater, and 0.4 and 0.3 (since approx. June 2017) require Rustc version 1.15 or greater. Subsets of the Rand code may work with older Rust versions, but this is not supported. -Continuous Integration (CI) will always test the oldest supported Rustc version +Continuous Integration (CI) will always test the minimum supported Rustc version (the MSRV). The current policy is that this can be updated in any Rand release if required, but the change must be noted in the changelog. diff --git a/rand_distr/src/cauchy.rs b/rand_distr/src/cauchy.rs index 1fadf0c6f61..952b7f29dc9 100644 --- a/rand_distr/src/cauchy.rs +++ b/rand_distr/src/cauchy.rs @@ -159,8 +159,7 @@ mod test { gen_samples(10f32, 7.0, &mut buf); let expected = [15.023088, -5.446413, 3.7092876, 3.112482]; for (a, b) in buf.iter().zip(expected.iter()) { - let (a, b) = (*a, *b); - assert!((a - b).abs() < 1e-5, "expected: {} = {}", a, b); + assert_almost_eq!(*a, *b, 1e-5); } } } diff --git a/rand_distr/src/normal.rs b/rand_distr/src/normal.rs index 4ab0fbd1901..8c3c8f8fd2f 100644 --- a/rand_distr/src/normal.rs +++ b/rand_distr/src/normal.rs @@ -345,7 +345,7 @@ mod tests { assert_almost_eq!(lnorm.norm.std_dev, 1.0, 2e-16); let lnorm = LogNormal::from_mean_cv(e.powf(1.5), (e - 1.0).sqrt()).unwrap(); - assert!((lnorm.norm.mean - 1.0).abs() < 1e-15); + assert_almost_eq!(lnorm.norm.mean, 1.0, 1e-15); assert_eq!(lnorm.norm.std_dev, 1.0); } #[test] diff --git a/rand_distr/src/pareto.rs b/rand_distr/src/pareto.rs index 0c62383b1cb..3250c86ffe9 100644 --- a/rand_distr/src/pareto.rs +++ b/rand_distr/src/pareto.rs @@ -87,7 +87,7 @@ where F: Float, OpenClosed01: Distribution #[cfg(test)] mod tests { use super::*; - use core::fmt::{Debug, Display}; + use core::fmt::{Debug, Display, LowerExp}; #[test] #[should_panic] @@ -109,13 +109,13 @@ mod tests { #[test] fn value_stability() { - fn test_samples>( + fn test_samples>( distr: D, thresh: F, expected: &[F], ) { let mut rng = crate::test::rng(213); for v in expected { let x = rng.sample(&distr); - assert!((x - *v).abs() < thresh, "not approx eq: {}, {}", x, *v); + assert_almost_eq!(x, *v, thresh); } } diff --git a/rand_distr/tests/value_stability.rs b/rand_distr/tests/value_stability.rs index aafb95552d5..65c49644a41 100644 --- a/rand_distr/tests/value_stability.rs +++ b/rand_distr/tests/value_stability.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use average::assert_almost_eq; use core::fmt::Debug; use rand::Rng; use rand_distr::*; @@ -20,32 +21,35 @@ fn get_rng(seed: u64) -> impl rand::Rng { /// We only assert approximate equality since some platforms do not perform /// identically (i686-unknown-linux-gnu and most notably x86_64-pc-windows-gnu). trait ApproxEq { - fn is_approx_eq(&self, rhs: &Self) -> bool; + fn assert_almost_eq(&self, rhs: &Self); } impl ApproxEq for f32 { - fn is_approx_eq(&self, rhs: &Self) -> bool { - (self - rhs).abs() < 1e-6 + fn assert_almost_eq(&self, rhs: &Self) { + assert_almost_eq!(self, rhs, 1e-6); } } impl ApproxEq for f64 { - fn is_approx_eq(&self, rhs: &Self) -> bool { - (self - rhs).abs() < 1e-14 + fn assert_almost_eq(&self, rhs: &Self) { + assert_almost_eq!(self, rhs, 1e-14); } } impl ApproxEq for u64 { - fn is_approx_eq(&self, rhs: &Self) -> bool { - self == rhs + fn assert_almost_eq(&self, rhs: &Self) { + assert_eq!(self, rhs); } } impl ApproxEq for [T; 2] { - fn is_approx_eq(&self, rhs: &Self) -> bool { - self[0].is_approx_eq(&rhs[0]) && self[1].is_approx_eq(&rhs[1]) + fn assert_almost_eq(&self, rhs: &Self) { + self[0].assert_almost_eq(&rhs[0]); + self[1].assert_almost_eq(&rhs[1]); } } impl ApproxEq for [T; 3] { - fn is_approx_eq(&self, rhs: &Self) -> bool { - self[0].is_approx_eq(&rhs[0]) && self[1].is_approx_eq(&rhs[1]) && self[2].is_approx_eq(&rhs[2]) + fn assert_almost_eq(&self, rhs: &Self) { + self[0].assert_almost_eq(&rhs[0]); + self[1].assert_almost_eq(&rhs[1]); + self[2].assert_almost_eq(&rhs[2]); } } @@ -55,7 +59,7 @@ fn test_samples>( let mut rng = get_rng(seed); for val in expected { let x = rng.sample(&distr); - assert!(x.is_approx_eq(&val), "not approx eq: {:?}, {:?}", x, val); + x.assert_almost_eq(val); } } @@ -373,6 +377,6 @@ fn cauchy_stability() { let expected = [15.023088, -5.446413, 3.7092876, 3.112482]; for &a in expected.iter() { let b = rng.sample(&distr); - assert!((a - b).abs() < 1e-5, "not approx eq: {:?}, {:?}", a, b); + assert_almost_eq!(a, b, 1e-5); } }