-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
81 lines (67 loc) · 1.99 KB
/
main_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
package main
import (
"reflect"
"testing"
"github.com/dece2183/hexowl/operators"
"github.com/dece2183/hexowl/utils"
)
const testExpr = "a = pow(2,6) + (1)"
const testExprRes = 65
var testParseRes = []utils.Word{
{Type: utils.W_UNIT, Literal: "a"},
{Type: utils.W_OP, Literal: "="},
{Type: utils.W_UNIT, Literal: "pow"},
{Type: utils.W_CTL, Literal: "("},
{Type: utils.W_NUM_DEC, Literal: "2"},
{Type: utils.W_OP, Literal: ","},
{Type: utils.W_NUM_DEC, Literal: "6"},
{Type: utils.W_CTL, Literal: ")"},
{Type: utils.W_OP, Literal: "+"},
{Type: utils.W_CTL, Literal: "("},
{Type: utils.W_NUM_DEC, Literal: "1"},
{Type: utils.W_CTL, Literal: ")"},
}
var testParsedExpr []utils.Word
var testGeneratedOps *operators.Operator
func TestParsePrompt(t *testing.T) {
testParsedExpr = utils.ParsePrompt(testExpr)
if testParsedExpr == nil {
t.Error("failed to parse prompt")
return
}
for i, w := range testParsedExpr {
if w != testParseRes[i] {
t.Errorf("failed to parse prompt at word #%d:\r\n\texpected: %+v\r\n\tresult: %+v\r\n", i, testParseRes[i], w)
return
}
}
}
func TestGenerateOp(t *testing.T) {
var err error
if testParsedExpr == nil {
t.Log("parsing prompt...")
TestParsePrompt(t)
}
testGeneratedOps, err = operators.Generate(testParsedExpr, make(map[string]interface{}))
if err != nil {
t.Errorf("failed to generate operators: %s", err)
}
}
func TestCalculateExpr(t *testing.T) {
if testGeneratedOps == nil {
t.Log("generating operators...")
TestGenerateOp(t)
}
res, err := operators.Calculate(testGeneratedOps, make(map[string]interface{}))
if err != nil {
t.Errorf("failed to calculate operators: %s", err)
}
resNum, ok := res.(float64)
if !ok {
resType := reflect.TypeOf(res)
t.Errorf("failed to calculate operators, result types mismatch:\r\n\texpected: float64\r\n\tresult: %+v\r\n", resType)
}
if resNum != testExprRes {
t.Errorf("failed to calculate operators, wrong result:\r\n\texpected: %d\r\n\tresult: %f\r\n", testExprRes, resNum)
}
}