-
Notifications
You must be signed in to change notification settings - Fork 4
/
dfa_test.go
74 lines (55 loc) · 1.39 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
package dfa
import "testing"
func TestBasic(t *testing.T) {
dfa := NewDFA(0, false)
dfa.AddState(1, false)
dfa.AddState(2, true)
dfa.AddTransition(0, "a", 1)
dfa.AddTransition(1, "b", 2)
if ret := dfa.Input("a"); ret != 1 {
t.Errorf("Expect 1, but get %d\n", ret)
}
if ret := dfa.Input("b"); ret != 2 {
t.Errorf("Expect 2, but get %d\n", ret)
}
if !dfa.Verify() {
t.Errorf("Verify is failed")
}
}
func TestVerifyInputs(t *testing.T) {
dfa := NewDFA(0, false)
dfa.AddState(1, false)
dfa.AddState(2, true)
dfa.AddTransition(0, "a", 1)
dfa.AddTransition(1, "b", 2)
var inputs []string
inputs = append(inputs, "a")
inputs = append(inputs, "b")
if !dfa.VerifyInputs(inputs) {
t.Errorf("Verify Inputs is failed")
}
dfa.PrintTransitionTable()
}
func TestAdvanceDFA(t *testing.T) {
dfa := NewDFA(0, false)
dfa.AddState(1, true)
dfa.AddState(2, false)
dfa.AddTransition(0, "0", 0)
dfa.AddTransition(0, "1", 1)
dfa.AddTransition(1, "0", 0)
dfa.AddTransition(1, "1", 2)
dfa.AddTransition(2, "0", 2)
dfa.AddTransition(2, "1", 2)
dfa.PrintTransitionTable()
inputs := []string{"0", "0", "1", "0", "1"}
if !dfa.VerifyInputs(inputs) {
t.Errorf("Verify inputs is failed")
}
//Reset the DFA for another verification
dfa.Reset()
//Test go to dead state 2
inputs2 := []string{"1", "1", "0", "0", "0"}
if dfa.VerifyInputs(inputs2) {
t.Errorf("Verify inputs is failed")
}
}