-
Notifications
You must be signed in to change notification settings - Fork 136
/
button_test.go
300 lines (254 loc) · 8.25 KB
/
button_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
package engo
import "testing"
type btnState struct {
down bool
justUp bool
justDown bool
}
// Button configuretion used when testing.
var btnSimpleCfg = [6]Button{
{Triggers: []Key{KeyA, KeyC}, Name: "Button 1"},
{Triggers: []Key{KeyB, KeyD}, Name: "Button 2"},
{Triggers: []Key{KeyF2, KeyF5}, Name: "Button 3"},
{Triggers: []Key{KeyF4, KeyF6}, Name: "Button 4"},
{Triggers: []Key{KeyOne, KeyFour}, Name: "Button 5"},
{Triggers: []Key{KeyTwo, KeyFive}, Name: "Button 6"},
}
// Expected button state @ pass 0
var btnPass0 = [6]btnState{
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
}
// Expected button state @ pass 1
var btnPass1 = [6]btnState{
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: true},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: true},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: true},
}
// Expected button state @ pass 2
var btnPass2 = [6]btnState{
{down: false, justUp: false, justDown: false},
{down: true, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: true, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: true, justUp: false, justDown: false},
}
// Expected button state @ pass 3
var btnPass3 = [6]btnState{
{down: false, justUp: false, justDown: false},
{down: false, justUp: true, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: true, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: true, justDown: false},
}
// Expected button state @ pass 4
var btnPass4 = [6]btnState{
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
{down: false, justUp: false, justDown: false},
}
// Checks the state of all configured buttons against the expected state.
func runBtnChecks(msg string, t *testing.T, expect [6]btnState) {
for i, cfg := range btnSimpleCfg {
exp := expect[i]
btn := Input.Button(cfg.Name)
if exp.down != btn.Down() {
t.Error(msg, " Invalid on: ", cfg.Name, " - Down")
}
if exp.justUp != btn.JustReleased() {
t.Error(msg, " Invalid on: ", cfg.Name, " - Just Up")
}
if exp.justDown != btn.JustPressed() {
t.Error(msg, " Invalid on: ", cfg.Name, " - Just Down")
}
}
}
// Test configured axes using a single key on one button.
func TestButtonSimple(t *testing.T) {
Input = NewInputManager()
for _, cfg := range btnSimpleCfg {
Input.RegisterButton(cfg.Name, cfg.Triggers[0])
}
runBtnChecks("Init (0.0)", t, btnPass0)
// Empty update pass0
Input.update()
runBtnChecks("Pass (0.1)", t, btnPass0)
Input.update()
runBtnChecks("Pass (0.2)", t, btnPass0)
Input.update()
runBtnChecks("Pass (0.3)", t, btnPass0)
// Set even true pass1
Input.update()
Input.keys.Set(btnSimpleCfg[1].Triggers[0], true)
Input.keys.Set(btnSimpleCfg[3].Triggers[0], true)
Input.keys.Set(btnSimpleCfg[5].Triggers[0], true)
// FixMe: this causes an error ? Because the the static
// arrays get filled with values before engo rewrites them!
//Input.keys.Set(F10, true)
runBtnChecks("Pass (1.0)", t, btnPass1)
// Keeps state on pass2
Input.update()
runBtnChecks("Pass (2.0)", t, btnPass2)
Input.update()
runBtnChecks("Pass (2.1)", t, btnPass2)
Input.update()
runBtnChecks("Pass (2.2)", t, btnPass2)
Input.update()
runBtnChecks("Pass (2.3)", t, btnPass2)
// Set even true pass3
Input.update()
Input.keys.Set(btnSimpleCfg[1].Triggers[0], false)
Input.keys.Set(btnSimpleCfg[3].Triggers[0], false)
Input.keys.Set(btnSimpleCfg[5].Triggers[0], false)
runBtnChecks("Pass (3.0)", t, btnPass3)
// Keeps state on pass4
Input.update()
runBtnChecks("Pass (4.0)", t, btnPass4)
Input.update()
runBtnChecks("Pass (4.1)", t, btnPass4)
Input.update()
runBtnChecks("Pass (4.2)", t, btnPass4)
Input.update()
runBtnChecks("Pass (4.3)", t, btnPass4)
}
// Test configured axes using a multiple keys on one button.
func TestButtonComplex(t *testing.T) {
Input = NewInputManager()
for _, cfg := range btnSimpleCfg {
Input.RegisterButton(cfg.Name, cfg.Triggers[0], cfg.Triggers[1])
}
runBtnChecks("Init (0.0)", t, btnPass0)
// Empty update pass0
Input.update()
runBtnChecks("Pass (0.1)", t, btnPass0)
Input.update()
runBtnChecks("Pass (0.2)", t, btnPass0)
Input.update()
runBtnChecks("Pass (0.3)", t, btnPass0)
// Set even true pass1
Input.update()
Input.keys.Set(btnSimpleCfg[1].Triggers[0], true)
Input.keys.Set(btnSimpleCfg[3].Triggers[0], true)
Input.keys.Set(btnSimpleCfg[5].Triggers[0], true)
runBtnChecks("Pass (1.0)", t, btnPass1)
// Keeps state on pass2
Input.update()
runBtnChecks("Pass (2.0)", t, btnPass2)
Input.update()
runBtnChecks("Pass (2.1)", t, btnPass2)
Input.update()
runBtnChecks("Pass (2.2)", t, btnPass2)
Input.update()
runBtnChecks("Pass (2.3)", t, btnPass2)
// Set even true pass3
Input.update()
Input.keys.Set(btnSimpleCfg[1].Triggers[0], false)
Input.keys.Set(btnSimpleCfg[3].Triggers[0], false)
Input.keys.Set(btnSimpleCfg[5].Triggers[0], false)
runBtnChecks("Pass (3.0)", t, btnPass3)
// Keeps state on pass4
Input.update()
runBtnChecks("Pass (4.0)", t, btnPass4)
Input.update()
runBtnChecks("Pass (4.1)", t, btnPass4)
Input.update()
runBtnChecks("Pass (4.2)", t, btnPass4)
Input.update()
runBtnChecks("Pass (4.3)", t, btnPass4)
// Set even true pass1 alt
Input.update()
Input.keys.Set(btnSimpleCfg[1].Triggers[1], true)
Input.keys.Set(btnSimpleCfg[3].Triggers[1], true)
Input.keys.Set(btnSimpleCfg[5].Triggers[1], true)
runBtnChecks("Pass alt (1.0)", t, btnPass1)
// Keeps state on pass2 alt
Input.update()
runBtnChecks("Pass alt (2.0)", t, btnPass2)
Input.update()
runBtnChecks("Pass alt (2.1)", t, btnPass2)
Input.update()
runBtnChecks("Pass alt (2.2)", t, btnPass2)
Input.update()
runBtnChecks("Pass alt (2.3)", t, btnPass2)
// Set even true pass3 alt
Input.update()
Input.keys.Set(btnSimpleCfg[1].Triggers[1], false)
Input.keys.Set(btnSimpleCfg[3].Triggers[1], false)
Input.keys.Set(btnSimpleCfg[5].Triggers[1], false)
runBtnChecks("Pass alt (3.0)", t, btnPass3)
// Keeps state on pass4 alt
Input.update()
runBtnChecks("Pass alt (4.0)", t, btnPass4)
Input.update()
runBtnChecks("Pass alt (4.1)", t, btnPass4)
Input.update()
runBtnChecks("Pass alt (4.2)", t, btnPass4)
Input.update()
runBtnChecks("Pass alt (4.3)", t, btnPass4)
}
// Used to store results when benchmarking.
var btnResult [6]btnState
func checkBtnConfigOptimal(b *testing.B) {
for i, cfg := range btnSimpleCfg {
btn := Input.Button(cfg.Name)
btnResult[i].down = btn.Down()
btnResult[i].justUp = btn.JustReleased()
btnResult[i].justDown = btn.JustPressed()
}
}
// Benchmark sub-optimal state checks
func BenchmarkInputMgr_ButtonCleanState(b *testing.B) {
Input = NewInputManager()
for _, cfg := range btnSimpleCfg {
Input.RegisterButton(cfg.Name, cfg.Triggers[0], cfg.Triggers[1])
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
checkBtnConfigOptimal(b)
}
}
// Benchmark sub-optimal state checks
func BenchmarkInputMgr_ButtonFilledState(b *testing.B) {
Input = NewInputManager()
keyFillManager(Input.keys)
for _, cfg := range btnSimpleCfg {
Input.RegisterButton(cfg.Name, cfg.Triggers[0], cfg.Triggers[1])
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
checkBtnConfigOptimal(b)
}
}
// Disabled but around when needed
//func checkBtnConfigSubOptimal(b *testing.B) {
// for i, cfg := range btnSimpleCfg {
// btnResult[i].down = Input.Button(cfg.Name).Down()
// btnResult[i].justUp = Input.Button(cfg.Name).JustReleased()
// btnResult[i].justDown = Input.Button(cfg.Name).JustPressed()
// }
//}
// Benchmark sub-optimal state checks
//func BenchmarkInputMgr_ButtonSubOptimal(b *testing.B) {
// Input = NewInputManager()
//
// for _, cfg := range btnSimpleCfg {
// Input.RegisterButton(cfg.Name, cfg.Triggers[0], cfg.Triggers[1])
// }
//
// b.ResetTimer()
// for n := 0; n < b.N; n++ {
// checkBtnConfigSubOptimal(b)
// }
//}