From a2ea1fada6397449fcd3e686bacfc718f6b6f439 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 10 Dec 2020 12:30:57 +0000 Subject: [PATCH] Lower precision of tests where required --- rand_distr/src/cauchy.rs | 2 +- rand_distr/src/normal.rs | 3 ++- rand_distr/src/pareto.rs | 16 ++++++++-------- rand_distr/tests/value_stability.rs | 24 ++++++++++++++++++------ 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/rand_distr/src/cauchy.rs b/rand_distr/src/cauchy.rs index ffe86d00a9b..1fadf0c6f61 100644 --- a/rand_distr/src/cauchy.rs +++ b/rand_distr/src/cauchy.rs @@ -160,7 +160,7 @@ mod test { 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-6, "expected: {} = {}", a, b); + assert!((a - b).abs() < 1e-5, "expected: {} = {}", a, b); } } } diff --git a/rand_distr/src/normal.rs b/rand_distr/src/normal.rs index ae82799147a..4ab0fbd1901 100644 --- a/rand_distr/src/normal.rs +++ b/rand_distr/src/normal.rs @@ -345,7 +345,8 @@ 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_eq!((lnorm.norm.mean, lnorm.norm.std_dev), (1.0, 1.0)); + assert!((lnorm.norm.mean - 1.0).abs() < 1e-15); + assert_eq!(lnorm.norm.std_dev, 1.0); } #[test] fn test_log_normal_invalid_sd() { diff --git a/rand_distr/src/pareto.rs b/rand_distr/src/pareto.rs index 217899ed9a7..0c62383b1cb 100644 --- a/rand_distr/src/pareto.rs +++ b/rand_distr/src/pareto.rs @@ -87,6 +87,7 @@ where F: Float, OpenClosed01: Distribution #[cfg(test)] mod tests { use super::*; + use core::fmt::{Debug, Display}; #[test] #[should_panic] @@ -108,21 +109,20 @@ mod tests { #[test] fn value_stability() { - fn test_samples>( - distr: D, zero: F, expected: &[F], + fn test_samples>( + distr: D, thresh: F, expected: &[F], ) { let mut rng = crate::test::rng(213); - let mut buf = [zero; 4]; - for x in &mut buf { - *x = rng.sample(&distr); + for v in expected { + let x = rng.sample(&distr); + assert!((x - *v).abs() < thresh, "not approx eq: {}, {}", x, *v); } - assert_eq!(buf, expected); } - test_samples(Pareto::new(1.0, 1.0).unwrap(), 0f32, &[ + test_samples(Pareto::new(1f32, 1.0).unwrap(), 1e-6, &[ 1.0423688, 2.1235929, 4.132709, 1.4679428, ]); - test_samples(Pareto::new(2.0, 0.5).unwrap(), 0f64, &[ + test_samples(Pareto::new(2.0, 0.5).unwrap(), 1e-14, &[ 9.019295276219136, 4.3097126018270595, 6.837815045397157, diff --git a/rand_distr/tests/value_stability.rs b/rand_distr/tests/value_stability.rs index 986b9963283..1d7b252fc11 100644 --- a/rand_distr/tests/value_stability.rs +++ b/rand_distr/tests/value_stability.rs @@ -6,7 +6,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core::{fmt::Debug, cmp::PartialEq}; +use core::fmt::{Debug, Display}; +use core::cmp::PartialEq; +use num_traits::Float; use rand::Rng; use rand_distr::*; @@ -26,6 +28,16 @@ fn test_samples>( } } +fn test_samples_approx>( + seed: u64, distr: D, thresh: F, expected: &[F], +) { + let mut rng = get_rng(seed); + for &val in expected { + let x = rng.sample(&distr); + assert!((x - val).abs() < thresh, "not approx eq: {}, {}", x, val); + } +} + #[test] fn binominal_stability() { // We have multiple code paths: np < 10, p > 0.5 @@ -95,7 +107,7 @@ fn pareto_stability() { test_samples(213, Pareto::new(1.0, 1.0).unwrap(), &[ 1.0423688f32, 2.1235929, 4.132709, 1.4679428, ]); - test_samples(213, Pareto::new(2.0, 0.5).unwrap(), &[ + test_samples_approx(213, Pareto::new(2.0, 0.5).unwrap(), 1e-14, &[ 9.019295276219136f64, 4.3097126018270595, 6.837815045397157, @@ -184,7 +196,7 @@ fn gamma_stability() { 0.5013580219361969, 0.1457735613733489, ]); - test_samples(223, ChiSquared::new(0.1).unwrap(), &[ + test_samples_approx(223, ChiSquared::new(0.1).unwrap(), 1e-15, &[ 0.014824404726978617f64, 0.021602123937134326, 0.0000003431429746851693, @@ -201,7 +213,7 @@ fn gamma_stability() { test_samples(223, FisherF::new(1.0, 1.0).unwrap(), &[ 0.29925257f32, 3.4392934, 9.567652, 0.020074, ]); - test_samples(223, FisherF::new(0.7, 13.5).unwrap(), &[ + test_samples_approx(223, FisherF::new(0.7, 13.5).unwrap(), 1e-14, &[ 3.3196593155045124f64, 0.3409169916262829, 0.03377989856426519, @@ -285,7 +297,7 @@ fn normal_stability() { test_samples(213, LogNormal::new(0.0, 1.0).unwrap(), &[ 0.88830346f32, 2.1844804, 1.0678421, 0.30322206, ]); - test_samples(213, LogNormal::new(2.0, 0.5).unwrap(), &[ + test_samples_approx(213, LogNormal::new(2.0, 0.5).unwrap(), 1e-14, &[ 6.964174338639032f64, 10.921015733601452, 7.6355881556915906, @@ -339,6 +351,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-6, "expected: {} = {}", a, b); + assert!((a - b).abs() < 1e-5, "expected: {} = {}", a, b); } }