trait TestRandom extends Random with Restorable
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.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.
- Alphabetic
- By Inheritance
- TestRandom
- Restorable
- Random
- Serializable
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
- abstract def clearBooleans(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearBytes(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearChars(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearDoubles(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearFloats(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearInts(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearLongs(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearStrings(implicit trace: ZTraceElement): UIO[Unit]
- abstract def clearUUIDs(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedBooleans(booleans: Boolean*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedBytes(bytes: Chunk[Byte]*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedChars(chars: Char*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedDoubles(doubles: Double*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedFloats(floats: Float*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedInts(ints: Int*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedLongs(longs: Long*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedStrings(strings: String*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def feedUUIDs(UUIDs: UUID*)(implicit trace: ZTraceElement): UIO[Unit]
- abstract def getSeed(implicit trace: ZTraceElement): UIO[Long]
-
abstract
def
nextBoolean(implicit trace: ZTraceElement): UIO[Boolean]
- Definition Classes
- Random
-
abstract
def
nextBytes(length: ⇒ Int)(implicit trace: ZTraceElement): UIO[Chunk[Byte]]
- Definition Classes
- Random
-
abstract
def
nextDouble(implicit trace: ZTraceElement): UIO[Double]
- Definition Classes
- Random
-
abstract
def
nextDoubleBetween(minInclusive: ⇒ Double, maxExclusive: ⇒ Double)(implicit trace: ZTraceElement): UIO[Double]
- Definition Classes
- Random
-
abstract
def
nextFloat(implicit trace: ZTraceElement): UIO[Float]
- Definition Classes
- Random
-
abstract
def
nextFloatBetween(minInclusive: ⇒ Float, maxExclusive: ⇒ Float)(implicit trace: ZTraceElement): UIO[Float]
- Definition Classes
- Random
-
abstract
def
nextGaussian(implicit trace: ZTraceElement): UIO[Double]
- Definition Classes
- Random
-
abstract
def
nextInt(implicit trace: ZTraceElement): UIO[Int]
- Definition Classes
- Random
-
abstract
def
nextIntBetween(minInclusive: ⇒ Int, maxExclusive: ⇒ Int)(implicit trace: ZTraceElement): UIO[Int]
- Definition Classes
- Random
-
abstract
def
nextIntBounded(n: ⇒ Int)(implicit trace: ZTraceElement): UIO[Int]
- Definition Classes
- Random
-
abstract
def
nextLong(implicit trace: ZTraceElement): UIO[Long]
- Definition Classes
- Random
-
abstract
def
nextLongBetween(minInclusive: ⇒ Long, maxExclusive: ⇒ Long)(implicit trace: ZTraceElement): UIO[Long]
- Definition Classes
- Random
-
abstract
def
nextLongBounded(n: ⇒ Long)(implicit trace: ZTraceElement): UIO[Long]
- Definition Classes
- Random
-
abstract
def
nextPrintableChar(implicit trace: ZTraceElement): UIO[Char]
- Definition Classes
- Random
-
abstract
def
nextString(length: ⇒ Int)(implicit trace: ZTraceElement): UIO[String]
- Definition Classes
- Random
-
abstract
def
nextUUID(implicit trace: ZTraceElement): UIO[UUID]
- Definition Classes
- Random
-
abstract
def
save(implicit trace: ZTraceElement): UIO[UIO[Unit]]
- Definition Classes
- Restorable
-
abstract
def
setSeed(seed: ⇒ Long)(implicit trace: ZTraceElement): UIO[Unit]
- Definition Classes
- TestRandom → Random
-
abstract
def
shuffle[A, Collection[+Element] <: Iterable[Element]](collection: ⇒ Collection[A])(implicit bf: zio.BuildFrom[Collection[A], A, Collection[A]], trace: ZTraceElement): UIO[Collection[A]]
- Definition Classes
- Random
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()