| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import math |
| 5 | |
| 6 | fn arithmetic_text(mut vm VM, source string) string { |
| 7 | return write(vm.eval(source) or { panic('${source}: ${err}') }) or { panic(err) } |
| 8 | } |
| 9 | |
| 10 | fn test_arithmetic_library_aliases_import_sets_availability_and_pristine_identity() { |
| 11 | mut vm := new() |
| 12 | assert arithmetic_text(mut vm, '(cond-expand |
| 13 | ((and (library (rnrs arithmetic fixnums)) (library (rnrs arithmetic flonums)) |
| 14 | (library (rnrs arithmetic bitwise))) 42) (else 0))') == '42' |
| 15 | for library in ['fixnums', 'flonums', 'bitwise'] { |
| 16 | for collection in ['rnrs arithmetic ${library}', 'vile arithmetic ${library}', |
| 17 | 'vile ${library}'] { |
| 18 | names := selected_library_export_names(library) or { panic(library) } |
| 19 | vm.eval('(import (prefix (${collection}) t:))') or { panic('${collection}: ${err}') } |
| 20 | for name in names.split(' ') { |
| 21 | assert eqv(vm.lookup(name) or { panic(err) }, vm.lookup('t:${name}') or { panic(err) }), name |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | original := vm.lookup('fx+') or { panic(err) } |
| 26 | vm.define('fx+', integer(0)) or { panic(err) } |
| 27 | vm.eval('(import (rename (only (rnrs arithmetic fixnums) fx+) (fx+ sum-fx)))') or { panic(err) } |
| 28 | assert eqv(original, vm.lookup('sum-fx') or { panic(err) }) |
| 29 | assert arithmetic_text(mut vm, '(sum-fx 20 22)') == '42' |
| 30 | for source in ['(import (rnrs arithmetic unknown))', '(import (rnrs fixnums))', |
| 31 | '(import (rnrs fixnums extra))', '(import (rnrs arithmetic fixnums (6)))'] { |
| 32 | if _ := vm.eval(source) { |
| 33 | assert false, source |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn test_arithmetic_alt_frontend_uses_shared_procedures_with_safe_names() { |
| 39 | mut vm := new() |
| 40 | for alias, name in { |
| 41 | 'fx-add': 'fx+' |
| 42 | 'fx-multiply': 'fx*' |
| 43 | 'fx-carry': 'fx+/carry' |
| 44 | 'fl-divide': 'fl/' |
| 45 | } { |
| 46 | vm.define(alias, vm.lookup(name) or { panic(err) }) or { panic(err) } |
| 47 | } |
| 48 | assert write(vm.eval_alt('n:=bitwise-arithmetic-shift(1,130)\n' + |
| 49 | '[fx-add(20,22),fx-multiply(6,7),bitwise-bit-field(n+17,0,5),fx<=?(1,2),fl-divide(1.0,0.0)]') or { panic(err) }) or { panic(err) } == '#(42 42 17 #t +inf.0)' |
| 50 | assert write(vm.eval_alt('flfloor(-1.5)') or { panic(err) }) or { panic(err) } == '-2.0' |
| 51 | } |
| 52 | |
| 53 | fn test_arithmetic_multiple_values_tail_calls_multishot_continuations_and_recovery() { |
| 54 | mut vm := new_with_options(Options{ max_steps: 0, max_frames: 50 }) or { panic(err) } |
| 55 | assert arithmetic_text(mut vm, '(let loop ((n 5000) (sum 0)) |
| 56 | (if (fxzero? n) sum (loop (fx- n 1) (fx+ sum 1))))') == '5000' |
| 57 | assert arithmetic_text(mut vm, '(define saved #f) |
| 58 | (call-with-values (lambda () (fx+/carry |
| 59 | (call/cc (lambda (k) (set! saved k) 0)) (greatest-fixnum) 0)) list)') == '(9223372036854775807 0)' |
| 60 | assert arithmetic_text(mut vm, '(saved 1)') == '(-9223372036854775808 1)' |
| 61 | assert arithmetic_text(mut vm, '(saved -1)') == '(9223372036854775806 0)' |
| 62 | assert arithmetic_text(mut vm, '(call-with-values (lambda () (fldiv0-and-mod0 -3.0 -2.0)) list)') == '(1.0 -1.0)' |
| 63 | for source in ['(fx+ (greatest-fixnum) 1)', '(flfloor 1)', '(bitwise-and 1/2 1)'] { |
| 64 | if _ := vm.eval(source) { |
| 65 | assert false, source |
| 66 | } |
| 67 | assert vm.active.len == 0 |
| 68 | assert arithmetic_text(mut vm, '(fx+ 20 22)') == '42' |
| 69 | } |
| 70 | assert vm.active.len == 0 |
| 71 | } |
| 72 | |
| 73 | fn arithmetic_owned_region(region &arena.Arena) string { |
| 74 | region.push() |
| 75 | defer { region.pop() } |
| 76 | mut vm := new() |
| 77 | value := vm.eval('(bitwise-copy-bit-field (bitwise-arithmetic-shift -1 130) 0 64 42)') or { panic(err) } |
| 78 | return write(value) or { panic(err) } |
| 79 | } |
| 80 | |
| 81 | fn test_arithmetic_source_release_owned_regions_and_cross_vm_native_ownership() { |
| 82 | mut vm := new() |
| 83 | mut source := '(define (worker x) (bitwise-xor x 1361129467683753853853498429727072845824))'.clone() |
| 84 | vm.eval(source) or { panic(err) } |
| 85 | unsafe { source.free() } |
| 86 | gc_collect() |
| 87 | assert arithmetic_text(mut vm, '(worker 17)') == '1361129467683753853853498429727072845841' |
| 88 | mut other := new() |
| 89 | if _ := other.call(vm.lookup('fx+') or { panic(err) }, [integer(20), integer(22)]) { |
| 90 | assert false |
| 91 | } |
| 92 | mut region := arena.new(chunk_size: 1024) |
| 93 | defer { region.free() } |
| 94 | for _ in 0 .. 4 { |
| 95 | borrowed := arithmetic_owned_region(®ion) |
| 96 | owned := borrowed.clone() |
| 97 | region.reset() |
| 98 | assert owned == '-1361129467683753853853498429727072845782' |
| 99 | unsafe { owned.free() } |
| 100 | } |
| 101 | value := vm.eval('(flsqrt -0.0)') or { panic(err) } |
| 102 | assert math.f64_bits(value.floating()) == u64(1) << 63 |
| 103 | } |
| 104 | |
| 105 | fn arithmetic_native_reentry(mut vm VM, args []Value) !Value { |
| 106 | check_arity('host-arithmetic', args.len, 0, 0)! |
| 107 | return vm.eval('(call-with-values (lambda () (fx*/carry 10 4 2)) (lambda (low high) (fx+ low high)))') |
| 108 | } |
| 109 | |
| 110 | fn test_arithmetic_native_reentry_budget_and_exit_use_existing_machine_protocol() { |
| 111 | mut vm := new() |
| 112 | vm.register('host-arithmetic', arithmetic_native_reentry) or { panic(err) } |
| 113 | assert arithmetic_text(mut vm, '(fx+ 1 (host-arithmetic))') == '43' |
| 114 | assert vm.active.len == 0 |
| 115 | if _ := vm.eval('(define wind-result 0) (dynamic-wind |
| 116 | (lambda () (set! wind-result (fx+ wind-result 20))) |
| 117 | (lambda () (exit 7)) |
| 118 | (lambda () (set! wind-result (fx+ wind-result 22))))') { |
| 119 | assert false |
| 120 | } else { |
| 121 | assert err is ExitRequest |
| 122 | } |
| 123 | assert arithmetic_text(mut vm, 'wind-result') == '42' |
| 124 | mut bounded := new_with_options(Options{ max_steps: 80 }) or { panic(err) } |
| 125 | if _ := bounded.eval('(let loop ((x 0)) (loop (fx+ x 1)))') { |
| 126 | assert false |
| 127 | } |
| 128 | assert bounded.active.len == 0 |
| 129 | assert arithmetic_text(mut bounded, '(fx+ 20 22)') == '42' |
| 130 | } |
| 131 | |
| 132 | fn test_exact_bitwise_storage_checks_reserve_intermediate_headroom_without_allocating_huge_values() { |
| 133 | assert arithmetic_max_bits + 1 == 2147483647 |
| 134 | assert u64(arithmetic_max_bits + 1) < u64(0xffffffff) |
| 135 | bitwise_growth(arithmetic_max_bits, 0) or { panic(err) } |
| 136 | bitwise_growth(arithmetic_max_bits - 1, 1) or { panic(err) } |
| 137 | for pair in [[arithmetic_max_bits, 1], [0, arithmetic_max_bits + 1], [arithmetic_max_bits + 1, |
| 138 | 0]] { |
| 139 | if _ := bitwise_growth(pair[0], pair[1]) { |
| 140 | assert false |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |