pub struct TestRng { /* private fields */ }
testing
only.Expand description
A fast, seedable pseudorandom number generator for use in tests which prints the seed if the thread in which it is created panics.
Only one TestRng
is permitted per thread.
Implementations§
Source§impl TestRng
impl TestRng
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new TestRng
using a seed generated from the env var CL_TEST_SEED
if set or
from cryptographically secure random data if not.
Note that new()
or default()
should only be called once per test. If a test needs to
spawn multiple threads each with their own TestRng
, then use new()
to create a single,
master TestRng
, then use it to create a seed per child thread. The child TestRng
s can
then be constructed in their own threads via from_seed()
.
§Panics
Panics if a TestRng
has already been created on this thread.
Sourcepub fn from_seed(seed: <Pcg64Mcg as SeedableRng>::Seed) -> Self
pub fn from_seed(seed: <Pcg64Mcg as SeedableRng>::Seed) -> Self
Constructs a new TestRng
using seed
. This should be used in cases where a test needs to
spawn multiple threads each with their own TestRng
. A single, master TestRng
should be
constructed before any child threads are spawned, and that one should be used to create
seeds for the child threads’ TestRng
s.
§Panics
Panics if a TestRng
has already been created on this thread.
Sourcepub fn random_string<R: SampleRange<usize>>(
&mut self,
length_range: R,
) -> String
pub fn random_string<R: SampleRange<usize>>( &mut self, length_range: R, ) -> String
Returns a random String
of length within the range specified by length_range
.
Sourcepub fn random_vec<R: SampleRange<usize>, T>(
&mut self,
length_range: R,
) -> Vec<T>where
Standard: Distribution<T>,
pub fn random_vec<R: SampleRange<usize>, T>(
&mut self,
length_range: R,
) -> Vec<T>where
Standard: Distribution<T>,
Returns a random Vec
of length within the range specified by length_range
.
Sourcepub fn create_child(&mut self) -> Self
pub fn create_child(&mut self) -> Self
Creates a child RNG.
The resulting RNG is seeded from self
deterministically.
Trait Implementations§
Source§impl RngCore for TestRng
impl RngCore for TestRng
Source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
dest
with random data. Read moreSource§impl SeedableRng for TestRng
impl SeedableRng for TestRng
Source§type Seed = <Mcg128Xsl64 as SeedableRng>::Seed
type Seed = <Mcg128Xsl64 as SeedableRng>::Seed
u8
arrays (we recommend [u8; N]
for some N
). Read moreSource§fn seed_from_u64(state: u64) -> Self
fn seed_from_u64(state: u64) -> Self
u64
seed. Read moreSource§fn from_rng<R>(rng: R) -> Result<Self, Error>where
R: RngCore,
fn from_rng<R>(rng: R) -> Result<Self, Error>where
R: RngCore,
Rng
. Read moreSource§fn from_entropy() -> Self
fn from_entropy() -> Self
impl CryptoRng for TestRng
Auto Trait Implementations§
impl Freeze for TestRng
impl RefUnwindSafe for TestRng
impl Send for TestRng
impl Sync for TestRng
impl Unpin for TestRng
impl UnwindSafe for TestRng
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CryptoRngCore for T
impl<T> CryptoRngCore for T
Source§fn as_rngcore(&mut self) -> &mut dyn RngCore
fn as_rngcore(&mut self) -> &mut dyn RngCore
RngCore
trait object.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<R> Rng for R
impl<R> Rng for R
Source§fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
Source§fn gen_bool(&mut self, p: f64) -> bool
fn gen_bool(&mut self, p: f64) -> bool
p
of being true. Read moreSource§fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator
of being
true. I.e. gen_ratio(2, 3)
has chance of 2 in 3, or about 67%, of
returning true. If numerator == denominator
, then the returned value
is guaranteed to be true
. If numerator == 0
, then the returned
value is guaranteed to be false
. Read more