Skip to content

Commit 131ef7a

Browse files
Added tests and more examples
1 parent f44da8b commit 131ef7a

File tree

8 files changed

+193
-3
lines changed

8 files changed

+193
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.out
2+
*.so
3+
*.a

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: go
2+
3+
go:
4+
- 1.2
5+
- 1.3
6+
- 1.14.x
7+
- 1.15.x
8+
- "1.16.3"
9+
10+
env:
11+
- GOARCH: amd64
12+
script:
13+
- go test -v

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
<h1 align="center"> Go Mathematical Expression Toolkit</h1>
33
</p>
44

5-
The Go Expression Toolkit (Go-ExprTk) is a wrapper library based on C++ Mathematical Expression Toolkit Library ([ExprTk](http://www.partow.net/programming/exprtk/)). It is a simple to use, easy to integrate and extremely efficient run-time mathematical expression parser and evaluation engine. Go-ExprTk supports numerous forms of functional, logical and vector processing semantics and is very easily extendible.
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/Pramod-Devireddy/go-exprtk)](https://goreportcard.com/report/github.com/Pramod-Devireddy/go-exprtk)
6+
7+
The Go Expression Toolkit (Go-ExprTk) is a wrapper library based on C++ Mathematical Expression Toolkit Library ([ExprTk](http://www.partow.net/programming/exprtk/)).
8+
9+
It is a simple to use, easy to integrate and extremely efficient run-time mathematical expression parser and evaluation engine.
10+
11+
Go-ExprTk supports numerous forms of functional, logical and vector processing semantics and is very easily extendible.
612

713
## Installation
814

@@ -85,3 +91,6 @@ The Go-ExprTk library has the following capabilities:
8591
## ❤️ Credits
8692

8793
This module could not be possible without the [ExprTk](http://www.partow.net/programming/exprtk/) library by [Arash Partow](https://github.com/ArashPartow) and the idea of creating the wrapper module by [Narayana Rao G S](https://twitter.com/narayanraogs)
94+
95+
96+
Published under MIT License

examples/example-01.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/Pramod-Devireddy/go-exprtk"
77
)
88

9-
func main() {
9+
func example01() {
1010
// Create a new exprtk instance
1111
exprtkObj := exprtk.NewExprtk()
1212

examples/example-02.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
7+
"github.com/Pramod-Devireddy/go-exprtk"
8+
)
9+
10+
func example02() {
11+
var eqn string
12+
eqn = "if (eqn == 'avg') avg(x); "
13+
eqn += "else if (eqn == 'max') max(x); "
14+
eqn += "else if (eqn == 'min') min(x); "
15+
eqn += "else if (eqn == 'sum') sum(x); "
16+
eqn += "else 0; "
17+
18+
var eqnStr string
19+
var array []float64 = []float64{1, 2, 3, -4.3, 10, -6.5, 7, 8, -1.3}
20+
21+
exprtkObj := exprtk.NewExprtk()
22+
exprtkObj.SetExpression(eqn)
23+
exprtkObj.AddStringVariable("eqn")
24+
exprtkObj.AddVectorVariable("x")
25+
exprtkObj.CompileExpression()
26+
exprtkObj.SetVectorVariableValue("x", array)
27+
28+
eqnStr = "avg"
29+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
30+
fmt.Println(math.Round(exprtkObj.GetEvaluatedValue()*100) / 100)
31+
32+
eqnStr = "max"
33+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
34+
fmt.Println(exprtkObj.GetEvaluatedValue())
35+
36+
eqnStr = "min"
37+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
38+
fmt.Println(exprtkObj.GetEvaluatedValue())
39+
40+
eqnStr = "sum"
41+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
42+
fmt.Println(exprtkObj.GetEvaluatedValue())
43+
44+
eqnStr = "xyz"
45+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
46+
fmt.Println(exprtkObj.GetEvaluatedValue())
47+
}

examples/examples.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
func main() {
4+
example01()
5+
example02()
6+
}

exprtk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (obj GoExprtk) SetVectorVariableValue(varName string, val []float64) {
6565
func (obj GoExprtk) CompileExpression() error {
6666
value := C.compileExpression(obj.exprtk)
6767
if value == 0 {
68-
return errors.New("Failed to compile the expression")
68+
return errors.New("failed to compile the expression")
6969
}
7070
return nil
7171
}

exprtk_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package exprtk
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
func TestNewExprtk(t *testing.T) {
11+
exprtkObj := NewExprtk()
12+
13+
if reflect.TypeOf(exprtkObj).String() != "exprtk.GoExprtk" {
14+
t.Error("NewExprtk returned incorrect type")
15+
}
16+
}
17+
18+
func TestCompileExpression(t *testing.T) {
19+
exprtkObj := NewExprtk()
20+
21+
exprtkObj.SetExpression("x.1 + y.1")
22+
23+
exprtkObj.AddDoubleVariable("x.1")
24+
exprtkObj.AddDoubleVariable("y.1")
25+
26+
err := exprtkObj.CompileExpression()
27+
if err.Error() != "failed to compile the expression" {
28+
t.Error("negative case failed")
29+
}
30+
31+
exprtkObj.SetExpression("x + y")
32+
33+
exprtkObj.AddDoubleVariable("x")
34+
exprtkObj.AddDoubleVariable("y")
35+
36+
err = exprtkObj.CompileExpression()
37+
if err != nil {
38+
t.Error("failed to compile the expression")
39+
}
40+
}
41+
42+
func TestDoubleVariables(t *testing.T) {
43+
exprtkObj := NewExprtk()
44+
45+
exprtkObj.SetExpression("(x + 2)*(y-2)")
46+
47+
exprtkObj.AddDoubleVariable("x")
48+
exprtkObj.AddDoubleVariable("y")
49+
50+
err := exprtkObj.CompileExpression()
51+
if err != nil {
52+
fmt.Println(err.Error())
53+
return
54+
}
55+
56+
exprtkObj.SetDoubleVariableValue("x", 18)
57+
exprtkObj.SetDoubleVariableValue("y", 32)
58+
59+
if exprtkObj.GetEvaluatedValue() != 600 {
60+
t.Error("Incorrect Value")
61+
}
62+
63+
}
64+
65+
func TestMixedVariables(t *testing.T) {
66+
var eqn string
67+
eqn = "if (eqn == 'avg') avg(x); "
68+
eqn += "else if (eqn == 'max') max(x); "
69+
eqn += "else if (eqn == 'min') min(x); "
70+
eqn += "else if (eqn == 'sum') sum(x); "
71+
eqn += "else 0; "
72+
73+
var eqnStr string
74+
var array []float64 = []float64{1, 2, 3, -4.3, 10, -6.5, 7, 8, -1.3}
75+
76+
exprtkObj := NewExprtk()
77+
exprtkObj.SetExpression(eqn)
78+
exprtkObj.AddStringVariable("eqn")
79+
exprtkObj.AddVectorVariable("x")
80+
exprtkObj.CompileExpression()
81+
exprtkObj.SetVectorVariableValue("x", array)
82+
83+
eqnStr = "avg"
84+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
85+
if math.Round(exprtkObj.GetEvaluatedValue()*10)/10 != 2.1 {
86+
t.Error("Incorrect Value")
87+
}
88+
89+
eqnStr = "max"
90+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
91+
if exprtkObj.GetEvaluatedValue() != 10 {
92+
t.Error("Incorrect Value")
93+
}
94+
95+
eqnStr = "min"
96+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
97+
if exprtkObj.GetEvaluatedValue() != -6.5 {
98+
t.Error("Incorrect Value")
99+
}
100+
101+
eqnStr = "sum"
102+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
103+
if exprtkObj.GetEvaluatedValue() != 18.9 {
104+
t.Error("Incorrect Value")
105+
}
106+
107+
eqnStr = "xyz"
108+
exprtkObj.SetStringVariableValue("eqn", eqnStr)
109+
if exprtkObj.GetEvaluatedValue() != 0.0 {
110+
t.Error("Incorrect Value")
111+
}
112+
}

0 commit comments

Comments
 (0)