-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
268 lines (245 loc) · 7.34 KB
/
table_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package lr0
import (
"testing"
)
func TestTable(t *testing.T) {
const rowsCount = 9
tbl := newTable(testTableItemsetGrammar)
if len(tbl.rows) != rowsCount {
t.Errorf("rows are %v", len(tbl.rows))
}
// row 0 (Goal : > Sum) ==================================================
row0 := tbl.rows[0]
if row0.AcceptEof() {
t.Error("row0 eof")
}
if row0.ReduceRule() != nil {
t.Error("row0 reduce rule")
}
if len(row0.terminals) != 2 {
t.Errorf("row0.terminals: %#v", row0.terminals)
}
var (
ok bool
next0zero tableStateIndex
next0one tableStateIndex
next0val tableStateIndex
next0sum tableStateIndex
)
if next0zero, ok = row0.terminals[tZero]; !ok || next0zero < 0 || next0zero >= rowsCount {
t.Errorf("row0.terminals zero: %v, %v", next0zero, ok)
}
if next0one, ok = row0.terminals[tOne]; !ok || next0one < 0 || next0one >= rowsCount {
t.Errorf("row0.terminals one: %v, %v", next0one, ok)
}
if len(row0.gotos) != 2 {
t.Errorf("row0.gotos: %#v", row0.gotos)
}
if next0val, ok = row0.gotos[nVal]; !ok || next0val < 0 || next0val >= rowsCount {
t.Errorf("row0.gotos val: %v, %v", next0val, ok)
}
if next0sum, ok = row0.gotos[nSum]; !ok || next0sum < 0 || next0sum >= rowsCount {
t.Errorf("row0.gotos sum: %v, %v", next0sum, ok)
}
if len(map[tableStateIndex]struct{}{next0zero: {}, next0one: {}, next0val: {}, next0sum: {}}) != 4 {
t.Errorf("row0 actions are not uniq: %v, %v, %v, %v", next0zero, next0one, next0val, next0sum)
}
// row 1 (Val : zero >) ==========================================
rowValZero := tbl.rows[next0zero]
if !rowValZero.IsReduceOnly() {
t.Errorf("rowValZero not reduce only: %#v", rowValZero)
}
if rowValZero.ReduceRule() != testTableItemsetRuleValZero {
t.Error("rowValZero reduce rule wrong")
}
// row 2 (Val : one >) ==========================================
rowValOne := tbl.rows[next0one]
if !rowValOne.IsReduceOnly() {
t.Errorf("rowValOne not reduce only: %#v", rowValOne)
}
if rowValOne.ReduceRule() != testTableItemsetRuleValOne {
t.Error("rowValOne reduce rule wrong")
}
// row 3 (Sum : Val >) ==========================================
rowSumVal := tbl.rows[next0val]
if !rowSumVal.IsReduceOnly() {
t.Errorf("rowValOne not reduce only: %#v", rowSumVal)
}
if rowSumVal.ReduceRule() != testTableItemsetRuleSumVal {
t.Error("rowValOne reduce rule wrong")
}
// row 4 (Goal : Sum > $) =======================================
rowGoal := tbl.rows[next0sum]
if !rowGoal.AcceptEof() {
t.Error("rowGoal not eof")
}
if rowGoal.ReduceRule() != testTableItemsetRuleGoal {
t.Error("rowGoal reduce rule wrong")
}
var (
nextGoalPlus tableStateIndex
nextGoalMinus tableStateIndex
)
if len(rowGoal.terminals) != 2 {
t.Errorf("rowGoal.terminals: %#v", rowGoal.terminals)
}
if nextGoalPlus, ok = rowGoal.terminals[tPlus]; !ok || nextGoalPlus < 0 || nextGoalPlus >= rowsCount {
t.Errorf("rowGoal.terminals plus: %v, %v", nextGoalPlus, ok)
}
if nextGoalMinus, ok = rowGoal.terminals[tMinus]; !ok || nextGoalMinus < 0 || nextGoalMinus >= rowsCount {
t.Errorf("rowGoal.terminals minus: %v, %v", nextGoalMinus, ok)
}
if len(rowGoal.gotos) != 0 {
t.Errorf("rowGoal.gotos: %#v", rowGoal.gotos)
}
if len(map[tableStateIndex]struct{}{nextGoalPlus: {}, nextGoalMinus: {}}) != 2 {
t.Errorf("rowGoal actions are not uniq: %v, %v", nextGoalPlus, nextGoalMinus)
}
// row 5 (Sum : Sum plus > Val) =====================================
rowSumPlus := tbl.rows[nextGoalPlus]
if rowSumPlus.AcceptEof() {
t.Error("rowSumPlus eof")
}
if rowSumPlus.ReduceRule() != nil {
t.Error("rowSumPlus reduce rule wrong")
}
var (
nextPlusZero tableStateIndex
nextPlusOne tableStateIndex
nextPlusVal tableStateIndex
)
if len(rowSumPlus.terminals) != 2 {
t.Errorf("rowSumPlus.terminals: %#v", rowSumPlus.terminals)
}
if nextPlusZero, ok = rowSumPlus.terminals[tZero]; !ok || nextPlusZero < 0 || nextPlusZero >= rowsCount {
t.Errorf("rowSumPlus.terminals zero: %v, %v", nextPlusZero, ok)
}
if nextPlusOne, ok = rowSumPlus.terminals[tOne]; !ok || nextPlusOne < 0 || nextPlusOne >= rowsCount {
t.Errorf("rowSumPlus.terminals one: %v, %v", nextPlusOne, ok)
}
if len(rowSumPlus.gotos) != 1 {
t.Errorf("rowSumPlus.gotos: %#v", rowSumPlus.gotos)
}
if nextPlusVal, ok = rowSumPlus.gotos[nVal]; !ok || nextPlusVal < 0 || nextPlusVal >= rowsCount {
t.Errorf("rowSumPlus.gotos val: %v, %v", nextPlusVal, ok)
}
if len(map[tableStateIndex]struct{}{nextPlusZero: {}, nextPlusOne: {}, nextPlusVal: {}}) != 3 {
t.Errorf("rowSumPlus actions are not uniq: %v, %v, %v", nextPlusZero, nextPlusOne, nextPlusVal)
}
// row 6 (Sum : Sum minus > Val) =====================================
rowSumMinus := tbl.rows[nextGoalMinus]
if rowSumMinus.AcceptEof() {
t.Error("rowSumMinus eof")
}
if rowSumMinus.ReduceRule() != nil {
t.Error("rowSumMinus reduce rule wrong")
}
var (
nextMinusZero tableStateIndex
nextMinusOne tableStateIndex
nextMinusVal tableStateIndex
)
if len(rowSumMinus.terminals) != 2 {
t.Errorf("rowSumMinus.terminals: %#v", rowSumMinus.terminals)
}
if nextMinusZero, ok = rowSumMinus.terminals[tZero]; !ok || nextMinusZero < 0 || nextMinusZero >= rowsCount {
t.Errorf("rowSumMinus.terminals zero: %v, %v", nextMinusZero, ok)
}
if nextMinusOne, ok = rowSumMinus.terminals[tOne]; !ok || nextMinusOne < 0 || nextMinusOne >= rowsCount {
t.Errorf("rowSumMinus.terminals one: %v, %v", nextMinusOne, ok)
}
if len(rowSumMinus.gotos) != 1 {
t.Errorf("rowSumMinus.gotos: %#v", rowSumMinus.gotos)
}
if nextMinusVal, ok = rowSumMinus.gotos[nVal]; !ok || nextMinusVal < 0 || nextMinusVal >= rowsCount {
t.Errorf("rowSumMinus.gotos val: %v, %v", nextMinusVal, ok)
}
if len(map[tableStateIndex]struct{}{nextMinusZero: {}, nextMinusOne: {}, nextMinusVal: {}}) != 3 {
t.Errorf("rowSumMinus actions are not uniq: %v, %v, %v", nextMinusZero, nextMinusOne, nextMinusVal)
}
// row 7 (Sum : Sum plus Val >) =============================
rowSumPlusVal := tbl.rows[nextPlusVal]
if !rowSumPlusVal.IsReduceOnly() {
t.Errorf("rowSumPlusVal not reduce only: %#v", rowSumPlusVal)
}
if rowSumPlusVal.ReduceRule() != testTableItemsetRuleSumPlus {
t.Error("rowSumPlusVal reduce rule wrong")
}
// row 8 (Sum : Sum minus Val >) =============================
rowSumMinusVal := tbl.rows[nextMinusVal]
if !rowSumMinusVal.IsReduceOnly() {
t.Errorf("rowSumMinusVal not reduce only: %#v", rowSumMinusVal)
}
if rowSumMinusVal.ReduceRule() != testTableItemsetRuleSumMinus {
t.Error("rowSumMinusVal reduce rule wrong")
}
const expectTableDump = `====[ table ]====
row 0 ---------
EOF: ACCEPT
terminals:
zero -> 1
one -> 2
goto:
Val -> 3
Sum -> 4
rule: -
row 1 ---------
EOF: ACCEPT
terminals: -
goto: -
rule:
Val : zero
row 2 ---------
EOF: ACCEPT
terminals: -
goto: -
rule:
Val : one
row 3 ---------
EOF: ACCEPT
terminals: -
goto: -
rule:
Sum : Val
row 4 ---------
EOF: -
terminals:
"+" -> 5
"-" -> 6
goto: -
rule:
Goal : Sum $
row 5 ---------
EOF: ACCEPT
terminals:
zero -> 1
one -> 2
goto:
Val -> 7
rule: -
row 6 ---------
EOF: ACCEPT
terminals:
zero -> 1
one -> 2
goto:
Val -> 8
rule: -
row 7 ---------
EOF: ACCEPT
terminals: -
goto: -
rule:
Sum : Sum "+" Val
row 8 ---------
EOF: ACCEPT
terminals: -
goto: -
rule:
Sum : Sum "-" Val
=================
`
if d := tbl.dump(testTableItemsetGrammar); d != expectTableDump {
t.Error("table dump is:\n", d)
}
}