-
Notifications
You must be signed in to change notification settings - Fork 13
/
exprtk_test.go
117 lines (91 loc) · 2.51 KB
/
exprtk_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package exprtk
import (
"fmt"
"math"
"reflect"
"testing"
)
func TestNewExprtk(t *testing.T) {
exprtkObj := NewExprtk()
defer exprtkObj.Delete()
if reflect.TypeOf(exprtkObj).String() != "exprtk.GoExprtk" {
t.Error("NewExprtk returned incorrect type")
}
}
func TestCompileExpression(t *testing.T) {
exprtkObj := NewExprtk()
defer exprtkObj.Delete()
exprtkObj.SetExpression("x.1 + y.1")
exprtkObj.AddDoubleVariable("x.1")
exprtkObj.AddDoubleVariable("y.1")
err := exprtkObj.CompileExpression()
if err.Error() != "failed to compile the expression" {
t.Error("negative case failed")
}
exprtkObj.SetExpression("x + y")
exprtkObj.AddDoubleVariable("x")
exprtkObj.AddDoubleVariable("y")
err = exprtkObj.CompileExpression()
if err != nil {
t.Error("failed to compile the expression")
}
}
func TestDoubleVariables(t *testing.T) {
exprtkObj := NewExprtk()
defer exprtkObj.Delete()
exprtkObj.SetExpression("(x + 2)*(y-2)")
exprtkObj.AddDoubleVariable("x")
exprtkObj.AddDoubleVariable("y")
err := exprtkObj.CompileExpression()
if err != nil {
fmt.Println(err.Error())
return
}
exprtkObj.SetDoubleVariableValue("x", 18)
exprtkObj.SetDoubleVariableValue("y", 32)
if exprtkObj.GetEvaluatedValue() != 600 {
t.Error("Incorrect Value")
}
}
func TestMixedVariables(t *testing.T) {
var eqn string
eqn = "if (eqn == 'avg') avg(x); "
eqn += "else if (eqn == 'max') max(x); "
eqn += "else if (eqn == 'min') min(x); "
eqn += "else if (eqn == 'sum') sum(x); "
eqn += "else 0; "
var eqnStr string
var array []float64 = []float64{1, 2, 3, -4.3, 10, -6.5, 7, 8, -1.3}
exprtkObj := NewExprtk()
defer exprtkObj.Delete()
exprtkObj.SetExpression(eqn)
exprtkObj.AddStringVariable("eqn")
exprtkObj.AddVectorVariable("x")
exprtkObj.CompileExpression()
exprtkObj.SetVectorVariableValue("x", array)
eqnStr = "avg"
exprtkObj.SetStringVariableValue("eqn", eqnStr)
if math.Round(exprtkObj.GetEvaluatedValue()*10)/10 != 2.1 {
t.Error("Incorrect Value")
}
eqnStr = "max"
exprtkObj.SetStringVariableValue("eqn", eqnStr)
if exprtkObj.GetEvaluatedValue() != 10 {
t.Error("Incorrect Value")
}
eqnStr = "min"
exprtkObj.SetStringVariableValue("eqn", eqnStr)
if exprtkObj.GetEvaluatedValue() != -6.5 {
t.Error("Incorrect Value")
}
eqnStr = "sum"
exprtkObj.SetStringVariableValue("eqn", eqnStr)
if exprtkObj.GetEvaluatedValue() != 18.9 {
t.Error("Incorrect Value")
}
eqnStr = "xyz"
exprtkObj.SetStringVariableValue("eqn", eqnStr)
if exprtkObj.GetEvaluatedValue() != 0.0 {
t.Error("Incorrect Value")
}
}