Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/Assets/Lua/UnitTests/Joypad_RoundTrip.lua
2 views
1
console.clear()
2
console.writeline("Unit test for joypad.get and joypad.set")
3
console.writeline("Core Required: NES (or any multi-player core with U,D,L,R,select,start,A,B as buttons)")
4
console.writeline("")
5
6
local test_event
7
local test_function
8
local test_frame = 0
9
10
function test_1()
11
-- clear previous input
12
if test_frame < 4 then
13
return
14
end
15
16
local test_case = "test_1: set/get round trip"
17
local test_result = false
18
19
-- set expected input
20
joypad.set({ ["P1 A"] = true, ["P1 B"] = true, ["P1 Select"] = true })
21
22
-- get actual input
23
local t = joypad.get()
24
25
-- assert_equal
26
if t["P1 A"] and t["P1 B"] and t["P1 Select"] then
27
test_result = true
28
end
29
30
-- finish
31
console.writeline(string.format("%s (%s): %s", test_case, test_event, test_result and "OK" or "FAILED"))
32
-- if not test_result then
33
-- console.writeline("Returned Value:")
34
-- console.writeline(t)
35
-- end
36
37
return next_test()
38
end
39
40
local test_2_first = true
41
local test_2_start_pressed = false
42
function test_2()
43
if test_frame < 4 then
44
return
45
end
46
47
local test_case = "test_2: get/set round trip"
48
if test_2_first then
49
test_2_first = false
50
51
console.writeline(test_case)
52
console.writeline("Does joypad work normally?")
53
console.writeline("Press 'P1 Start' if it works good.")
54
end
55
56
if test_frame == 4 then
57
console.writeline(string.format("%s: running...", test_event))
58
end
59
60
-- round trip (no changes)
61
local t = joypad.get()
62
joypad.set(t)
63
64
if t["P1 Start"] then
65
test_2_start_pressed = true
66
else
67
if test_2_start_pressed then
68
test_2_start_pressed = false
69
next_test()
70
end
71
end
72
end
73
74
local test_3_first = true
75
function test_3()
76
local test_case = "test_3: test if joypad.get() == movie.getinput(now)?"
77
78
if test_3_first then
79
test_3_first = false
80
console.writeline(test_case)
81
end
82
83
console.writeline(string.format("%s: test code is not available yet: SKIPPED", test_event))
84
next_test()
85
end
86
87
local tests = { test_1, test_2, test_3 }
88
local events = { "event.onframestart", "event.onframeend", "emu.frameadvance" }
89
90
local test_event_index = 1
91
local test_function_index = 1
92
test_event = events[1]
93
test_function = tests[1]
94
95
function test_reset()
96
test_frame = 0
97
end
98
test_reset()
99
100
function next_test()
101
test_event_index = test_event_index + 1
102
if test_event_index > #events then
103
console.writeline("")
104
test_event_index = 1
105
test_function_index = test_function_index + 1
106
if test_function_index > #tests then
107
console.writeline("Test Finished.")
108
console.writeline("")
109
error("Done.")
110
end
111
end
112
test_event = events[test_event_index]
113
test_function = tests[test_function_index]
114
test_reset()
115
end
116
117
event.onframestart(function()
118
if test_event == "event.onframestart" then
119
test_function()
120
test_frame = test_frame + 1
121
end
122
end)
123
124
event.onframeend(function()
125
if test_event == "event.onframeend" then
126
test_function()
127
test_frame = test_frame + 1
128
end
129
end)
130
131
while true do
132
if test_event == "emu.frameadvance" then
133
test_function()
134
test_frame = test_frame + 1
135
end
136
emu.frameadvance()
137
end
138
139