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
Bytes(int)
Returns a strategy that generates random byte arrays of length size.
public static Strategy<byte[]> Bytes(int size)
Parameters
sizeint
Returns
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
factoryFunc<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
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
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
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
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
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 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
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
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 draw is an IR integer, so the shrinker reduces it toward 0, producing shallower structures when minimising a counterexample.
Type Parameters
TThe 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
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 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
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) => [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
Returns
Text(int, int)
Alias for Strings(int, int, int, int, string?).
public static Strategy<string> Text(int minLength = 0, int maxLength = 20)
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
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)