pub struct Mock<'d> { /* private fields */ }Expand description
Mock RNG, containing invocation state and delegate closures.
Implementations§
Source§impl<'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§
Source§impl Rand for Mock<'_>
impl Rand for Mock<'_>
Source§fn 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.
Source§fn 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.
Source§fn 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.
Source§fn next_lim_u16(&mut self, lim: u16) -> u16
fn next_lim_u16(&mut self, lim: u16) -> u16
0..lim.Source§fn next_lim_u32(&mut self, lim: u32) -> u32
fn next_lim_u32(&mut self, lim: u32) -> u32
0..lim.Source§fn next_lim_u64(&mut self, lim: u64) -> u64
fn next_lim_u64(&mut self, lim: u64) -> u64
0..lim.Source§fn next_usize(&mut self) -> usize
fn next_usize(&mut self) -> usize
usize.Source§fn next_lim_usize(&mut self, lim: usize) -> usize
fn next_lim_usize(&mut self, lim: usize) -> usize
0..lim.