Skip to content

Commit 1a213fd

Browse files
committed
Add fns crate::test::{const_rng, step_rng}
1 parent fdaf424 commit 1a213fd

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

src/distr/float.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ float_impls! { feature = "simd_support", f64x8, u64x8, f64, u64, 52, 1023 }
175175
#[cfg(test)]
176176
mod tests {
177177
use super::*;
178-
use crate::rngs::mock::StepRng;
178+
use crate::test::const_rng;
179179

180180
const EPSILON32: f32 = f32::EPSILON;
181181
const EPSILON64: f64 = f64::EPSILON;
@@ -187,30 +187,30 @@ mod tests {
187187
let two = $ty::splat(2.0);
188188

189189
// StandardUniform
190-
let mut zeros = StepRng::new(0, 0);
190+
let mut zeros = const_rng(0);
191191
assert_eq!(zeros.random::<$ty>(), $ZERO);
192-
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
192+
let mut one = const_rng(1 << 8 | 1 << (8 + 32));
193193
assert_eq!(one.random::<$ty>(), $EPSILON / two);
194-
let mut max = StepRng::new(!0, 0);
194+
let mut max = const_rng(!0);
195195
assert_eq!(max.random::<$ty>(), $ty::splat(1.0) - $EPSILON / two);
196196

197197
// OpenClosed01
198-
let mut zeros = StepRng::new(0, 0);
198+
let mut zeros = const_rng(0);
199199
assert_eq!(zeros.sample::<$ty, _>(OpenClosed01), $ZERO + $EPSILON / two);
200-
let mut one = StepRng::new(1 << 8 | 1 << (8 + 32), 0);
200+
let mut one = const_rng(1 << 8 | 1 << (8 + 32));
201201
assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON);
202-
let mut max = StepRng::new(!0, 0);
202+
let mut max = const_rng(!0);
203203
assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + $ty::splat(1.0));
204204

205205
// Open01
206-
let mut zeros = StepRng::new(0, 0);
206+
let mut zeros = const_rng(0);
207207
assert_eq!(zeros.sample::<$ty, _>(Open01), $ZERO + $EPSILON / two);
208-
let mut one = StepRng::new(1 << 9 | 1 << (9 + 32), 0);
208+
let mut one = const_rng(1 << 9 | 1 << (9 + 32));
209209
assert_eq!(
210210
one.sample::<$ty, _>(Open01),
211211
$EPSILON / two * $ty::splat(3.0)
212212
);
213-
let mut max = StepRng::new(!0, 0);
213+
let mut max = const_rng(!0);
214214
assert_eq!(
215215
max.sample::<$ty, _>(Open01),
216216
$ty::splat(1.0) - $EPSILON / two
@@ -235,30 +235,30 @@ mod tests {
235235
let two = $ty::splat(2.0);
236236

237237
// StandardUniform
238-
let mut zeros = StepRng::new(0, 0);
238+
let mut zeros = const_rng(0);
239239
assert_eq!(zeros.random::<$ty>(), $ZERO);
240-
let mut one = StepRng::new(1 << 11, 0);
240+
let mut one = const_rng(1 << 11);
241241
assert_eq!(one.random::<$ty>(), $EPSILON / two);
242-
let mut max = StepRng::new(!0, 0);
242+
let mut max = const_rng(!0);
243243
assert_eq!(max.random::<$ty>(), $ty::splat(1.0) - $EPSILON / two);
244244

245245
// OpenClosed01
246-
let mut zeros = StepRng::new(0, 0);
246+
let mut zeros = const_rng(0);
247247
assert_eq!(zeros.sample::<$ty, _>(OpenClosed01), $ZERO + $EPSILON / two);
248-
let mut one = StepRng::new(1 << 11, 0);
248+
let mut one = const_rng(1 << 11);
249249
assert_eq!(one.sample::<$ty, _>(OpenClosed01), $EPSILON);
250-
let mut max = StepRng::new(!0, 0);
250+
let mut max = const_rng(!0);
251251
assert_eq!(max.sample::<$ty, _>(OpenClosed01), $ZERO + $ty::splat(1.0));
252252

253253
// Open01
254-
let mut zeros = StepRng::new(0, 0);
254+
let mut zeros = const_rng(0);
255255
assert_eq!(zeros.sample::<$ty, _>(Open01), $ZERO + $EPSILON / two);
256-
let mut one = StepRng::new(1 << 12, 0);
256+
let mut one = const_rng(1 << 12);
257257
assert_eq!(
258258
one.sample::<$ty, _>(Open01),
259259
$EPSILON / two * $ty::splat(3.0)
260260
);
261-
let mut max = StepRng::new(!0, 0);
261+
let mut max = const_rng(!0);
262262
assert_eq!(
263263
max.sample::<$ty, _>(Open01),
264264
$ty::splat(1.0) - $EPSILON / two

src/distr/uniform_float.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ uniform_float_impl! { feature = "simd_support", f64x8, u64x8, f64, u64, 64 - 52
219219
mod tests {
220220
use super::*;
221221
use crate::distr::{utils::FloatSIMDScalarUtils, Uniform};
222-
use crate::rngs::mock::StepRng;
222+
use crate::test::{const_rng, step_rng};
223223

224224
#[test]
225225
#[cfg_attr(miri, ignore)] // Miri is too slow
226226
fn test_floats() {
227227
let mut rng = crate::test::rng(252);
228-
let mut zero_rng = StepRng::new(0, 0);
229-
let mut max_rng = StepRng::new(0xffff_ffff_ffff_ffff, 0);
228+
let mut zero_rng = const_rng(0);
229+
let mut max_rng = const_rng(0xffff_ffff_ffff_ffff);
230230
macro_rules! t {
231231
($ty:ty, $f_scalar:ident, $bits_shifted:expr) => {{
232232
let v: &[($f_scalar, $f_scalar)] = &[
@@ -318,10 +318,8 @@ mod tests {
318318
// since for those rounding might result in selecting high for a very
319319
// long time.
320320
if (high_scalar - low_scalar) > 0.0001 {
321-
let mut lowering_max_rng = StepRng::new(
322-
0xffff_ffff_ffff_ffff,
323-
(-1i64 << $bits_shifted) as u64,
324-
);
321+
let mut lowering_max_rng =
322+
step_rng(0xffff_ffff_ffff_ffff, (-1i64 << $bits_shifted) as u64);
325323
assert!(
326324
<$ty as SampleUniform>::Sampler::sample_single(
327325
low,

src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,33 @@ mod test {
309309
rand_pcg::Pcg32::new(seed, INC)
310310
}
311311

312+
/// Construct a generator yielding a constant value
313+
pub fn const_rng(x: u64) -> StepRng {
314+
StepRng(x, 0)
315+
}
316+
317+
/// Construct a generator yielding an arithmetic sequence
318+
pub fn step_rng(x: u64, increment: u64) -> StepRng {
319+
StepRng(x, increment)
320+
}
321+
322+
pub(crate) struct StepRng(u64, u64);
323+
impl RngCore for StepRng {
324+
fn next_u32(&mut self) -> u32 {
325+
self.next_u64() as u32
326+
}
327+
328+
fn next_u64(&mut self) -> u64 {
329+
let res = self.0;
330+
self.0 = self.0.wrapping_add(self.1);
331+
res
332+
}
333+
334+
fn fill_bytes(&mut self, dst: &mut [u8]) {
335+
rand_core::impls::fill_bytes_via_next(self, dst)
336+
}
337+
}
338+
312339
#[test]
313340
#[cfg(feature = "thread_rng")]
314341
fn test_random() {

src/rng.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,13 @@ where
479479
#[cfg(test)]
480480
mod test {
481481
use super::*;
482-
use crate::rngs::mock::StepRng;
483-
use crate::test::rng;
482+
use crate::test::{const_rng, rng};
484483
#[cfg(feature = "alloc")]
485484
use alloc::boxed::Box;
486485

487486
#[test]
488487
fn test_fill_bytes_default() {
489-
let mut r = StepRng::new(0x11_22_33_44_55_66_77_88, 0);
488+
let mut r = const_rng(0x11_22_33_44_55_66_77_88);
490489

491490
// check every remainder mod 8, both in small and big vectors.
492491
let lengths = [0, 1, 2, 3, 4, 5, 6, 7, 80, 81, 82, 83, 84, 85, 86, 87];
@@ -507,7 +506,7 @@ mod test {
507506
#[test]
508507
fn test_fill() {
509508
let x = 9041086907909331047; // a random u64
510-
let mut rng = StepRng::new(x, 0);
509+
let mut rng = const_rng(x);
511510

512511
// Convert to byte sequence and back to u64; byte-swap twice if BE.
513512
let mut array = [0u64; 2];
@@ -537,7 +536,7 @@ mod test {
537536
#[test]
538537
fn test_fill_empty() {
539538
let mut array = [0u32; 0];
540-
let mut rng = StepRng::new(0, 1);
539+
let mut rng = rng(1);
541540
rng.fill(&mut array);
542541
rng.fill(&mut array[..]);
543542
}

src/rngs/reseeding.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,15 @@ where
253253
#[cfg(feature = "std_rng")]
254254
#[cfg(test)]
255255
mod test {
256-
use crate::rngs::mock::StepRng;
257256
use crate::rngs::std::Core;
257+
use crate::test::const_rng;
258258
use crate::Rng;
259259

260260
use super::ReseedingRng;
261261

262262
#[test]
263263
fn test_reseeding() {
264-
let zero = StepRng::new(0, 0);
264+
let zero = const_rng(0);
265265
let thresh = 1; // reseed every time the buffer is exhausted
266266
let mut reseeding = ReseedingRng::<Core, _>::new(thresh, zero).unwrap();
267267

@@ -281,7 +281,7 @@ mod test {
281281
#[test]
282282
#[allow(clippy::redundant_clone)]
283283
fn test_clone_reseeding() {
284-
let zero = StepRng::new(0, 0);
284+
let zero = const_rng(0);
285285
let mut rng1 = ReseedingRng::<Core, _>::new(32 * 4, zero).unwrap();
286286

287287
let first: u32 = rng1.random();

0 commit comments

Comments
 (0)