Repository navigation
Expand file tree
/
Copy pathllms.txt
More file actions
70 lines (53 loc) · 7.14 KB
/
Copy pathllms.txt
File metadata and controls
70 lines (53 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# CsCheck
> CsCheck is a C# property-based / random testing library (QuickCheck-style) for .NET. Generation and shrinking are both based on the PCG random number generator, so shrinking is automatic, parallelized, and reproducible from a seed. It supports random, model-based, metamorphic, specification (exhaustive state-space and temporal requirements), parallel/concurrency, causal-profiling, regression, and performance testing.
Key facts an assistant should know before generating CsCheck code:
- You compose a `Gen<T>` and call a terminating method (`Sample`, `SampleModelBased`, `SampleMetamorphic`, `SampleParallel`, `Faster`, `Check.Hash`). There are **no `Arb` classes** and you **never write shrinkers**; shrinking is automatic.
- A failing test shrinks to the simplest case and prints a **seed** plus `(N shrinks, M skipped, K total)`; re-run with `seed:` to reproduce. `skipped` = shrink-phase candidates that weren't smaller than the current minimal failure, so they were never asserted, **not** errors or `Where`-filtered inputs, and `0` in a passing run.
- CsCheck is **test-framework agnostic**: it signals failure by throwing an exception, with no dependency on any test runner. Use it with xUnit, NUnit, MSTest, TUnit, or no framework at all. This repo's own tests happen to use TUnit + Microsoft.Testing.Platform, but that is not required.
- The `Tests/` project is the canonical example collection.
- Install: `dotnet add package CsCheck`. License: Apache-2.0.
## API cheat sheet
Generators (compose with LINQ: `Select`, `SelectMany`, `Where`, query syntax):
- Primitives: `Gen.Int`, `Gen.Long`, `Gen.Double`, `Gen.Float`, `Gen.Byte`, `Gen.Bool`, `Gen.Char`, `Gen.String`, `Gen.Guid`, `Gen.DateTime`, `Gen.Decimal`, ...
- Ranges: `Gen.Int[0, 9]`, `Gen.Long[start, finish]`.
- Sub-ranges: `Gen.Single.Unit`, `Gen.Double.Unit`, `Gen.Char.AlphaNumeric`, `Gen.Int.Uniform`.
- Collections: `gen.Array`, `gen.Array[n]`, `gen.Array[min, max]`, `gen.List`, `gen.Array2D`, `Gen.Dictionary(keyGen, valGen)`.
- Combinators: `Gen.Select(...)` (tuple fields `.Item1`, `.Item2`, ...), `Gen.SelectMany`, `gen.Where(...)`, `Gen.OneOf(...)`, `Gen.Const(() => ...)`, `gen.Null()`, `Gen.Recursive<T>((depth, self) => ...)`.
- Many generators: `Gen.Select` takes up to 16 generators with a selector (up to 8 without one, giving a tuple). For more, nest them: `Gen.Select(Gen.Select(a, b, c, d), Gen.Select(e, f, g, h), (x, y) => ...)`, which shrinks the same as one flat `Select`. Query syntax (`from a in ga from b in gb ... select`) has no limit but shrinks the earlier values first, so keep it for values that depend on earlier ones.
Terminating methods:
- `gen.Sample(t => bool | throw)`: random testing.
- `gen.SampleModelBased(Gen...Operation<Actual, Model>(...))`: apply random ops to actual + model, assert equal. Add `classify:` over the model state with `writeLine:` for a table of how often each operation ran, split by the state it acted on.
- `gen.SampleMetamorphic(Gen...Metamorphic<T>(f1, f2))`: two routes, same result.
- `Spec.From(state).Action(...).Invariant/Reachable/Rule/Never/AtMost/Response/Precedes/NeverAfter(...)` then `.Exhaustive()` (enumerate the whole reachable state space, a proof when it closes), `.Sample()` (random walks with shrinking), `.Faults()` (mutation test the requirements, or `.SampleFaults()` when the space will not close), `.Conform(create, apply)` (does the real implementation conform to the spec on sampled traces) or `.ConformExhaustive(create, apply)` (the same on every transition of the proof, each down a shortest path, or with `clone:` as one step on a copy). `.Mermaid()` returns the reachable state graph as a Mermaid flowchart for a model small enough to look at. State must be an immutable record with saturating counters; where a counter cannot saturate, `.Boundary(predicate)` closes `Exhaustive` over a chosen region instead of giving up at `maxStates`. Keep the state narrow in bytes as well as bounded in values: every guard and requirement takes it by value, so padding costs roughly a nanosecond per byte per transition (1.84x measured from 12 to 96 bytes); pack flags into an int rather than a field each. See `docs/Spec.md`, and `docs/SpecDesign.md` for the design rationale and prior art.
- `gen.SampleParallel(Gen...Operation<State>(...))`: concurrency/linearizability testing.
- `gen.Faster(fast, slow, writeLine: ...)`: statistical performance comparison.
- `gen.Single(predicate, "seed")` + `Check.Hash(h => h.Add(...), expectedHash)`: regression testing. `Single` pins one generated example (by predicate, reproduced from the printed seed); `Check.Hash` hashes results and caches them so later runs report the first difference.
Common optional parameters: `iter` (default 100), `time` (seconds), `seed`, `threads`, `timeout` (Faster, 60s), `sigma` (Faster, 6), `replay` (SampleParallel, 100), `print`, `equal`. Env-var defaults: `CsCheck_Iter`, `CsCheck_Time`, `CsCheck_Seed`, `CsCheck_Threads`, `CsCheck_Timeout`, `CsCheck_Sigma` and `CsCheck_Replay` for those parameters, plus `CsCheck_AllocAll` (Faster counts allocations on all threads, default false), `CsCheck_WarmUp` (Faster waits for the JIT before measuring, default false), `CsCheck_Ulps` (`Check.Print` shows a double or float as a shorter number within that many ulps, default 4), `CsCheck_WhereLimit` (tries before `Where` throws, default 100) and `CsCheck_SingleLimit` (tries before `Single` throws, default 1,000,000).
Canonical example:
```csharp
[Test]
public void Long_Range()
{
(from t in Gen.Select(Gen.Long, Gen.Long)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Long[start, finish]
select (value, start, finish))
.Sample(i => i.start <= i.value && i.value <= i.finish);
}
```
## Documentation
- [README](https://github.com/AnthonyLloyd/CsCheck/blob/master/README.md): full guide with worked examples for every testing style.
- [AGENTS.md](https://github.com/AnthonyLloyd/CsCheck/blob/master/AGENTS.md): conventions and build/test commands for coding agents.
- [Why](https://github.com/AnthonyLloyd/CsCheck/blob/master/docs/Why.md): rationale and motivation for the design.
- [Comparison](https://github.com/AnthonyLloyd/CsCheck/blob/master/docs/Comparison.md): comparison with other random testing libraries.
## Examples
- [Tests directory](https://github.com/AnthonyLloyd/CsCheck/tree/master/Tests): the maintained example suite (these tests double as samples).
- [GenTests.cs](https://github.com/AnthonyLloyd/CsCheck/blob/master/Tests/GenTests.cs): generator composition examples.
- [CheckTests.cs](https://github.com/AnthonyLloyd/CsCheck/blob/master/Tests/CheckTests.cs): Sample / Faster / Hash usage.
- [ShrinkingChallengeTests.cs](https://github.com/AnthonyLloyd/CsCheck/blob/master/Tests/ShrinkingChallengeTests.cs): shrinking-challenge solutions.
## Source
- [Gen.cs](https://github.com/AnthonyLloyd/CsCheck/blob/master/CsCheck/Gen.cs): generators and combinators.
- [Check.cs](https://github.com/AnthonyLloyd/CsCheck/blob/master/CsCheck/Check.cs): Sample, Faster, Hash and related entry points.
- [Causal.cs](https://github.com/AnthonyLloyd/CsCheck/blob/master/CsCheck/Causal.cs): causal profiling.
- [NuGet package](https://www.nuget.org/packages/CsCheck/)