vile / scheme / integer_dispatch_budget_test.v
47 lines · 44 sloc · 1.44 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn numeric_budget_observation(mut vm VM, source string, budget int) !Value {
6 options := vm.options
7 vm.options = Options{ ...options, max_steps: budget }
8 return vm.eval(source)
9}
10
11// Bounded VMs retain the complete primitive dispatcher. Compare its exact
12// budget/error behavior with the ordinary integer path, including promotion
13// and calls that must fall back rather than charging their invocation twice.
14fn test_integer_dispatch_keeps_the_complete_dispatchers_budget_boundaries() {
15 mut plain := new()
16 mut bounded := new_bounded(Options{}, arena.ChunkLimits{ max_live_bytes: 16 * 1024 * 1024 })!
17 defer { bounded.close() or { panic(err) } }
18 for source in [
19 '(+ 40 2)',
20 '(list (+ 40 2) (- 44 2))',
21 '(let ((f +)) (f 40 2))',
22 '(+ 9223372036854775807 1)',
23 '(/ 1 0)',
24 ] {
25 for budget in 0 .. 40 {
26 mut plain_error := ?IError(none)
27 want := numeric_budget_observation(mut plain, source, budget) or {
28 plain_error = err
29 unspecified()
30 }
31 mut bounded_error := ?IError(none)
32 got := bounded.run(fn [source, budget] (mut vm VM) !Value {
33 return numeric_budget_observation(mut vm, source, budget)
34 }) or {
35 bounded_error = err
36 unspecified()
37 }
38 if expected := plain_error {
39 actual := bounded_error or { panic('bounded dispatcher accepted a denied call') }
40 assert expected.msg() == actual.msg()
41 } else {
42 assert bounded_error == none
43 assert write(want)! == write(got)!
44 }
45 }
46 }
47}
48