TestRandom
TestRandom allows for deterministically testing effects involving
randomness.
TestRandom operates in two modes. In the first mode, TestRandom is a
purely functional pseudo-random number generator. It will generate
pseudo-random values just like scala.util.Random except that no internal
state is mutated. Instead, methods like nextInt describe state
transitions from one random state to another that are automatically
composed together through methods like flatMap. The random seed can be
set using setSeed and TestRandom is guaranteed to return the same
sequence of values for any given seed. This is useful for deterministically
generating a sequence of pseudo-random values and powers the property based
testing functionality in ZIO Test.
In the second mode, TestRandom maintains an internal buffer of values
that can be "fed" with methods such as feedInts and then when random
values of that type are generated they will first be taken from the buffer.
This is useful for verifying that functions produce the expected output for
a given sequence of "random" inputs.
import zio.random._
import zio.test.environment.TestRandom
for {
_ <- TestRandom.feedInts(4, 5, 2)
x <- random.nextIntBounded(6)
y <- random.nextIntBounded(6)
z <- random.nextIntBounded(6)
} yield x + y + z == 11
TestRandom will automatically take values from the buffer if a value of
the appropriate type is available and otherwise generate a pseudo-random
value, so there is nothing you need to do to switch between the two modes.
Just generate random values as you normally would to get pseudo-random
values, or feed in values of your own to get those values back. You can
also use methods like clearInts to clear the buffer of values of a given
type so you can fill the buffer with new values or go back to pseudo-random
number generation.
Type members
Classlikes
The buffer of the TestRandom.
The buffer of the TestRandom.
The seed of the TestRandom.
The seed of the TestRandom.
Value members
Concrete methods
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of booleans.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of booleans.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of chunks of bytes.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of chunks of bytes.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of characters.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of characters.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of doubles.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of doubles.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of floats.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of floats.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of integers.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of integers.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of longs.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of longs.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of strings.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of strings.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of UUIDs.
Accesses a TestRandom instance in the environment and feeds the buffer
with the specified sequence of UUIDs.
Constructs a new TestRandom with the specified initial state. This can
be useful for providing the required environment to an effect that
requires a Random, such as with ZIO#provide.
Constructs a new TestRandom with the specified initial state. This can
be useful for providing the required environment to an effect that
requires a Random, such as with ZIO#provide.
Constructs a new Test object that implements the TestRandom
interface. This can be useful for mixing in with implementations of other
interfaces.
Constructs a new Test object that implements the TestRandom
interface. This can be useful for mixing in with implementations of other
interfaces.
Concrete fields
Accesses a TestRandom instance in the environment and clears the buffer
of booleans.
Accesses a TestRandom instance in the environment and clears the buffer
of booleans.
Accesses a TestRandom instance in the environment and clears the buffer
of bytes.
Accesses a TestRandom instance in the environment and clears the buffer
of bytes.
Accesses a TestRandom instance in the environment and clears the buffer
of characters.
Accesses a TestRandom instance in the environment and clears the buffer
of characters.
Accesses a TestRandom instance in the environment and clears the buffer
of doubles.
Accesses a TestRandom instance in the environment and clears the buffer
of doubles.
Accesses a TestRandom instance in the environment and clears the buffer
of floats.
Accesses a TestRandom instance in the environment and clears the buffer
of floats.
Accesses a TestRandom instance in the environment and clears the buffer
of integers.
Accesses a TestRandom instance in the environment and clears the buffer
of integers.
Accesses a TestRandom instance in the environment and clears the buffer
of longs.
Accesses a TestRandom instance in the environment and clears the buffer
of longs.
Accesses a TestRandom instance in the environment and clears the buffer
of strings.
Accesses a TestRandom instance in the environment and clears the buffer
of strings.
Accesses a TestRandom instance in the environment and clears the buffer
of UUIDs.
Accesses a TestRandom instance in the environment and clears the buffer
of UUIDs.
Accesses a TestRandom instance in the environment and gets the seed.
Accesses a TestRandom instance in the environment and gets the seed.
Accesses a TestRandom instance in the environment and saves the random
state in an effect which, when run, will restore the TestRandom to the
saved state.
Accesses a TestRandom instance in the environment and saves the random
state in an effect which, when run, will restore the TestRandom to the
saved state.