liveBook · Manning

Sized generation

As explained in Functional Programming in Scala, Second Edition

A sized generator is a test-value generator whose generated values or search space vary according to a maximum size parameter.

Sized generation produces test values through a size-indexed generator: given a size, it returns an ordinary generator for values at that size. Rather than first finding any failure and then reducing it, a test runner can begin with small, simple cases and increase size and complexity until a property fails. This makes the earliest failure a useful candidate for the smallest failing case. section 8.2 section 8.6

The separation is deliberate. An SGen decides how to generate a value for one requested size; the property runner decides which sizes to test and how many cases to allocate to each. Generators therefore need not know the search schedule, while the runner remains free to use a sequential schedule or make larger jumps through sizes. section 8.2

A size-indexed list generator

Represent an SGen[A] as Int => Gen[A]. An ordinary Gen[A] can be lifted by ignoring the size. To generate lists whose length follows the requested size, map each n to listOfN(n). A nonempty variant uses n.max(1), allowing properties such as finding the maximum of a generated nonempty integer list.

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines SGen as Int => Gen, derives map and flatMap, and implements maximum-size scheduling without detailing alternative schedules.
Examples
Uses size-indexed lists, nonempty lists for a maximum property, and a runner that distributes cases across sizes.
What it assumes
Builds from existing Gen combinators while introducing SGen as a function from size to generator.
What you can do after
Build it from scratch as size-indexed generators and run properties across a configurable maximum size.
How it connects
Connects property-based testing with shrinking, Gen, Prop, map, flatMap, and list generation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.