net.corda.client.mock / Generator

Generator

class Generator<out A>

This file defines a basic Generator library for composing random generators of objects.

An object of type Generator<A> captures a generator of As. Generators may be composed in several ways.

Generator.choice picks a generator from the specified list and runs that. Generator.frequency is similar to choice but the probability may be specified for each generator (it is normalised before picking). Generator.combine combines two generators of A and B with a function (A, B) -> C. Variants exist for other arities. Generator.bind sequences two generators using an arbitrary A->Generator function. Keep the usage of this function minimal as it may explode the stack, especially when using recursion.

There are other utilities as well, the type of which are usually descriptive.

Example: val birdNameGenerator = Generator.pickOne(listOf("raven", "pigeon")) val birdHeightGenerator = Generator.doubleRange(from = 10.0, to = 30.0) val birdGenerator = birdNameGenerator.combine(birdHeightGenerator) { name, height -> Bird(name, height) } val birdsGenerator = Generator.replicate(2, birdGenerator) val mammalsGenerator = Generator.sampleBernoulli(listOf(Mammal("fox"), Mammal("elephant"))) val animalsGenerator = Generator.frequency( 0.2 to birdsGenerator, 0.8 to mammalsGenerator ) val animals = animalsGenerator.generate(Random()).getOrThrow()

The above will generate a random list of animals.





Constructors

<init> Generator(generate: (Random) -> ErrorOr<A>)

This file defines a basic Generator library for composing random generators of objects.

Properties

generate val generate: (Random) -> ErrorOr<A>

Functions

bind fun <B> bind(function: (A) -> Generator<B>): Generator<Nothing>
combine fun <B, R> combine(other1: Generator<B>, function: (A, B) -> R): Generator<R>
fun <B, C, R> combine(other1: Generator<B>, other2: Generator<C>, function: (A, B, C) -> R): Generator<R>
fun <B, C, D, R> combine(other1: Generator<B>, other2: Generator<C>, other3: Generator<D>, function: (A, B, C, D) -> R): Generator<R>
fun <B, C, D, E, R> combine(other1: Generator<B>, other2: Generator<C>, other3: Generator<D>, other4: Generator<E>, function: (A, B, C, D, E) -> R): Generator<R>
map fun <B> map(function: (A) -> B): Generator<B>
product fun <B> product(other: Generator<(A) -> B>): Generator<B>

Companion Object Functions

choice fun <A> choice(generators: List<Generator<A>>): Generator<Nothing>
fail fun <A> fail(error: Exception): Generator<A>
frequency fun <A> frequency(vararg generators: <ERROR CLASS><Double, Generator<A>>): Generator<A>
impure fun <A> impure(valueClosure: () -> A): Generator<A>
pure fun <A> pure(value: A): Generator<A>
sequence fun <A> sequence(generators: List<Generator<A>>): Generator<List<A>>
success fun <A> success(generate: (Random) -> A): Generator<A>

Extension Functions

generateOrFail fun <A> Generator<A>.generateOrFail(random: Random, numberOfTries: Int = 1): A

Companion Object Extension Functions

double fun Generator.Companion.double(): Generator<Double>
doubleRange fun Generator.Companion.doubleRange(from: Double, to: Double): Generator<Double>
int fun Generator.Companion.int(): Generator<Int>
intRange fun Generator.Companion.intRange(from: Int, to: Int): Generator<Int>
oneOf fun <A> Generator.Companion.oneOf(list: List<A>): Generator<A>
pickOne fun <A> Generator.Companion.pickOne(list: List<A>): Generator<A>
replicate fun <A> Generator.Companion.replicate(number: Int, generator: Generator<A>): Generator<List<A>>
replicatePoisson fun <A> Generator.Companion.replicatePoisson(meanSize: Double, generator: Generator<A>): Generator<List<A>>
sampleBernoulli fun <A> Generator.Companion.sampleBernoulli(maxRatio: Double = 1.0, vararg collection: A): <ERROR CLASS>
fun <A> Generator.Companion.sampleBernoulli(collection: Collection<A>, maxRatio: Double = 1.0): Generator<List<A>>