module scheme import os fn test_variadic_exact_arithmetic_keeps_representable_results_after_prefix_overflow() { mut vm := new() for source, expected in { '(+ 9223372036854775807 1 -1)': i64(9223372036854775807) '(+ -9223372036854775808 -1 1)': exact_min '(+ 9223372036854775807 9223372036854775807 -9223372036854775808)': i64(9223372036854775806) '(+ -9223372036854775808 -9223372036854775808 9223372036854775807 9223372036854775807)': i64(-2) '(- 9223372036854775807 -1 1)': exact_max '(- -9223372036854775808 1 -1)': exact_min '(- 0 -9223372036854775808 1)': exact_max '(- -1 9223372036854775807 -9223372036854775808)': i64(0) '(* 9223372036854775807 9223372036854775807 0)': i64(0) '(* -9223372036854775808 -1 -1)': exact_min '(* -1 -9223372036854775808 -1)': exact_min '(* -9223372036854775808 -1 -1 1 1)': exact_min } { answer := vm.eval(source) or { panic('${source}: ${err}') } assert answer.tag == .integer assert answer.whole() == expected, source } } fn test_variadic_overflow_recovery_still_rejects_invalid_types() { mut vm := new() for source in ['(* 9223372036854775807 2 0 #f)', '(+ 9223372036854775807 1 -1 #f)'] { if answer := vm.eval(source) { assert false, '${source} returned ${answer.kind()}' } assert vm.active.len == 0 } } fn test_wide_sum_handles_long_positive_and_negative_runs() { mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) } plus := vm.lookup('+') or { panic(err) } minus := vm.lookup('-') or { panic(err) } mut args := []Value{len: 20001, init: integer(exact_max)} for i in 10000 .. 20000 { args[i] = integer(-exact_max) } args[20000] = integer(42) assert vm.call(plus, args) or { panic(err) }.whole() == 42 for i in 0 .. 10000 { args[i] = integer(-exact_max) } for i in 10000 .. 20000 { args[i] = integer(exact_max) } assert vm.call(plus, args) or { panic(err) }.whole() == 42 args[0] = integer(0) args[20000] = integer(-42) // Removing one negative maximum from the earlier run leaves +max in the // subtrahends, so the final difference is -max + 42. assert vm.call(minus, args) or { panic(err) }.whole() == -exact_max + 42 } fn test_variadic_exact_folds_match_an_independent_arbitrary_integer_oracle() { fixture := os.read_file(@VMODROOT + '/scheme/testdata/integer_folds.tsv') or { panic(err) } mut vm := new() mut count := 0 for row in fixture.split_into_lines() { if row.len == 0 || row[0] == `#` { continue } fields := row.split('\t') assert fields.len == 2 answer := vm.eval(fields[0]) or { panic('${fields[0]}: ${err}') } assert is_exact_integer(answer) assert write(answer) or { panic(err) } == fields[1], fields[0] count++ } assert count == 450 }