Skip to main content

Mock

Struct Mock 

Source
pub struct Mock<'d> { /* private fields */ }
Expand description

Mock RNG, containing invocation state and delegate closures.

Implementations§

Source§

impl<'d> Mock<'d>

Source

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());
Source

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)));
Source

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));
Source

pub fn state(&self) -> &State

Obtains the underlying mock state.

Trait Implementations§

Source§

impl<'a> Default for Mock<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Rand for Mock<'_>

Source§

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

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

Delegates to the underlying closure and increments the state.next_lim_u128_invocations counter after the closure returns.

Source§

fn next_u64(&mut self) -> u64

Returns the next random u64.
Source§

fn next_lim_u16(&mut self, lim: u16) -> u16

Generates a random number in 0..lim.
Source§

fn next_lim_u32(&mut self, lim: u32) -> u32

Generates a random number in 0..lim.
Source§

fn next_lim_u64(&mut self, lim: u64) -> u64

Generates a random number in 0..lim.
Source§

fn next_u16(&mut self) -> u16

Returns the next random u16.
Source§

fn next_u32(&mut self) -> u32

Returns the next random u32.
Source§

fn next_usize(&mut self) -> usize

Returns the next random usize.
Source§

fn next_lim_usize(&mut self, lim: usize) -> usize

Generates a random number in 0..lim.

Auto Trait Implementations§

§

impl<'d> Freeze for Mock<'d>

§

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> UnsafeUnpin for Mock<'d>

§

impl<'d> !UnwindSafe for Mock<'d>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R> RandLim<u128> for R
where R: Rand,

Source§

fn next_lim(&mut self, lim: u128) -> u128

Generates a random number in 0..N.
Source§

impl<R> RandLim<u16> for R
where R: Rand,

Source§

fn next_lim(&mut self, lim: u16) -> u16

Generates a random number in 0..N.
Source§

impl<R> RandLim<u32> for R
where R: Rand,

Source§

fn next_lim(&mut self, lim: u32) -> u32

Generates a random number in 0..N.
Source§

impl<R> RandLim<u64> for R
where R: Rand,

Source§

fn next_lim(&mut self, lim: u64) -> u64

Generates a random number in 0..N.
Source§

impl<R> RandLim<usize> for R
where R: Rand,

Source§

fn next_lim(&mut self, lim: usize) -> usize

Generates a random number in 0..N.
Source§

impl<R> RandRange<Duration> for R
where R: Rand,

Source§

fn next_range(&mut self, range: Range<Duration>) -> Duration

Generates a random number in the given range.
Source§

impl<R> RandRange<u128> for R
where R: Rand,

Source§

fn next_range(&mut self, range: Range<u128>) -> u128

Generates a random number in the given range.
Source§

impl<R> RandRange<u16> for R
where R: Rand,

Source§

fn next_range(&mut self, range: Range<u16>) -> u16

Generates a random number in the given range.
Source§

impl<R> RandRange<u32> for R
where R: Rand,

Source§

fn next_range(&mut self, range: Range<u32>) -> u32

Generates a random number in the given range.
Source§

impl<R> RandRange<u64> for R
where R: Rand,

Source§

fn next_range(&mut self, range: Range<u64>) -> u64

Generates a random number in the given range.
Source§

impl<R> RandRange<usize> for R
where R: Rand,

Source§

fn next_range(&mut self, range: Range<usize>) -> usize

Generates a random number in the given range.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.