Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to GitHub Actions #1073

Merged
merged 8 commits into from Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rand_distr/src/cauchy.rs
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also gives a worse error message: it does not tell the difference/error.

}
}
}
3 changes: 2 additions & 1 deletion rand_distr/src/normal.rs
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one does not give a nice error message.

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
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
44 changes: 39 additions & 5 deletions rand_distr/tests/value_stability.rs
Expand Up @@ -6,7 +6,7 @@
// 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;
use rand::Rng;
use rand_distr::*;

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

fn test_samples<F: Debug + Copy + PartialEq, D: Distribution<F>>(
/// 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;
}

impl ApproxEq for f32 {
fn is_approx_eq(&self, rhs: &Self) -> bool {
(self - rhs).abs() < 1e-6
}
}
impl ApproxEq for f64 {
fn is_approx_eq(&self, rhs: &Self) -> bool {
(self - rhs).abs() < 1e-14
}
}
impl ApproxEq for u64 {
fn is_approx_eq(&self, rhs: &Self) -> bool {
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])
}
}
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 test_samples<F: Debug + ApproxEq, D: Distribution<F>>(
seed: u64, distr: D, expected: &[F],
) {
let mut rng = get_rng(seed);
for &val in expected {
assert_eq!(val, rng.sample(&distr));
for val in expected {
let x = rng.sample(&distr);
assert!(x.is_approx_eq(&val), "not approx eq: {:?}, {:?}", x, val);
}
}

Expand Down Expand Up @@ -334,11 +367,12 @@ fn cauchy_stability() {

// Unfortunately this test is not fully portable due to reliance on the
// system's implementation of tanf (see doc on Cauchy struct).
// We use a lower threshold of 1e-5 here.
let distr = Cauchy::new(10f32, 7.0).unwrap();
let mut rng = get_rng(353);
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, "not approx eq: {:?}, {:?}", a, b);
}
}