// `Dec` property tests (ADR 0070, ADR 0064): std cannot depend on a // package and `tests/run` compiles against std only, so the gen-based // checks of a std type live here, where `Dec` earns its keep. import gen import gen/check import std/dec.{type Dec} import std/random.{type random} import std/result import numeric /// A Dec with up to 6 integral and 18 fractional digits: `hi` whole /// units plus `lo` quanta. (A named function, not a returned lambda: a /// generator gets its caller's `random` handler, ADR 0010.) fn a_dec :: -> Dec / { let hi = gen.int ~low: (-1000000) ~high: 1000000 let lo = gen.int ~low: 0 ~high: 999999999999999999 result.expect (dec.from_scaled (hi * 1000000000000000000 + lo)) "a Dec" } fn a_dec_pair :: -> (Dec, Dec) / { (a_dec (), a_dec ()) } fn a_dec_and_places :: -> (Dec, Int) / { (a_dec (), gen.int ~low: 0 ~high: 18) } @test fn parse_undoes_show :: { check.property ~runs: 200 a_dec fn x { dec.parse (show x) == Ok(x) } } @test fn addition_is_exact_and_commutative :: { check.property ~runs: 200 a_dec fn x { x + dec.zero () == x && x - x == dec.zero () } check.property ~runs: 200 a_dec_pair fn (x, y) { x + y == y + x && (x + y) - y == x } } @test fn multiplication_by_one_and_negation :: { check.property ~runs: 200 a_dec fn x { x * dec.one () == x && -(-x) == x } } @test fn numeric_binary_round_trips_without_loss :: { check.property ~runs: 200 a_dec fn x { numeric.to_dec (numeric.from_dec x) == Ok(x) } } @test fn rounding_to_places_is_idempotent :: { check.property ~runs: 200 a_dec_and_places fn (x, places) { let r = dec.round x ~places dec.round r ~places == r } }