Skip to content

Commit

Permalink
Lower precision of tests where required
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Dec 13, 2020
1 parent 33a78de commit a2ea1fa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion rand_distr/src/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion rand_distr/src/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 8 additions & 8 deletions rand_distr/src/pareto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ where F: Float, OpenClosed01: Distribution<F>
#[cfg(test)]
mod tests {
use super::*;
use core::fmt::{Debug, Display};

#[test]
#[should_panic]
Expand All @@ -108,21 +109,20 @@ mod tests {

#[test]
fn value_stability() {
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
fn test_samples<F: Float + Debug + Display, D: Distribution<F>>(
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,
Expand Down
24 changes: 18 additions & 6 deletions rand_distr/tests/value_stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -26,6 +28,16 @@ fn test_samples<F: Debug + Copy + PartialEq, D: Distribution<F>>(
}
}

fn test_samples_approx<F: Float + Display + Copy + PartialEq, D: Distribution<F>>(
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a2ea1fa

Please sign in to comment.