-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsm_test.go
226 lines (200 loc) · 5.45 KB
/
fsm_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
package fsm
import "testing"
const (
test_input_1 = iota
test_input_2
test_input_3
)
const (
test_state_1 = iota
test_state_2
test_state_3
)
func assertState(t *testing.T, fsm *FSM, i Input, s int) {
oldState := fsm.current
err := fsm.Spin(i)
if err != nil {
t.Fatal(err.Error())
return
}
if fsm.current != s {
t.Errorf("FSM in wrong state. (Input: %v, State: %v -> %v)", i, oldState, s)
}
}
func TestStates(t *testing.T) {
state1 := State{
Index: test_state_1,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2, NO_ACTION},
test_input_2: Outcome{test_state_3, NO_ACTION},
test_input_3: Outcome{test_state_1, NO_ACTION},
},
}
state2 := State{
Index: test_state_2,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_1, NO_ACTION},
test_input_2: Outcome{test_state_1, NO_ACTION},
test_input_3: Outcome{test_state_1, NO_ACTION},
},
}
state3 := State{
Index: test_state_3,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2, NO_ACTION},
test_input_2: Outcome{test_state_1, NO_ACTION},
test_input_3: Outcome{test_state_1, NO_ACTION},
},
}
fsm, err := Define(state1, state2, state3)
if err != nil {
t.Fatal("Failed to define FSM: ", err)
}
t.Log("1: 1 -> 2")
assertState(t, fsm, test_input_1, test_state_2)
t.Log("3: 2 -> 1")
assertState(t, fsm, test_input_3, test_state_1)
t.Log("2: 1 -> 3")
assertState(t, fsm, test_input_2, test_state_3)
t.Log("1: 3 -> 2")
assertState(t, fsm, test_input_1, test_state_2)
t.Log("2: 2 -> 1")
assertState(t, fsm, test_input_2, test_state_1)
t.Log("3: 1 -> 1")
assertState(t, fsm, test_input_3, test_state_1)
}
// Test actions which return next inputs.
func TestChain(t *testing.T) {
func1_hit := false
func2_hit := false
func3_hit := false
state1 := State{
Index: test_state_1,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2,
func() Input { func1_hit = true; return test_input_2 }},
test_input_2: Outcome{test_state_3,
func() Input { func3_hit = true; return NO_INPUT }},
test_input_3: Outcome{test_state_1, NO_ACTION},
},
}
state2 := State{
Index: test_state_2,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_1, NO_ACTION},
test_input_2: Outcome{test_state_1,
func() Input { func2_hit = true; return test_input_2 }},
test_input_3: Outcome{test_state_1, NO_ACTION},
},
}
state3 := State{
Index: test_state_3,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2, NO_ACTION},
test_input_2: Outcome{test_state_1, NO_ACTION},
test_input_3: Outcome{test_state_1, NO_ACTION},
},
}
fsm, err := Define(state1, state2, state3)
if err != nil {
t.Fatal("Failed to define FSM: ", err)
}
// Spin with input 1.
// The state chain should go 1 -> 2 -> 1 -> 3 and all 3 functions should have been hit.
assertState(t, fsm, test_input_1, test_state_3)
if !(func1_hit && func2_hit && func3_hit) {
t.Errorf("Didn't hit all 3 actions. 1: %v, 2: %v, 3: %v.", func1_hit, func2_hit, func3_hit)
}
}
func TestImpossibleState(t *testing.T) {
state1 := State{
Index: test_state_1,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2, NO_ACTION},
test_input_2: Outcome{test_state_3, NO_ACTION},
},
}
state2 := State{
Index: test_state_2,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_1, NO_ACTION},
test_input_2: Outcome{test_state_1, NO_ACTION},
},
}
fsm, err := Define(state1, state2)
if err != nil {
t.Fatal("Failed to define FSM: ", err)
}
// Put the FSM in an impossible state.
fsm.current = test_state_3
err = fsm.Spin(test_input_1)
if err == nil {
t.Fatalf("FSM didn't error when spun in impossible state.")
}
switch err.(type) {
case ImpossibleStateError:
t.Log("FSM corrently returned error: %v", err.Error())
default:
t.Fatalf("FSM returned wrong error type: %T", err)
}
}
func TestInvalidInput(t *testing.T) {
state1 := State{
Index: test_state_1,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2, NO_ACTION},
test_input_2: Outcome{test_state_3, NO_ACTION},
},
}
state2 := State{
Index: test_state_2,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_1, NO_ACTION},
test_input_2: Outcome{test_state_1, NO_ACTION},
},
}
fsm, err := Define(state1, state2)
if err != nil {
t.Fatal("Failed to define FSM: ", err)
}
// Spin with invalid input value.
err = fsm.Spin(test_input_3)
if err == nil {
t.Fatalf("FSM didn't error when spun with invalid input.")
}
switch err.(type) {
case InvalidInputError:
t.Log("FSM corrently returned error: %v", err.Error())
default:
t.Fatalf("FSM returned wrong error type: %T", err)
}
}
// Test that we error if you try to create an FSM with clashing states.
func TestStateClash(t *testing.T) {
// Define two states with the same index.
state1 := State{
Index: test_state_1,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_2, NO_ACTION},
test_input_2: Outcome{test_state_3, NO_ACTION},
},
}
state2 := State{
Index: test_state_1,
Outcomes: map[Input]Outcome{
test_input_1: Outcome{test_state_1, NO_ACTION},
test_input_2: Outcome{test_state_1, NO_ACTION},
},
}
// We should error on this define.
_, err := Define(state1, state2)
if err == nil {
t.Fatalf("Didn't error creating FSM with clashing states.")
}
switch err.(type) {
case ClashingStateError:
t.Log("FSM corrently returned error: %v", err.Error())
default:
t.Fatalf("FSM returned wrong error type: %T", err)
}
}