| 1 | module scheme |
| 2 | |
| 3 | fn test_exact_boundaries_and_normalization() { |
| 4 | assert rational(exact_min, -2) or { panic(err) }.as_integer() or { panic(err) } == 4611686018427387904 |
| 5 | assert rational(exact_min, exact_min) or { panic(err) }.as_integer() or { panic(err) } == 1 |
| 6 | assert rational(0, exact_min) or { panic(err) }.as_integer() or { panic(err) } == 0 |
| 7 | assert number_text('(- -9223372036854775808 -9223372036854775808)') == '0' |
| 8 | assert number_text('(/ -9223372036854775808 -9223372036854775808)') == '1' |
| 9 | assert number_text('(* -9223372036854775808 0)') == '0' |
| 10 | for source, expected in { |
| 11 | '(- 9223372036854775807 -1)': '9223372036854775808' |
| 12 | '(- -9223372036854775808 1)': '-9223372036854775809' |
| 13 | '(* -9223372036854775808 -1)': '9223372036854775808' |
| 14 | '(* -9223372036854775808 2)': '-18446744073709551616' |
| 15 | '(* -9223372036854775808 -2)': '18446744073709551616' |
| 16 | '(/ -9223372036854775808 -1)': '9223372036854775808' |
| 17 | } { |
| 18 | assert number_text(source) == expected |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn test_small_rational_arithmetic_matches_independent_integer_identities() { |
| 23 | for an in -9 .. 10 { |
| 24 | for bn in -9 .. 10 { |
| 25 | for ad in 1 .. 5 { |
| 26 | for bd in 1 .. 5 { |
| 27 | a := rational(i64(an), i64(ad)) or { panic(err) } |
| 28 | b := rational(i64(bn), i64(bd)) or { panic(err) } |
| 29 | expected_sum := rational(i64(an * bd + bn * ad), i64(ad * bd)) or { panic(err) } |
| 30 | expected_difference := rational(i64(an * bd - bn * ad), i64(ad * bd)) or { panic(err) } |
| 31 | expected_product := rational(i64(an * bn), i64(ad * bd)) or { panic(err) } |
| 32 | assert eqv(numeric_add(a, b) or { panic(err) }, expected_sum) |
| 33 | assert eqv(numeric_subtract(a, b) or { panic(err) }, expected_difference) |
| 34 | assert eqv(numeric_multiply(a, b) or { panic(err) }, expected_product) |
| 35 | left, right := an * bd, bn * ad |
| 36 | expected_order := if left < right { |
| 37 | -1 |
| 38 | } else if left > right { |
| 39 | 1 |
| 40 | } else { |
| 41 | 0 |
| 42 | } |
| 43 | assert numeric_compare(a, b) or { panic(err) } == expected_order |
| 44 | if bn != 0 { |
| 45 | expected_quotient := rational(i64(an * bd), i64(ad * bn)) or { panic(err) } |
| 46 | assert eqv(numeric_divide(a, b) or { panic(err) }, expected_quotient) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn number_text(source string) string { |
| 55 | mut vm := new() |
| 56 | value := vm.eval(source) or { panic(err) } |
| 57 | return write(value) or { panic(err) } |
| 58 | } |
| 59 | |