Skip to content

Commit

Permalink
Address review: use assert_almost_eq macro
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Dec 14, 2020
1 parent 354468e commit 33b8f37
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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.

Expand Down
3 changes: 1 addition & 2 deletions rand_distr/src/cauchy.rs
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion rand_distr/src/normal.rs
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions rand_distr/src/pareto.rs
Expand Up @@ -87,7 +87,7 @@ where F: Float, OpenClosed01: Distribution<F>
#[cfg(test)]
mod tests {
use super::*;
use core::fmt::{Debug, Display};
use core::fmt::{Debug, Display, LowerExp};

#[test]
#[should_panic]
Expand All @@ -109,13 +109,13 @@ mod tests {

#[test]
fn value_stability() {
fn test_samples<F: Float + Debug + Display, D: Distribution<F>>(
fn test_samples<F: Float + Debug + Display + LowerExp, D: Distribution<F>>(
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);
}
}

Expand Down
30 changes: 17 additions & 13 deletions rand_distr/tests/value_stability.rs
Expand Up @@ -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::*;
Expand All @@ -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<T: ApproxEq> 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<T: ApproxEq> 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]);
}
}

Expand All @@ -55,7 +59,7 @@ fn test_samples<F: Debug + ApproxEq, D: Distribution<F>>(
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);
}
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 33b8f37

Please sign in to comment.