vile / scheme / complex_io_test.v
79 lines · 75 sloc · 4.09 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import math
4
5fn test_complex_source_string_radix_roundtrips_and_malformed_notation() {
6 mut vm := new()
7 parse := vm.lookup('string->number') or { panic(err) }
8 format := vm.lookup('number->string') or { panic(err) }
9 for spelling in ['1+2i', '1-2i', '+i', '-I', '+2i', '-2i', '1+i', '1-i', '1/2-3/4i', '1e-2+3e+4i',
10 '#xA+Bi', '#e#xA+BI', '#i1+2i', '#e1.25+2.5i', '1+0i', '1+0.0i', '1-0.0i', '1#+2#i', '#e2@0',
11 '#e0@2', '2@0', '1/2@3/4'] {
12 source := vm.eval(spelling) or { panic('${spelling}: ${err}') }
13 alt := vm.eval_alt(spelling) or { panic('${spelling}: ${err}') }
14 converted := vm.call(parse, [string_value(spelling)]) or { panic(err) }
15 assert eqv(source, converted) && eqv(source, alt), spelling
16 for radix in [2, 8, 10, 16] {
17 notation := vm.call(format, [source, integer(radix)]) or { panic(err) }
18 roundtrip := vm.call(parse, [notation, integer(radix)]) or { panic(err) }
19 assert eqv(roundtrip, source), '${spelling}/${radix}: ${notation.text()}'
20 }
21 }
22 for spelling in ['i', '2i', '+', '1++2i', '1+2ii', '1+2i junk', ' 1+2i', '#e#i1+2i', '#x#x1+2i',
23 '1@2@3', '#e1@2', '1/+inf.0i', '1++inf.0i', '#b1+inf.0i', '#e1+inf.0i', '1+nan.0ijunk',
24 'inf.0i'] {
25 assert !vm.call(parse, [string_value(spelling)]) or { panic(err) }.is_true(), spelling
26 }
27 assert write(vm.eval('(read (open-input-string "1/2+3/4i"))') or { panic(err) }) or { panic(err) } == '1/2+3/4i'
28 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"'
29 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)'
30
31 // Selected newer numeric spellings now pass through both source readers.
32 for spelling in ['+inf.0', '-inf.0', '+nan.0', '1+inf.0i', '-nan.0i'] {
33 expected := vm.call(parse, [string_value(spelling)]) or { panic(err.msg()) }
34 for value in [vm.eval(spelling) or { panic(err.msg()) },
35 vm.eval_alt(spelling) or { panic(err.msg()) }] {
36 assert write(value) or { panic(err.msg()) } == write(expected) or { panic(err.msg()) }, spelling
37 }
38 }
39 for radix in [2, 8, 16] {
40 assert !vm.call(parse, [string_value('1+inf.0i'), integer(radix)]) or { panic(err) }.is_true()
41 }
42}
43
44fn test_nonfinite_complex_construction_printing_predicates_and_exact_errors() {
45 mut vm := new()
46 parse := vm.lookup('string->number') or { panic(err) }
47 format := vm.lookup('number->string') or { panic(err) }
48 for spelling in ['+inf.0+i', '-inf.0-i', '1+inf.0i', '1-inf.0i', '+inf.0+2i', '-inf.0+2i',
49 '+inf.0-nan.0i', '+nan.0+inf.0i', '+INF.0+NaN.0i', '#d#i1+inf.0i', '+inf.0i', '-nan.0i'] {
50 value := vm.call(parse, [string_value(spelling)]) or { panic(err) }
51 assert value.tag == .inexact_complex, spelling
52 notation := vm.call(format, [value]) or { panic(err) }
53 assert notation.text() == write(value) or { panic(err) }
54 roundtrip := vm.call(parse, [notation]) or { panic(err) }
55 assert roundtrip.tag == .inexact_complex, notation.text()
56 z, w := inexact_complex(value) or { panic(err) }, inexact_complex(roundtrip) or { panic(err) }
57 assert math.f64_bits(z.re) == math.f64_bits(w.re), spelling
58 assert math.f64_bits(z.im) == math.f64_bits(w.im), spelling
59 vm.define('z', value) or { panic(err) }
60 assert !vm.eval('(finite? z)') or { panic(err) }.is_true()
61 assert vm.eval('(infinite? z)') or { panic(err) }.is_true() == (math.is_inf(z.re, 0) || math.is_inf(z.im, 0))
62 assert vm.eval('(nan? z)') or { panic(err) }.is_true() == (math.is_nan(z.re) || math.is_nan(z.im))
63 if _ := vm.eval('(exact z)') {
64 assert false
65 } else {
66 assert err is EvalError
67 }
68 if _ := vm.call(format, [value, integer(16)]) {
69 assert false
70 } else {
71 assert err is EvalError
72 }
73 }
74 assert write(vm.eval('(define z (* 1e308+1e308i 1e308-1e308i)) z') or { panic(err) }) or { panic(err) } == '+inf.0'
75 for expression in ['(sqrt (make-rectangular 1 (string->number "+inf.0")))',
76 '(log (make-rectangular (string->number "+inf.0") 1))'] {
77 assert is_number(vm.eval(expression) or { panic(err) })
78 }
79}
80