Skip to content

Commit 987a975

Browse files
authored
Resolve Clippy lints, replace rand::rngs::mock::StepRng (#23)
1 parent 2e2dc45 commit 987a975

File tree

7 files changed

+34
-12
lines changed

7 files changed

+34
-12
lines changed

distr_test/tests/cdf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ fn geometric() {
395395
#[test]
396396
fn hypergeometric() {
397397
fn cdf(x: i64, n: u64, k: u64, n_: u64) -> f64 {
398-
let min = if n_ + k > n { n_ + k - n } else { 0 };
398+
let min = (n_ + k).saturating_sub(n);
399399
let max = k.min(n_);
400400
if x < min as i64 {
401401
return 0.0;

distr_test/tests/ks/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ pub fn test_continuous(seed: u64, dist: impl Distribution<f64>, cdf: impl Fn(f64
112112

113113
let critical_value = critical_value();
114114

115-
println!("KS statistic: {}", ks_statistic);
116-
println!("Critical value: {}", critical_value);
115+
println!("KS statistic: {ks_statistic}");
116+
println!("Critical value: {critical_value}");
117117
assert!(ks_statistic < critical_value);
118118
}
119119

@@ -131,7 +131,7 @@ where
131131
// This critical value is bigger than it could be for discrete distributions, but because of large sample sizes this should not matter too much
132132
let critical_value = critical_value();
133133

134-
println!("KS statistic: {}", ks_statistic);
135-
println!("Critical value: {}", critical_value);
134+
println!("KS statistic: {ks_statistic}");
135+
println!("Critical value: {critical_value}");
136136
assert!(ks_statistic < critical_value);
137137
}

distr_test/tests/skew_normal.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ fn owen_t(h: f64, a: f64) -> f64 {
257257
}
258258

259259
fn normal_cdf(x: f64, mean: f64, std_dev: f64) -> f64 {
260+
// f64::erfc from feature `float_erf` may be used over special::Primitive::erfc
261+
#![allow(unstable_name_collisions)]
262+
260263
0.5 * ((mean - x) / (std_dev * core::f64::consts::SQRT_2)).erfc()
261264
}
262265

src/beta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ mod test {
287287
let beta = Beta::<f64>::new(1e-3, 1e-3).unwrap();
288288
let mut rng = crate::test::rng(206);
289289
for i in 0..1000 {
290-
assert!(!beta.sample(&mut rng).is_nan(), "failed at i={}", i);
290+
assert!(!beta.sample(&mut rng).is_nan(), "failed at i={i}");
291291
}
292292
}
293293

src/cauchy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ mod test {
141141
}
142142
let median = median(&mut numbers);
143143
#[cfg(feature = "std")]
144-
std::println!("Cauchy median: {}", median);
144+
std::println!("Cauchy median: {median}");
145145
assert!((median - 10.0).abs() < 0.4); // not 100% certain, but probable enough
146146
let mean = sum / 1000.0;
147147
#[cfg(feature = "std")]
148-
std::println!("Cauchy mean: {}", mean);
148+
std::println!("Cauchy mean: {mean}");
149149
// for a Cauchy distribution the mean should not converge
150150
assert!((mean - 10.0).abs() > 0.4); // not 100% certain, but probable enough
151151
}

src/triangular.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ where
113113
#[cfg(test)]
114114
mod test {
115115
use super::*;
116-
use rand::{rngs::mock, Rng};
116+
use crate::utils::ConstRng;
117+
use rand::Rng;
117118

118119
#[test]
119120
fn test_triangular() {
120-
let mut half_rng = mock::StepRng::new(0x8000_0000_0000_0000, 0);
121+
let mut half_rng = ConstRng(0x8000_0000_0000_0000);
121122
assert_eq!(half_rng.random::<f64>(), 0.5);
122123
for &(min, max, mode, median) in &[
123124
(-1., 1., 0., 0.),
@@ -128,7 +129,7 @@ mod test {
128129
(-4., -0.5, -2., -4.0 + 3.5f64.sqrt()),
129130
] {
130131
#[cfg(feature = "std")]
131-
std::println!("{} {} {} {}", min, max, mode, median);
132+
std::println!("{min} {max} {mode} {median}");
132133
let distr = Triangular::new(min, max, mode).unwrap();
133134
// Test correct value at median:
134135
assert_eq!(distr.sample(&mut half_rng), median);

src/utils.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ use num_traits::Float; // Used for `no_std` to get `f64::abs()` working before `
1414
use rand::distr::hidden_export::IntoFloat;
1515
use rand::Rng;
1616

17+
/// An RNG yielding a constant value
18+
#[cfg(test)]
19+
pub(crate) struct ConstRng(pub(crate) u64);
20+
#[cfg(test)]
21+
impl rand::RngCore for ConstRng {
22+
fn next_u32(&mut self) -> u32 {
23+
self.next_u64() as u32
24+
}
25+
26+
fn next_u64(&mut self) -> u64 {
27+
self.0
28+
}
29+
30+
fn fill_bytes(&mut self, _: &mut [u8]) {
31+
unimplemented!()
32+
}
33+
}
34+
1735
/// Sample a random number using the Ziggurat method (specifically the
1836
/// ZIGNOR variant from Doornik 2005). Most of the arguments are
1937
/// directly from the paper:
@@ -25,7 +43,7 @@ use rand::Rng;
2543
/// * `F_DIFF`: precomputed values of $f(x_i) - f(x_{i+1})$
2644
/// * `pdf`: the probability density function
2745
/// * `zero_case`: manual sampling from the tail when we chose the
28-
/// bottom box (i.e. i == 0)
46+
/// bottom box (i.e. i == 0)
2947
#[inline(always)] // Forced inlining improves the perf by 25-50%
3048
pub(crate) fn ziggurat<R: Rng + ?Sized, P, Z>(
3149
rng: &mut R,

0 commit comments

Comments
 (0)