-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
147 lines (121 loc) · 4.14 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
package whitespace
import (
"testing"
"strings"
inst "github.com/zorchenhimer/whitespace/instructions"
)
type TestCase struct {
Name string
Input string
Output []inst.Instruction
}
func TestParseInstructions(t *testing.T) {
tests := []TestCase{
{"Add", "\t ", []inst.Instruction{inst.Add{}}},
{"Subtract", "\t \t", []inst.Instruction{inst.Subtract{}}},
{"Multiply", "\t \n", []inst.Instruction{inst.Multiply{}}},
{"Divide", "\t \t ", []inst.Instruction{inst.Divide{}}},
{"Modulo", "\t \t\t", []inst.Instruction{inst.Modulo{}}},
{"Push 0", " \n", []inst.Instruction{inst.Push{0}}},
{"Push 1", " \t\n", []inst.Instruction{inst.Push{1}}},
{"Push -75", " \t\t \t \t\t\n", []inst.Instruction{inst.Push{-75}}},
{"Copy 1", " \t \t\n", []inst.Instruction{inst.Copy{1}}},
{"Copy -75", " \t \t \t \t\t\n", []inst.Instruction{inst.Copy{-75}}},
{"Slide 1", " \t\n \t\n", []inst.Instruction{inst.Slide{1}}},
{"Slide 75", " \t\n \t \t \t\t\n", []inst.Instruction{inst.Slide{75}}},
{"Discard", " \n\n", []inst.Instruction{inst.Discard{}}},
{"Duplicate", " \n ", []inst.Instruction{inst.Duplicate{}}},
{"Swap", " \n\t", []inst.Instruction{inst.Swap{}}},
{"Store", "\t\t ", []inst.Instruction{inst.Store{}}},
{"Load", "\t\t\t", []inst.Instruction{inst.Load{}}},
{"Label SSS", "\n \n", []inst.Instruction{inst.Label{" "}}},
{"Label TST", "\n \t \t\n", []inst.Instruction{inst.Label{"\t \t"}}},
{"Call SSS", "\n \t \n", []inst.Instruction{inst.Call{" "}}},
{"Call TST", "\n \t\t \t\n", []inst.Instruction{inst.Call{"\t \t"}}},
{"Jump SSS", "\n \n \n", []inst.Instruction{inst.Jump{" "}}},
{"Jump TST", "\n \n\t \t\n", []inst.Instruction{inst.Jump{"\t \t"}}},
{"JumpZero SSS", "\n\t \n", []inst.Instruction{inst.JumpZero{" "}}},
{"JumpZero TST", "\n\t \t \t\n", []inst.Instruction{inst.JumpZero{"\t \t"}}},
{"JumpMinus SSS", "\n\t\t \n", []inst.Instruction{inst.JumpMinus{" "}}},
{"JumpMinus TST", "\n\t\t\t \t\n", []inst.Instruction{inst.JumpMinus{"\t \t"}}},
{"Return", "\n\t\n", []inst.Instruction{inst.Return{}}},
{"Stop", "\n\n\n", []inst.Instruction{inst.Stop{}}},
{"Print Char", "\t\n ", []inst.Instruction{inst.PrintChar{}}},
{"Print Number", "\t\n \t", []inst.Instruction{inst.PrintNumber{}}},
{"Read Char", "\t\n\t ", []inst.Instruction{inst.ReadChar{}}},
{"Read Number", "\t\n\t\t", []inst.Instruction{inst.ReadNumber{}}},
}
runParseTests(t, tests)
}
func TestParseNumber(t *testing.T) {
tests := []struct{
Name string
Input string
Output int64
}{
{"0", " \n", 0},
{"75", " \t \t \t\t\n", 75},
{"-75", "\t\t \t \t\t\n", -75},
{"50", " \t\t \t \n", 50},
{"-50", "\t\t\t \t \n", -50},
}
for _, tst := range tests {
t.Logf("%s: %q", tst.Name, tst.Input)
p := NewParser(NewReader(strings.NewReader(tst.Input)))
n, err := p.parseNumber()
if err != nil {
t.Logf("Parse error: %s", err)
t.Fail()
continue
}
if n != tst.Output {
t.Logf("Value mismatch: %d vs %d", n, tst.Output)
t.Fail()
}
}
}
func TestParseSmallProgram(t *testing.T) {
tests := []TestCase{
{"Addition", " \t\n \t \n\t \t\n \t\n\n\n", []inst.Instruction{
inst.Push{1},
inst.Push{2},
inst.Add{},
inst.PrintNumber{},
inst.Stop{},
}},
}
runParseTests(t, tests)
}
func runParseTests(t *testing.T, tests []TestCase) {
t.Helper()
for _, tst := range tests {
t.Log(tst.Name)
p := NewParser(NewReader(strings.NewReader(tst.Input)))
lst, err := p.Parse()
if err != nil {
t.Logf("Parse failed: %s", err)
t.Fail()
continue
}
if !instEqual(t, lst, tst.Output) {
t.Logf("Unexpected output\n Rec: %v\n Exp: %v", lst, tst.Output)
t.Fail()
}
}
}
func instEqual(t *testing.T, a, b []inst.Instruction) bool {
t.Helper()
if len(a) != len(b) {
return false
}
t.Logf("len: %d", len(a))
for i := 0; i < len(a); i++ {
if a[i].Type() != b[i].Type() {
t.Logf("%v != %v", a[i].Type(), b[i].Type())
return false
}
//t.Logf("%v == %v", a[i].Type(), b[i].Type())
}
//t.Logf("equal\n%v\n%v", a, b)
return true
}