/// Property checks: run a generator many times, and when a case breaks the /// property, shrink it to a small one before reporting it. /// /// ```louise /// import gen /// import gen/check /// /// @test /// fn decoding_undoes_encoding :: /// { /// check.property ~runs: 200 events fn e { codec.decode (codec.encode e) == Ok(e) } /// } /// ``` /// /// `property` takes its seed from `random`, so in a test each run tries /// new cases; a failure reports the seed, and `seeded ~seed: …` replays it. /// /// **Shrinking** works on the bytes a case drew, not on its value /// (Hypothesis's approach): each case records the bytes `random` gave it; /// a failing case's bytes are made shorter (blocks deleted) and smaller /// (blocks zeroed, bytes made 0, halved or one less), and a change is kept /// while the generator, replaying them (zeros past the end), still makes a /// case that fails. The generators of `gen` give smaller values from /// smaller bytes, so this needs no shrinker per type. import gen import std/bytes as raw import std/list import std/prng import std/random.{type random, bytes} import std/ref import std/u64 /// The most changes a shrink tries, so a slow property stays bounded. fn budget :: -> Int { 2000 } /// Panics, naming the seed and the shrunk case, unless `holds` is true of /// `runs` cases of `generate`. The seed comes from `random`. pub fn property :: ~runs: Int generate: fn() -> a / holds: fn(a) -> Bool -> () / where Debug { seeded ~seed: (random.int 1000000000) ~runs generate holds } /// [`property`] with a given seed: the same cases every run. pub fn seeded :: ~seed: Int ~runs: Int generate: fn() -> a / holds: fn(a) -> Bool -> () where Debug { match failure seed runs 0 generate holds { None -> () , Some((case, original, shrunk)) -> panic "property does not hold (seed {seed}, case {case} of {runs}):\n {debug shrunk}\nshrunk from:\n {debug original}\nreplay: check.seeded ~seed: {seed} ~runs: {runs}" } } /// The shrunk case for which `holds` is false, if one of `runs` cases of /// `generate` with `seed` is one. pub fn counterexample :: ~seed: Int ~runs: Int generate: fn() -> a / holds: fn(a) -> Bool -> Option { match failure seed runs 0 generate holds { None -> None, Some((_, _, shrunk)) -> Some(shrunk) } } /// The first failing case from `index` on: its number, its value and its /// shrunk value. fn failure :: seed: Int runs: Int index: Int generate: fn() -> a / holds: fn(a) -> Bool -> Option<(Int, a, a)> { if index >= runs { None } else { let (value, drawn) = recorded (seed + index) generate if holds value { failure seed runs (index + 1) generate holds } else { let smaller = shrunk drawn generate holds Some((index + 1, value, gen.from_bytes smaller generate)) } } } /// `generate`'s value with bytes from a SplitMix64 generator seeded with /// `seed` (as `random.with_seed`), and the bytes it drew. fn recorded :: seed: Int generate: fn() -> a / -> (a, List) { region { let state = ref.new (prng.seeded seed) let drawn = ref.new [] let value = handle { generate () } with { bytes count -> { let (out, next) = draw (ref.get state) count [] ref.set state next ref.set drawn (list.append (list.reverse out) (ref.get drawn)) raw.from_list out } } (value, list.reverse (ref.get drawn)) } } /// `count` bytes from `g`, eight from each output, low first. fn draw :: g: prng.Prng count: Int acc: List -> (List, prng.Prng) { if count <= 0 { (list.reverse acc, g) } else { let (x, next) = prng.next g let n = if count < 8 { count } else { 8 } draw next (count - 8) (low_bytes x n acc) } } /// The `n` low bytes of `x`, lowest first, onto `acc` (reversed). fn low_bytes :: x: u64.U64 n: Int acc: List -> List { if n == 0 { acc } else { let byte = u64.to_int (u64.and x (u64.from_int 255)) low_bytes (u64.shift_right x 8) (n - 1) [byte, ..acc] } } /// `drawn`, made as short and small as it can be while the case it makes /// still fails, within [`budget`] tries. fn shrunk :: drawn: List generate: fn() -> a / holds: fn(a) -> Bool -> List { smaller drawn (budget ()) generate holds } /// Keep the first change of `drawn` whose case still fails, and start over /// from it, until none does or `left` tries are spent. fn smaller :: drawn: List left: Int generate: fn() -> a / holds: fn(a) -> Bool -> List { match first (changes drawn) left generate holds { (Some(better), still) -> smaller better still generate holds , (None, _) -> drawn } } /// The first of `candidates` whose case fails, cut to the bytes it read, /// and the tries left. fn first :: candidates: List> left: Int generate: fn() -> a / holds: fn(a) -> Bool -> (Option>, Int) { match candidates { [c, ..rest] if left > 0 -> { let (value, read) = gen.replay c generate if holds value { first rest (left - 1) generate holds } else { (Some(list.take c read), left - 1) } } , _ -> (None, left) } } /// The changes to try, each shorter or smaller than `drawn`: blocks of 8, /// 4, 2 and 1 bytes deleted, then zeroed, then each byte made 0, halved or /// one less. fn changes :: drawn: List -> List> { let n = list.length drawn let blocks = list.flat_map [8, 4, 2, 1] fn size { list.map (starts n size) fn at { (at, size) } } let deleted = list.map blocks fn block { list.append (list.take drawn block.0) (list.drop drawn (block.0 + block.1)) } let zeroed = list.filter blocks fn block { list.any (slice drawn block) fn b { b != 0 } } |> list.map fn block { list.append (list.take drawn block.0) (list.append (zeros block.1) (list.drop drawn (block.0 + block.1))) } let lowered = list.flat_map (starts n 1) fn at { lowered_at drawn at } list.append deleted (list.append zeroed lowered) } /// Where a block of `size` fits in `n` bytes. fn starts :: n: Int size: Int -> List { if size > n { [] } else { list.range 0 (n - size) } } fn slice :: drawn: List block: (Int, Int) -> List { list.take (list.drop drawn block.0) block.1 } fn zeros :: count: Int -> List { list.map (list.range 1 count) fn _ { 0 } } /// `drawn` with its byte at `at` made 0, halved or one less: each smaller. fn lowered_at :: drawn: List at: Int -> List> { match list.drop drawn at { [b, ..rest] if b > 0 -> list.map (distinct [0, b / 2, b - 1]) fn v { list.append (list.take drawn at) [v, ..rest] } , _ -> [] } } fn distinct :: xs: List -> List { list.fold xs [] fn kept x { if list.any kept fn k { k == x } { kept } else { list.append kept [x] } } }