-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodes_test.go
90 lines (85 loc) · 1.56 KB
/
nodes_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
package formulae
import "testing"
func TestOptimize(t *testing.T) {
tests := []struct {
in string
out string
}{
{"0+2", "2"},
{"2+0", "2"},
{"2-0", "2"},
{"0-2", "-2"},
{"0*2", "0"},
{"2*0", "0"},
{"1*2", "2"},
{"2*1", "2"},
{"-1*2", "-2"},
{"2*-1", "-2"},
{"2/1", "2"},
{"2/-1", "-2"},
{"2^0", "1"},
{"2^1", "2"},
{"a^-1", "1/a"},
{"1^2", "1"},
{"--2", "2"},
{"a+-1", "a-1"},
{"a--2", "a+2"},
{"ln(e)", "1"},
{"log(e)", "1"},
{"log10(10)", "1"},
{"e^ln(x)", "x"},
{"10^log10(x)", "x"},
{"sin(0)", "0"},
{"cos(0)", "1"},
{"tan(0)", "0"},
{"sin(-1)", "-sin(1)"},
{"cos(-1)", "cos(1)"},
{"tan(-1)", "-tan(1)"},
{"5+(-1/x)", "5-1/x"},
{"x+x", "2*x"},
{"-a*-b", "a*b"},
{"a*-b", "-(a*b)"},
{"(-a)^2", "a^2"},
{"(-a)^3", "-(a^3)"},
{"x*2", "2*x"},
{"2*(x*3)", "6*x"},
{"(2*x)*3", "6*x"},
}
for _, test := range tests {
t.Run(test.in, func(t *testing.T) {
f, errs := Parse(test.in)
if len(errs) > 0 {
t.Fatal(f, errs)
}
f.Optimize()
if f.root.String() != test.out {
t.Fatal(f.root.String(), "!=", test.out)
}
})
}
}
func TestDerivative(t *testing.T) {
tests := []struct {
in string
out string
}{
{"x", "1"},
{"sin x", "cos(x)"},
{"x^2", "2*x"},
{"2^x", "2^x*log(2)"},
{"e^x", "e^x"},
{"ln x", "1/x"},
}
for _, test := range tests {
t.Run(test.in, func(t *testing.T) {
f, errs := Parse(test.in)
if len(errs) > 0 {
t.Fatal(f, errs)
}
df := f.Derivative()
if df.String() != test.out {
t.Fatal(df.String(), "!=", test.out)
}
})
}
}