/// Generators: random values for tests, built on `std/random`. /// /// A generator is any `fn() -> a / `, so generators compose as /// ordinary code, and a test controls them by handling `random`: /// `random.with_seed` gives the same values on every run, and /// [`from_bytes`] replays chosen bytes. Every generator draws its choices /// from `random.bytes` so that zero bytes give its smallest value: the /// lowest Int, the shortest list, the first choice, `None`. `gen/check`'s /// shrinking relies on that. /// /// ```louise /// import gen /// /// let names = gen.list ~min: 1 ~max: 3 fn { gen.string ~alphabet: (gen.lower ()) ~min: 3 ~max: 8 } /// ``` import std/bytes as raw import std/list import std/random.{type random, bytes} import std/ref import std/string /// An Int in `low..=high`; panics if `high < low`. pub fn int :: ~low: Int ~high: Int -> Int / { random.range low high } pub fn bool :: -> Bool / { random.int 2 == 1 } /// One of `xs`; panics if there is none. pub fn pick :: xs: List -> a / { match list.drop xs (random.int (list.length xs)) { [x, ..] -> x, [] -> panic "gen.pick: nothing to pick from" } } /// One of `weighted`'s values, each chosen in proportion to its weight; /// panics unless the weights are positive. To choose between generators, /// choose a value and `match` on it: a generator stored in a list would /// not get its caller's `random` (handlers are lexical, ADR 0010). pub fn weighted :: choices: List<(Int, a)> -> a / { let total = list.fold choices 0 fn sum c { sum + c.0 } if total < 1 { panic "gen.weighted: the weights must be positive" } weighed choices (random.int total) } fn weighed :: choices: List<(Int, a)> at: Int -> a { match choices { [ (weight, value) , ..rest ] -> if at < weight { value } else { weighed rest (at - weight) } , [] -> panic "gen.weighted: the weights must be positive" } } /// `min` to `max` values of `item`. pub fn list :: ~min: Int ~max: Int item: fn() -> a / -> List / { let count = random.range min max list.map (list.range 1 count) fn _ { item () } } /// `min` to `max` characters of `alphabet`. pub fn string :: ~alphabet: String ~min: Int ~max: Int -> String / { let chars = string.chars alphabet string.concat (list ~min ~max fn { pick chars }) } /// `None` or `Some` of `item`. pub fn option :: item: fn() -> a / -> Option / { if bool () { Some(item ()) } else { None } } pub fn lower :: -> String { "abcdefghijklmnopqrstuvwxyz" } pub fn digits :: -> String { "0123456789" } pub fn alnum :: -> String { lower () <> "ABCDEFGHIJKLMNOPQRSTUVWXYZ" <> digits () } /// Printable ASCII, the space included. pub fn printable :: -> String { " !\"#$%&'()*+,-./" <> digits () <> ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" <> lower () <> "\{|\}~" } /// `generate`'s value when its random bytes are `choices`, then zeros. pub fn from_bytes :: choices: List generate: fn() -> a / -> a / { (replay choices generate).0 } /// `generate`'s value when its random bytes are `choices`, then zeros, /// and how many bytes it read: how `gen/check` tries a shrunk case. pub fn replay :: choices: List generate: fn() -> a / -> (a, Int) / { region { let rest = ref.new choices let read = ref.new 0 let value = handle { generate () } with { bytes count -> { let taken = list.take (ref.get rest) count ref.set rest (list.drop (ref.get rest) count) ref.set read (ref.get read + count) raw.from_list (list.append taken (zeros (count - list.length taken))) } } (value, ref.get read) } } fn zeros :: count: Int -> List { if count <= 0 { [] } else { list.map (list.range 1 count) fn _ { 0 } } }