←back to thread

90 points birdculture | 1 comments | | HN request time: 0.266s | source
Show context
moomin ◴[] No.43746952[source]
I’m honestly completely failing to understand the basic idea here. What does this look like for generating and shrinking random strings,
replies(1): >>43749016 #
1. chriswarbo ◴[] No.43749016[source]
One straightforward approach would be:

- Generate a random number N for the size (maybe restricted to some Range)

- Generate N `Char` values, by using a random number for each code point.

- Combine those Chars into a string

falsify runs a generator by applying it to an infinite binary tree, with random numbers in the nodes. A generator can either consume a single number (taken from the root node of a tree), or it can run two other generators (one gets run on the left child, the other gets run on the right). Hence the above generator would use the value in the left child as N, then run the "generate N Chars" generator on the right child. The latter generator would run a Char generator on its left child, and an 'N-1 Chars' generator on its right child; and so on.

To shrink, we just run the generator on a tree with smaller numbers. In this case, a smaller number in the left child will cause fewer Chars to be generated; and smaller numbers in the right tree will cause lower code-points to be generated. falsify's tree representation also has a special case for the smallest tree (which returns 0 for its root, and itself for each child).