Table of Contents

Class Generate

Namespace
Conjecture.Core
Assembly
Conjecture.Core.dll

Factory methods for creating and composing Conjecture strategies.

public static class Generate
Inheritance
Generate
Inherited Members

Methods

Booleans()

Returns a strategy that generates random bool values.

public static Strategy<bool> Booleans()

Returns

Strategy<bool>

Bytes(int)

Returns a strategy that generates random byte arrays of length size.

public static Strategy<byte[]> Bytes(int size)

Parameters

size int

Returns

Strategy<byte[]>

Compose<T>(Func<IGeneratorContext, T>)

Creates a strategy from an imperative factory function using IGeneratorContext.

public static Strategy<T> Compose<T>(Func<IGeneratorContext, T> factory)

Parameters

factory Func<IGeneratorContext, T>

Returns

Strategy<T>

Type Parameters

T

DateOnlyValues()

Returns a strategy that generates random DateOnly values across the full range.

public static Strategy<DateOnly> DateOnlyValues()

Returns

Strategy<DateOnly>

DateOnlyValues(DateOnly, DateOnly)

Returns a strategy that generates random DateOnly values in [min, max].

public static Strategy<DateOnly> DateOnlyValues(DateOnly min, DateOnly max)

Parameters

min DateOnly
max DateOnly

Returns

Strategy<DateOnly>

DateTimeOffsets()

Returns a strategy that generates random DateTimeOffset values across the full range.

public static Strategy<DateTimeOffset> DateTimeOffsets()

Returns

Strategy<DateTimeOffset>

DateTimeOffsets(DateTimeOffset, DateTimeOffset)

Returns a strategy that generates random DateTimeOffset values in [min, max].

public static Strategy<DateTimeOffset> DateTimeOffsets(DateTimeOffset min, DateTimeOffset max)

Parameters

min DateTimeOffset
max DateTimeOffset

Returns

Strategy<DateTimeOffset>

Dictionaries<TKey, TValue>(Strategy<TKey>, Strategy<TValue>, int, int)

Returns a strategy that generates IReadOnlyDictionary<TKey, TValue> with unique keys and size in [minSize, maxSize].

public static Strategy<IReadOnlyDictionary<TKey, TValue>> Dictionaries<TKey, TValue>(Strategy<TKey> keyStrategy, Strategy<TValue> valueStrategy, int minSize = 0, int maxSize = 100) where TKey : notnull

Parameters

keyStrategy Strategy<TKey>
valueStrategy Strategy<TValue>
minSize int
maxSize int

Returns

Strategy<IReadOnlyDictionary<TKey, TValue>>

Type Parameters

TKey
TValue

Doubles()

Returns a strategy that generates random double values across the full range.

public static Strategy<double> Doubles()

Returns

Strategy<double>

Doubles(double, double)

Returns a strategy that generates random double values in [min, max].

public static Strategy<double> Doubles(double min, double max)

Parameters

min double
max double

Returns

Strategy<double>

Enums<T>()

Returns a strategy that generates random T enum values.

public static Strategy<T> Enums<T>() where T : struct, Enum

Returns

Strategy<T>

Type Parameters

T

Floats()

Returns a strategy that generates random float values across the full range.

public static Strategy<float> Floats()

Returns

Strategy<float>

Floats(float, float)

Returns a strategy that generates random float values in [min, max].

public static Strategy<float> Floats(float min, float max)

Parameters

min float
max float

Returns

Strategy<float>

Identifiers(int, int, int, int)

Returns a strategy that generates identifier strings of the form [a-z]+\d+. The alpha prefix is drawn via IR string nodes so StringAwarePass can simplify it toward 'a', and the digit suffix is drawn so NumericAwareShrinkPass can minimize it.

public static Strategy<string> Identifiers(int minPrefixLength = 1, int maxPrefixLength = 6, int minDigits = 1, int maxDigits = 4)

Parameters

minPrefixLength int
maxPrefixLength int
minDigits int
maxDigits int

Returns

Strategy<string>

Integers<T>()

Returns a strategy that generates random T values across the full range of the type.

public static Strategy<T> Integers<T>() where T : IBinaryInteger<T>, IMinMaxValue<T>

Returns

Strategy<T>

Type Parameters

T

Integers<T>(T, T)

Returns a strategy that generates random T values in [min, max].

public static Strategy<T> Integers<T>(T min, T max) where T : IBinaryInteger<T>

Parameters

min T
max T

Returns

Strategy<T>

Type Parameters

T

Just<T>(T)

Returns a strategy that always produces value.

public static Strategy<T> Just<T>(T value)

Parameters

value T

Returns

Strategy<T>

Type Parameters

T

Lists<T>(Strategy<T>, int, int)

Returns a strategy that generates List<T> with size in [minSize, maxSize].

public static Strategy<List<T>> Lists<T>(Strategy<T> inner, int minSize = 0, int maxSize = 100)

Parameters

inner Strategy<T>
minSize int
maxSize int

Returns

Strategy<List<T>>

Type Parameters

T

Nullable<T>(Strategy<T>)

Returns a strategy that produces nullable T values, with ~10% null probability.

public static Strategy<T?> Nullable<T>(Strategy<T> inner) where T : struct

Parameters

inner Strategy<T>

Returns

Strategy<T?>

Type Parameters

T

NumericStrings(int, int, string?, string?)

Returns a strategy that generates strings of the form [prefix][digits][suffix] where the digit part is drawn via IR string nodes so NumericAwareShrinkPass can minimize it.

public static Strategy<string> NumericStrings(int minDigits = 1, int maxDigits = 6, string? prefix = null, string? suffix = null)

Parameters

minDigits int
maxDigits int
prefix string
suffix string

Returns

Strategy<string>

OneOf<T>(params Strategy<T>[])

Returns a strategy that picks uniformly among strategies.

public static Strategy<T> OneOf<T>(params Strategy<T>[] strategies)

Parameters

strategies Strategy<T>[]

Returns

Strategy<T>

Type Parameters

T

Recursive<T>(Strategy<T>, Func<Strategy<T>, Strategy<T>>, int)

Returns a strategy that generates recursive data structures up to maxDepth levels deep.

public static Strategy<T> Recursive<T>(Strategy<T> baseCase, Func<Strategy<T>, Strategy<T>> recursive, int maxDepth = 5)

Parameters

baseCase Strategy<T>

Strategy for leaf nodes (depth 0). Used whenever the target depth is exhausted.

recursive Func<Strategy<T>, Strategy<T>>

Factory that receives a self strategy (which recurses at depth − 1) and returns a strategy for non-leaf nodes. The engine substitutes baseCase for self at depth 0.

maxDepth int

Maximum recursion depth. Generated values have depth in [0, maxDepth]. Must be ≥ 0.

Returns

Strategy<T>

A Strategy<T> whose generated values have depth at most maxDepth. The depth draw is an IR integer, so the shrinker reduces it toward 0, producing shallower structures when minimising a counterexample.

Type Parameters

T

The type of value to generate.

Examples

// Expression tree: Literal | Add | Mul, up to 5 levels deep
Strategy<Expr> exprStrategy = Generate.Recursive<Expr>(
    baseCase: Generate.Integers<int>(0, 100).Select(n => (Expr)new Literal(n)),
    recursive: self => Generate.OneOf(
        Generate.Integers<int>(0, 100).Select(n => (Expr)new Literal(n)),
        Generate.Tuples(self, self).Select(t => (Expr)new Add(t.Item1, t.Item2)),
        Generate.Tuples(self, self).Select(t => (Expr)new Mul(t.Item1, t.Item2))),
    maxDepth: 5);

SampledFrom<T>(IReadOnlyList<T>)

Returns a strategy that picks uniformly from values.

public static Strategy<T> SampledFrom<T>(IReadOnlyList<T> values)

Parameters

values IReadOnlyList<T>

Returns

Strategy<T>

Type Parameters

T

Sets<T>(Strategy<T>, int, int)

Returns a strategy that generates IReadOnlySet<T> with unique elements and size in [minSize, maxSize].

public static Strategy<IReadOnlySet<T>> Sets<T>(Strategy<T> inner, int minSize = 0, int maxSize = 100)

Parameters

inner Strategy<T>
minSize int
maxSize int

Returns

Strategy<IReadOnlySet<T>>

Type Parameters

T

StateMachine<TMachine, TState, TCommand>(int)

Returns a strategy that generates StateMachineRun<TState> values by running TMachine for up to maxSteps steps.

public static Strategy<StateMachineRun<TState>> StateMachine<TMachine, TState, TCommand>(int maxSteps = 50) where TMachine : IStateMachine<TState, TCommand>, new()

Parameters

maxSteps int

Maximum number of commands to draw per run. Defaults to 50.

Returns

Strategy<StateMachineRun<TState>>

A Strategy<T> that produces StateMachineRun<TState> values. If Invariant(TState) throws during generation, the strategy propagates the exception so the enclosing property fails with a shrunk command sequence.

Type Parameters

TMachine

The state machine type. Must implement IStateMachine<TState, TCommand> and expose a public parameterless constructor.

TState

The type representing the system's state.

TCommand

The type representing a command that can be applied to the state.

Examples

public sealed class CounterMachine : IStateMachine<int, string>
{
    public int InitialState() => 0;
    public IEnumerable<Strategy<string>> Commands(int state) => [Generate.Just("inc")];
    public int RunCommand(int state, string cmd) => state + 1;
    public void Invariant(int state)
    {
        if (state < 0)
            throw new InvalidOperationException("counter went negative");
    }
}

Strategy<StateMachineRun<int>> strategy =
    Generate.StateMachine<CounterMachine, int, string>(maxSteps: 50);

Strings(int, int, int, int, string?)

Returns a strategy that generates random strings. When alphabet is provided it takes precedence and minCodepoint/maxCodepoint are ignored.

public static Strategy<string> Strings(int minLength = 0, int maxLength = 20, int minCodepoint = 32, int maxCodepoint = 126, string? alphabet = null)

Parameters

minLength int
maxLength int
minCodepoint int
maxCodepoint int
alphabet string

Returns

Strategy<string>

Text(int, int)

public static Strategy<string> Text(int minLength = 0, int maxLength = 20)

Parameters

minLength int
maxLength int

Returns

Strategy<string>

TimeOnlyValues()

Returns a strategy that generates random TimeOnly values across the full range.

public static Strategy<TimeOnly> TimeOnlyValues()

Returns

Strategy<TimeOnly>

TimeOnlyValues(TimeOnly, TimeOnly)

Returns a strategy that generates random TimeOnly values in [min, max].

public static Strategy<TimeOnly> TimeOnlyValues(TimeOnly min, TimeOnly max)

Parameters

min TimeOnly
max TimeOnly

Returns

Strategy<TimeOnly>

TimeSpans()

Returns a strategy that generates random TimeSpan values across the full range.

public static Strategy<TimeSpan> TimeSpans()

Returns

Strategy<TimeSpan>

TimeSpans(TimeSpan, TimeSpan)

Returns a strategy that generates random TimeSpan values in [min, max].

public static Strategy<TimeSpan> TimeSpans(TimeSpan min, TimeSpan max)

Parameters

min TimeSpan
max TimeSpan

Returns

Strategy<TimeSpan>

Tuples<T1, T2>(Strategy<T1>, Strategy<T2>)

Returns a strategy that produces (T1, T2) tuples from two component strategies.

public static Strategy<(T1, T2)> Tuples<T1, T2>(Strategy<T1> first, Strategy<T2> second)

Parameters

first Strategy<T1>
second Strategy<T2>

Returns

Strategy<(T1, T2)>

Type Parameters

T1
T2

Tuples<T1, T2, T3>(Strategy<T1>, Strategy<T2>, Strategy<T3>)

Returns a strategy that produces 3-element tuples from three component strategies.

public static Strategy<(T1, T2, T3)> Tuples<T1, T2, T3>(Strategy<T1> first, Strategy<T2> second, Strategy<T3> third)

Parameters

first Strategy<T1>
second Strategy<T2>
third Strategy<T3>

Returns

Strategy<(T1, T2, T3)>

Type Parameters

T1
T2
T3

Tuples<T1, T2, T3, T4>(Strategy<T1>, Strategy<T2>, Strategy<T3>, Strategy<T4>)

Returns a strategy that produces 4-element tuples from four component strategies.

public static Strategy<(T1, T2, T3, T4)> Tuples<T1, T2, T3, T4>(Strategy<T1> first, Strategy<T2> second, Strategy<T3> third, Strategy<T4> fourth)

Parameters

first Strategy<T1>
second Strategy<T2>
third Strategy<T3>
fourth Strategy<T4>

Returns

Strategy<(T1, T2, T3, T4)>

Type Parameters

T1
T2
T3
T4

VersionStrings(int, int, int)

Returns a strategy that generates version strings of the form MAJOR.MINOR.PATCH where each component is a numeric string drawn via IR string nodes so NumericAwareShrinkPass can minimize each segment independently.

public static Strategy<string> VersionStrings(int maxMajor = 9, int maxMinor = 9, int maxPatch = 9)

Parameters

maxMajor int
maxMinor int
maxPatch int

Returns

Strategy<string>