packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /// Generators: random values for tests, built on `std/random`. |
| 2 | /// |
| 3 | /// A generator is any `fn() -> a / <random>`, so generators compose as |
| 4 | /// ordinary code, and a test controls them by handling `random`: |
| 5 | /// `random.with_seed` gives the same values on every run, and |
| 6 | /// [`from_bytes`] replays chosen bytes. Every generator draws its choices |
| 7 | /// from `random.bytes` so that zero bytes give its smallest value: the |
| 8 | /// lowest Int, the shortest list, the first choice, `None`. `gen/check`'s |
| 9 | /// shrinking relies on that. |
| 10 | /// |
| 11 | /// ```louise |
| 12 | /// import gen |
| 13 | /// |
| 14 | /// let names = gen.list ~min: 1 ~max: 3 fn { gen.string ~alphabet: (gen.lower ()) ~min: 3 ~max: 8 } |
| 15 | /// ``` |
| 16 | |
| 17 | import std/bytes as raw |
| 18 | import std/list |
| 19 | import std/random.{type random, bytes} |
| 20 | import std/ref |
| 21 | import std/string |
| 22 | |
| 23 | /// An Int in `low..=high`; panics if `high < low`. |
| 24 | pub fn int :: ~low: Int ~high: Int -> Int / <random> |
| 25 | { |
| 26 | random.range low high |
| 27 | } |
| 28 | |
| 29 | pub fn bool :: -> Bool / <random> |
| 30 | { |
| 31 | random.int 2 == 1 |
| 32 | } |
| 33 | |
| 34 | /// One of `xs`; panics if there is none. |
| 35 | pub fn pick :: xs: List<a> -> a / <random> |
| 36 | { |
| 37 | match list.drop xs (random.int (list.length xs)) |
| 38 | { [x, ..] -> x, [] -> panic "gen.pick: nothing to pick from" } |
| 39 | } |
| 40 | |
| 41 | /// One of `weighted`'s values, each chosen in proportion to its weight; |
| 42 | /// panics unless the weights are positive. To choose between generators, |
| 43 | /// choose a value and `match` on it: a generator stored in a list would |
| 44 | /// not get its caller's `random` (handlers are lexical, ADR 0010). |
| 45 | pub fn weighted :: choices: List<(Int, a)> -> a / <random> |
| 46 | { |
| 47 | let total = list.fold choices 0 fn sum c { sum + c.0 } |
| 48 | if total < 1 |
| 49 | { |
| 50 | panic "gen.weighted: the weights must be positive" |
| 51 | } |
| 52 | weighed choices (random.int total) |
| 53 | } |
| 54 | |
| 55 | fn weighed :: choices: List<(Int, a)> at: Int -> a |
| 56 | { |
| 57 | match choices |
| 58 | { [ (weight, value) |
| 59 | , ..rest |
| 60 | ] -> if at < weight { value } else { weighed rest (at - weight) } |
| 61 | , [] -> panic "gen.weighted: the weights must be positive" |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// `min` to `max` values of `item`. |
| 66 | pub fn list :: |
| 67 | ~min: Int |
| 68 | ~max: Int |
| 69 | item: fn() -> a / <random> |
| 70 | -> List<a> / <random> |
| 71 | { |
| 72 | let count = random.range min max |
| 73 | list.map (list.range 1 count) fn _ { item () } |
| 74 | } |
| 75 | |
| 76 | /// `min` to `max` characters of `alphabet`. |
| 77 | pub fn string :: ~alphabet: String ~min: Int ~max: Int -> String / <random> |
| 78 | { |
| 79 | let chars = string.chars alphabet |
| 80 | string.concat (list ~min ~max fn { pick chars }) |
| 81 | } |
| 82 | |
| 83 | /// `None` or `Some` of `item`. |
| 84 | pub fn option :: item: fn() -> a / <random> -> Option<a> / <random> |
| 85 | { |
| 86 | if bool () |
| 87 | { |
| 88 | Some(item ()) |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | None |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn lower :: -> String |
| 97 | { |
| 98 | "abcdefghijklmnopqrstuvwxyz" |
| 99 | } |
| 100 | |
| 101 | pub fn digits :: -> String |
| 102 | { |
| 103 | "0123456789" |
| 104 | } |
| 105 | |
| 106 | pub fn alnum :: -> String |
| 107 | { |
| 108 | lower () <> "ABCDEFGHIJKLMNOPQRSTUVWXYZ" <> digits () |
| 109 | } |
| 110 | |
| 111 | /// Printable ASCII, the space included. |
| 112 | pub fn printable :: -> String |
| 113 | { |
| 114 | " !\"#$%&'()*+,-./" <> digits () <> ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" |
| 115 | <> lower () |
| 116 | <> "\{|\}~" |
| 117 | } |
| 118 | |
| 119 | /// `generate`'s value when its random bytes are `choices`, then zeros. |
| 120 | pub fn from_bytes :: |
| 121 | choices: List<Int> |
| 122 | generate: fn() -> a / <random | e> |
| 123 | -> a / <e> |
| 124 | { |
| 125 | (replay choices generate).0 |
| 126 | } |
| 127 | |
| 128 | /// `generate`'s value when its random bytes are `choices`, then zeros, |
| 129 | /// and how many bytes it read: how `gen/check` tries a shrunk case. |
| 130 | pub fn replay :: |
| 131 | choices: List<Int> |
| 132 | generate: fn() -> a / <random | e> |
| 133 | -> (a, Int) / <e> |
| 134 | { |
| 135 | region |
| 136 | { |
| 137 | let rest = ref.new choices |
| 138 | let read = ref.new 0 |
| 139 | let value = handle |
| 140 | { |
| 141 | generate () |
| 142 | } |
| 143 | with |
| 144 | { bytes count -> |
| 145 | { |
| 146 | let taken = list.take (ref.get rest) count |
| 147 | ref.set rest (list.drop (ref.get rest) count) |
| 148 | ref.set read (ref.get read + count) |
| 149 | raw.from_list (list.append taken (zeros (count - list.length taken))) |
| 150 | } |
| 151 | } |
| 152 | (value, ref.get read) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | fn zeros :: count: Int -> List<Int> |
| 157 | { |
| 158 | if count <= 0 |
| 159 | { |
| 160 | [] |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | list.map (list.range 1 count) fn _ { 0 } |
| 165 | } |
| 166 | } |