Commit 5cd0b222 authored by Damien George's avatar Damien George
Browse files

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.
parent d7a4b690
...@@ -2,3 +2,7 @@ This directory contains tests for various functionality areas of MicroPython. ...@@ -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 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 that bytecode tests are not yet stable and should be run separately in
"bytecode" subdirectory. "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.
try:
1 / 0
except ZeroDivisionError:
print("ZeroDivisionError")
try: try:
1 // 0 1 // 0
except ZeroDivisionError: except ZeroDivisionError:
......
...@@ -22,8 +22,3 @@ print(x) ...@@ -22,8 +22,3 @@ print(x)
print(x[1:]) print(x[1:])
print(x[:-1]) print(x[:-1])
print(x[2:3]) print(x[2:3])
try:
print(x[1.0])
except TypeError:
print("TypeError")
# check modulo matches python definition # check modulo matches python definition
# This test compiler version
# this tests compiler constant folding
print(123 % 7) print(123 % 7)
print(-123 % 7) print(-123 % 7)
print(123 % -7) print(123 % -7)
...@@ -20,17 +21,3 @@ print(a % b) ...@@ -20,17 +21,3 @@ print(a % b)
print(a % -b) print(a % -b)
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: ...@@ -23,11 +23,9 @@ except TypeError:
print("%s" % True) print("%s" % True)
print("%s" % 1) print("%s" % 1)
print("%s" % 1.0)
print("%r" % True) print("%r" % True)
print("%r" % 1) print("%r" % 1)
print("%r" % 1.0)
print("%c" % 48) print("%c" % 48)
print("%c" % 'a') print("%c" % 'a')
...@@ -37,28 +35,16 @@ print("%d" % 10) ...@@ -37,28 +35,16 @@ print("%d" % 10)
print("%+d" % 10) print("%+d" % 10)
print("% d" % 10) print("% d" % 10)
print("%d" % -10) print("%d" % -10)
print("%d" % 1.0)
print("%d" % True) print("%d" % True)
print("%i" % -10) print("%i" % -10)
print("%i" % 1.0)
print("%i" % True) print("%i" % True)
print("%u" % -10) print("%u" % -10)
print("%u" % 1.0)
print("%u" % True) print("%u" % True)
print("%x" % 18) print("%x" % 18)
print("%x" % 18.0)
print("%o" % 18) print("%o" % 18)
print("%o" % 18.0)
print("%X" % 18) print("%X" % 18)
print("%X" % 18.0)
print("%#x" % 18) print("%#x" % 18)
print("%#X" % 18) print("%#X" % 18)
print("%#6o" % 18) print("%#6o" % 18)
print("%#6x" % 18) print("%#6x" % 18)
print("%#06x" % 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) ...@@ -60,26 +60,6 @@ test("{:@<6d}", -123)
test("{:@=6d}", -123) test("{:@=6d}", -123)
test("{:06d}", -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): def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
fmt = '{' fmt = '{'
if conv: if conv:
...@@ -137,71 +117,4 @@ if full_tests: ...@@ -137,71 +117,4 @@ if full_tests:
for str in ('', 'a', 'bcd', 'This is a test with a longer string'): for str in ('', 'a', 'bcd', 'This is a test with a longer string'):
test_fmt(conv, fill, alignment, '', '', width, '', 's', str) 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. # TODO Add tests for erroneous format strings.
...@@ -9,12 +9,6 @@ if not None: ...@@ -9,12 +9,6 @@ if not None:
if not 0: if not 0:
print("0") print("0")
if not 0.0:
print("float 0")
if not 0+0j:
print("complex 0")
if not "": if not "":
print("Empty string") print("Empty string")
if "foo": if "foo":
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
print(bool) print(bool)
print(int) print(int)
print(float)
print(complex)
print(tuple) print(tuple)
print(list) print(list)
print(set) print(set)
...@@ -11,8 +9,6 @@ print(dict) ...@@ -11,8 +9,6 @@ print(dict)
print(type(bool()) == bool) print(type(bool()) == bool)
print(type(int()) == int) print(type(int()) == int)
print(type(float()) == float)
print(type(complex()) == complex)
print(type(tuple()) == tuple) print(type(tuple()) == tuple)
print(type(list()) == list) print(type(list()) == list)
print(type(set()) == set) print(type(set()) == set)
...@@ -20,8 +16,6 @@ print(type(dict()) == dict) ...@@ -20,8 +16,6 @@ print(type(dict()) == dict)
print(type(False) == bool) print(type(False) == bool)
print(type(0) == int) print(type(0) == int)
print(type(0.0) == float)
print(type(1j) == complex)
print(type(()) == tuple) print(type(()) == tuple)
print(type([]) == list) print(type([]) == list)
print(type({None}) == set) print(type({None}) == set)
......
...@@ -9,4 +9,6 @@ print(int == int) ...@@ -9,4 +9,6 @@ print(int == int)
print(int != list) print(int != list)
d = {} d = {}
d[int] = float d[int] = list
d[list] = int
print(len(d))
try:
1 / 0
except ZeroDivisionError:
print("ZeroDivisionError")
x = [1, 2]
print(x[1])
try:
print(x[1.0])
except TypeError:
print("TypeError")
# 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)
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)
# 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)
# Test true-ish value handling
if not 0.0:
print("float 0")
if not 0+0j:
print("complex 0")
# 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))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment