module scheme import math fn test_complex_source_string_radix_roundtrips_and_malformed_notation() { mut vm := new() parse := vm.lookup('string->number') or { panic(err) } format := vm.lookup('number->string') or { panic(err) } for spelling in ['1+2i', '1-2i', '+i', '-I', '+2i', '-2i', '1+i', '1-i', '1/2-3/4i', '1e-2+3e+4i', '#xA+Bi', '#e#xA+BI', '#i1+2i', '#e1.25+2.5i', '1+0i', '1+0.0i', '1-0.0i', '1#+2#i', '#e2@0', '#e0@2', '2@0', '1/2@3/4'] { source := vm.eval(spelling) or { panic('${spelling}: ${err}') } alt := vm.eval_alt(spelling) or { panic('${spelling}: ${err}') } converted := vm.call(parse, [string_value(spelling)]) or { panic(err) } assert eqv(source, converted) && eqv(source, alt), spelling for radix in [2, 8, 10, 16] { notation := vm.call(format, [source, integer(radix)]) or { panic(err) } roundtrip := vm.call(parse, [notation, integer(radix)]) or { panic(err) } assert eqv(roundtrip, source), '${spelling}/${radix}: ${notation.text()}' } } for spelling in ['i', '2i', '+', '1++2i', '1+2ii', '1+2i junk', ' 1+2i', '#e#i1+2i', '#x#x1+2i', '1@2@3', '#e1@2', '1/+inf.0i', '1++inf.0i', '#b1+inf.0i', '#e1+inf.0i', '1+nan.0ijunk', 'inf.0i'] { assert !vm.call(parse, [string_value(spelling)]) or { panic(err) }.is_true(), spelling } assert write(vm.eval('(read (open-input-string "1/2+3/4i"))') or { panic(err) }) or { panic(err) } == '1/2+3/4i' assert write(vm.eval('(define port (open-output-string)) (write 1/2+3/4i port) (display 1-2i port) (get-output-string port)') or { panic(err) }) or { panic(err) } == '"1/2+3/4i1-2i"' assert write(vm.eval('(define c 1/2+3/4i) (define first (number->string c)) (define second (number->string c)) (string-set! first 0 #\\x) (list second c)') or { panic(err) }) or { panic(err) } == '("1/2+3/4i" 1/2+3/4i)' // Selected newer numeric spellings now pass through both source readers. for spelling in ['+inf.0', '-inf.0', '+nan.0', '1+inf.0i', '-nan.0i'] { expected := vm.call(parse, [string_value(spelling)]) or { panic(err.msg()) } for value in [vm.eval(spelling) or { panic(err.msg()) }, vm.eval_alt(spelling) or { panic(err.msg()) }] { assert write(value) or { panic(err.msg()) } == write(expected) or { panic(err.msg()) }, spelling } } for radix in [2, 8, 16] { assert !vm.call(parse, [string_value('1+inf.0i'), integer(radix)]) or { panic(err) }.is_true() } } fn test_nonfinite_complex_construction_printing_predicates_and_exact_errors() { mut vm := new() parse := vm.lookup('string->number') or { panic(err) } format := vm.lookup('number->string') or { panic(err) } for spelling in ['+inf.0+i', '-inf.0-i', '1+inf.0i', '1-inf.0i', '+inf.0+2i', '-inf.0+2i', '+inf.0-nan.0i', '+nan.0+inf.0i', '+INF.0+NaN.0i', '#d#i1+inf.0i', '+inf.0i', '-nan.0i'] { value := vm.call(parse, [string_value(spelling)]) or { panic(err) } assert value.tag == .inexact_complex, spelling notation := vm.call(format, [value]) or { panic(err) } assert notation.text() == write(value) or { panic(err) } roundtrip := vm.call(parse, [notation]) or { panic(err) } assert roundtrip.tag == .inexact_complex, notation.text() z, w := inexact_complex(value) or { panic(err) }, inexact_complex(roundtrip) or { panic(err) } assert math.f64_bits(z.re) == math.f64_bits(w.re), spelling assert math.f64_bits(z.im) == math.f64_bits(w.im), spelling vm.define('z', value) or { panic(err) } assert !vm.eval('(finite? z)') or { panic(err) }.is_true() assert vm.eval('(infinite? z)') or { panic(err) }.is_true() == (math.is_inf(z.re, 0) || math.is_inf(z.im, 0)) assert vm.eval('(nan? z)') or { panic(err) }.is_true() == (math.is_nan(z.re) || math.is_nan(z.im)) if _ := vm.eval('(exact z)') { assert false } else { assert err is EvalError } if _ := vm.call(format, [value, integer(16)]) { assert false } else { assert err is EvalError } } assert write(vm.eval('(define z (* 1e308+1e308i 1e308-1e308i)) z') or { panic(err) }) or { panic(err) } == '+inf.0' for expression in ['(sqrt (make-rectangular 1 (string->number "+inf.0")))', '(log (make-rectangular (string->number "+inf.0") 1))'] { assert is_number(vm.eval(expression) or { panic(err) }) } }