-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction_test.go
98 lines (86 loc) · 1.74 KB
/
function_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package formulae
import (
"math/cmplx"
"testing"
)
var Epsilon = 1e-6
func TestCalc(t *testing.T) {
tests := []struct {
in string
out complex128
}{
{"1+2*3", 7},
{"4x", 20},
{"sin(pi)", 0},
{"sin(pi/2)", 1},
{"ln(e)", 1},
{"LN(E)", 1},
{"5+1i+6+2i", 11 + 3i},
{"i", 1i},
}
x := 5 + 0i
for _, test := range tests {
t.Run(test.in, func(t *testing.T) {
function, errs := Parse(test.in)
if len(errs) > 0 {
t.Fatal(function, errs)
}
y, err := function.Calc(x)
if err != nil {
t.Fatal(err)
}
if cmplx.Abs(y-test.out) > Epsilon {
t.Fatal(y, "!=", test.out)
}
})
}
}
func TestCalcErr(t *testing.T) {
tests := []struct {
in string
err string
}{
{"4y", "undefined variable 'y'"},
{"3/(5-x)", "division by zero"},
}
x := 5 + 0i
for _, test := range tests {
t.Run(test.in, func(t *testing.T) {
function, errs := Parse(test.in)
if len(errs) > 0 {
t.Fatal(function, errs)
}
_, err := function.Calc(x)
if err == nil {
t.Fatal("nil !=", test.err)
} else if err.Error() != test.err {
t.Fatal(err.Error(), "!=", test.err)
}
})
}
}
var function, _ = Parse("sin(x)^2+1/x+0.001x^3")
var N = 100
var xs []complex128
var ys []complex128
func init() {
xs = make([]complex128, N)
ys = make([]complex128, N)
for j := 0; j < N; j++ {
xs[j] = complex(float64(j)+1.0, 0)
}
}
func BenchmarkCalcNative(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < N; j++ {
ys[j] = cmplx.Pow(cmplx.Sin(xs[j]), 2) + (1 / xs[j]) + 0.001*cmplx.Pow(xs[j], 3)
}
}
}
func BenchmarkCalc(b *testing.B) {
for i := 0; i < b.N; i++ {
for i, x := range xs {
ys[i], _ = function.Calc(x)
}
}
}