Table of Contents

Class Strategy

Namespace
Conjecture.Core
Assembly
Conjecture.Core.dll

Static methods for creating and composing Conjecture strategies.

public static class Strategy
Inheritance
Strategy
Inherited Members

Methods

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

Returns a strategy that generates T arrays with length in [minSize, maxSize] and elements drawn from inner.

public static Strategy<T[]> Arrays<T>(Strategy<T> inner, int minSize = 0, int maxSize = 100)

Parameters

inner Strategy<T>
minSize int
maxSize int

Returns

Strategy<T[]>

Type Parameters

T

Booleans()

Returns a strategy that generates random bool values.

public static Strategy<bool> Booleans()

Returns

Strategy<bool>

Chars()

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

public static Strategy<char> Chars()

Returns

Strategy<char>

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

Creates a strategy from an imperative body using IGenerationContext.

public static Strategy<T> Compose<T>(Func<IGenerationContext, T> body)

Parameters

body Func<IGenerationContext, T>

Returns

Strategy<T>

Type Parameters

T

Cultures()

Returns a strategy that generates random CultureInfo values from all cultures, with InvariantCulture at index 0 to guide shrinking.

public static Strategy<CultureInfo> Cultures()

Returns

Strategy<CultureInfo>

Cultures(CultureTypes)

Returns a strategy that generates random CultureInfo values matching types, with InvariantCulture at index 0 to guide shrinking.

public static Strategy<CultureInfo> Cultures(CultureTypes types)

Parameters

types CultureTypes

Returns

Strategy<CultureInfo>

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>

DateTimes()

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

public static Strategy<DateTime> DateTimes()

Returns

Strategy<DateTime>

DateTimes(DateTime, DateTime)

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

public static Strategy<DateTime> DateTimes(DateTime min, DateTime max)

Parameters

min DateTime
max DateTime

Returns

Strategy<DateTime>

Decimals()

Returns a strategy that generates random decimal values.

public static Strategy<decimal> Decimals()

Returns

Strategy<decimal>

Decimals(decimal, decimal)

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

public static Strategy<decimal> Decimals(decimal min, decimal max)

Parameters

min decimal
max decimal

Returns

Strategy<decimal>

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>

EmailAddressStrings()

Returns a strategy that generates RFC 5321-shaped email address strings.

public static Strategy<string> EmailAddressStrings()

Returns

Strategy<string>

EmailAddresses()

Returns a strategy that generates MailAddress values with locally-generated user and host parts.

public static Strategy<MailAddress> EmailAddresses()

Returns

Strategy<MailAddress>

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>

For<T>()

Returns a strategy for T using its registered Conjecture.Core.IStrategyProvider<T>. The type must be decorated with [Arbitrary].

public static Strategy<T> For<T>()

Returns

Strategy<T>

Type Parameters

T

For<T>(Action<ForConfiguration<T>>)

Returns a strategy for T with property overrides applied via configure. The type must be decorated with [Arbitrary].

public static Strategy<T> For<T>(Action<ForConfiguration<T>> configure)

Parameters

configure Action<ForConfiguration<T>>

Returns

Strategy<T>

Type Parameters

T

FromBytes<T>(ReadOnlySpan<byte>)

Creates a strategy that replays values from a fixed byte array (IR replay source). Useful for deterministic seed replay and round-trip testing.

public static Strategy<T> FromBytes<T>(ReadOnlySpan<byte> buffer)

Parameters

buffer ReadOnlySpan<byte>

Returns

Strategy<T>

Type Parameters

T

Guids()

Returns a strategy that generates random Guid values.

public static Strategy<Guid> Guids()

Returns

Strategy<Guid>

Halves()

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

public static Strategy<Half> Halves()

Returns

Strategy<Half>

Halves(Half, Half)

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

public static Strategy<Half> Halves(Half min, Half max)

Parameters

min Half
max Half

Returns

Strategy<Half>

Hosts(int, int)

Returns a strategy that generates DNS-like host names: 1..maxLabels labels of lowercase alphanumerics joined by '.', with a TLD-shaped final label (lowercase letters only, length >= 2).

public static Strategy<string> Hosts(int minLabels = 1, int maxLabels = 3)

Parameters

minLabels int
maxLabels int

Returns

Strategy<string>

IPAddresses(IPAddressKind)

Returns a strategy that generates IPAddress values for the specified address family.

public static Strategy<IPAddress> IPAddresses(IPAddressKind kind = IPAddressKind.Both)

Parameters

kind IPAddressKind

Which address families to generate. Must not be zero.

Returns

Strategy<IPAddress>

Exceptions

ArgumentException

Thrown when kind has no flags set.

IPEndPoints(Strategy<IPAddress>?, Strategy<int>?)

Returns a strategy that generates IPEndPoint values composed from addresses and ports.

public static Strategy<IPEndPoint> IPEndPoints(Strategy<IPAddress>? addresses = null, Strategy<int>? ports = null)

Parameters

addresses Strategy<IPAddress>

Address strategy; defaults to IPAddresses(IPAddressKind.Both).

ports Strategy<int>

Port strategy; defaults to Integers<int>(0, 65535).

Returns

Strategy<IPEndPoint>

Identifiers(int, int, int, int)

Returns a strategy that generates identifier strings of the form [a-z]+\d+. The alpha prefix is generated via IR string nodes so StringAwarePass can shrink it toward 'a', and the digit suffix is generated so NumericAwareShrinkPass can shrink 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>

Indices(int)

Returns a strategy that generates Index values whose Value falls in [0, maxValue]. Both from-start and from-end forms are produced; shrinks toward (Index)0.

public static Strategy<Index> Indices(int maxValue = 100)

Parameters

maxValue int

Returns

Strategy<Index>

Exceptions

ArgumentOutOfRangeException

Thrown when maxValue is negative.

Integers(BigInteger, BigInteger)

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

public static Strategy<BigInteger> Integers(BigInteger min, BigInteger max)

Parameters

min BigInteger
max BigInteger

Returns

Strategy<BigInteger>

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 generated via IR string nodes so NumericAwareShrinkPass can shrink 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

OneOf<T>(params ReadOnlySpan<Strategy<T>>)

Returns a strategy that picks uniformly among strategies. Stack-allocated call sites avoid heap-array allocation.

public static Strategy<T> OneOf<T>(params ReadOnlySpan<Strategy<T>> strategies)

Parameters

strategies ReadOnlySpan<Strategy<T>>

Returns

Strategy<T>

Type Parameters

T

Ranges(int)

Returns a strategy that generates Range values whose Start and End indices fall in [0, maxValue]. Mixed from-start/from-end forms are produced. The generated range is well-formed against an array of length maxValue; shrinks toward 0..0.

public static Strategy<Range> Ranges(int maxValue = 100)

Parameters

maxValue int

Returns

Strategy<Range>

Exceptions

ArgumentOutOfRangeException

Thrown when maxValue is negative.

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 node is an IR integer, so the shrinker shrinks it toward 0, producing shallower structures when shrinking 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 = Strategy.Recursive<Expr>(
    baseCase: Strategy.Integers<int>(0, 100).Select(n => (Expr)new Literal(n)),
    recursive: self => Strategy.OneOf(
        Strategy.Integers<int>(0, 100).Select(n => (Expr)new Literal(n)),
        Strategy.Tuples(self, self).Select(t => (Expr)new Add(t.Item1, t.Item2)),
        Strategy.Tuples(self, self).Select(t => (Expr)new Mul(t.Item1, t.Item2))),
    maxDepth: 5);

Runes()

Returns a strategy that generates random Rune values across the full Unicode scalar range (U+0000..U+10FFFF, excluding surrogates).

public static Strategy<Rune> Runes()

Returns

Strategy<Rune>

Runes(Rune, Rune)

Returns a strategy that generates random Rune values in [min, max], excluding surrogates.

public static Strategy<Rune> Runes(Rune min, Rune max)

Parameters

min Rune
max Rune

Returns

Strategy<Rune>

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 generate 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) => [Strategy.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 =
    Strategy.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>

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

Uris(UriKind, IReadOnlyList<string>?)

Returns a strategy that generates Uri values of the requested kind, optionally restricted to schemes.

public static Strategy<Uri> Uris(UriKind kind = UriKind.Absolute, IReadOnlyList<string>? schemes = null)

Parameters

kind UriKind
schemes IReadOnlyList<string>

Returns

Strategy<Uri>

VersionStrings(int, int, int)

Returns a strategy that generates version strings of the form MAJOR.MINOR.PATCH where each component is a numeric string generated via IR string nodes so NumericAwareShrinkPass can shrink 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>

Versions(int, int, int, int)

Returns a strategy that generates Version values with components in the configured ranges. Components default to a small range so shrinking converges quickly.

public static Strategy<Version> Versions(int maxMajor = 9, int maxMinor = 9, int maxBuild = 9, int maxRevision = 9)

Parameters

maxMajor int
maxMinor int
maxBuild int
maxRevision int

Returns

Strategy<Version>