Skip to content

Commit 354468e

Browse files
committed
Use approximate assertions in value stability and increase some tolerences slightly
1 parent 33a78de commit 354468e

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-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: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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;
1010
use rand::Rng;
1111
use rand_distr::*;
1212

@@ -17,12 +17,45 @@ fn get_rng(seed: u64) -> impl rand::Rng {
1717
rand_pcg::Pcg32::new(seed, INC)
1818
}
1919

20-
fn test_samples<F: Debug + Copy + PartialEq, D: Distribution<F>>(
20+
/// We only assert approximate equality since some platforms do not perform
21+
/// identically (i686-unknown-linux-gnu and most notably x86_64-pc-windows-gnu).
22+
trait ApproxEq {
23+
fn is_approx_eq(&self, rhs: &Self) -> bool;
24+
}
25+
26+
impl ApproxEq for f32 {
27+
fn is_approx_eq(&self, rhs: &Self) -> bool {
28+
(self - rhs).abs() < 1e-6
29+
}
30+
}
31+
impl ApproxEq for f64 {
32+
fn is_approx_eq(&self, rhs: &Self) -> bool {
33+
(self - rhs).abs() < 1e-14
34+
}
35+
}
36+
impl ApproxEq for u64 {
37+
fn is_approx_eq(&self, rhs: &Self) -> bool {
38+
self == rhs
39+
}
40+
}
41+
impl<T: ApproxEq> ApproxEq for [T; 2] {
42+
fn is_approx_eq(&self, rhs: &Self) -> bool {
43+
self[0].is_approx_eq(&rhs[0]) && self[1].is_approx_eq(&rhs[1])
44+
}
45+
}
46+
impl<T: ApproxEq> ApproxEq for [T; 3] {
47+
fn is_approx_eq(&self, rhs: &Self) -> bool {
48+
self[0].is_approx_eq(&rhs[0]) && self[1].is_approx_eq(&rhs[1]) && self[2].is_approx_eq(&rhs[2])
49+
}
50+
}
51+
52+
fn test_samples<F: Debug + ApproxEq, D: Distribution<F>>(
2153
seed: u64, distr: D, expected: &[F],
2254
) {
2355
let mut rng = get_rng(seed);
24-
for &val in expected {
25-
assert_eq!(val, rng.sample(&distr));
56+
for val in expected {
57+
let x = rng.sample(&distr);
58+
assert!(x.is_approx_eq(&val), "not approx eq: {:?}, {:?}", x, val);
2659
}
2760
}
2861

@@ -334,11 +367,12 @@ fn cauchy_stability() {
334367

335368
// Unfortunately this test is not fully portable due to reliance on the
336369
// system's implementation of tanf (see doc on Cauchy struct).
370+
// We use a lower threshold of 1e-5 here.
337371
let distr = Cauchy::new(10f32, 7.0).unwrap();
338372
let mut rng = get_rng(353);
339373
let expected = [15.023088, -5.446413, 3.7092876, 3.112482];
340374
for &a in expected.iter() {
341375
let b = rng.sample(&distr);
342-
assert!((a - b).abs() < 1e-6, "expected: {} = {}", a, b);
376+
assert!((a - b).abs() < 1e-5, "not approx eq: {:?}, {:?}", a, b);
343377
}
344378
}

0 commit comments

Comments
 (0)