-
Notifications
You must be signed in to change notification settings - Fork 10
/
parser_test.go
203 lines (177 loc) · 6.02 KB
/
parser_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2016 Steven Oud. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package mathcat
import (
"math/big"
"testing"
)
var RatZero = big.NewRat(0, 1)
func TestFloatBitwise(t *testing.T) {
badExpressions := []string{
"2.4 | -2", "5.5 & 32", "7.7 ^ 2.1", "9 << 20.1", "7 >> 21.2", "~5.3",
"2 + 2 = 4",
}
for _, expr := range badExpressions {
_, err := Eval(expr)
if err == nil {
t.Error("expected error on using bitwise operator on float")
}
}
okExpressions := []string{
"2.0 | 2", "5 & 32.0", "7 ^ 2", "9 << 20", "7.0 >> -21.0", "~255",
}
for _, expr := range okExpressions {
_, err := Eval(expr)
if err != nil {
t.Errorf("unexpected error on using bitwise operator on int: %s", err)
}
}
}
func TestNumberLiterals(t *testing.T) {
invalidNumbers := []string{
"0x", "0X", "0x12p345", "0b", "0B", "0b2", "0x22.3", "0b10.1", "0b1e2",
"0o8", "0oea",
}
for _, n := range invalidNumbers {
_, err := Eval(n)
if err == nil {
t.Error("no error on invalid number literal")
}
}
validNumbers := map[string]*big.Rat{
"0xa": big.NewRat(10, 1),
"0Xaaabe": big.NewRat(699070, 1),
"0x12345": big.NewRat(74565, 1),
"0xe": big.NewRat(14, 1),
".200001": big.NewRat(200001, 1000000),
".2e5": big.NewRat(20000, 1),
"81e2": big.NewRat(8100, 1),
"32": big.NewRat(32, 1),
"100.0": big.NewRat(100, 1),
"0x0": RatZero,
"0": RatZero,
"0b110011": big.NewRat(51, 1),
"0b1": big.NewRat(1, 1),
"0o666": big.NewRat(438, 1),
"0O6120": big.NewRat(3152, 1),
"0o0": RatZero,
}
for n, expected := range validNumbers {
res, err := Eval(n)
if err != nil {
t.Errorf("error on valid number literal: %s", err)
}
if res.Cmp(expected) != 0 {
t.Errorf("invalid literal evaluation on %s, expected '%s', got '%s'", n, expected, res)
}
}
}
func TestEval(t *testing.T) {
okExpressions := map[string]*big.Rat{
"()": RatZero,
"-1": big.NewRat(-1, 1),
"(1)": big.NewRat(1, 1),
"12**12": big.NewRat(8916100448256, 1),
"~(~(1))": big.NewRat(1, 1),
"1000 > 10": big.NewRat(1, 1),
"1000 < 10": RatZero,
"55.0 == 55": big.NewRat(1, 1),
"55 <= 55": big.NewRat(1, 1),
"55 >= 55": big.NewRat(1, 1),
"pi * 2 == tau": big.NewRat(1, 1),
"-0 > 0": RatZero,
"0 < -0": RatZero,
"2 != 2": RatZero,
"5 % 25": big.NewRat(5, 1),
"5.5 % 25": new(big.Rat).SetFloat64(5.5),
"((((((((((((1))))))))))))": big.NewRat(1, 1),
"(1 + (2 + (3 + (4 + (5 + (6 + (7)))))))": big.NewRat(28, 1),
"(((((((1) + 2) + 3) + 4) + 5) + 6) + 7)": big.NewRat(28, 1),
"((2 + 2 - 3) / (5 + 5 * 8 / 9)) - (9 + 2)": big.NewRat(-926, 85),
"((2 * 4 - 6 / 3) * (3 * 5 + 8 / 4)) - (2 + 3)": big.NewRat(97, 1),
"0xdeadbeef & 0xff000000": big.NewRat(3724541952, 1),
"325-2*5+2": big.NewRat(317, 1),
"3**pi * (6 - -7)": big.NewRat(57713016890376237, 140737488355328),
"(2 == 2) == true": RatTrue,
"(2 == 3) == false": RatTrue,
"true == 1 & false == 0": RatTrue,
"false": RatFalse,
"33**11": big.NewRat(50542106513726817, 1),
}
for expr, expected := range okExpressions {
res, err := Eval(expr)
if err != nil {
t.Errorf("parser error occured on correct expression '%s': %s", expr, err)
}
if expected.Cmp(res) != 0 {
t.Errorf("wrong result in expression '%s' (expected %s, got %s)",
expr, expected, res)
}
}
badExpressions := []string{
"2 / 0", "2 % 0", "+", "2 + 2 +", ")", "(2 + 2 * 8", "@#%@#*%&@#",
"a + a", "~~2", "2 == ()", "5 < -", "2 * (9 ** 2))", "5 ~ 3",
}
for _, expr := range badExpressions {
_, err := Eval(expr)
if err == nil {
t.Error("no parser error occured on bad expression")
}
}
}
func TestExec(t *testing.T) {
type execTest struct {
expr string
vars map[string]*big.Rat
expected *big.Rat
}
okExpressions := []execTest{
{"酷 + b * b", map[string]*big.Rat{
"酷": big.NewRat(1, 1),
"b": big.NewRat(3, 1)},
big.NewRat(10, 1)},
{"a + b_ * pi", map[string]*big.Rat{
"a": big.NewRat(1, 1),
"b_": big.NewRat(3, 1),
"pi": big.NewRat(3, 1)},
big.NewRat(10, 1)},
{"a2 + b5 * pi3", map[string]*big.Rat{
"a2": big.NewRat(1, 1),
"b5": big.NewRat(3, 1),
"pi3": big.NewRat(3, 1)},
big.NewRat(10, 1)},
{"Å ** Å", map[string]*big.Rat{"Å": big.NewRat(1, 1)}, big.NewRat(1, 1)},
}
for _, test := range okExpressions {
res, err := Exec(test.expr, test.vars)
if err != nil {
t.Errorf("error on correct Exec: %s", err)
}
if res.Cmp(test.expected) != 0 {
t.Error("wrong result in Exec")
}
}
badExpressions := []execTest{
{"", map[string]*big.Rat{"-1": nil}, nil},
{"", map[string]*big.Rat{"55": nil}, nil},
{"", map[string]*big.Rat{"55a": nil}, nil},
{"", map[string]*big.Rat{".": nil}, nil},
{"", map[string]*big.Rat{")": nil}, nil},
{"", map[string]*big.Rat{"(": nil}, nil},
{"", map[string]*big.Rat{"@": nil}, nil},
}
for _, test := range badExpressions {
_, err := Exec(test.expr, test.vars)
if err == nil {
t.Error("no error on bad Exec")
}
}
}
func TestGetVar(t *testing.T) {
p := New()
p.Run("酷 = -33")
if _, err := p.GetVar("酷"); err != nil {
t.Error("GetVar failed: " + err.Error())
}
}