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
Returns
- Strategy<T[]>
Type Parameters
T
Booleans()
Returns a strategy that generates random bool values.
public static Strategy<bool> Booleans()
Returns
Chars()
Returns a strategy that generates random char values across the full Unicode range.
public static Strategy<char> Chars()
Returns
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
bodyFunc<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
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
typesCultureTypes
Returns
DateOnlyValues()
Returns a strategy that generates random DateOnly values across the full range.
public static Strategy<DateOnly> DateOnlyValues()
Returns
DateOnlyValues(DateOnly, DateOnly)
Returns a strategy that generates random DateOnly values in [min, max].
public static Strategy<DateOnly> DateOnlyValues(DateOnly min, DateOnly max)
Parameters
Returns
DateTimeOffsets()
Returns a strategy that generates random DateTimeOffset values across the full range.
public static Strategy<DateTimeOffset> DateTimeOffsets()
Returns
DateTimeOffsets(DateTimeOffset, DateTimeOffset)
Returns a strategy that generates random DateTimeOffset values in [min, max].
public static Strategy<DateTimeOffset> DateTimeOffsets(DateTimeOffset min, DateTimeOffset max)
Parameters
minDateTimeOffsetmaxDateTimeOffset
Returns
DateTimes()
Returns a strategy that generates random DateTime values across the full range.
public static Strategy<DateTime> DateTimes()
Returns
DateTimes(DateTime, DateTime)
Returns a strategy that generates random DateTime values in [min, max].
public static Strategy<DateTime> DateTimes(DateTime min, DateTime max)
Parameters
Returns
Decimals()
Returns a strategy that generates random decimal values.
public static Strategy<decimal> Decimals()
Returns
Decimals(decimal, decimal)
Returns a strategy that generates random decimal values in [min, max].
public static Strategy<decimal> Decimals(decimal min, decimal max)
Parameters
Returns
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
Returns
- Strategy<IReadOnlyDictionary<TKey, TValue>>
Type Parameters
TKeyTValue
Doubles()
Returns a strategy that generates random double values across the full range.
public static Strategy<double> Doubles()
Returns
Doubles(double, double)
Returns a strategy that generates random double values in [min, max].
public static Strategy<double> Doubles(double min, double max)
Parameters
Returns
EmailAddressStrings()
Returns a strategy that generates RFC 5321-shaped email address strings.
public static Strategy<string> EmailAddressStrings()
Returns
EmailAddresses()
Returns a strategy that generates MailAddress values with locally-generated user and host parts.
public static Strategy<MailAddress> EmailAddresses()
Returns
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
Floats(float, float)
Returns a strategy that generates random float values in [min, max].
public static Strategy<float> Floats(float min, float max)
Parameters
Returns
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
configureAction<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
bufferReadOnlySpan<byte>
Returns
- Strategy<T>
Type Parameters
T
Guids()
Returns a strategy that generates random Guid values.
public static Strategy<Guid> Guids()
Returns
Halves()
Returns a strategy that generates random Half values across the full range.
public static Strategy<Half> Halves()
Returns
Halves(Half, Half)
Returns a strategy that generates random Half values in [min, max].
public static Strategy<Half> Halves(Half min, Half max)
Parameters
Returns
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
Returns
IPAddresses(IPAddressKind)
Returns a strategy that generates IPAddress values for the specified address family.
public static Strategy<IPAddress> IPAddresses(IPAddressKind kind = IPAddressKind.Both)
Parameters
kindIPAddressKindWhich address families to generate. Must not be zero.
Returns
Exceptions
- ArgumentException
Thrown when
kindhas 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
addressesStrategy<IPAddress>Address strategy; defaults to
IPAddresses(IPAddressKind.Both).portsStrategy<int>Port strategy; defaults to
Integers<int>(0, 65535).
Returns
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
Returns
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
maxValueint
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
maxValueis 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
minBigIntegermaxBigInteger
Returns
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
minTmaxT
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
valueT
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
Returns
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
innerStrategy<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
Returns
OneOf<T>(params Strategy<T>[])
Returns a strategy that picks uniformly among strategies.
public static Strategy<T> OneOf<T>(params Strategy<T>[] strategies)
Parameters
strategiesStrategy<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
strategiesReadOnlySpan<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
maxValueint
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
maxValueis 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
baseCaseStrategy<T>Strategy for leaf nodes (depth 0). Used whenever the target depth is exhausted.
recursiveFunc<Strategy<T>, Strategy<T>>Factory that receives a
selfstrategy (which recurses at depth − 1) and returns a strategy for non-leaf nodes. The engine substitutesbaseCaseforselfat depth 0.maxDepthintMaximum 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
TThe 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
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
Returns
SampledFrom<T>(IReadOnlyList<T>)
Returns a strategy that picks uniformly from values.
public static Strategy<T> SampledFrom<T>(IReadOnlyList<T> values)
Parameters
valuesIReadOnlyList<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
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
maxStepsintMaximum 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
TMachineThe state machine type. Must implement IStateMachine<TState, TCommand> and expose a public parameterless constructor.
TStateThe type representing the system's state.
TCommandThe 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
Returns
TimeOnlyValues()
Returns a strategy that generates random TimeOnly values across the full range.
public static Strategy<TimeOnly> TimeOnlyValues()
Returns
TimeOnlyValues(TimeOnly, TimeOnly)
Returns a strategy that generates random TimeOnly values in [min, max].
public static Strategy<TimeOnly> TimeOnlyValues(TimeOnly min, TimeOnly max)
Parameters
Returns
TimeSpans()
Returns a strategy that generates random TimeSpan values across the full range.
public static Strategy<TimeSpan> TimeSpans()
Returns
TimeSpans(TimeSpan, TimeSpan)
Returns a strategy that generates random TimeSpan values in [min, max].
public static Strategy<TimeSpan> TimeSpans(TimeSpan min, TimeSpan max)
Parameters
Returns
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
Returns
- Strategy<(T1, T2)>
Type Parameters
T1T2
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
Returns
- Strategy<(T1, T2, T3)>
Type Parameters
T1T2T3
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
Returns
- Strategy<(T1, T2, T3, T4)>
Type Parameters
T1T2T3T4
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
kindUriKindschemesIReadOnlyList<string>
Returns
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
Returns
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)