nxu/test-fix16.py

125 lines
3.7 KiB
Python

#!/usr/bin/python
from math import floor, log
from os import environ
from random import randint
from subprocess import Popen, PIPE, run
u8 = {'sz': 1 << 8, 'fmt': b'%02x'}
u16 = {'sz': 1 << 16, 'fmt': b'%04x'}
z16 = {'sz': 1 << 16, 'fmt': b'%04x'}
def eq(got, expected):
return got == expected
def booleq(got, expected):
return bool(got) == bool(expected)
def releq(got, expected):
if (expected - 1) <= got and got <= (expected + 1):
return True
else:
error = abs(got - expected) / (abs(expected) + 0.001)
return error < 0.01
def testcase(p, sym, args, out, f, eq):
vals = []
for name, g in args:
val = randint(0, g['sz'] - 1)
while val == 0 and g is z16:
val = randint(0, g['sz'] - 1)
vals.append((name, g, val))
#vals = [(name, g, randint(0, g['sz'] - 1)) for (name, g) in args]
p.stdin.write(sym)
for _, g, x in vals:
p.stdin.write(g['fmt'] % x)
p.stdin.write(b'\n')
p.stdin.flush()
got = int(p.stdout.readline().strip().decode('utf-8'), 16)
xs = [x for _, _, x in vals]
z = f(*xs)
expected = z
if eq(got, expected):
return None
else:
res = {'got': got, 'expected': expected}
for name, _, x in vals:
res[name] = x
return res
def test(p, trials, sym, args, out, f, eq=eq):
fails = 0
cases = []
for i in range(0, trials):
case1 = testcase(p, sym, args, out, f, eq)
if case1 is not None:
fails += 1
cases.append(case1)
name = sym.decode('utf-8')
if fails == 0:
print('%s passed %d trials' % (name, trials))
else:
print('%s failed %d/%d trials (%r)' % (name, fails, trials, cases))
def pipe():
return Popen(['uxncli', 'run.rom'], stdin=PIPE, stdout=PIPE)
def x16_add(x, y):
return (x + y) % 65536
def x16_sub(x, y):
return (x - y) % 65536
def x16_mul(x, y):
return ((x * y) // 256) % 65536
def x16_div(x, y):
return ((x * 256) // y) % 65536
def x16_quot(x, y):
return x16_div(x, y) & 0xff00
def x16_rem(x, y):
return x % y
def x16_is_whole(x):
return int((x & 0xff) == 0)
def x16_negate(x):
if x == 32768 or x == 0:
return x
else:
return 65536 - x
def x16_eq(x, y):
return x == y
def x16_ne(x, y):
return x != y
def tosigned(x):
return x if x < 32768 else x - 65536
def x16_lt(x, y):
return int(tosigned(x) < tosigned(y))
def x16_gt(x, y):
return int(tosigned(x) > tosigned(y))
def x16_lteq(x, y):
return int(tosigned(x) <= tosigned(y))
def x16_gteq(x, y):
return int(tosigned(x) >= tosigned(y))
def main():
trials = 100000
run(['uxnasm', 'test-fix16.tal', 'run.rom'])
p = pipe()
test(p, trials, b'+', [('x', u16), ('y', u16)], u16, x16_add)
test(p, trials, b'-', [('x', u16), ('y', u16)], u16, x16_sub)
test(p, trials, b'*', [('x', u16), ('y', u16)], u16, x16_mul)
test(p, trials, b'/', [('x', u16), ('y', z16)], u16, x16_div, eq=releq)
test(p, trials, b'\\', [('x', u16), ('y', z16)], u16, x16_quot)
test(p, trials, b'%', [('x', u16), ('y', z16)], u16, x16_rem)
test(p, trials, b'w', [('x', u16)], u8, x16_is_whole, eq=booleq)
test(p, trials, b'N', [('x', u16)], u16, x16_negate)
test(p, trials, b'=', [('x', u16), ('y', u16)], u8, x16_eq)
test(p, trials, b'!', [('x', u16), ('y', u16)], u8, x16_ne)
test(p, trials, b'<', [('x', u16), ('y', u16)], u8, x16_lt)
test(p, trials, b'>', [('x', u16), ('y', u16)], u8, x16_gt)
test(p, trials, b'{', [('x', u16), ('y', u16)], u8, x16_lteq)
test(p, trials, b'}', [('x', u16), ('y', u16)], u8, x16_gteq)
p.stdin.write(b'\n\n')
p.stdin.flush()
p.stdin.close()
p.stdout.close()
p.kill()
if __name__ == "__main__":
main()