-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokens_test.go
71 lines (55 loc) · 1.54 KB
/
tokens_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
package eval
import "testing"
func equal(first, second []string) bool {
if len(first) != len(second) {
return false
}
for i := 0; i < len(first); i++ {
if first[i] != second[i] {
return false
}
}
return true
}
func TestSplitIntoTokens_Simple(t *testing.T) {
input := "56 + 27 * 2"
expected := []string{"56", "+", "27", "*", "2"}
actual := splitIntoTokens(input)
if !equal(actual, expected) {
t.Errorf("Got %#v, expected %#v", actual, expected)
}
input = "56+27*2"
actual = splitIntoTokens(input)
if !equal(actual, expected) {
t.Errorf("Got %#v, expected %#v", actual, expected)
}
}
func TestSplitIntoTokens_Complex(t *testing.T) {
input := "2 * ( 5 + 5 ) / 2 - 16 + 2 ^ 3"
expected := []string{"2", "*", "(", "5", "+", "5", ")", "/", "2", "-", "16", "+", "2", "^", "3"}
actual := splitIntoTokens(input)
if !equal(actual, expected) {
t.Errorf("Got %#v,\nexpected %#v", actual, expected)
}
input = "2*(5+5)/2-16+2^3"
actual = splitIntoTokens(input)
if !equal(actual, expected) {
t.Errorf("Got %#v,\nexpected %#v", actual, expected)
}
}
func TestSplitIntoTokens_Invalid(t *testing.T) {
input := ")(+2-)("
expected := []string{")", "(", "+", "2", "-", ")", "("}
actual := splitIntoTokens(input)
if !equal(actual, expected) {
t.Errorf("Got %#v, expected %#v", actual, expected)
}
}
func TestSplitIntoTokens_Negative(t *testing.T) {
input := "2 + -10"
expected := []string{"2", "+", "-", "10"}
actual := splitIntoTokens(input)
if !equal(actual, expected) {
t.Errorf("Got %#v, expected %#v", actual, expected)
}
}