| @@ -0,0 +1,300 @@ |
| 1 | +/// Property checks: run a generator many times, and when a case breaks the |
| 2 | +/// property, shrink it to a small one before reporting it. |
| 3 | +/// |
| 4 | +/// ```louise |
| 5 | +/// import gen |
| 6 | +/// import gen/check |
| 7 | +/// |
| 8 | +/// @test |
| 9 | +/// fn decoding_undoes_encoding :: |
| 10 | +/// { |
| 11 | +/// check.property ~runs: 200 events fn e { codec.decode (codec.encode e) == Ok(e) } |
| 12 | +/// } |
| 13 | +/// ``` |
| 14 | +/// |
| 15 | +/// `property` takes its seed from `random`, so in a test each run tries |
| 16 | +/// new cases; a failure reports the seed, and `seeded ~seed: …` replays it. |
| 17 | +/// |
| 18 | +/// **Shrinking** works on the bytes a case drew, not on its value |
| 19 | +/// (Hypothesis's approach): each case records the bytes `random` gave it; |
| 20 | +/// a failing case's bytes are made shorter (blocks deleted) and smaller |
| 21 | +/// (blocks zeroed, bytes made 0, halved or one less), and a change is kept |
| 22 | +/// while the generator, replaying them (zeros past the end), still makes a |
| 23 | +/// case that fails. The generators of `gen` give smaller values from |
| 24 | +/// smaller bytes, so this needs no shrinker per type. |
| 25 | + |
| 26 | +import gen |
| 27 | +import std/bytes as raw |
| 28 | +import std/list |
| 29 | +import std/prng |
| 30 | +import std/random.{type random, bytes} |
| 31 | +import std/ref |
| 32 | +import std/u64 |
| 33 | + |
| 34 | +/// The most changes a shrink tries, so a slow property stays bounded. |
| 35 | +fn budget :: -> Int |
| 36 | +{ |
| 37 | + 2000 |
| 38 | +} |
| 39 | + |
| 40 | +/// Panics, naming the seed and the shrunk case, unless `holds` is true of |
| 41 | +/// `runs` cases of `generate`. The seed comes from `random`. |
| 42 | +pub fn property :: |
| 43 | + ~runs: Int |
| 44 | + generate: fn() -> a / <random> |
| 45 | + holds: fn(a) -> Bool |
| 46 | + -> () / <random> |
| 47 | + where Debug<a> |
| 48 | +{ |
| 49 | + seeded ~seed: (random.int 1000000000) ~runs generate holds |
| 50 | +} |
| 51 | + |
| 52 | +/// [`property`] with a given seed: the same cases every run. |
| 53 | +pub fn seeded :: |
| 54 | + ~seed: Int |
| 55 | + ~runs: Int |
| 56 | + generate: fn() -> a / <random> |
| 57 | + holds: fn(a) -> Bool |
| 58 | + -> () |
| 59 | + where Debug<a> |
| 60 | +{ |
| 61 | + match failure seed runs 0 generate holds |
| 62 | + { None -> () |
| 63 | + , Some((case, original, shrunk)) -> |
| 64 | + panic |
| 65 | + "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}" |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// The shrunk case for which `holds` is false, if one of `runs` cases of |
| 70 | +/// `generate` with `seed` is one. |
| 71 | +pub fn counterexample :: |
| 72 | + ~seed: Int |
| 73 | + ~runs: Int |
| 74 | + generate: fn() -> a / <random> |
| 75 | + holds: fn(a) -> Bool |
| 76 | + -> Option<a> |
| 77 | +{ |
| 78 | + match failure seed runs 0 generate holds |
| 79 | + { None -> None, Some((_, _, shrunk)) -> Some(shrunk) } |
| 80 | +} |
| 81 | + |
| 82 | +/// The first failing case from `index` on: its number, its value and its |
| 83 | +/// shrunk value. |
| 84 | +fn failure :: |
| 85 | + seed: Int |
| 86 | + runs: Int |
| 87 | + index: Int |
| 88 | + generate: fn() -> a / <random> |
| 89 | + holds: fn(a) -> Bool |
| 90 | + -> Option<(Int, a, a)> |
| 91 | +{ |
| 92 | + if index >= runs |
| 93 | + { |
| 94 | + None |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + let (value, drawn) = recorded (seed + index) generate |
| 99 | + if holds value |
| 100 | + { |
| 101 | + failure seed runs (index + 1) generate holds |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + let smaller = shrunk drawn generate holds |
| 106 | + Some((index + 1, value, gen.from_bytes smaller generate)) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// `generate`'s value with bytes from a SplitMix64 generator seeded with |
| 112 | +/// `seed` (as `random.with_seed`), and the bytes it drew. |
| 113 | +fn recorded :: seed: Int generate: fn() -> a / <random> -> (a, List<Int>) |
| 114 | +{ |
| 115 | + region |
| 116 | + { |
| 117 | + let state = ref.new (prng.seeded seed) |
| 118 | + let drawn = ref.new [] |
| 119 | + let value = handle |
| 120 | + { |
| 121 | + generate () |
| 122 | + } |
| 123 | + with |
| 124 | + { bytes count -> |
| 125 | + { |
| 126 | + let (out, next) = draw (ref.get state) count [] |
| 127 | + ref.set state next |
| 128 | + ref.set drawn (list.append (list.reverse out) (ref.get drawn)) |
| 129 | + raw.from_list out |
| 130 | + } |
| 131 | + } |
| 132 | + (value, list.reverse (ref.get drawn)) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// `count` bytes from `g`, eight from each output, low first. |
| 137 | +fn draw :: g: prng.Prng count: Int acc: List<Int> -> (List<Int>, prng.Prng) |
| 138 | +{ |
| 139 | + if count <= 0 |
| 140 | + { |
| 141 | + (list.reverse acc, g) |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + let (x, next) = prng.next g |
| 146 | + let n = if count < 8 { count } else { 8 } |
| 147 | + draw next (count - 8) (low_bytes x n acc) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/// The `n` low bytes of `x`, lowest first, onto `acc` (reversed). |
| 152 | +fn low_bytes :: x: u64.U64 n: Int acc: List<Int> -> List<Int> |
| 153 | +{ |
| 154 | + if n == 0 |
| 155 | + { |
| 156 | + acc |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + let byte = u64.to_int (u64.and x (u64.from_int 255)) |
| 161 | + low_bytes (u64.shift_right x 8) (n - 1) [byte, ..acc] |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/// `drawn`, made as short and small as it can be while the case it makes |
| 166 | +/// still fails, within [`budget`] tries. |
| 167 | +fn shrunk :: |
| 168 | + drawn: List<Int> |
| 169 | + generate: fn() -> a / <random> |
| 170 | + holds: fn(a) -> Bool |
| 171 | + -> List<Int> |
| 172 | +{ |
| 173 | + smaller drawn (budget ()) generate holds |
| 174 | +} |
| 175 | + |
| 176 | +/// Keep the first change of `drawn` whose case still fails, and start over |
| 177 | +/// from it, until none does or `left` tries are spent. |
| 178 | +fn smaller :: |
| 179 | + drawn: List<Int> |
| 180 | + left: Int |
| 181 | + generate: fn() -> a / <random> |
| 182 | + holds: fn(a) -> Bool |
| 183 | + -> List<Int> |
| 184 | +{ |
| 185 | + match first (changes drawn) left generate holds |
| 186 | + { (Some(better), still) -> smaller better still generate holds |
| 187 | + , (None, _) -> drawn |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +/// The first of `candidates` whose case fails, cut to the bytes it read, |
| 192 | +/// and the tries left. |
| 193 | +fn first :: |
| 194 | + candidates: List<List<Int>> |
| 195 | + left: Int |
| 196 | + generate: fn() -> a / <random> |
| 197 | + holds: fn(a) -> Bool |
| 198 | + -> (Option<List<Int>>, Int) |
| 199 | +{ |
| 200 | + match candidates |
| 201 | + { [c, ..rest] if left > 0 -> |
| 202 | + { |
| 203 | + let (value, read) = gen.replay c generate |
| 204 | + if holds value |
| 205 | + { |
| 206 | + first rest (left - 1) generate holds |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + (Some(list.take c read), left - 1) |
| 211 | + } |
| 212 | + } |
| 213 | + , _ -> (None, left) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +/// The changes to try, each shorter or smaller than `drawn`: blocks of 8, |
| 218 | +/// 4, 2 and 1 bytes deleted, then zeroed, then each byte made 0, halved or |
| 219 | +/// one less. |
| 220 | +fn changes :: drawn: List<Int> -> List<List<Int>> |
| 221 | +{ |
| 222 | + let n = list.length drawn |
| 223 | + let blocks = |
| 224 | + list.flat_map |
| 225 | + [8, 4, 2, 1] |
| 226 | + fn size { list.map (starts n size) fn at { (at, size) } } |
| 227 | + let deleted = |
| 228 | + list.map |
| 229 | + blocks |
| 230 | + fn block |
| 231 | + { |
| 232 | + list.append |
| 233 | + (list.take drawn block.0) |
| 234 | + (list.drop drawn (block.0 + block.1)) |
| 235 | + } |
| 236 | + let zeroed = |
| 237 | + list.filter blocks fn block { list.any (slice drawn block) fn b { b != 0 } } |
| 238 | + |> list.map |
| 239 | + fn block |
| 240 | + { |
| 241 | + list.append |
| 242 | + (list.take drawn block.0) |
| 243 | + (list.append (zeros block.1) (list.drop drawn (block.0 + block.1))) |
| 244 | + } |
| 245 | + let lowered = list.flat_map (starts n 1) fn at { lowered_at drawn at } |
| 246 | + list.append deleted (list.append zeroed lowered) |
| 247 | +} |
| 248 | + |
| 249 | +/// Where a block of `size` fits in `n` bytes. |
| 250 | +fn starts :: n: Int size: Int -> List<Int> |
| 251 | +{ |
| 252 | + if size > n |
| 253 | + { |
| 254 | + [] |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 258 | + list.range 0 (n - size) |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +fn slice :: drawn: List<Int> block: (Int, Int) -> List<Int> |
| 263 | +{ |
| 264 | + list.take (list.drop drawn block.0) block.1 |
| 265 | +} |
| 266 | + |
| 267 | +fn zeros :: count: Int -> List<Int> |
| 268 | +{ |
| 269 | + list.map (list.range 1 count) fn _ { 0 } |
| 270 | +} |
| 271 | + |
| 272 | +/// `drawn` with its byte at `at` made 0, halved or one less: each smaller. |
| 273 | +fn lowered_at :: drawn: List<Int> at: Int -> List<List<Int>> |
| 274 | +{ |
| 275 | + match list.drop drawn at |
| 276 | + { [b, ..rest] if b > 0 -> |
| 277 | + list.map |
| 278 | + (distinct [0, b / 2, b - 1]) |
| 279 | + fn v { list.append (list.take drawn at) [v, ..rest] } |
| 280 | + , _ -> [] |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +fn distinct :: xs: List<Int> -> List<Int> |
| 285 | +{ |
| 286 | + list.fold |
| 287 | + xs |
| 288 | + [] |
| 289 | + fn kept x |
| 290 | + { |
| 291 | + if list.any kept fn k { k == x } |
| 292 | + { |
| 293 | + kept |
| 294 | + } |
| 295 | + else |
| 296 | + { |
| 297 | + list.append kept [x] |
| 298 | + } |
| 299 | + } |
| 300 | +} |