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

StepRng: better documentation of outputs for other types #1304

Merged
merged 1 commit into from
Mar 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/rngs/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ use rand_core::{impls, Error, RngCore};
#[cfg(feature = "serde1")]
use serde::{Serialize, Deserialize};

/// A simple implementation of `RngCore` for testing purposes.
/// A mock generator yielding very predictable output
///
/// This generates an arithmetic sequence (i.e. adds a constant each step)
/// over a `u64` number, using wrapping arithmetic. If the increment is 0
/// the generator yields a constant.
///
/// Other integer types (64-bit and smaller) are produced via cast from `u64`.
///
/// Other types are produced via their implementation of [`Rng`](crate::Rng) or
/// [`Distribution`](crate::distributions::Distribution).
/// Output values may not be intuitive and may change in future releases but
/// are considered
/// [portable](https://rust-random.github.io/book/portability.html).
/// (`bool` output is true when bit `1u64 << 31` is set.)
Copy link

Choose a reason for hiding this comment

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

just want to be sure, it is 1u64 << 31 for any endianness / arch ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes.

///
/// # Example
///
/// ```
/// use rand::Rng;
/// use rand::rngs::mock::StepRng;
Expand Down Expand Up @@ -72,16 +83,29 @@ impl RngCore for StepRng {

#[cfg(test)]
mod tests {
#[cfg(any(feature = "alloc", feature = "serde1"))]
use super::StepRng;

#[test]
#[cfg(feature = "serde1")]
fn test_serialization_step_rng() {
use super::StepRng;

let some_rng = StepRng::new(42, 7);
let de_some_rng: StepRng =
bincode::deserialize(&bincode::serialize(&some_rng).unwrap()).unwrap();
assert_eq!(some_rng.v, de_some_rng.v);
assert_eq!(some_rng.a, de_some_rng.a);

}

#[test]
#[cfg(feature = "alloc")]
fn test_bool() {
use crate::{Rng, distributions::Standard};

// If this result ever changes, update doc on StepRng!
let rng = StepRng::new(0, 1 << 31);
let result: alloc::vec::Vec<bool> =
rng.sample_iter(Standard).take(6).collect();
assert_eq!(&result, &[false, true, false, true, false, true]);
}
}