vile / scheme / integer_fold_test.v
69 lines · 64 sloc · 3.16 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import os
4
5fn test_variadic_exact_arithmetic_keeps_representable_results_after_prefix_overflow() {
6 mut vm := new()
7 for source, expected in {
8 '(+ 9223372036854775807 1 -1)': i64(9223372036854775807)
9 '(+ -9223372036854775808 -1 1)': exact_min
10 '(+ 9223372036854775807 9223372036854775807 -9223372036854775808)': i64(9223372036854775806)
11 '(+ -9223372036854775808 -9223372036854775808 9223372036854775807 9223372036854775807)': i64(-2)
12 '(- 9223372036854775807 -1 1)': exact_max
13 '(- -9223372036854775808 1 -1)': exact_min
14 '(- 0 -9223372036854775808 1)': exact_max
15 '(- -1 9223372036854775807 -9223372036854775808)': i64(0)
16 '(* 9223372036854775807 9223372036854775807 0)': i64(0)
17 '(* -9223372036854775808 -1 -1)': exact_min
18 '(* -1 -9223372036854775808 -1)': exact_min
19 '(* -9223372036854775808 -1 -1 1 1)': exact_min
20 } {
21 answer := vm.eval(source) or { panic('${source}: ${err}') }
22 assert answer.tag == .integer
23 assert answer.whole() == expected, source
24 }
25}
26
27fn test_variadic_overflow_recovery_still_rejects_invalid_types() {
28 mut vm := new()
29 for source in ['(* 9223372036854775807 2 0 #f)', '(+ 9223372036854775807 1 -1 #f)'] {
30 if answer := vm.eval(source) {
31 assert false, '${source} returned ${answer.kind()}'
32 }
33 assert vm.active.len == 0
34 }
35}
36
37fn test_wide_sum_handles_long_positive_and_negative_runs() {
38 mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) }
39 plus := vm.lookup('+') or { panic(err) }
40 minus := vm.lookup('-') or { panic(err) }
41 mut args := []Value{len: 20001, init: integer(exact_max)}
42 for i in 10000 .. 20000 { args[i] = integer(-exact_max) }
43 args[20000] = integer(42)
44 assert vm.call(plus, args) or { panic(err) }.whole() == 42
45 for i in 0 .. 10000 { args[i] = integer(-exact_max) }
46 for i in 10000 .. 20000 { args[i] = integer(exact_max) }
47 assert vm.call(plus, args) or { panic(err) }.whole() == 42
48 args[0] = integer(0)
49 args[20000] = integer(-42)
50 // Removing one negative maximum from the earlier run leaves +max in the
51 // subtrahends, so the final difference is -max + 42.
52 assert vm.call(minus, args) or { panic(err) }.whole() == -exact_max + 42
53}
54
55fn test_variadic_exact_folds_match_an_independent_arbitrary_integer_oracle() {
56 fixture := os.read_file(@VMODROOT + '/scheme/testdata/integer_folds.tsv') or { panic(err) }
57 mut vm := new()
58 mut count := 0
59 for row in fixture.split_into_lines() {
60 if row.len == 0 || row[0] == `#` { continue }
61 fields := row.split('\t')
62 assert fields.len == 2
63 answer := vm.eval(fields[0]) or { panic('${fields[0]}: ${err}') }
64 assert is_exact_integer(answer)
65 assert write(answer) or { panic(err) } == fields[1], fields[0]
66 count++
67 }
68 assert count == 450
69}
70