nxu/tester.py

51 lines
1.3 KiB
Python
Raw Normal View History

2021-12-25 23:07:41 -05:00
#!/usr/bin/python
from os import environ
from subprocess import Popen, PIPE
from random import randint
ubyte = {'sz': 1 << 8, 'fmt': b'%02x'}
ushort = {'sz': 1 << 16, 'fmt': b'%04x'}
ulong = {'sz': 1 << 32, 'fmt': b'%08x'}
def u32(x):
return x % 4294967296
def case(p, x, y, sym, f):
p.stdin.write(b'%s %08x %08x\n' % (sym, x, y))
p.stdin.flush()
got = p.stdout.readline().strip().decode('utf-8')
expected = '%08x' % u32(f(x, y))
return got == expected
def test(p, runs, sym, f):
fails = 0
cases = []
maximum = (1 << 32) - 1
for i in range(0, runs):
x = randint(0, maximum)
y = randint(0, maximum)
ok = case(p, x, y, sym, f)
if not ok:
fails += 1
cases.append((x, y))
name = sym.decode('utf-8')
if fails == 0:
print('%s passed %d runs' % (name, runs))
else:
print('%s failed %d/%d runs (%r)' % (name, fails, runs, cases))
def main():
home = environ['HOME']
p = Popen([home + '/w/uxn/bin/uxncli', 'run.rom'], stdin=PIPE, stdout=PIPE)
runs = 1000
test(p, runs, b'+', lambda x, y: x + y)
test(p, runs, b'-', lambda x, y: x - y)
test(p, runs, b'*', lambda x, y: x * y)
#test(p, runs, b'L', lambda x, y: x << y)
p.stdin.close()
p.stdout.close()
if __name__ == "__main__":
main()