-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-tests.py
executable file
·421 lines (342 loc) · 12.2 KB
/
run-tests.py
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python3
import sys
import argparse
from cgi import test
import os
from typing import List, Optional
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
RELEASE_MODE = True
MOONEYE_DIR = f"{SCRIPT_DIR}/test/mooneye-test-suite"
BLARGG_DIR = f"{SCRIPT_DIR}/test/blargg"
RUSTBOY = (
f"{SCRIPT_DIR}/target/release/rustboy-gb"
if RELEASE_MODE
else f"{SCRIPT_DIR}/target/debug/rustboy-gb"
)
TESTS_PER_ROW = 15
PASS_EMOJI = ":green_heart:"
FAIL_EMOJI = ":red_circle:"
SKIPPED_EMOJI = "🙅"
OUTPUT_DIR = SCRIPT_DIR
test_suites = []
try:
import colorama
BRIGHT = colorama.Style.BRIGHT
WHITE = colorama.Fore.WHITE
YELLOW = colorama.Fore.YELLOW
GREEN = colorama.Fore.GREEN
RED = colorama.Fore.RED
RESET_ALL = colorama.Style.RESET_ALL
except ModuleNotFoundError:
BRIGHT = ""
WHITE = ""
YELLOW = ""
GREEN = ""
RED = ""
RESET_ALL = ""
def split_in_chunks(v: List, n: int):
for i in range(0, len(v), n):
yield v[i : i + n]
class Test:
def __init__(
self,
name,
rom_path,
variant: Optional[str] = None,
expect: Optional[str] = None,
machine: str = "dmg",
):
self.name = name
self.rom_path = rom_path
self.result = None
self.variant = variant
self.expect = expect
self.machine = machine
def run(self):
print(f"{BRIGHT}{YELLOW}{self.name}{RESET_ALL}")
machine = f"-m {self.machine}" if self.machine else ""
if self.expect:
print(f"[{self.expect}]")
sys.stdout.flush()
result = os.system(
f'{RUSTBOY} --test-expect="{self.expect}" {machine} "{self.rom_path}"'
)
self.result = result == 0
elif self.variant:
sys.stdout.flush()
result = os.system(
f'{RUSTBOY} --test="{self.variant}" {machine} "{self.rom_path}"'
)
self.result = result == 0
else:
raise Exception("Not sure how to run test")
if self.result:
print(f"{BRIGHT}{GREEN}Pass{RESET_ALL}\n")
else:
print(f"{BRIGHT}{RED}Fail{RESET_ALL}\n")
def skip(self):
self.result = None
def pretty_print(self):
if self.result is None:
res = f"{BRIGHT}{YELLOW}skipped{RESET_ALL}"
elif self.result:
res = f"{BRIGHT}{GREEN}Pass{RESET_ALL}"
else:
res = f"{BRIGHT}{RED}Fail{RESET_ALL}"
print(f"{self.name}: {res}")
class TestGroup:
"""A group of tests"""
"""Name of the test"""
name: str
"""Directory that holds the test ROM's for this group"""
romdir: str
"""All tests in this group"""
tests: List[Test]
def __init__(self, name: str, romdir: str):
self.name = name
self.romdir = romdir
self.tests = []
def run(self, skip: List[str] = []):
for test in self.tests:
if test.name not in skip:
test.run()
else:
test.skip()
def get_roms(self):
"""Return all ROM's in self.romdir in sorted order"""
return [
os.path.join(self.romdir, rom)
for rom in sorted(os.listdir(self.romdir))
if rom.endswith(".gb")
]
def pretty_print(self):
print(f"{BRIGHT}{WHITE}{self.name}:{RESET_ALL}")
if not self.tests:
print("No tests\n")
else:
for test in self.tests:
test.pretty_print()
def add_test(self, rom_name, test_name, path):
raise NotImplemented
def setup(self):
for path in self.get_roms():
rom_name = os.path.basename(path)
test_name = rom_name[:-3]
self.add_test(rom_name, test_name, path)
class TestSuite:
name: str
groups: List[TestGroup]
basedir: str
def __init__(self, name, basedir: str):
self.name = name
self.groups = []
self.basedir = basedir
def run(self, skip: List[str] = []):
for grp in self.groups:
grp.run(skip=skip)
def pretty_print(self):
print(f"{BRIGHT}{WHITE}{self.name}")
print("-" * len(self.name) + RESET_ALL)
for grp in self.groups:
grp.pretty_print()
print()
def build_report(self, with_title=False, tests_per_row=TESTS_PER_ROW):
if with_title:
report = f"## {self.name}\n\n"
else:
report = ""
report += "|" + "|".join([" "] * (tests_per_row + 1)) + "|\n"
report += "|" + "|".join([" :---: "] * (tests_per_row + 1)) + "|\n"
for grp in sorted(self.groups, key=lambda x: x.name):
chunks = [
grp.tests[i : i + tests_per_row]
for i in range(0, len(grp.tests), tests_per_row)
]
first = True
for chunk in chunks:
if first:
report += f"| {grp.name} "
first = False
else:
report += f"| "
for test in chunk:
if test.result is None:
status = SKIPPED_EMOJI
status_text = ": SKIPPED"
elif test.result:
status = PASS_EMOJI
status_text = ": PASS"
else:
status = FAIL_EMOJI
status_text = ": FAIL"
report += f'| [{status}](x "{test.name}{status_text}") '
report += "|\n"
return report
class BlarggTestGroup(TestGroup):
subdir: str
tests: List[Test]
def __init__(self, name, subdir, romdir):
super().__init__(name, romdir)
self.subdir = subdir
def add_test(self, rom_name, test_name, path):
if "cgb" in path:
machine = "cgb"
else:
machine = "dmg"
if test_name == "instr_timing":
expect = f"{test_name}\n\n\nPassed\n"
test = Test(test_name, path, expect=expect, machine=machine)
elif self.romdir.endswith("individual"):
expect = f"{test_name}\n\n\nPassed"
test = Test(test_name, path, expect=expect, machine=machine)
else:
test = Test(test_name, path, variant="blargg", machine=machine)
self.tests.append(test)
class MooneyeTestGroup(TestGroup):
def __init__(self, name, romdir):
super().__init__(name, romdir)
def add_test(self, rom_name, test_name, path):
test = Test(test_name, path, variant="mooneye")
self.tests.append(test)
class BlarggTestSuite(TestSuite):
def __init__(self, basedir):
super().__init__("Blargg Test Suite", basedir=basedir)
def setup(self):
subdirs = [
n
for n in sorted(os.listdir(self.basedir))
if os.path.isdir(os.path.join(self.basedir, n))
]
for dir in subdirs:
# The actual ROM's are kept in a subdirectory called either "individual"
# or "rom_singles", and the way they are validated depends on the name.
romdir = os.path.join(self.basedir, dir, "individual")
if not os.path.exists(romdir):
romdir = os.path.join(self.basedir, dir, "rom_singles")
if not os.path.exists(romdir):
romdir = os.path.join(self.basedir, dir)
grp = BlarggTestGroup(
name=dir,
subdir=os.path.join(self.basedir, dir),
romdir=romdir,
)
grp.setup()
self.groups.append(grp)
class MooneyeTestSuite(TestSuite):
def __init__(self, basedir):
super().__init__("Mooneye Test Suite", basedir=basedir)
def setup(self):
# There are a few first-level test groups in the Mooneye Test Suite,
# but only the "acceptance" group is usable. The rest are for example
# "emulator-only", "manual-only", "madness", etc.
#
# The "acceptance" group has a bunch of ROM's in it, but also a number
# of subtests, so we add the "acceptance" roms as one group, and the
# subtests as separate groups.
acceptance_dir = os.path.join(self.basedir, "acceptance")
emulator_only_dir = os.path.join(self.basedir, "emulator-only")
grp = MooneyeTestGroup(name="acceptance", romdir=acceptance_dir)
self.groups.append(grp)
for name in os.listdir(acceptance_dir):
path = os.path.join(acceptance_dir, name)
if os.path.isdir(path):
grp = MooneyeTestGroup(name=f"acceptance/{name}", romdir=path)
self.groups.append(grp)
for name in os.listdir(emulator_only_dir):
path = os.path.join(emulator_only_dir, name)
if os.path.isdir(path):
grp = MooneyeTestGroup(name=f"emulator-only/{name}", romdir=path)
self.groups.append(grp)
for grp in self.groups:
grp.setup()
class AcidTest:
rom_path: str
success: Optional[bool]
def __init__(self, rom_path: str):
self.rom_path = rom_path
self.success = None
def setup(self):
pass
def run(self):
from PIL import Image, ImageChops, ImageOps
test = Test(name="DMG ACID2", rom_path=self.rom_path, variant="capture")
test.run()
reference_image_path = "./test/dmg-acid2-ref.png"
emulator_image_path = "./capture.png"
diff_image_path = "./dmg-acid2-result.png"
ref = Image.open(reference_image_path).convert(mode="RGB")
emu = Image.open(emulator_image_path).convert(mode="RGB")
assert ref.size == emu.size
width, height = ref.size
refp = ref.load()
emup = emu.load()
if refp is None or emup is None:
print("This should not happen. Test only to quiet the linter.")
return
self.success = True
for y in range(height):
for x in range(width):
if any(abs(aa - bb) > 2 for aa, bb in zip(refp[x, y], emup[x, y])):
emup[x, y] = (255, 0, 0)
self.success = False
emu.save(diff_image_path)
def build_report(self, with_title: bool):
if with_title:
report = f"## dmg-acid2\n"
else:
report = "\n"
if self.success:
report += f"Pass ${PASS_EMOJI}"
else:
report += f"Fail ${FAIL_EMOJI}"
return report
parser = argparse.ArgumentParser(description="Run test suites")
parser.add_argument("suites", type=str, nargs="+", help="Test suite selection")
parser.add_argument("--report", type=str, help="Write report to file")
args = parser.parse_args()
all_suites = "all" in args.suites or len(args.suites) == 0
single_test = not all_suites and len(args.suites) < 2
reports = []
if all_suites or "mooneye" in args.suites:
mooneye = MooneyeTestSuite(MOONEYE_DIR)
mooneye.setup()
mooneye.run(
skip=[
"intr_1_2_timing-GS",
# All the following broke when rewriting the PPU.
# Most likely it's the interrupts that are broken.
"oam_dma_start",
"hblank_ly_scx_timing-GS",
"intr_2_0_timing",
"intr_2_mode0_timing",
"intr_2_mode0_timing_sprites",
"intr_2_mode3_timing",
"intr_2_oam_ok_timing",
"vblank_stat_intr-GS",
]
)
mooneye.pretty_print()
if args.report:
reports.append(
mooneye.build_report(with_title=not single_test, tests_per_row=12)
)
if all_suites or "blargg" in args.suites:
skip = ["interrupt_time"] # CGB only
blargg = BlarggTestSuite(BLARGG_DIR)
blargg.setup()
blargg.run(skip=skip)
blargg.pretty_print()
if args.report:
reports.append(
blargg.build_report(with_title=not single_test, tests_per_row=12)
)
if all_suites or "acid2" in args.suites:
acid = AcidTest("./test/dmg-acid2.gb")
acid.setup()
acid.run()
if args.report:
reports.append(acid.build_report(with_title=not single_test))
if len(reports) > 0:
with open(args.report, "w") as f:
for report in reports:
f.write(report)