-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfa_test.go
361 lines (335 loc) · 17.5 KB
/
dfa_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package gocompute
import (
//"errors"
"github.com/jophish/golang-set"
"strconv"
"testing"
)
//things to test:
// constructing a dfa, catching errors:
// -states of arbitrary type (string, int, struct)
// - error catching:
// - states, alphabet have elements of different type
// - accept not in states
// - transition invalid
//
// simulating a dfa:
// -
//test constructor, simulate, union, intersection, complement, difference
var d1, d1err = makeEvenOnesDFAStringStates()
var d2, d2err = makeEvenOnesDFAIntStates()
var d3, d3err = makeEvenOnesDFAStructStates()
var d4, d4err = makeOddZerosDFAStringStates()
var d5, d5err = makeOddOnesDFAStringStates()
var d6, d6err = makeEvenZerosDFAStringStates()
var d7, d7err = makeAllZerosOnesStringStates()
var d8, d8err = makeNoneZerosOnesStringStates()
var simulateTests = []struct {
d *DFA
err error
testStrings map[string]bool
descriptor string
}{
{d1, d1err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "DFA accepting strings with even number of 1s using strings for states"},
{d2, d2err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "DFA accepting strings with even number of 1s using ints for states"},
{d3, d3err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "DFA accepting strings with even number of 1s using structs for states"},
{d4, d4err, map[string]bool{"00011": true, "": false, "0001011011": true, "100": false, "001001011001001011": false}, "DFA accepting strings with odd number of 0s using strings for states"},
{d5, d5err, map[string]bool{"0001": true, "": false, "0001011011": true, "100": true, "001001011001001011": false}, "DFA accepting strings with odd number of 1s using strings for states"},
{d6, d6err, map[string]bool{"0001": false, "": true, "0001011011": false, "100": true, "001001011001001011": true}, "DFA accepting strings with even number of 0s using strings for states"},
{d7, d7err, map[string]bool{"0001": true, "": true, "0001011011": true, "100": true, "001001011001001011": true}, "DFA accepting all strings of 0s and 1s using strings for states"},
{d8, d8err, map[string]bool{"0001": false, "": false, "0001011011": false, "100": false, "001001011001001011": false}, "DFA accepting no strings using strings for states"},
}
var uniond1, uniond1err = d1.Union(d6) // should accept strings which contain even number of 0s or 1s
var uniond2, uniond2err = d4.Union(d5) //should accept strings which contain odd number of 0s or 1s
//for union, check that union of same dfa is same as original
//maybe test union between different representations
var unionTests = []struct {
d1 *DFA
d1err error
d2 *DFA
d2err error
testStrings map[string]bool
descriptor string
}{
{d1, d1err, d1, d1err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Union of two DFAs accepting even number of 1s, both using strings as states"},
{d1, d1err, d3, d3err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Union of two DFAs accepting even number of 1s, one using strings, other using structs as states"},
{d1, d1err, d6, d6err, map[string]bool{"011": true, "": true, "0001011011": false, "10": false, "000101101": true}, "Union of DFAs, one accepting even number 1s, other accepting even number 0s"},
{d4, d4err, d5, d5err, map[string]bool{"0011": false, "": false, "0001011011": true, "100": true, "001001011001001011": false}, "Union of DFAs, one accepting odd number 1s, other accepting odd number 0s"},
{d1, d1err, d5, d5err, map[string]bool{"0011": true, "": true, "0001011011": true, "100": true, "001001011001001011": true}, "Union of DFAs, one accepting even number 1s, other accepting odd number 1s"},
{uniond1, uniond1err, uniond2, uniond2err, map[string]bool{"0011": true, "": true, "0001011011": true, "100": true, "001001011001001011": true}, "Union of union of DFAs, should accept all strings of 0s and 1s"},
{compld1, compld1err, d1, d1err, map[string]bool{"0011": true, "": true, "0001011011": true, "100": true, "001001011001001011": true}, "Union of a DFA accepting strings of even number of 1s and its complement"},
{interd1, interd1err, d5, d5err, map[string]bool{"0011": true, "": true, "0001011011": true, "100": true, "00100101100100111": false}, "Union of an intersection and a DFA: should accept strings with even number of 0s and 1s OR strings with odd number 1s"},
}
var interd1, interd1err = d1.Intersection(d6) // should accept strings with even number of 0s and 1s
var intersectionTests = []struct {
d1 *DFA
d1err error
d2 *DFA
d2err error
testStrings map[string]bool
descriptor string
}{
{d1, d1err, d1, d1err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Intersection of two DFAs accepting even nummber of 1s, both using strings as states"},
{d1, d1err, d3, d3err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Intersection of two DFAs accepting even number of 1s, one using strings, other using structs as states"},
{d1, d1err, d5, d5err, map[string]bool{"0011": false, "": false, "0001011011": false, "100": false, "001001011001001011": false}, "Intersection of DFAs, one accepting even number 1s, other accepting odd number 1s"},
{d4, d4err, d5, d5err, map[string]bool{"0011": false, "": false, "0001011011": true, "100": false, "001001011001001011": false}, "Intersection of DFAs, one accepting odd number 1s, other accepting odd number 0s"},
{d1, d1err, d7, d7err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Intersection of DFAs, one accepting all strings of 0s, 1s, other accepting even number 1s"},
{compld1, compld1err, d1, d1err, map[string]bool{"0011": false, "": false, "0001011011": false, "100": false, "001001011001001011": false}, "Intersection of a DFA accepting strings of even number of 1s and its complement"},
{uniond1, uniond1err, uniond2, uniond2err, map[string]bool{"0011": false, "": false, "000101101": true, "100": true, "001001011001001011": false}, "Intersection of union of DFAs, should accept strings with even number 1s and odd number 0s OR strings with odd number 1s and even number 0s"},
{interd1, interd1err, d6, d6err, map[string]bool{"0011": true, "": true, "0001011011": false, "010": false, "001001011001001011": true}, "Intersection of an intersection and another DFA: should accept strings with even number of 0s and 1s"},
}
var compld1, compld1err = d1.Complement()
var complementTests = []struct {
d1 *DFA
d1err error
testStrings map[string]bool
descriptor string
}{
{d1, d1err, map[string]bool{"0011": false, "": false, "0001011011": true, "100": true, "001001011001001011": false}, "Complement of a DFA recognizing an even number of 1s"},
{d7, d7err, map[string]bool{"0011": false, "": false, "0001011011": false, "100": false, "001001011001001011": false}, "Complement of a DFA recognizing all strings of 0s and 1s"},
{d8, d8err, map[string]bool{"0001": true, "": true, "0001011011": true, "100": true, "001001011001001011": true}, "Complement of a DFA recognizing no strings"},
{uniond1, uniond1err, map[string]bool{"0001": true, "": false, "0001011011": true, "100": false, "001001011001001011": false}, "Complement of a union of DFAs which should recognize strings containing an even number of 0s or 1s"},
{interd1, interd1err, map[string]bool{"0001": true, "": false, "0001011011": true, "100": true, "001001011001001011": false}, "Complement of an intersection of DFAs which should recognize strings containing an even number of 0s and 1s"},
{compld1, compld1err, map[string]bool{"0001": false, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Complement of a complement of a DFA recognizing strings with even number of 1s"},
}
var differenceTests = []struct {
d1 *DFA
d1err error
d2 *DFA
d2err error
testStrings map[string]bool
descriptor string
}{
{d1, d1err, d1, d1err, map[string]bool{"0011": false, "": false, "0001011011": false, "100": false, "001001011001001011": false}, "Difference of two identical DFAs"},
{d3, d3err, d4, d4err, map[string]bool{"00011": false, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Difference between DFAs using different representations for states: should accept strings with even number of 0s and 1s"},
{d1, d1err, d5, d5err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Difference of DFAs, first accepting even number 1s, other accepting odd number 1s"},
{d4, d4err, d5, d5err, map[string]bool{"0011": false, "": false, "00010110111": true, "100": false, "01001011001001011": true}, "Difference of DFAs, first accepting odd number 0s, other accepting odd number 1s: should accept odd number 0s even number 1s"},
{d7, d7err, d1, d1err, map[string]bool{"0011": false, "": false, "0001011011": true, "100": true, "001001011001001011": false}, "Difference of DFAs, first accepting all strings of 0s, 1s, other accepting even number 1s"},
{d1, d1err, compld1, compld1err, map[string]bool{"0011": true, "": true, "0001011011": false, "100": false, "001001011001001011": true}, "Difference of a DFA accepting strings of even number of 1s and its complement"},
{uniond1, uniond1err, uniond2, uniond2err, map[string]bool{"0011": true, "": true, "000101101": false, "100": false, "001001011001001011": true}, "Difference of union of DFAs, should accept strings with an even number of 0s and 1s"},
{interd1, interd1err, d6, d6err, map[string]bool{"0011": false, "": false, "0001011011": false, "010": false, "001001011001001011": false}, "Difference of an intersection and another DFA: should not accept any strings"},
}
func TestDFASimulate(t *testing.T) {
for _, test := range simulateTests {
if test.err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.err.Error())
t.FailNow()
}
for k, v := range test.testStrings {
ans, err := test.d.Simulate(k)
if ans != v {
t.Error("On test: " + test.descriptor + ", error: DFA should have answered " + strconv.FormatBool(v) + " to string " + k)
}
if err != nil {
t.Error("On test: " + test.descriptor + ", while testing string " + k + ", error: " + err.Error())
}
}
}
}
func TestDFAUnion(t *testing.T) {
for _, test := range unionTests {
if test.d1err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d1err.Error())
t.FailNow()
}
if test.d2err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d2err.Error())
t.FailNow()
}
testd, err := test.d1.Union(test.d2)
if err != nil {
t.Error("On test: " + test.descriptor + ", error: " + err.Error())
t.FailNow()
}
for k, v := range test.testStrings {
ans, err := testd.Simulate(k)
if ans != v {
t.Error("On test: " + test.descriptor + ", error: DFA should have answered " + strconv.FormatBool(v) + " to string " + k)
}
if err != nil {
t.Error("On test: " + test.descriptor + ", while testing string " + k + ", error: " + err.Error())
}
}
}
}
func TestDFAIntersection(t *testing.T) {
for _, test := range intersectionTests {
if test.d1err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d1err.Error())
t.FailNow()
}
if test.d2err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d2err.Error())
t.FailNow()
}
testd, err := test.d1.Intersection(test.d2)
if err != nil {
t.Error("On test: " + test.descriptor + ", error: " + err.Error())
t.FailNow()
}
for k, v := range test.testStrings {
ans, err := testd.Simulate(k)
if ans != v {
t.Error("On test: " + test.descriptor + ", error: DFA should have answered " + strconv.FormatBool(v) + " to string " + k)
}
if err != nil {
t.Error("On test: " + test.descriptor + ", while testing string " + k + ", error: " + err.Error())
}
}
}
}
func TestDFAComplement(t *testing.T) {
for _, test := range complementTests {
if test.d1err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d1err.Error())
t.FailNow()
}
testd, err := test.d1.Complement()
if err != nil {
t.Error("On test: " + test.descriptor + ", error: " + err.Error())
t.FailNow()
}
for k, v := range test.testStrings {
ans, err := testd.Simulate(k)
if ans != v {
t.Error("On test: " + test.descriptor + ", error: DFA should have answered " + strconv.FormatBool(v) + " to string " + k)
}
if err != nil {
t.Error("On test: " + test.descriptor + ", while testing string " + k + ", error: " + err.Error())
}
}
}
}
func TestDFADifference(t *testing.T) {
for _, test := range differenceTests {
if test.d1err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d1err.Error())
t.FailNow()
}
if test.d2err != nil {
t.Error("On test: " + test.descriptor + ", error: " + test.d2err.Error())
t.FailNow()
}
testd, err := test.d1.Difference(test.d2)
if err != nil {
t.Error("On test: " + test.descriptor + ", error: " + err.Error())
t.FailNow()
}
for k, v := range test.testStrings {
ans, err := testd.Simulate(k)
if ans != v {
t.Error("On test: " + test.descriptor + ", error: DFA should have answered " + strconv.FormatBool(v) + " to string " + k)
}
if err != nil {
t.Error("On test: " + test.descriptor + ", while testing string " + k + ", error: " + err.Error())
}
}
}
}
//the three methods below produce equivalent DFAs, with different representations. all should accept
//strings of 0s and 1s with an even number of 1s.
func makeEvenOnesDFAStringStates() (*DFA, error) {
states := mapset.NewSet("q0", "q1")
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
q0map := map[string]interface{}{"0": "q0", "1": "q1"}
q1map := map[string]interface{}{"0": "q1", "1": "q0"}
fullmap := map[interface{}](map[string]interface{}){"q0": q0map, "q1": q1map}
return fullmap[state][input]
}
start := "q0"
accept := mapset.NewSet("q0")
return NewDFA(states, alphabet, transition, start, accept)
}
func makeEvenOnesDFAIntStates() (*DFA, error) {
states := mapset.NewSet(0, 1)
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
q0map := map[string]interface{}{"0": 0, "1": 1}
q1map := map[string]interface{}{"0": 1, "1": 0}
fullmap := map[interface{}](map[string]interface{}){0: q0map, 1: q1map}
return fullmap[state][input]
}
start := 0
accept := mapset.NewSet(0)
return NewDFA(states, alphabet, transition, start, accept)
}
func makeEvenOnesDFAStructStates() (*DFA, error) {
type state struct {
str string
num int
}
q0 := state{"q0", 0}
q1 := state{"q1", 1}
states := mapset.NewSet(q0, q1)
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
q0map := map[string]interface{}{"0": q0, "1": q1}
q1map := map[string]interface{}{"0": q1, "1": q0}
fullmap := map[interface{}](map[string]interface{}){q0: q0map, q1: q1map}
return fullmap[state][input]
}
start := q0
accept := mapset.NewSet(q0)
return NewDFA(states, alphabet, transition, start, accept)
}
/////
func makeOddZerosDFAStringStates() (*DFA, error) {
states := mapset.NewSet("q0", "q1")
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
q0map := map[string]interface{}{"0": "q1", "1": "q0"}
q1map := map[string]interface{}{"0": "q0", "1": "q1"}
fullmap := map[interface{}](map[string]interface{}){"q0": q0map, "q1": q1map}
return fullmap[state][input]
}
start := "q0"
accept := mapset.NewSet("q1")
return NewDFA(states, alphabet, transition, start, accept)
}
func makeOddOnesDFAStringStates() (*DFA, error) {
states := mapset.NewSet("q0", "q1")
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
q0map := map[string]interface{}{"0": "q0", "1": "q1"}
q1map := map[string]interface{}{"0": "q1", "1": "q0"}
fullmap := map[interface{}](map[string]interface{}){"q0": q0map, "q1": q1map}
return fullmap[state][input]
}
start := "q0"
accept := mapset.NewSet("q1")
return NewDFA(states, alphabet, transition, start, accept)
}
func makeEvenZerosDFAStringStates() (*DFA, error) {
states := mapset.NewSet("q0", "q1")
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
q0map := map[string]interface{}{"0": "q1", "1": "q0"}
q1map := map[string]interface{}{"0": "q0", "1": "q1"}
fullmap := map[interface{}](map[string]interface{}){"q0": q0map, "q1": q1map}
return fullmap[state][input]
}
start := "q0"
accept := mapset.NewSet("q0")
return NewDFA(states, alphabet, transition, start, accept)
}
func makeAllZerosOnesStringStates() (*DFA, error) {
states := mapset.NewSet("q0")
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
return "q0"
}
start := "q0"
accept := mapset.NewSet("q0")
return NewDFA(states, alphabet, transition, start, accept)
}
func makeNoneZerosOnesStringStates() (*DFA, error) {
states := mapset.NewSet("q0", "q1")
alphabet := mapset.NewSet("0", "1")
transition := func(state interface{}, input string) (nextState interface{}) {
return "q0"
}
start := "q0"
accept := mapset.NewSet("q1")
return NewDFA(states, alphabet, transition, start, accept)
}