# Test data: generators, shrinkers and Arbitrary instances

Test data is produced by test data generators. FsCheck defines default generators for some often used types, but you can use your own, and will need to define your own generators for any new types you introduce.

Generators have types of the form Gen<'a>; this is a generator for values of type a. To build your own generators in F#, a computation expression called gen is provided by FsCheck, and all the functions in the Gen module are at your disposal. For C#, there are some LINQ methods you can use (select, where) and a number of methods on the Gen class. The name for the methods in F# and C# are largely the same except for casing.

Shrinkers have types of the form 'a -> seq<'a> aka Func<T,IEnumerable<T>; given a value, a shrinker produces a sequence of values that are (in some way) smaller than the given value. If FsCheck finds a set of values that falsify a given property, it will try to make that value smaller than the original (random) value by getting the shrinks for the value and trying each one in turn to check that the property is still false. If it is, the smaller value becomes the new counter example and the shrinking process continues with that value.

Shrinkers have no special support from FsCheck - this is not needed, since you have all you need in seq computation expressions and the Seq module, or in LINQ and IEnumerable.

Finally, an Arbitrary<'a> instance packages a generator and shrinker together to be used in properties. FsCheck also allows you to register Arbitrary instances in a Type to Arbitrary dictionary. This dictionary is used to find an arbitrary instance for properties that have arguments, based on the argument's type.

Arbitrary instances have some helper functions in Arb.

## Generators

Generators are built from the function choose, which makes a random choice of a value from an interval, with a uniform distribution. For example, to make a random choice between the elements of a list, use

let chooseFromList xs =
gen { let! i = Gen.choose (0, List.length xs-1)
return List.item i xs }

 public Gen ChooseFrom(T[] xs) { return from i in Gen.Choose(0,xs.Length-1) select xs[i]; }

### Choosing between alternatives

A generator may take the form Gen.oneof <sequence of generators> which chooses among the generators in the list with equal probability. For example, this generates a random boolean which is true with probability one half:

Gen.oneof [ gen { return true }; gen { return false } ]

 var chooseBool = Gen.OneOf(Gen.Constant(true), Gen.Constant(false));

We can control the distribution of results using frequency instead. frequency chooses a generator from the list randomly, but weighs the probability of choosing each alternative by the factor given. For example, this generates true two thirds of the time.

Gen.frequency [ (2, gen { return true }); (1, gen { return false })]

 var chooseBool2 = Gen.Frequency( Tuple.Create(2, Gen.Constant(true)), Tuple.Create(1, Gen.Constant(false)));

### The size of test data

Test data generators have an implicit size parameter; FsCheck begins by generating small test cases, and gradually increases the size as testing progresses. Different test data generators interpret the size parameter in different ways: some ignore it, while the list generator, for example, interprets it as an upper bound on the length of generated lists. You are free to use it as you wish to control your own test data generators.

You can obtain the value of the size parameter using sized. sized g calls g, passing it the current size as a parameter. For example, to generate natural numbers in the range 0 to size, use

Gen.sized <| fun s -> Gen.choose (0,s)

 var sizedInt = Gen.Sized(s => Gen.Choose(0, s));

The purpose of size control is to ensure that test cases are large enough to reveal errors, while remaining small enough to test fast. Sometimes the default size control does not achieve this. For example, towards the end of a test run arbitrary lists may have up to 50 elements, so arbitrary lists of lists may have up to 2500, which is too large for efficient testing. In such cases it can be useful to modify the size parameter explicitly. You can do so using resize.

resize n g invokes generator g with size parameter n. The size parameter should never be negative. For example, to generate a random matrix it might be appropriate to take the square root of the original size:

let matrix gen = Gen.sized <| fun s -> Gen.resize (s|>float|>sqrt|>int) gen

 public static Gen Matrix(Gen generator) { return Gen.Sized(s => generator.Resize(Convert.ToInt32(Math.Sqrt(s)))); }

### Generating recursive data types

Generators for recursive data types are easy to express using oneof or frequency to choose between constructors, and F#'s computation expressions or C# LINQ to form a generator for each case. There are also map functions for arity up to 6 to lift constructors and functions into the Gen type. For example, if the type of trees is defined by

type Tree = Leaf of int | Branch of Tree * Tree


then a generator for trees might be defined by

let rec unsafeTree() =
Gen.oneof [ Gen.map Leaf Arb.generate<int>
Gen.map2 (fun x y -> Branch (x,y)) (unsafeTree()) (unsafeTree())]


In C#, we elide the type because it is quite a bit more verbose than in F# - assume the typical composite of having an abstract superclass Tree with two subclasses, one for Leaf and one for Branch. Basically this is the code F# generates for the type definition above. Assuming that, unsafeTree in C# looks like:

 public static Gen UnsafeTree() { return Gen.OneOf(Arb.Generate().Select(i => (Tree) new Leaf(i)), Gen.Two(UnsafeTree()).Select(t => (Tree) new Branch(t.Item1,t.Item2))); }

However, a recursive generator like this may fail to terminate with a StackOverflowException, or produce very large results. To avoid this, recursive generators should always use the size control mechanism:

let tree =
let rec tree' s =
match s with
| 0 -> Gen.map Leaf Arb.generate<int>
| n when n>0 ->
let subtree = tree' (n/2)
Gen.oneof [ Gen.map Leaf Arb.generate<int>
Gen.map2 (fun x y -> Branch (x,y)) subtree subtree]
| _ -> invalidArg "s" "Only positive arguments are allowed"
Gen.sized tree'

 public static Gen SafeTreeHelper(int size) { if (size == 0) { return Arb.Generate().Select(i => (Tree)new Leaf(i)); } else { var subtree = SafeTreeHelper(size / 2); return Gen.OneOf(Arb.Generate().Select(i => (Tree) new Leaf(i)), Gen.Two(subtree).Select(t => (Tree) new Branch(t.Item1,t.Item2))); } } public static Gen SafeTree() { return Gen.Sized(SafeTreeHelper); }

Note that

• We guarantee termination by forcing the result to be a leaf when the size is zero.
• We halve the size at each recursion, so that the size gives an upper bound on the number of nodes in the tree. We are free to interpret the size as we will.
• The fact that we share the subtree generator between the two branches of a Branch does not mean that we generate the same tree in each case.

### Useful Generator Combinators

If g is a generator for type t, then

• two g generates a pair of t's,
• three g generates a triple of t's,
• four g generates a quadruple of t's,
• If xs is a list, then elements xs generates an arbitrary element of xs.
• If xs is a list, then growingElements xs generates an arbitrary element among an initial segment of xs. The size of this initial segment increases with the size parameter.
• listOfLength n g generates a list of exactly n t's.
• listOf g generates a list of t's whose length is determined by the size parameter
• nonEmptyListOf g generates a non-empty list of t's whose length is determined by the size parameter.
• constant v generates the value v.
• where p g or filter p g generates t's that satisfy the predicate p. Make sure there is a high chance that the predicate is satisfied.
• tryWhere p g or tryFilter p g generates Some t's that satisfy the predicate p, and None if none are found. (After 'trying hard')
• If xs is a sequence, then shuffle xs generates a random permutation of xs.

All the generator combinators are functions on the Gen module. In C#, the names are the same just capitalized differently.

### Generator examples

The following examples use Gen.sample in order to show example output. In general, you shouldn't use Gen.sample when writing properties, but it can be helpful when developing or troubleshooting a useful custom generator.

Please be aware that due to the non-deterministic nature of FsCheck, the output of calling Gen.sample will, in most cases, differ between calls.

The Gen.sample function takes two arguments, in addition to the generator from which it samples. The first argument is the size of the generated data. Some generators (like Gen.constant and Gen.elements) don't use the size argument. For these generators, any integer value will do.

The second argument is the number of sample values to generate. Most examples below use Gen.sample to generate a small list of example values, for example a list of ten generated values.

#### Constant

The Gen.constant function is perhaps the simplest, and easiest, generator to understand. Even though it's part of a system that generates random values, this particular generator always returns the same value:

Gen.constant (1, "Foo") |> Gen.sample 0 10


In this example, the constant is a complex value (a tuple); it can also be a simple value, as for example a string or an integer. Since Gen.constant doesn't rely on the size argument, it's 0 in this example, but any value would do; it wouldn't change the result. As you can see from the return value, all singular elements returned is the same tuple.

 [(1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo"); (1, "Foo")]

Since the purpose of FsCheck is to generate random values, you shouldn't need to use Gen.constant often. Still, it can come in handy if you need to keep the value of a particular type constant while you vary other values.

#### Choose

You can use the Gen.choose function to create a generator of singular integer values between a minimum and maximum value, both inclusive:

Gen.choose (0, 9) |> Gen.sample 0 10


This example generates a single integer value between 0 and 9. Since Gen.choose doesn't rely on the size argument, it's 0 in this example, but any value would do; it wouldn't change the result.

While Gen.choose (0, 9) generates a single integer value, Gen.sample 0 10 generates 10 sample values:

 [0; 1; 4; 5; 8; 9; 2; 6; 7; 4]

If you supply values in the 'wrong order', Gen.choose will follow Postel's law and 'know what you meant':

Gen.choose (99, 42) |> Gen.sample 0 10


In this example, the first value is greater than the second value, but Gen.choose will happily interpret this as a range, and produce values between 42 and 99, both included:

 [88; 65; 44; 79; 58; 93; 72; 49; 86; 63]

Since both values are included, if you set both to the same value, you'll effectively constrain the generator to that single value, and it'll behave like Gen.constant.

#### Elements

You can use the Gen.elements function to create a generator of singular values drawn from a collection of possible values. The collection is inclusive, which means that both the first and last element, as well as all elements between, can be drawn.

In the following example, a list of arbitrary integers define the collection of possible values. The result is a generator that creates int values guaranteed to be one of these values. Since Gen.elements doesn't rely on the size argument, it's 0 in this example, but any value would do; it wouldn't change the result.

Gen.elements [42; 1337; 7; -100; 1453; -273] |> Gen.sample 0 10


The result of this expression is a list of ten sample values. Each value is a single integer drawn from the collection of numbers:

 [7; -273; 42; -100; 1453; 1337; 7; -273; 42; 42]

All elements are equally likely to be drawn from the collection; we say that the random function has a uniform distribution. One easy way to affect the distribution is to put more than one identical element into the collection:

Gen.elements ["foo"; "foo"; "bar"] |> Gen.sample 0 10


In the above example, the value "foo" appears twice, so is twice as likely to be drawn from the collection:

 ["foo"; "bar"; "bar"; "foo"; "bar"; "foo"; "foo"; "foo"; "foo"; "foo"]

The above examples all use list values as input, but you can use any seq expression, including list and array values, as long as the sequence is finite.

#### GrowingElements

Essentially Gen.growingElements is like Gen.elements but also taking size into account.

You can use the Gen.growingElements function to create a generator of singular values drawn among an initial segment of possible values. The size of this initial segment increases with the size parameter.

In the following example, a list of ten characters define the collection of possible values. The result is a generator that creates char values guaranteed to be one of these values. Since Gen.growingElements relies on the size argument, it's 3 in this example, which means only values from the segment ['a'; 'b'; 'c'] will be returned.

Gen.growingElements ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'] |> Gen.sample 3 10


The result of this expression is a list of ten sample values. Each value is a single character drawn from the segment ['a'; 'b'; 'c']:

 ['b'; 'a'; 'a'; 'b'; 'b'; 'a'; 'c'; 'c'; 'c'; 'c']

Let's run Gen.growingElements again, with the same input but with size 7:

Gen.growingElements ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'] |> Gen.sample 7 10


The result of this expression is a list of ten sample values. Each value is now a single character drawn from the segment ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g']:

 ['a'; 'f'; 'a'; 'f'; 'a'; 'c'; 'c'; 'a'; 'c'; 'f']

The above examples all use list values as input, but you can use any seq expression, including list and array values, as long as the sequence is finite.

#### Map

Sometimes, you need to use a generator of one type in order to create a generator of another type. For instance, you may need a byte value between 0 and 127. That sounds just like a job for Gen.choose, but unfortunately, Gen.choose (0, 127) is a Gen<int>, and not a Gen<byte>. One way to produce a Gen<byte> from a Gen<int> is to use Gen.map:

Gen.choose (0, 127) |> Gen.map byte |> Gen.sample 0 10


This example uses the byte function to cast any int created by Gen.choose (0, 127) to a byte value:

 [12uy; 89uy; 118uy; 53uy; 31uy; 114uy; 25uy; 51uy; 85uy; 94uy]

This is only a basic example of the concept of Gen.map. In this particular example, you could also have used Gen.elements [0uy..127uy] to achieve the same result without using Gen.map, so let's consider a second example.

Assume that you need to create a date in a particular month; e.g. November 2019. You can do that by creating an integer for the day of the month, and then combine Gen.map with an anymous function to get the desired date:

Gen.choose (1, 30)
|> Gen.map (fun i -> DateTime(2019, 11, i).ToString "u")
|> Gen.sample 0 10


In this example, the generated DateTime value is immediately formatted as a string, so that the output is more readable:

 ["2019-11-22 00:00:00Z"; "2019-11-05 00:00:00Z"; "2019-11-26 00:00:00Z"; "2019-11-09 00:00:00Z"; "2019-11-30 00:00:00Z"; "2019-11-13 00:00:00Z"; "2019-11-04 00:00:00Z"; "2019-11-17 00:00:00Z"; "2019-11-08 00:00:00Z"; "2019-11-21 00:00:00Z"]

This causes the resulting generator to have the type Gen<string>, but if you omit calling ToString "u", its type would have been Gen<DateTime>.

#### Lists

You can generate lists from individual value generators using Gen.listOf, Gen.listOfLength, and Gen.nonEmptyListOf. These functions are combinators, which means that they don't generate individual values themselves, but rather use another generator to build values. For instance, you can use Gen.constant to generate lists that all contain the same value:

Gen.constant 42 |> Gen.listOf |> Gen.sample 1 10


This combination uses Gen.constant 42 as an individual generator, and then generates lists containing the the number 42. While the value(s) in the list is always 42, the length of the generated lists varies.

 [[]; []; []; [42]; [42]; [42]; [42]; [42]; []; []]

The length of the generated list is determined by the size argument. In this example, the size argument is 1, so the generated lists are short. Note that while there's a correlation beteen size and the length of the lists, you can't rely on a deterministic length. For that, there's Gen.listOfLength:

Gen.choose (24, 42) |> Gen.listOfLength 5 |> Gen.sample 0 10


This example uses Gen.choose (24, 42) in order to generate individual integer values between 24 and 42. It then pipes this generator into Gen.listOfLength 5 in order to generate lists with exactly five elements:

 [[42; 26; 27; 30; 32]; [40; 42; 26; 27; 30]; [37; 40; 42; 26; 27]; [36; 37; 40; 42; 26]; [33; 36; 37; 40; 42]; [31; 33; 36; 37; 40]; [29; 31; 33; 36; 37]; [28; 29; 31; 33; 36]; [34; 28; 29; 31; 33]; [39; 34; 28; 29; 31]]

Notice that all sample lists have exactly five elements.

You can also use Gen.nonEmptyListOf to create lists that are guaranteed to have at least one element. Like the other list generators, it uses a single-value generator to generate its elements:

Gen.elements ["foo"; "bar"; "baz"] |> Gen.nonEmptyListOf |> Gen.sample 20 4


Like Gen.listOf, Gen.nonEmptyListOf uses size to control the length of the generated lists. They may still be small, but the larger the size argument, the larger the lists may become.

 [["foo"; "bar"; "bar"; "foo"; "bar"; "baz"; "baz"; "bar"; "baz"; "foo"; "foo"; "baz"; "foo"; "bar"]; ["baz"; "foo"; "bar"; "bar"; "foo"; "bar"; "baz"; "baz"; "bar"; "baz"; "foo"; "foo"; "baz"; "foo"; "bar"; "foo"; "foo"; "bar"; "foo"; "bar"]; ["foo"; "baz"; "foo"; "bar"; "bar"; "foo"; "bar"; "baz"; "baz"; "bar"; "baz"]; ["baz"; "foo"; "baz"; "foo"; "bar"; "bar"; "foo"; "bar"; "baz"; "baz"; "bar"; "baz"; "foo"; "foo"; "baz"; "foo"; "bar"; "foo"; "foo"]]

In this example, each element is drawn from the small set "foo", "bar", and "baz". The lists are guaranteed to have at least a single element, but may be longer.

#### Shuffle

You can use the Gen.shuffle function to create a generator that generates a random permutation of a given finite sequence.

In the following example, the metasyntactic variables "foo", "bar", "baz", and "qux" define the input sequence:

Gen.shuffle ["foo"; "bar"; "baz"; "qux"] |> Gen.sample 0 6


Since Gen.shuffle doesn't rely on the size argument, it's 0 in this example, but any value would do; it wouldn't change the result.

The result of this expression is a list of lists, where each list contains the original input list, but shuffled:

 [[|"bar"; "qux"; "foo"; "baz"|]; [|"bar"; "qux"; "baz"; "foo"|]; [|"bar"; "baz"; "foo"; "qux"|]; [|"qux"; "baz"; "foo"; "bar"|]; [|"foo"; "qux"; "bar"; "baz"|]; [|"baz"; "bar"; "qux"; "foo"|]]

The above example uses a list value as input, but you can use any seq expression, including list and array values, as long as the sequence is finite.

All shuffles are equally likely; the input order isn't excluded, so the output may be the same as the input. Due to the nature of combinatorics, this is more likely to happen the smaller the input list is.

#### Tuples

Sometimes you need to generate tuples of values. You can use the functions Gen.two, Gen.three, and Gen.four to turn a single-value generator into a generator of tuples.

Imagine that you need to generate two-dimensional points; you may, for instance, be implementing Conway's Game of Life. Points can be modelled as tuples of numbers. If you're modelling a grid, you can use integers:

Gen.choose (-100, 100) |> Gen.two |> Gen.sample 0 10


Gen.two uses a single-value generator to create a generator of two-element tuples. This example generates 10 sample points, where each coordinate is between -100 and 100:

 [(74, 32); (97, -49); (-16, 42); (-91, -39); (-46, 52); (-29, -29); (-59, 56); (63, 58); (55, 47); (28, 33)]

If you want to model a coordinate system in three-dimensional space, you may decide to use floating points instead:

Gen.elements [-10.0..0.01..10.0] |> Gen.three |> Gen.sample 0 10


In this example, you first use Gen.elements to draw a floating point value from between -10 and 10, with two decimals; that defines a Gen<float>. Second, Gen.three takes that Gen<float> and turns it into a Gen<float * float * float>:

 [(-5.65, -3.73, 6.26); (3.26, 6.21, -8.74); (-9.56, -2.14, -2.01); (-0.65, 6.17, 3.0); (6.54, 1.39, 9.73); (-4.56, -8.68, -5.27); (2.63, 2.98, 1.46); (-8.47, -7.09, 6.47); (-1.14, 5.35, -6.81); (8.61, -0.33, -1.8)]

Finally, Gen.four transforms a single-value generator into a generator of four-element tuples. As all the other combinators in the Gen module, you can combine it with other functions to define more specific values. Imagine, for instance, that you need to create System.Version values. This type, which captures a version of something, for example an operating system, or a library, models version numbers as a composite of four numbers: major, minor, build, and revision - all integers. One of the constructor overloads of this class takes all four numbers, so you can combine Gen.four with Gen.map to create Version values:

Gen.choose (0, 9)
|> Gen.four
|> Gen.map (System.Version >> string)
|> Gen.sample 0 10


This example starts with Gen.choose (0, 9) to define a Gen<int> that creates integer values betwen 0 and 9 (both included). Second, you pipe the Gen<int> value into Gen.four, which returns a Gen<int * int * int * int>. Third, you can pipe that generator into Gen.map, using the constructor overload of Version that takes four integers; in F# 4, constructors can be treated as functions, and a constructor with four arguments can be treated as a function that takes a four-element tuple.

 ["6.7.8.3"; "0.0.2.4"; "2.5.0.7"; "6.8.1.8"; "8.3.7.1"; "2.6.1.2"; "4.1.7.5"; "0.4.5.6"; "9.9.6.9"; "4.3.3.0"]

This example composes the Version constructor with the string function, in order to produce a more readable output. The resulting generator has the type Gen<string>, but if you remove the string composition, the type would be Gen<Version>.

#### Filter

While you can use the above generators and combinators to define various custom rules for generating values, occasionally you have a requirement where the easiest solution is to throw away some generated candidates. Gen.filter gives you that opportunity.

Imagine, for example, that you have to create lists with two elements, but with the restriction that the two elements must be different. One way to do that could be to first generate a pair of values, and then use Gen.filter to remove all pairs where the elements are equal. Subsequently, you can use Gen.map to convert the pair to a list:

Gen.choose (1, 100)
|> Gen.two
|> Gen.filter (fun (x, y) -> x <> y)
|> Gen.map (fun (x, y) -> [x; y])
|> Gen.sample 0 10


This expression generates 10 sample lists, each containing two different numbers:

 [[70; 73]; [52; 66]; [68; 31]; [22; 24]; [4; 89]; [58; 82]; [1; 13]; [1; 13]; [94; 38]; [74; 92]]

When using Gen.filter, be sure to provide a predicate with a high chance of returning true. If the predicate discards 'too many' candidates, it may cause tests to run slower, or to not terminate at all. If your filter is aggressive, consider using Gen.tryFilter instead of Gen.filter.

## Default Generators and Shrinkers based on type

FsCheck defines default test data generators and shrinkers for some often used types, for example unit, bool, byte, int, float, char, string, DateTime, lists, array 1D and 2D, Set, Map, objects and functions from and to any of the above. Furthermore, by using reflection, FsCheck can derive default implementations of record types, discriminated unions, tuples, enums and basic classes in terms of any primitive types that are defined (either in FsCheck or by you).

You do not need to define these explicity for every property: FsCheck can provide a property with appropriate generators and shrinkers for all of the property's arguments, if it knows them or can derive them. Usually you can let type inference do the job of finding out these types based on your properties. However if you want to coerce FsCheck to use a particular generator and shrinker, you can do so by providing the appropriate type annotations.

FsCheck packages a generator and shrinker for a particular type in an Arbitrary type. You can provide FsCheck with an Arbitrary instance for your own types, by defining static members that return an instance of a subclass of Arbitrary<'a>:

type MyGenerators =
static member Tree() =
{new Arbitrary<Tree>() with
override x.Generator = tree
override x.Shrinker t = Seq.empty }

 public class MyGenerators { public static Arbitrary Trees() { return Arb.From(SafeTree()); } }

Replace the 'a by the particular type you are defining an Arbitary instance for. Only the Generator method needs to be defined; Shrinker by default returns the empty sequence which means no shrinking will be done for this type).

As the F# code shows, you can create your own subclass of Arbitrary and return that, or you can use one of the Arb.from methods or functions.

Now, to register all Arbitrary instances in this class:

Arb.register<MyGenerators>()

 Arb.Register();

FsCheck now knows about Tree types, and can not only generate Tree values, but also e.g. lists, tuples and option values containing Trees:

let revRevTree (xs:list<Tree>) =
List.rev(List.rev xs) = xs
Check.Quick revRevTree

 Ok, passed 100 tests.

To generate types with a generic type argument, e.g.

type Box<'a> = Whitebox of 'a | Blackbox of 'a


you can use the same principle. So the class MyGenerators can be writtten as follows:

let boxGen<'a> : Gen<Box<'a>> =
gen { let! a = Arb.generate<'a>
return! Gen.elements [ Whitebox a; Blackbox a] }

type MyTreeGenerator =
static member Tree() =
{new Arbitrary<Tree>() with
override x.Generator = tree
override x.Shrinker t = Seq.empty }
static member Box() = Arb.fromGen boxGen


Notice that we use the function generate<'a> from the Arb module to get the generator for the type argument of Box. This allows you to define generators recursively. Similarly, there is a function shrink<'a>. Look at the FsCheck source for examples of default Arbitrary implementations to get a feeling of how to write such Arbitrary instances. The Arb module should help you with this task as well.

Now, the following property can be checked:

let revRevBox (xs:list<Box<int>>) =
List.rev(List.rev xs) = xs
Check.Quick revRevBox

 Ok, passed 100 tests.

Note that the class needs not be tagged with attributes in any way. FsCheck determines the type of the generator by the return type of each static member.

Also note that in this case we actually didn't need to write a generator or shrinker: FsCheck can derive suitable instances using reflection for discriminated unions, record types and enums.

### Notes about the default Generators and Shrinkers

Most of the default Arbitrary instances are documented with xml comments that can be discovered via IntelliSense. However, there are some important things to notice that are listed here to avoid much duplicating comments.

• Most of the default sized generators of the number-like types produce the uniformly distributed values in the ranges specified in the comments.
• The same thing with the default DoNotSize generators of the number-like types except Decimal.
• Most of the default generators of the collection types are just "wrappers" around the F# list. Thus, you can assume that they are generated and shrinked the same way.

## Useful methods on the Arb module

• Arb.from<'a> returns the registered Arbitrary instance for the given type 'a
• Arb.fromGen makes a new Arbitrary instance from just a given generator - the shrinker return the empty sequence
• Arb.fromGenShrink make a new Arbitrary instance from a given generator and shrinker. This is equivalent to implementing Arbitrary yourself, but may be shorter.
• Arb.generate<'a> returns the generator of the registered Arbitrary instance for the given type 'a
• Arb.shrink return the immediate shrinks of the registered Arbitrary instance for the given value
• Arb.convert given conversion functions to ('a ->'b) and from ('b ->'a), converts an Arbitrary<'a> instance to an Arbitrary<'b>
• Arb.filter filters the generator and shrinker for a given Arbitrary instance to contain only those values that match with the given filter function
• Arb.mapFilter maps the generator and filter the shrinkers for a given Arbitrary instance. Mapping the generator is sometimes faster, e.g. for a PositiveInt it is faster to take the absolute value than to filter the negative values.
• Arb.Default is a type that contains all the default Arbitrary instances as they are shipped and registerd by FsCheck by default. This is useful when you override a default generator - typically this is because you want to filter certain values from it, and then you need to be able to refer to the default generator in your overriding generator.
namespace FsCheck
namespace System
val chooseFromList : xs:'a list -> Gen<'a>
val xs : 'a list
val gen : GenBuilder
val i : int
Multiple items
module Gen

from FsCheck

--------------------
type Gen<'a> =
private | Gen of (int -> StdGen -> 'a)
interface IGen
member private Map : f:('a -> 'b) -> Gen<'b>
static member ( <!> ) : f:('a1 -> 'a2) * a:Gen<'a1> -> Gen<'a2>
static member ( <*> ) : f:Gen<('a1 -> 'a2)> * a:Gen<'a1> -> Gen<'a2>
static member ( >>= ) : m:Gen<'a1> * k:('a1 -> Gen<'a2>) -> Gen<'a2>
val choose : l:int * h:int -> Gen<int>
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member GetReverseIndex : rank:int * offset:int -> int
member GetSlice : startIndex:int option * endIndex:int option -> 'T list
static member Cons : head:'T * tail:'T list -> 'T list
member IsEmpty : bool
member Item : index:int -> 'T with get
...
val length : list:'T list -> int
val item : index:int -> list:'T list -> 'T
val oneof : gens:seq<Gen<'a>> -> Gen<'a>
val frequency : xs:seq<int * Gen<'a18>> -> Gen<'a18>
val sized : fgen:(int -> Gen<'b>) -> Gen<'b>
val s : int
val matrix : gen:Gen<'a> -> Gen<'a>
val gen : Gen<'a>
val resize : newSize:int -> Gen<'b> -> Gen<'b>
Multiple items
val float : value:'T -> float (requires member op_Explicit)

--------------------
[<Struct>]
type float = Double

--------------------
type float<'Measure> =
float
val sqrt : value:'T -> 'U (requires member Sqrt)
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
[<Struct>]
type int = int32

--------------------
type int<'Measure> =
int
type Tree =
| Leaf of int
| Branch of Tree * Tree
union case Tree.Leaf: int -> Tree
union case Tree.Branch: Tree * Tree -> Tree
val unsafeTree : unit -> Gen<Tree>
val map : f:('b -> 'c) -> gen:Gen<'b> -> Gen<'c>
module Arb

from FsCheck
val generate<'Value> : Gen<'Value>
val map2 : f:('a18 -> 'a19 -> 'a20) -> a:Gen<'a18> -> b:Gen<'a19> -> Gen<'a20>
val x : Tree
val y : Tree
val tree : Gen<Tree>
val tree' : (int -> Gen<Tree>)
val n : int
val subtree : Gen<Tree>
val invalidArg : argumentName:string -> message:string -> 'T
val constant : v:'b -> Gen<'b>
val sample : size:int -> n:int -> generator:Gen<'b> -> 'b list
val elements : xs:seq<'b> -> Gen<'b>
val growingElements : xs:seq<'a> -> Gen<'a>
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

--------------------
[<Struct>]
type byte = Byte

--------------------
type byte<'Measure> =
byte
Multiple items
[<Struct>]
type DateTime =
new : year: int * month: int * day: int -> unit + 10 overloads
member Add : value: TimeSpan -> DateTime
member AddDays : value: float -> DateTime
member AddHours : value: float -> DateTime
member AddMilliseconds : value: float -> DateTime
member AddMinutes : value: float -> DateTime
member AddMonths : months: int -> DateTime
member AddSeconds : value: float -> DateTime
member AddTicks : value: int64 -> DateTime
member AddYears : value: int -> DateTime
...

--------------------
DateTime ()
DateTime(ticks: int64) : DateTime
DateTime(ticks: int64, kind: DateTimeKind) : DateTime
DateTime(year: int, month: int, day: int) : DateTime
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : DateTime
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : DateTime
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : DateTime
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : DateTime
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : DateTime
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : DateTime
val listOf : gn:Gen<'b> -> Gen<'b list>
val listOfLength : n:int -> arb:Gen<'b> -> Gen<'b list>
val nonEmptyListOf : gn:Gen<'b> -> Gen<'b list>
val shuffle : xs:seq<'a> -> Gen<'a array>
val two : g:Gen<'a18> -> Gen<'a18 * 'a18>
val three : g:Gen<'b> -> Gen<'b * 'b * 'b>
val four : g:Gen<'b> -> Gen<'b * 'b * 'b * 'b>
Multiple items
type Version =
interface ICloneable
interface IComparable
interface IComparable<Version>
interface IEquatable<Version>
new : unit -> unit + 4 overloads
member Clone : unit -> obj
member CompareTo : version: obj -> int + 1 overload
member Equals : obj: obj -> bool + 1 overload
member GetHashCode : unit -> int
member ToString : unit -> string + 1 overload
...

--------------------
Version() : Version
Version(version: string) : Version
Version(major: int, minor: int) : Version
Version(major: int, minor: int, build: int) : Version
Version(major: int, minor: int, build: int, revision: int) : Version
Multiple items
val string : value:'T -> string

--------------------
type string = String
val filter : predicate:('a18 -> bool) -> generator:Gen<'a18> -> Gen<'a18>
val x : int
val y : int
Multiple items
type Arbitrary<'a> =
interface IArbitrary
new : unit -> Arbitrary<'a>
abstract member Shrinker : 'a -> seq<'a> + 1 overload
abstract member Generator : Gen<'a>

--------------------
new : unit -> Arbitrary<'a>
val x : Arbitrary<Tree>
property Arbitrary.Generator: Gen<Tree> with get
override Arbitrary.Shrinker : 'a -> seq<'a>
val t : Tree
module Seq

from Microsoft.FSharp.Collections
val empty<'T> : seq<'T>
val register<'t> : unit -> TypeClass.TypeClassComparison
type MyGenerators =
static member Tree : unit -> Arbitrary<Tree>
val revRevTree : xs:Tree list -> bool
val xs : Tree list
type 'T list = List<'T>
val rev : list:'T list -> 'T list
type Check =
static member All : config:Config * test:Type -> unit + 1 overload
static member Method : config:Config * methodInfo:MethodInfo * ?target:obj -> unit
static member One : config:Config * property:'Testable -> unit + 1 overload
static member Quick : property:'Testable -> unit + 1 overload
static member QuickAll : test:Type -> unit + 1 overload
static member QuickThrowOnFailure : property:'Testable -> unit
static member QuickThrowOnFailureAll : test:Type -> unit + 1 overload
static member Verbose : property:'Testable -> unit + 1 overload
static member VerboseAll : test:Type -> unit + 1 overload
static member VerboseThrowOnFailure : property:'Testable -> unit
...
static member Check.Quick : property:'Testable -> unit
static member Check.Quick : name:string * property:'Testable -> unit
type Box<'a> =
| Whitebox of 'a
| Blackbox of 'a
union case Box.Whitebox: 'a -> Box<'a>
union case Box.Blackbox: 'a -> Box<'a>
val boxGen<'a> : Gen<Box<'a>>
val a : 'a
val fromGen : gen:Gen<'Value> -> Arbitrary<'Value>
val revRevBox : xs:Box<int> list -> bool
val xs : Box<int> list