object TestRandom extends Serializable
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.
- Alphabetic
- By Inheritance
- TestRandom
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final case class Buffer(booleans: List[Boolean] = List.empty, bytes: List[Chunk[Byte]] = List.empty, chars: List[Char] = List.empty, doubles: List[Double] = List.empty, floats: List[Float] = List.empty, integers: List[Int] = List.empty, longs: List[Long] = List.empty, strings: List[String] = List.empty) extends Product with Serializable
The buffer of the
TestRandom. - final case class Data(seed1: Int, seed2: Int, nextNextGaussians: scala.collection.immutable.Queue[Double] = Queue.empty) extends Product with Serializable
The seed of the
TestRandom. - trait Service extends Restorable
- final case class Test(randomState: Ref[Data], bufferState: Ref[Buffer]) extends random.Random.Service with Service with Product with Serializable
Adapted from @gzmo work in Scala.js (https://github.com/scala-js/scala-js/pull/780)
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
- val DefaultData: Data
An arbitrary initial seed for the
TestRandom. - val any: ZLayer[Random with TestRandom, Nothing, Random with TestRandom]
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- val clearBooleans: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of booleans. - val clearBytes: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of bytes. - val clearChars: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of characters. - val clearDoubles: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of doubles. - val clearFloats: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of floats. - val clearInts: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of integers. - val clearLongs: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of longs. - val clearStrings: URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and clears the buffer of strings. - def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- val deterministic: Layer[Nothing, Random with TestRandom]
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def feedBooleans(booleans: Boolean*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of booleans. - def feedBytes(bytes: Chunk[Byte]*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of chunks of bytes. - def feedChars(chars: Char*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of characters. - def feedDoubles(doubles: Double*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of doubles. - def feedFloats(floats: Float*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of floats. - def feedInts(ints: Int*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of integers. - def feedLongs(longs: Long*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of longs. - def feedStrings(strings: String*): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and feeds the buffer with the specified sequence of strings. - def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- val getSeed: URIO[TestRandom, Long]
Accesses a
TestRandominstance in the environment and gets the seed. - def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def make(data: Data): Layer[Nothing, Random with TestRandom]
Constructs a new
TestRandomwith the specified initial state.Constructs a new
TestRandomwith the specified initial state. This can be useful for providing the required environment to an effect that requires aRandom, such as withZIO#provide. - def makeTest(data: Data): UIO[Test]
Constructs a new
Testobject that implements theTestRandominterface.Constructs a new
Testobject that implements theTestRandominterface. This can be useful for mixing in with implementations of other interfaces. - 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()
- val random: ZLayer[Clock, Nothing, Random with TestRandom]
- val save: ZIO[TestRandom, Nothing, UIO[Unit]]
Accesses a
TestRandominstance in the environment and saves the random state in an effect which, when run, will restore theTestRandomto the saved state. - def setSeed(seed: => Long): URIO[TestRandom, Unit]
Accesses a
TestRandominstance in the environment and sets the seed to the specified value. - 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(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()