Skip to content

Commit 2ef2e66

Browse files
committed
Add value_stability tests for uniform
1 parent 096a5dd commit 2ef2e66

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/distributions/uniform.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,8 @@ impl UniformSampler for UniformDuration {
934934

935935
#[cfg(test)]
936936
mod tests {
937-
use crate::Rng;
937+
use super::*;
938938
use crate::rngs::mock::StepRng;
939-
use crate::distributions::uniform::Uniform;
940-
use crate::distributions::utils::FloatAsSIMD;
941-
#[cfg(feature="simd_support")] use packed_simd::*;
942939

943940
#[should_panic]
944941
#[test]
@@ -1268,4 +1265,42 @@ mod tests {
12681265
assert!(r.0.scale > 5.0);
12691266
assert!(r.0.scale < 5.0 + 1e-14);
12701267
}
1268+
1269+
#[test]
1270+
fn value_stability() {
1271+
fn test_samples<T: SampleUniform + Copy + core::fmt::Debug + PartialEq>(
1272+
lb: T, ub: T, expected_single: &[T], expected_multiple: &[T]
1273+
)
1274+
where Uniform<T>: Distribution<T>
1275+
{
1276+
let mut rng = crate::test::rng(897);
1277+
let mut buf = [lb; 3];
1278+
1279+
for x in &mut buf {
1280+
*x = T::Sampler::sample_single(lb, ub, &mut rng);
1281+
}
1282+
assert_eq!(&buf, expected_single);
1283+
1284+
let distr = Uniform::new(lb, ub);
1285+
for x in &mut buf {
1286+
*x = rng.sample(&distr);
1287+
}
1288+
assert_eq!(&buf, expected_multiple);
1289+
}
1290+
1291+
// We test on a sub-set of types; possibly we should do more.
1292+
// TODO: SIMD types
1293+
1294+
test_samples(11u8, 219, &[17, 66, 214], &[181, 93, 165]);
1295+
test_samples(11u32, 219, &[17, 66, 214], &[181, 93, 165]);
1296+
1297+
test_samples(0f32, 1e-2f32, &[0.0003070104, 0.0026630748, 0.00979833],
1298+
&[0.008194133, 0.00398172, 0.007428536]);
1299+
test_samples(-1e10f64, 1e10f64,
1300+
&[-4673848682.871551, 6388267422.932352, 4857075081.198343],
1301+
&[1173375212.1808167, 1917642852.109581, 2365076174.3153973]);
1302+
1303+
test_samples(Duration::new(2, 0), Duration::new(4, 0),
1304+
&[Duration::new(2,532615131), Duration::new(3,638826742), Duration::new(3,485707508)], &[Duration::new(3,117337521), Duration::new(3,191764285), Duration::new(3,236507617)]);
1305+
}
12711306
}

0 commit comments

Comments
 (0)