vile / scheme / complex_oracle_test.v
35 lines · 32 sloc · 1.25 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import math
4import strconv
5
6fn complex_near(actual f64, expected f64) bool {
7 if actual == expected { return true }
8 if math.is_nan(actual) || math.is_inf(actual, 0) { return false }
9 // Relative tolerance with a subnormal absolute floor; an expected zero
10 // cannot hide a branch residue or an entirely lost tiny component.
11 return math.abs(actual - expected) <= math.max(math.abs(expected) * 4e-13, math.f64_from_bits(8))
12}
13
14fn test_independent_fraction_and_cmath_with_r5_cut_oracles() {
15 fixture := $embed_file('testdata/complex_cases.tsv').to_string()
16 mut vm := new()
17 mut count := 0
18 for row in fixture.split_into_lines() {
19 if row.len == 0 || row[0] == `#` { continue }
20 f := row.split('\t')
21 assert f.len == 6, row
22 value := vm.eval(f[2]) or { panic('${f[0]} ${f[2]}: ${err.msg()}') }
23 if f[3] == 'exact' {
24 assert write(value) or { panic(err) } == f[4], '${f[0]} ${f[2]}'
25 assert value.tag.str() == f[5], f[0]
26 } else {
27 z := inexact_complex(value) or { panic(err) }
28 r, i := strconv.atof64(f[4]) or { panic(err) }, strconv.atof64(f[5]) or { panic(err) }
29 assert complex_near(z.re, r), '${f[0]} ${f[2]} real ${z.re} != ${r}'
30 assert complex_near(z.im, i), '${f[0]} ${f[2]} imag ${z.im} != ${i}'
31 }
32 count++
33 }
34 assert count == 1566
35}
36