tests: Split out those tests requiring float and import.
Tests in basics (which should probably be renamed to core) should not rely on float, or import any non-built-in files. This way these tests can be run when those features are not available. All test in basics now pass on the pyboard using stmhal port, except for string-repr which has some issues with character hex printing.
This commit is contained in:
parent
d7a4b69039
commit
5cd0b2227f
|
@ -2,3 +2,7 @@ This directory contains tests for various functionality areas of MicroPython.
|
|||
To run all stable tests, run "run-tests" script in this directory. Note
|
||||
that bytecode tests are not yet stable and should be run separately in
|
||||
"bytecode" subdirectory.
|
||||
|
||||
When creating new tests, anything that relies on float support should go in the
|
||||
float/ subdirectory. Anything that relies on import x, where x is not a built-in
|
||||
module, should go in the import/ subdirectory.
|
||||
|
|
|
@ -1,8 +1,3 @@
|
|||
try:
|
||||
1 / 0
|
||||
except ZeroDivisionError:
|
||||
print("ZeroDivisionError")
|
||||
|
||||
try:
|
||||
1 // 0
|
||||
except ZeroDivisionError:
|
||||
|
|
|
@ -22,8 +22,3 @@ print(x)
|
|||
print(x[1:])
|
||||
print(x[:-1])
|
||||
print(x[2:3])
|
||||
|
||||
try:
|
||||
print(x[1.0])
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# check modulo matches python definition
|
||||
# This test compiler version
|
||||
|
||||
# this tests compiler constant folding
|
||||
print(123 % 7)
|
||||
print(-123 % 7)
|
||||
print(123 % -7)
|
||||
|
@ -20,17 +21,3 @@ print(a % b)
|
|||
print(a % -b)
|
||||
print(-a % b)
|
||||
print(-a % -b)
|
||||
|
||||
if False:
|
||||
print(1.23456 % 0.7)
|
||||
print(-1.23456 % 0.7)
|
||||
print(1.23456 % -0.7)
|
||||
print(-1.23456 % -0.7)
|
||||
|
||||
a = 1.23456
|
||||
b = 0.7
|
||||
print(a % b)
|
||||
print(a % -b)
|
||||
print(-a % b)
|
||||
print(-a % -b)
|
||||
|
||||
|
|
|
@ -23,11 +23,9 @@ except TypeError:
|
|||
|
||||
print("%s" % True)
|
||||
print("%s" % 1)
|
||||
print("%s" % 1.0)
|
||||
|
||||
print("%r" % True)
|
||||
print("%r" % 1)
|
||||
print("%r" % 1.0)
|
||||
|
||||
print("%c" % 48)
|
||||
print("%c" % 'a')
|
||||
|
@ -37,28 +35,16 @@ print("%d" % 10)
|
|||
print("%+d" % 10)
|
||||
print("% d" % 10)
|
||||
print("%d" % -10)
|
||||
print("%d" % 1.0)
|
||||
print("%d" % True)
|
||||
print("%i" % -10)
|
||||
print("%i" % 1.0)
|
||||
print("%i" % True)
|
||||
print("%u" % -10)
|
||||
print("%u" % 1.0)
|
||||
print("%u" % True)
|
||||
print("%x" % 18)
|
||||
print("%x" % 18.0)
|
||||
print("%o" % 18)
|
||||
print("%o" % 18.0)
|
||||
print("%X" % 18)
|
||||
print("%X" % 18.0)
|
||||
print("%#x" % 18)
|
||||
print("%#X" % 18)
|
||||
print("%#6o" % 18)
|
||||
print("%#6x" % 18)
|
||||
print("%#06x" % 18)
|
||||
print("%e" % 1.23456)
|
||||
print("%E" % 1.23456)
|
||||
print("%f" % 1.23456)
|
||||
print("%F" % 1.23456)
|
||||
print("%g" % 1.23456)
|
||||
print("%G" % 1.23456)
|
||||
|
|
|
@ -60,26 +60,6 @@ test("{:@<6d}", -123)
|
|||
test("{:@=6d}", -123)
|
||||
test("{:06d}", -123)
|
||||
|
||||
test("{:10.4e}", 123.456)
|
||||
test("{:10.4e}", -123.456)
|
||||
test("{:10.4f}", 123.456)
|
||||
test("{:10.4f}", -123.456)
|
||||
test("{:10.4g}", 123.456)
|
||||
test("{:10.4g}", -123.456)
|
||||
test("{:e}", 100)
|
||||
test("{:f}", 200)
|
||||
test("{:g}", 300)
|
||||
|
||||
test("{:10.4E}", 123.456)
|
||||
test("{:10.4E}", -123.456)
|
||||
test("{:10.4F}", 123.456)
|
||||
test("{:10.4F}", -123.456)
|
||||
test("{:10.4G}", 123.456)
|
||||
test("{:10.4G}", -123.456)
|
||||
|
||||
# The following fails right now
|
||||
#test("{:10.1}", 0.0)
|
||||
|
||||
def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
|
||||
fmt = '{'
|
||||
if conv:
|
||||
|
@ -137,71 +117,4 @@ if full_tests:
|
|||
for str in ('', 'a', 'bcd', 'This is a test with a longer string'):
|
||||
test_fmt(conv, fill, alignment, '', '', width, '', 's', str)
|
||||
|
||||
eg_nums = (0.0, -0.0, 0.1, 1.234, 12.3459, 1.23456789, 123456789.0, -0.0,
|
||||
-0.1, -1.234, -12.3459, 1e4, 1e-4, 1e5, 1e-5, 1e6, 1e-6, 1e10,
|
||||
1e37, -1e37, 1e-37, -1e-37,
|
||||
1.23456e8, 1.23456e7, 1.23456e6, 1.23456e5, 1.23456e4, 1.23456e3, 1.23456e2, 1.23456e1, 1.23456e0,
|
||||
1.23456e-1, 1.23456e-2, 1.23456e-3, 1.23456e-4, 1.23456e-5, 1.23456e-6, 1.23456e-7, 1.23456e-8,
|
||||
-1.23456e8, -1.23456e7, -1.23456e6, -1.23456e5, -1.23456e4, -1.23456e3, -1.23456e2, -1.23456e1, -1.23456e0,
|
||||
-1.23456e-1, -1.23456e-2, -1.23456e-3, -1.23456e-4, -1.23456e-5, -1.23456e-6, -1.23456e-7, -1.23456e-8)
|
||||
|
||||
if full_tests:
|
||||
for type in ('e', 'E', 'g', 'G', 'n'):
|
||||
for width in ('', '4', '6', '8', '10'):
|
||||
for alignment in ('', '<', '>', '=', '^'):
|
||||
for fill in ('', '@', '0', ' '):
|
||||
for sign in ('', '+', '-', ' '):
|
||||
for prec in ('', '1', '3', '6'):
|
||||
for num in eg_nums:
|
||||
test_fmt('', fill, alignment, sign, '', width, prec, type, num)
|
||||
|
||||
# Note: We use 1.23459 rather than 1.2345 because '{:3f}'.format(1.2345)
|
||||
# rounds differently than print("%.3f", 1.2345);
|
||||
|
||||
f_nums = (0.0, -0.0, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0,
|
||||
0.0012, 0.0123, 0.1234, 1.23459, 12.3456,
|
||||
-0.0001, -0.001, -0.01, -0.1, -1.0, -10.0,
|
||||
-0.0012, -0.0123, -0.1234, -1.23459, -12.3456)
|
||||
|
||||
if full_tests:
|
||||
for type in ('f', 'F'):
|
||||
for width in ('', '4', '6', '8', '10'):
|
||||
for alignment in ('', '<', '>', '=', '^'):
|
||||
for fill in ('', ' ', '0', '@'):
|
||||
for sign in ('', '+', '-', ' '):
|
||||
# An empty precision defaults to 6, but when uPy is
|
||||
# configured to use a float, we can only use a
|
||||
# precision of 6 with numbers less than 10 and still
|
||||
# get results that compare to CPython (which uses
|
||||
# long doubles).
|
||||
for prec in ('1', '2', '3'):
|
||||
for num in f_nums:
|
||||
test_fmt('', fill, alignment, sign, '', width, prec, type, num)
|
||||
for num in int_nums2:
|
||||
test_fmt('', fill, alignment, sign, '', width, '', type, num)
|
||||
|
||||
pct_nums1 = (0.1, 0.58, 0.99, -0.1, -0.58, -0.99)
|
||||
pct_nums2 = (True, False, 1, 0, -1)
|
||||
|
||||
if full_tests:
|
||||
type = '%'
|
||||
for width in ('', '4', '6', '8', '10'):
|
||||
for alignment in ('', '<', '>', '=', '^'):
|
||||
for fill in ('', ' ', '0', '@'):
|
||||
for sign in ('', '+', '-', ' '):
|
||||
# An empty precision defaults to 6, but when uPy is
|
||||
# configured to use a float, we can only use a
|
||||
# precision of 6 with numbers less than 10 and still
|
||||
# get results that compare to CPython (which uses
|
||||
# long doubles).
|
||||
for prec in ('1', '2', '3'):
|
||||
for num in pct_nums1:
|
||||
test_fmt('', fill, alignment, sign, '', width, prec, type, num)
|
||||
for num in pct_nums2:
|
||||
test_fmt('', fill, alignment, sign, '', width, '', type, num)
|
||||
|
||||
# We don't currently test a type of '' with floats (see the detailed comment
|
||||
# in objstr.c)
|
||||
|
||||
# TODO Add tests for erroneous format strings.
|
||||
|
||||
|
|
|
@ -9,12 +9,6 @@ if not None:
|
|||
if not 0:
|
||||
print("0")
|
||||
|
||||
if not 0.0:
|
||||
print("float 0")
|
||||
|
||||
if not 0+0j:
|
||||
print("complex 0")
|
||||
|
||||
if not "":
|
||||
print("Empty string")
|
||||
if "foo":
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
print(bool)
|
||||
print(int)
|
||||
print(float)
|
||||
print(complex)
|
||||
print(tuple)
|
||||
print(list)
|
||||
print(set)
|
||||
|
@ -11,8 +9,6 @@ print(dict)
|
|||
|
||||
print(type(bool()) == bool)
|
||||
print(type(int()) == int)
|
||||
print(type(float()) == float)
|
||||
print(type(complex()) == complex)
|
||||
print(type(tuple()) == tuple)
|
||||
print(type(list()) == list)
|
||||
print(type(set()) == set)
|
||||
|
@ -20,8 +16,6 @@ print(type(dict()) == dict)
|
|||
|
||||
print(type(False) == bool)
|
||||
print(type(0) == int)
|
||||
print(type(0.0) == float)
|
||||
print(type(1j) == complex)
|
||||
print(type(()) == tuple)
|
||||
print(type([]) == list)
|
||||
print(type({None}) == set)
|
||||
|
|
|
@ -9,4 +9,6 @@ print(int == int)
|
|||
print(int != list)
|
||||
|
||||
d = {}
|
||||
d[int] = float
|
||||
d[int] = list
|
||||
d[list] = int
|
||||
print(len(d))
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
try:
|
||||
1 / 0
|
||||
except ZeroDivisionError:
|
||||
print("ZeroDivisionError")
|
|
@ -0,0 +1,8 @@
|
|||
x = [1, 2]
|
||||
|
||||
print(x[1])
|
||||
|
||||
try:
|
||||
print(x[1.0])
|
||||
except TypeError:
|
||||
print("TypeError")
|
|
@ -0,0 +1,14 @@
|
|||
# check modulo matches python definition
|
||||
# TODO we currenty fail with this
|
||||
if False:
|
||||
print(1.23456 % 0.7)
|
||||
print(-1.23456 % 0.7)
|
||||
print(1.23456 % -0.7)
|
||||
print(-1.23456 % -0.7)
|
||||
|
||||
a = 1.23456
|
||||
b = 0.7
|
||||
print(a % b)
|
||||
print(a % -b)
|
||||
print(-a % b)
|
||||
print(-a % -b)
|
|
@ -0,0 +1,16 @@
|
|||
print("%s" % 1.0)
|
||||
print("%r" % 1.0)
|
||||
|
||||
print("%d" % 1.0)
|
||||
print("%i" % 1.0)
|
||||
print("%u" % 1.0)
|
||||
print("%x" % 18.0)
|
||||
print("%o" % 18.0)
|
||||
print("%X" % 18.0)
|
||||
|
||||
print("%e" % 1.23456)
|
||||
print("%E" % 1.23456)
|
||||
print("%f" % 1.23456)
|
||||
print("%F" % 1.23456)
|
||||
print("%g" % 1.23456)
|
||||
print("%G" % 1.23456)
|
|
@ -0,0 +1,123 @@
|
|||
# Change the following to True to get a much more comprehensive set of tests
|
||||
# to run, albeit, which take considerably longer.
|
||||
|
||||
full_tests = False
|
||||
|
||||
def test(fmt, *args):
|
||||
print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<')
|
||||
|
||||
test("{:10.4e}", 123.456)
|
||||
test("{:10.4e}", -123.456)
|
||||
test("{:10.4f}", 123.456)
|
||||
test("{:10.4f}", -123.456)
|
||||
test("{:10.4g}", 123.456)
|
||||
test("{:10.4g}", -123.456)
|
||||
test("{:e}", 100)
|
||||
test("{:f}", 200)
|
||||
test("{:g}", 300)
|
||||
|
||||
test("{:10.4E}", 123.456)
|
||||
test("{:10.4E}", -123.456)
|
||||
test("{:10.4F}", 123.456)
|
||||
test("{:10.4F}", -123.456)
|
||||
test("{:10.4G}", 123.456)
|
||||
test("{:10.4G}", -123.456)
|
||||
|
||||
# The following fails right now
|
||||
#test("{:10.1}", 0.0)
|
||||
|
||||
def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
|
||||
fmt = '{'
|
||||
if conv:
|
||||
fmt += '!'
|
||||
fmt += conv
|
||||
fmt += ':'
|
||||
if alignment:
|
||||
fmt += fill
|
||||
fmt += alignment
|
||||
fmt += sign
|
||||
fmt += prefix
|
||||
fmt += width
|
||||
if precision:
|
||||
fmt += '.'
|
||||
fmt += precision
|
||||
fmt += type
|
||||
fmt += '}'
|
||||
test(fmt, arg)
|
||||
if fill == '0' and alignment == '=':
|
||||
fmt = '{:'
|
||||
fmt += sign
|
||||
fmt += prefix
|
||||
fmt += width
|
||||
if precision:
|
||||
fmt += '.'
|
||||
fmt += precision
|
||||
fmt += type
|
||||
fmt += '}'
|
||||
test(fmt, arg)
|
||||
|
||||
eg_nums = (0.0, -0.0, 0.1, 1.234, 12.3459, 1.23456789, 123456789.0, -0.0,
|
||||
-0.1, -1.234, -12.3459, 1e4, 1e-4, 1e5, 1e-5, 1e6, 1e-6, 1e10,
|
||||
1e37, -1e37, 1e-37, -1e-37,
|
||||
1.23456e8, 1.23456e7, 1.23456e6, 1.23456e5, 1.23456e4, 1.23456e3, 1.23456e2, 1.23456e1, 1.23456e0,
|
||||
1.23456e-1, 1.23456e-2, 1.23456e-3, 1.23456e-4, 1.23456e-5, 1.23456e-6, 1.23456e-7, 1.23456e-8,
|
||||
-1.23456e8, -1.23456e7, -1.23456e6, -1.23456e5, -1.23456e4, -1.23456e3, -1.23456e2, -1.23456e1, -1.23456e0,
|
||||
-1.23456e-1, -1.23456e-2, -1.23456e-3, -1.23456e-4, -1.23456e-5, -1.23456e-6, -1.23456e-7, -1.23456e-8)
|
||||
|
||||
if full_tests:
|
||||
for type in ('e', 'E', 'g', 'G', 'n'):
|
||||
for width in ('', '4', '6', '8', '10'):
|
||||
for alignment in ('', '<', '>', '=', '^'):
|
||||
for fill in ('', '@', '0', ' '):
|
||||
for sign in ('', '+', '-', ' '):
|
||||
for prec in ('', '1', '3', '6'):
|
||||
for num in eg_nums:
|
||||
test_fmt('', fill, alignment, sign, '', width, prec, type, num)
|
||||
|
||||
# Note: We use 1.23459 rather than 1.2345 because '{:3f}'.format(1.2345)
|
||||
# rounds differently than print("%.3f", 1.2345);
|
||||
|
||||
f_nums = (0.0, -0.0, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0,
|
||||
0.0012, 0.0123, 0.1234, 1.23459, 12.3456,
|
||||
-0.0001, -0.001, -0.01, -0.1, -1.0, -10.0,
|
||||
-0.0012, -0.0123, -0.1234, -1.23459, -12.3456)
|
||||
|
||||
if full_tests:
|
||||
for type in ('f', 'F'):
|
||||
for width in ('', '4', '6', '8', '10'):
|
||||
for alignment in ('', '<', '>', '=', '^'):
|
||||
for fill in ('', ' ', '0', '@'):
|
||||
for sign in ('', '+', '-', ' '):
|
||||
# An empty precision defaults to 6, but when uPy is
|
||||
# configured to use a float, we can only use a
|
||||
# precision of 6 with numbers less than 10 and still
|
||||
# get results that compare to CPython (which uses
|
||||
# long doubles).
|
||||
for prec in ('1', '2', '3'):
|
||||
for num in f_nums:
|
||||
test_fmt('', fill, alignment, sign, '', width, prec, type, num)
|
||||
for num in int_nums2:
|
||||
test_fmt('', fill, alignment, sign, '', width, '', type, num)
|
||||
|
||||
pct_nums1 = (0.1, 0.58, 0.99, -0.1, -0.58, -0.99)
|
||||
pct_nums2 = (True, False, 1, 0, -1)
|
||||
|
||||
if full_tests:
|
||||
type = '%'
|
||||
for width in ('', '4', '6', '8', '10'):
|
||||
for alignment in ('', '<', '>', '=', '^'):
|
||||
for fill in ('', ' ', '0', '@'):
|
||||
for sign in ('', '+', '-', ' '):
|
||||
# An empty precision defaults to 6, but when uPy is
|
||||
# configured to use a float, we can only use a
|
||||
# precision of 6 with numbers less than 10 and still
|
||||
# get results that compare to CPython (which uses
|
||||
# long doubles).
|
||||
for prec in ('1', '2', '3'):
|
||||
for num in pct_nums1:
|
||||
test_fmt('', fill, alignment, sign, '', width, prec, type, num)
|
||||
for num in pct_nums2:
|
||||
test_fmt('', fill, alignment, sign, '', width, '', type, num)
|
||||
|
||||
# We don't currently test a type of '' with floats (see the detailed comment
|
||||
# in objstr.c)
|
|
@ -0,0 +1,7 @@
|
|||
# Test true-ish value handling
|
||||
|
||||
if not 0.0:
|
||||
print("float 0")
|
||||
|
||||
if not 0+0j:
|
||||
print("complex 0")
|
|
@ -0,0 +1,17 @@
|
|||
# float types
|
||||
|
||||
print(float)
|
||||
print(complex)
|
||||
|
||||
print(type(float()) == float)
|
||||
print(type(complex()) == complex)
|
||||
|
||||
print(type(0.0) == float)
|
||||
print(type(1j) == complex)
|
||||
|
||||
# hashing float types
|
||||
|
||||
d = dict()
|
||||
d[float] = complex
|
||||
d[complex] = float
|
||||
print(len(d))
|
|
@ -29,7 +29,8 @@ failed_tests = []
|
|||
tests = []
|
||||
|
||||
if not sys.argv[1:]:
|
||||
tests = sorted(glob('basics/*.py') + glob('io/*.py') + glob('misc/*.py'))
|
||||
test_dirs = ('basics', 'float', 'import', 'io', 'misc')
|
||||
tests = sorted(test_file for test_files in (glob('{}/*.py'.format(dir)) for dir in test_dirs) for test_file in test_files)
|
||||
else:
|
||||
tests = sys.argv[1:]
|
||||
|
||||
|
@ -56,13 +57,9 @@ for test_file in tests:
|
|||
if test_on_pyboard:
|
||||
pyb.enter_raw_repl()
|
||||
try:
|
||||
if test_file == 'basics/memoryerror.py':
|
||||
# this test crashes the pyboard
|
||||
output_mupy = b'CRASH'
|
||||
else:
|
||||
output_mupy = pyb.execfile(test_file).replace(b'\r\n', b'\n')
|
||||
except pyboard.PyboardError:
|
||||
output_mupy = b'CRASH\n' + output_mupy
|
||||
output_mupy = b'CRASH'
|
||||
else:
|
||||
try:
|
||||
output_mupy = subprocess.check_output([MP_PY, '-X', 'emit=bytecode', test_file])
|
||||
|
|
Loading…
Reference in New Issue