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 | // `Dec` property tests (ADR 0070, ADR 0064): std cannot depend on a |
| 2 | // package and `tests/run` compiles against std only, so the gen-based |
| 3 | // checks of a std type live here, where `Dec` earns its keep. |
| 4 | |
| 5 | import gen |
| 6 | import gen/check |
| 7 | import std/dec.{type Dec} |
| 8 | import std/random.{type random} |
| 9 | import std/result |
| 10 | |
| 11 | import numeric |
| 12 | |
| 13 | /// A Dec with up to 6 integral and 18 fractional digits: `hi` whole |
| 14 | /// units plus `lo` quanta. (A named function, not a returned lambda: a |
| 15 | /// generator gets its caller's `random` handler, ADR 0010.) |
| 16 | fn a_dec :: -> Dec / <random> |
| 17 | { |
| 18 | let hi = gen.int ~low: (-1000000) ~high: 1000000 |
| 19 | let lo = gen.int ~low: 0 ~high: 999999999999999999 |
| 20 | result.expect (dec.from_scaled (hi * 1000000000000000000 + lo)) "a Dec" |
| 21 | } |
| 22 | |
| 23 | fn a_dec_pair :: -> (Dec, Dec) / <random> |
| 24 | { |
| 25 | (a_dec (), a_dec ()) |
| 26 | } |
| 27 | |
| 28 | fn a_dec_and_places :: -> (Dec, Int) / <random> |
| 29 | { |
| 30 | (a_dec (), gen.int ~low: 0 ~high: 18) |
| 31 | } |
| 32 | |
| 33 | @test |
| 34 | fn parse_undoes_show :: |
| 35 | { |
| 36 | check.property ~runs: 200 a_dec fn x { dec.parse (show x) == Ok(x) } |
| 37 | } |
| 38 | |
| 39 | @test |
| 40 | fn addition_is_exact_and_commutative :: |
| 41 | { |
| 42 | check.property |
| 43 | ~runs: 200 |
| 44 | a_dec |
| 45 | fn x { x + dec.zero () == x && x - x == dec.zero () } |
| 46 | check.property |
| 47 | ~runs: 200 |
| 48 | a_dec_pair |
| 49 | fn (x, y) { x + y == y + x && (x + y) - y == x } |
| 50 | } |
| 51 | |
| 52 | @test |
| 53 | fn multiplication_by_one_and_negation :: |
| 54 | { |
| 55 | check.property ~runs: 200 a_dec fn x { x * dec.one () == x && -(-x) == x } |
| 56 | } |
| 57 | |
| 58 | @test |
| 59 | fn numeric_binary_round_trips_without_loss :: |
| 60 | { |
| 61 | check.property |
| 62 | ~runs: 200 |
| 63 | a_dec |
| 64 | fn x { numeric.to_dec (numeric.from_dec x) == Ok(x) } |
| 65 | } |
| 66 | |
| 67 | @test |
| 68 | fn rounding_to_places_is_idempotent :: |
| 69 | { |
| 70 | check.property |
| 71 | ~runs: 200 |
| 72 | a_dec_and_places |
| 73 | fn (x, places) |
| 74 | { |
| 75 | let r = dec.round x ~places |
| 76 | dec.round r ~places == r |
| 77 | } |
| 78 | } |