Skip to content

Commit 06ed527

Browse files
committed
Lower precision of tests where required
1 parent 33a78de commit 06ed527

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

rand_distr/src/cauchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ mod test {
160160
let expected = [15.023088, -5.446413, 3.7092876, 3.112482];
161161
for (a, b) in buf.iter().zip(expected.iter()) {
162162
let (a, b) = (*a, *b);
163-
assert!((a - b).abs() < 1e-6, "expected: {} = {}", a, b);
163+
assert!((a - b).abs() < 1e-5, "expected: {} = {}", a, b);
164164
}
165165
}
166166
}

rand_distr/src/normal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ mod tests {
345345
assert_almost_eq!(lnorm.norm.std_dev, 1.0, 2e-16);
346346

347347
let lnorm = LogNormal::from_mean_cv(e.powf(1.5), (e - 1.0).sqrt()).unwrap();
348-
assert_eq!((lnorm.norm.mean, lnorm.norm.std_dev), (1.0, 1.0));
348+
assert!((lnorm.norm.mean - 1.0).abs() < 1e-15);
349+
assert_eq!(lnorm.norm.std_dev, 1.0);
349350
}
350351
#[test]
351352
fn test_log_normal_invalid_sd() {

rand_distr/src/pareto.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ where F: Float, OpenClosed01: Distribution<F>
8787
#[cfg(test)]
8888
mod tests {
8989
use super::*;
90+
use core::fmt::{Debug, Display};
9091

9192
#[test]
9293
#[should_panic]
@@ -108,21 +109,20 @@ mod tests {
108109

109110
#[test]
110111
fn value_stability() {
111-
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
112-
distr: D, zero: F, expected: &[F],
112+
fn test_samples<F: Float + Debug + Display, D: Distribution<F>>(
113+
distr: D, thresh: F, expected: &[F],
113114
) {
114115
let mut rng = crate::test::rng(213);
115-
let mut buf = [zero; 4];
116-
for x in &mut buf {
117-
*x = rng.sample(&distr);
116+
for v in expected {
117+
let x = rng.sample(&distr);
118+
assert!((x - *v).abs() < thresh, "not approx eq: {}, {}", x, *v);
118119
}
119-
assert_eq!(buf, expected);
120120
}
121121

122-
test_samples(Pareto::new(1.0, 1.0).unwrap(), 0f32, &[
122+
test_samples(Pareto::new(1f32, 1.0).unwrap(), 1e-6, &[
123123
1.0423688, 2.1235929, 4.132709, 1.4679428,
124124
]);
125-
test_samples(Pareto::new(2.0, 0.5).unwrap(), 0f64, &[
125+
test_samples(Pareto::new(2.0, 0.5).unwrap(), 1e-14, &[
126126
9.019295276219136,
127127
4.3097126018270595,
128128
6.837815045397157,

rand_distr/tests/value_stability.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use core::{fmt::Debug, cmp::PartialEq};
9+
use core::fmt::{Debug, Display};
10+
use core::cmp::PartialEq;
11+
use num_traits::Float;
1012
use rand::Rng;
1113
use rand_distr::*;
1214

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

31+
fn test_samples_approx<F: Float + Display + Copy + PartialEq, D: Distribution<F>>(
32+
seed: u64, distr: D, thresh: F, expected: &[F],
33+
) {
34+
let mut rng = get_rng(seed);
35+
for &val in expected {
36+
let x = rng.sample(&distr);
37+
assert!((x - val).abs() < thresh, "not approx eq: {}, {}", x, val);
38+
}
39+
}
40+
2941
#[test]
3042
fn binominal_stability() {
3143
// We have multiple code paths: np < 10, p > 0.5
@@ -95,7 +107,7 @@ fn pareto_stability() {
95107
test_samples(213, Pareto::new(1.0, 1.0).unwrap(), &[
96108
1.0423688f32, 2.1235929, 4.132709, 1.4679428,
97109
]);
98-
test_samples(213, Pareto::new(2.0, 0.5).unwrap(), &[
110+
test_samples_approx(213, Pareto::new(2.0, 0.5).unwrap(), 1e-14, &[
99111
9.019295276219136f64,
100112
4.3097126018270595,
101113
6.837815045397157,
@@ -184,7 +196,7 @@ fn gamma_stability() {
184196
0.5013580219361969,
185197
0.1457735613733489,
186198
]);
187-
test_samples(223, ChiSquared::new(0.1).unwrap(), &[
199+
test_samples_approx(223, ChiSquared::new(0.1).unwrap(), 1e-15, &[
188200
0.014824404726978617f64,
189201
0.021602123937134326,
190202
0.0000003431429746851693,
@@ -285,7 +297,7 @@ fn normal_stability() {
285297
test_samples(213, LogNormal::new(0.0, 1.0).unwrap(), &[
286298
0.88830346f32, 2.1844804, 1.0678421, 0.30322206,
287299
]);
288-
test_samples(213, LogNormal::new(2.0, 0.5).unwrap(), &[
300+
test_samples_approx(213, LogNormal::new(2.0, 0.5).unwrap(), 1e-14, &[
289301
6.964174338639032f64,
290302
10.921015733601452,
291303
7.6355881556915906,
@@ -339,6 +351,6 @@ fn cauchy_stability() {
339351
let expected = [15.023088, -5.446413, 3.7092876, 3.112482];
340352
for &a in expected.iter() {
341353
let b = rng.sample(&distr);
342-
assert!((a - b).abs() < 1e-6, "expected: {} = {}", a, b);
354+
assert!((a - b).abs() < 1e-5, "expected: {} = {}", a, b);
343355
}
344356
}

0 commit comments

Comments
 (0)