Trait/Object

zio.test.environment

TestRandom

Related Docs: object TestRandom | package environment

Permalink

trait TestRandom extends 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.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.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TestRandom
  2. Restorable
  3. Serializable
  4. Serializable
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def clearBooleans: UIO[Unit]

    Permalink
  2. abstract def clearBytes: UIO[Unit]

    Permalink
  3. abstract def clearChars: UIO[Unit]

    Permalink
  4. abstract def clearDoubles: UIO[Unit]

    Permalink
  5. abstract def clearFloats: UIO[Unit]

    Permalink
  6. abstract def clearInts: UIO[Unit]

    Permalink
  7. abstract def clearLongs: UIO[Unit]

    Permalink
  8. abstract def clearStrings: UIO[Unit]

    Permalink
  9. abstract def feedBooleans(booleans: Boolean*): UIO[Unit]

    Permalink
  10. abstract def feedBytes(bytes: Chunk[Byte]*): UIO[Unit]

    Permalink
  11. abstract def feedChars(chars: Char*): UIO[Unit]

    Permalink
  12. abstract def feedDoubles(doubles: Double*): UIO[Unit]

    Permalink
  13. abstract def feedFloats(floats: Float*): UIO[Unit]

    Permalink
  14. abstract def feedInts(ints: Int*): UIO[Unit]

    Permalink
  15. abstract def feedLongs(longs: Long*): UIO[Unit]

    Permalink
  16. abstract def feedStrings(strings: String*): UIO[Unit]

    Permalink
  17. abstract def getSeed: UIO[Long]

    Permalink
  18. abstract val save: UIO[UIO[Unit]]

    Permalink
    Definition Classes
    Restorable
  19. abstract def setSeed(seed: Long): UIO[Unit]

    Permalink

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  11. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  14. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  16. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Restorable

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped