-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnfa_test.go
183 lines (155 loc) · 3.43 KB
/
nfa_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
// Based on https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton
//
// === RUN TestNfa
// === RUN TestNfa/test_101010_(OK)
// [state] +StepX +Start
// [state] +Input1
// [state] +Input1Done
// [state] +Input0
// [state] +Input0Done
// [state] +Input1
// [state] +Step0 -StepX
// [state] +Input1Done
// [state] +Input0
// [state] +Step1 -Step0
// [state] +Input0Done
// [state] +Input1
// [state] +Step2 -Step1
// [state] +Input1Done
// [state] +Input0
// [state] +Step3 -Step2
// [state] +Input0Done
// [state] +Ready
// [state] -Start -Input0 -Input0Done -Input1 -Input1Done -Ready
// --- PASS: TestNfa/test_101010_(OK) (0.00s)
// --- PASS: TestNfa (1.01s)
// PASS
package nfa
import (
"context"
"strings"
"testing"
"time"
"github.com/pancsta/asyncmachine-go/examples/nfa/states"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
)
var (
ss = states.NfaStates
sg = states.NfaGroups
)
func init() {
// DEBUG
//
// - am-dbg is required for TUI debugging, you can go run it
// - go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest
// - enable stdout logging
//
// amhelp.EnableDebugging(true)
// amhelp.SetLogLevel(am.LogOps)
}
// handlers
type Regexp struct {
*am.ExceptionHandler
input string
cursor int
}
func (r *Regexp) StartState(e *am.Event) {
r.input = e.Args["input"].(string)
mach := e.Machine()
go func() {
for _, c := range strings.Split(r.input, "") {
if c == "0" {
mach.Add1(ss.Input0, nil)
<-mach.When1(ss.Input0Done, nil)
} else if c == "1" {
mach.Add1(ss.Input1, nil)
<-mach.When1(ss.Input1Done, nil)
}
}
mach.Add1(ss.Ready, nil)
}()
}
func (r *Regexp) Input0State(e *am.Event) {
mach := e.Machine()
defer func() { r.cursor++ }()
defer mach.Add1(ss.Input0Done, nil)
switch mach.Switch(sg.Steps) {
case ss.StepX:
mach.Add1(ss.StepX, nil)
case ss.Step0:
mach.Add1(ss.Step1, nil)
case ss.Step1:
mach.Add1(ss.Step2, nil)
case ss.Step2:
mach.Add1(ss.Step3, nil)
}
}
func (r *Regexp) Input1State(e *am.Event) {
mach := e.Machine()
defer func() { r.cursor++ }()
defer mach.Add1(ss.Input1Done, nil)
switch mach.Switch(sg.Steps) {
case ss.StepX:
// TODO should use CanAdd and Step0Enter
if r.cursor+3 == len(r.input)-1 {
mach.Add1(ss.Step0, nil)
} else {
mach.Add1(ss.StepX, nil)
}
case ss.Step0:
mach.Add1(ss.Step1, nil)
case ss.Step1:
mach.Add1(ss.Step2, nil)
case ss.Step2:
mach.Add1(ss.Step3, nil)
}
}
// example
func TestNfa(t *testing.T) {
ctx := context.Background()
// tests
tests := []struct {
name string
input string
expect bool
}{
{
name: "test 101010 (OK)",
input: "101010",
expect: true,
},
{
name: "test 1010 (OK)",
input: "1010",
expect: true,
},
{
name: "test 100010 (NOT OK)",
input: "100010",
expect: false,
},
}
// code
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mach, err := am.NewCommon(ctx, "nfa-"+tt.name, states.NfaStruct, ss.Names(), &Regexp{}, nil, nil)
if err != nil {
t.Fatal(err)
}
mach.SetLogId(false)
amhelp.MachDebugEnv(mach)
mach.Add1(ss.Start, am.A{"input": tt.input})
<-mach.When1(ss.Ready, nil)
// assert
if tt.expect && mach.Not1(ss.Step3) {
t.Fatal("Expected Step3")
} else if !tt.expect && mach.Is1(ss.Step3) {
t.Fatal("Didn't expect Step3")
}
mach.Remove1(ss.Start, nil)
})
}
// am-dbg
time.Sleep(time.Second)
}