Struct tinyrand_alloc::mock::Mock
source · [−]pub struct Mock<'d> { /* private fields */ }
Expand description
Mock RNG, containing invocation state and delegate closures.
Implementations
sourceimpl<'d> Mock<'d>
impl<'d> Mock<'d>
sourcepub fn with_next_u128(self, delegate: impl FnMut(&State) -> u128 + 'd) -> Self
pub fn with_next_u128(self, delegate: impl FnMut(&State) -> u128 + 'd) -> Self
Assigns a Rand::next_u128
delegate to the mock. I.e., when the Rand::next_u128
method is invoked on the mock (directly, or via another method), it will delegate to
the given closure.
Examples
use tinyrand::Rand;
use tinyrand_alloc::Mock;
let mut mock = Mock::default()
.with_next_u128(|_| 42);
assert_eq!(42, mock.next_usize());
assert_eq!(42, mock.next_u64());
assert_eq!(42, mock.next_u128());
sourcepub fn with_next_bool(
self,
delegate: impl FnMut(Surrogate<'_, '_>, Probability) -> bool + 'd
) -> Self
pub fn with_next_bool(
self,
delegate: impl FnMut(Surrogate<'_, '_>, Probability) -> bool + 'd
) -> Self
Assigns a Rand::next_bool
delegate to the mock. I.e., when the Rand::next_bool
method is invoked on the mock, it will delegate to the given closure.
Examples
use tinyrand::{Probability, Rand};
use tinyrand_alloc::Mock;
let mut mock = Mock::default()
.with_next_bool(|_, _| true);
assert!(mock.next_bool(Probability::new(0.01)));
sourcepub fn with_next_lim_u128(
self,
delegate: impl FnMut(Surrogate<'_, '_>, u128) -> u128 + 'd
) -> Self
pub fn with_next_lim_u128(
self,
delegate: impl FnMut(Surrogate<'_, '_>, u128) -> u128 + 'd
) -> Self
Assigns a Rand::next_lim_u128
delegate to the mock. I.e., when the Rand::next_lim_u128
method is invoked on the mock, it will delegate to the given closure. This delegate can be
used to effectively mock Rand::next_lim
and Rand::next_range
methods.
Examples
use tinyrand::{Rand, RandRange};
use tinyrand_alloc::Mock;
let mut mock = Mock::default()
.with_next_lim_u128(|_, _| 17);
assert_eq!(17, mock.next_lim_u64(66));
assert_eq!(27, mock.next_range(10..100u16));
Trait Implementations
sourceimpl Rand for Mock<'_>
impl Rand for Mock<'_>
sourcefn next_u128(&mut self) -> u128
fn next_u128(&mut self) -> u128
Delegates to the underlying closure and increments the state.next_u128_invocations
counter
after the closure returns.
sourcefn next_bool(&mut self, p: Probability) -> bool
fn next_bool(&mut self, p: Probability) -> bool
Delegates to the underlying closure and increments the state.next_bool_invocations
counter
after the closure returns.
sourcefn next_lim_u128(&mut self, lim: u128) -> u128
fn next_lim_u128(&mut self, lim: u128) -> u128
Delegates to the underlying closure and increments the state.next_lim_u128_invocations
counter
after the closure returns.
sourcefn next_lim_u16(&mut self, lim: u16) -> u16
fn next_lim_u16(&mut self, lim: u16) -> u16
Generates a random number in 0..lim
.
sourcefn next_lim_u32(&mut self, lim: u32) -> u32
fn next_lim_u32(&mut self, lim: u32) -> u32
Generates a random number in 0..lim
.
sourcefn next_lim_u64(&mut self, lim: u64) -> u64
fn next_lim_u64(&mut self, lim: u64) -> u64
Generates a random number in 0..lim
.
sourcefn next_usize(&mut self) -> usize
fn next_usize(&mut self) -> usize
Returns the next random usize
.
sourcefn next_lim_usize(&mut self, lim: usize) -> usize
fn next_lim_usize(&mut self, lim: usize) -> usize
Generates a random number in 0..lim
.
Auto Trait Implementations
impl<'d> !RefUnwindSafe for Mock<'d>
impl<'d> !Send for Mock<'d>
impl<'d> !Sync for Mock<'d>
impl<'d> Unpin for Mock<'d>
impl<'d> !UnwindSafe for Mock<'d>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<R> RandRange<Duration> for Rwhere
R: Rand,
impl<R> RandRange<Duration> for Rwhere
R: Rand,
sourcefn next_range(&mut self, range: Range<Duration>) -> Duration
fn next_range(&mut self, range: Range<Duration>) -> Duration
Generates a random number in the given range.
sourceimpl<R> RandRange<u128> for Rwhere
R: Rand,
impl<R> RandRange<u128> for Rwhere
R: Rand,
sourcefn next_range(&mut self, range: Range<u128>) -> u128
fn next_range(&mut self, range: Range<u128>) -> u128
Generates a random number in the given range.
sourceimpl<R> RandRange<u16> for Rwhere
R: Rand,
impl<R> RandRange<u16> for Rwhere
R: Rand,
sourcefn next_range(&mut self, range: Range<u16>) -> u16
fn next_range(&mut self, range: Range<u16>) -> u16
Generates a random number in the given range.
sourceimpl<R> RandRange<u32> for Rwhere
R: Rand,
impl<R> RandRange<u32> for Rwhere
R: Rand,
sourcefn next_range(&mut self, range: Range<u32>) -> u32
fn next_range(&mut self, range: Range<u32>) -> u32
Generates a random number in the given range.
sourceimpl<R> RandRange<u64> for Rwhere
R: Rand,
impl<R> RandRange<u64> for Rwhere
R: Rand,
sourcefn next_range(&mut self, range: Range<u64>) -> u64
fn next_range(&mut self, range: Range<u64>) -> u64
Generates a random number in the given range.
sourceimpl<R> RandRange<usize> for Rwhere
R: Rand,
impl<R> RandRange<usize> for Rwhere
R: Rand,
sourcefn next_range(&mut self, range: Range<usize>) -> usize
fn next_range(&mut self, range: Range<usize>) -> usize
Generates a random number in the given range.