From 5cd0b2227f1c36584129cdce86dd20952b45581c Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 17 Apr 2014 16:21:43 +0100 Subject: 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. --- tests/README | 4 ++ tests/basics/float1.py | 16 ----- tests/basics/import-pkg1.py | 11 --- tests/basics/import-pkg2.py | 18 ----- tests/basics/import-pkg3.py | 6 -- tests/basics/import-pkg4.py | 2 - tests/basics/import-pkg5.py | 6 -- tests/basics/import1a.py | 2 - tests/basics/import1b.py | 4 -- tests/basics/import2a.py | 2 - tests/basics/import3a.py | 2 - tests/basics/int-divzero.py | 5 -- tests/basics/list1.py | 5 -- tests/basics/math-fun-bool.py | 12 ---- tests/basics/math-fun.py | 46 ------------- tests/basics/modulo.py | 17 +---- tests/basics/pkg/__init__.py | 0 tests/basics/pkg/mod.py | 2 - tests/basics/pkg2/__init__.py | 1 - tests/basics/pkg2/mod1.py | 1 - tests/basics/pkg2/mod2.py | 1 - tests/basics/pkg3/__init__.py | 1 - tests/basics/pkg3/mod1.py | 2 - tests/basics/pkg3/mod2.py | 5 -- tests/basics/pkg3/subpkg1/__init__.py | 1 - tests/basics/pkg3/subpkg1/mod1.py | 2 - tests/basics/string-format-modulo.py | 14 ---- tests/basics/string-format.py | 87 ------------------------ tests/basics/true-value.py | 6 -- tests/basics/try-module.py | 15 ----- tests/basics/types1.py | 6 -- tests/basics/types2.py | 4 +- tests/float/float1.py | 16 +++++ tests/float/int-divzero.py | 4 ++ tests/float/list-index.py | 8 +++ tests/float/math-fun-bool.py | 12 ++++ tests/float/math-fun.py | 46 +++++++++++++ tests/float/modulo.py | 14 ++++ tests/float/string-format-modulo.py | 16 +++++ tests/float/string-format.py | 123 ++++++++++++++++++++++++++++++++++ tests/float/true-value.py | 7 ++ tests/float/types.py | 17 +++++ tests/import/import-pkg1.py | 11 +++ tests/import/import-pkg2.py | 18 +++++ tests/import/import-pkg3.py | 6 ++ tests/import/import-pkg4.py | 2 + tests/import/import-pkg5.py | 6 ++ tests/import/import1a.py | 2 + tests/import/import1b.py | 4 ++ tests/import/import2a.py | 2 + tests/import/import3a.py | 2 + tests/import/pkg/__init__.py | 0 tests/import/pkg/mod.py | 2 + tests/import/pkg2/__init__.py | 1 + tests/import/pkg2/mod1.py | 1 + tests/import/pkg2/mod2.py | 1 + tests/import/pkg3/__init__.py | 1 + tests/import/pkg3/mod1.py | 2 + tests/import/pkg3/mod2.py | 5 ++ tests/import/pkg3/subpkg1/__init__.py | 1 + tests/import/pkg3/subpkg1/mod1.py | 2 + tests/import/try-module.py | 15 +++++ tests/run-tests | 11 ++- 63 files changed, 360 insertions(+), 304 deletions(-) delete mode 100644 tests/basics/float1.py delete mode 100644 tests/basics/import-pkg1.py delete mode 100644 tests/basics/import-pkg2.py delete mode 100644 tests/basics/import-pkg3.py delete mode 100644 tests/basics/import-pkg4.py delete mode 100644 tests/basics/import-pkg5.py delete mode 100644 tests/basics/import1a.py delete mode 100644 tests/basics/import1b.py delete mode 100644 tests/basics/import2a.py delete mode 100644 tests/basics/import3a.py delete mode 100644 tests/basics/math-fun-bool.py delete mode 100644 tests/basics/math-fun.py delete mode 100644 tests/basics/pkg/__init__.py delete mode 100644 tests/basics/pkg/mod.py delete mode 100644 tests/basics/pkg2/__init__.py delete mode 100644 tests/basics/pkg2/mod1.py delete mode 100644 tests/basics/pkg2/mod2.py delete mode 100644 tests/basics/pkg3/__init__.py delete mode 100644 tests/basics/pkg3/mod1.py delete mode 100644 tests/basics/pkg3/mod2.py delete mode 100644 tests/basics/pkg3/subpkg1/__init__.py delete mode 100644 tests/basics/pkg3/subpkg1/mod1.py delete mode 100644 tests/basics/try-module.py create mode 100644 tests/float/float1.py create mode 100644 tests/float/int-divzero.py create mode 100644 tests/float/list-index.py create mode 100644 tests/float/math-fun-bool.py create mode 100644 tests/float/math-fun.py create mode 100644 tests/float/modulo.py create mode 100644 tests/float/string-format-modulo.py create mode 100644 tests/float/string-format.py create mode 100644 tests/float/true-value.py create mode 100644 tests/float/types.py create mode 100644 tests/import/import-pkg1.py create mode 100644 tests/import/import-pkg2.py create mode 100644 tests/import/import-pkg3.py create mode 100644 tests/import/import-pkg4.py create mode 100644 tests/import/import-pkg5.py create mode 100644 tests/import/import1a.py create mode 100644 tests/import/import1b.py create mode 100644 tests/import/import2a.py create mode 100644 tests/import/import3a.py create mode 100644 tests/import/pkg/__init__.py create mode 100644 tests/import/pkg/mod.py create mode 100644 tests/import/pkg2/__init__.py create mode 100644 tests/import/pkg2/mod1.py create mode 100644 tests/import/pkg2/mod2.py create mode 100644 tests/import/pkg3/__init__.py create mode 100644 tests/import/pkg3/mod1.py create mode 100644 tests/import/pkg3/mod2.py create mode 100644 tests/import/pkg3/subpkg1/__init__.py create mode 100644 tests/import/pkg3/subpkg1/mod1.py create mode 100644 tests/import/try-module.py (limited to 'tests') diff --git a/tests/README b/tests/README index ef0070571..0cb526c3b 100644 --- a/tests/README +++ b/tests/README @@ -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. diff --git a/tests/basics/float1.py b/tests/basics/float1.py deleted file mode 100644 index bf1305c3d..000000000 --- a/tests/basics/float1.py +++ /dev/null @@ -1,16 +0,0 @@ -# basic float -x = 1 / 2 -print(x) - -print(1.0 // 2) -print(2.0 // 2) - -try: - 1.0 / 0 -except ZeroDivisionError: - print("ZeroDivisionError") - -try: - 1.0 // 0 -except ZeroDivisionError: - print("ZeroDivisionError") diff --git a/tests/basics/import-pkg1.py b/tests/basics/import-pkg1.py deleted file mode 100644 index 8cd77af79..000000000 --- a/tests/basics/import-pkg1.py +++ /dev/null @@ -1,11 +0,0 @@ -import pkg.mod - -print(pkg.__name__) -print(pkg.mod.__name__) -print(pkg.mod.foo()) - -# Import 2nd time, must be same module objects -pkg_ = __import__("pkg.mod") -print(pkg_ is not pkg.mod) -print(pkg_ is pkg) -print(pkg_.mod is pkg.mod) diff --git a/tests/basics/import-pkg2.py b/tests/basics/import-pkg2.py deleted file mode 100644 index 2e9f34121..000000000 --- a/tests/basics/import-pkg2.py +++ /dev/null @@ -1,18 +0,0 @@ -from pkg.mod import foo - -try: - pkg -except NameError: - print("NameError") -try: - pkg.mod -except NameError: - print("NameError") -print(foo()) - -# Import few times, must be same module objects -mod_1 = __import__("pkg.mod", None, None, ("foo",)) -mod_2 = __import__("pkg.mod", None, None, ("foo",)) -print(mod_1 is mod_2) -print(mod_1.foo is mod_2.foo) -print(foo is mod_1.foo) diff --git a/tests/basics/import-pkg3.py b/tests/basics/import-pkg3.py deleted file mode 100644 index 0ee885b22..000000000 --- a/tests/basics/import-pkg3.py +++ /dev/null @@ -1,6 +0,0 @@ -from pkg import mod - -print(mod.foo()) - -import pkg.mod -print(mod is pkg.mod) diff --git a/tests/basics/import-pkg4.py b/tests/basics/import-pkg4.py deleted file mode 100644 index 90b6f2e0e..000000000 --- a/tests/basics/import-pkg4.py +++ /dev/null @@ -1,2 +0,0 @@ -# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work -import pkg2 diff --git a/tests/basics/import-pkg5.py b/tests/basics/import-pkg5.py deleted file mode 100644 index aa74bb45f..000000000 --- a/tests/basics/import-pkg5.py +++ /dev/null @@ -1,6 +0,0 @@ -# This tests relative imports as used in pkg3 -import pkg3 -import pkg3.mod1 -import pkg3.subpkg1.mod1 - -pkg3.subpkg1.mod1.foo() diff --git a/tests/basics/import1a.py b/tests/basics/import1a.py deleted file mode 100644 index 16b2d4d30..000000000 --- a/tests/basics/import1a.py +++ /dev/null @@ -1,2 +0,0 @@ -import import1b -print(import1b.var) diff --git a/tests/basics/import1b.py b/tests/basics/import1b.py deleted file mode 100644 index be74eca09..000000000 --- a/tests/basics/import1b.py +++ /dev/null @@ -1,4 +0,0 @@ -var = 123 - -def throw(): - raise ValueError diff --git a/tests/basics/import2a.py b/tests/basics/import2a.py deleted file mode 100644 index ce32b10b1..000000000 --- a/tests/basics/import2a.py +++ /dev/null @@ -1,2 +0,0 @@ -from import1b import var -print(var) diff --git a/tests/basics/import3a.py b/tests/basics/import3a.py deleted file mode 100644 index 2e9d41f71..000000000 --- a/tests/basics/import3a.py +++ /dev/null @@ -1,2 +0,0 @@ -from import1b import * -print(var) diff --git a/tests/basics/int-divzero.py b/tests/basics/int-divzero.py index d1fc57932..28ec2a699 100644 --- a/tests/basics/int-divzero.py +++ b/tests/basics/int-divzero.py @@ -1,8 +1,3 @@ -try: - 1 / 0 -except ZeroDivisionError: - print("ZeroDivisionError") - try: 1 // 0 except ZeroDivisionError: diff --git a/tests/basics/list1.py b/tests/basics/list1.py index a0c8afc4f..c8317baa3 100644 --- a/tests/basics/list1.py +++ b/tests/basics/list1.py @@ -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") diff --git a/tests/basics/math-fun-bool.py b/tests/basics/math-fun-bool.py deleted file mode 100644 index cf718d4b8..000000000 --- a/tests/basics/math-fun-bool.py +++ /dev/null @@ -1,12 +0,0 @@ -# Test the bool functions from math - -from math import isfinite, isnan, isinf - -test_values = [1, 0, -1, 1.0, 0.0, -1.0, float('NaN'), float('Inf'), - -float('NaN'), -float('Inf')] - -functions = [isfinite, isnan, isinf] - -for val in test_values: - for f in functions: - print(f(val)) diff --git a/tests/basics/math-fun.py b/tests/basics/math-fun.py deleted file mode 100644 index 7a37c5845..000000000 --- a/tests/basics/math-fun.py +++ /dev/null @@ -1,46 +0,0 @@ -# Tests the functions imported from math - -from math import * - -test_values = [-100., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.] -p_test_values = [0.1, 0.5, 1.23456] -unit_range_test_values = [-1., -0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.] - -functions = [('sqrt', sqrt, p_test_values), - ('exp', exp, test_values), - ('expm1', expm1, test_values), - ('log', log, p_test_values), - ('log2', log2, p_test_values), - ('log10', log10, p_test_values), - ('cosh', cosh, test_values), - ('sinh', sinh, test_values), - ('tanh', tanh, test_values), - ('acosh', acosh, [1.0, 5.0, 1.0]), - ('asinh', asinh, test_values), - ('atanh', atanh, [-0.99, -0.5, 0.0, 0.5, 0.99]), - ('cos', cos, test_values), - ('sin', sin, test_values), - ('tan', tan, test_values), - ('acos', acos, unit_range_test_values), - ('asin', asin, unit_range_test_values), - ('atan', atan, test_values), - ('ceil', ceil, test_values), - ('fabs', fabs, test_values), - ('floor', floor, test_values), - #('frexp', frexp, test_values), - ('trunc', trunc, test_values) - ] - -for function_name, function, test_vals in functions: - print(function_name) - for value in test_vals: - print("{:.7g}".format(function(value))) - -binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.), - (-23., -42.), (1., 0.0), (1., -0.0)]) - ] - -for function_name, function, test_vals in binary_functions: - print(function_name) - for value1, value2 in test_vals: - print("{:.7g}".format(function(value1, value2))) diff --git a/tests/basics/modulo.py b/tests/basics/modulo.py index 4d83db6ec..c95305d13 100644 --- a/tests/basics/modulo.py +++ b/tests/basics/modulo.py @@ -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) - diff --git a/tests/basics/pkg/__init__.py b/tests/basics/pkg/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/basics/pkg/mod.py b/tests/basics/pkg/mod.py deleted file mode 100644 index 9e67bdd29..000000000 --- a/tests/basics/pkg/mod.py +++ /dev/null @@ -1,2 +0,0 @@ -def foo(): - return 42 diff --git a/tests/basics/pkg2/__init__.py b/tests/basics/pkg2/__init__.py deleted file mode 100644 index 101ac7d40..000000000 --- a/tests/basics/pkg2/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from pkg2 import mod1 diff --git a/tests/basics/pkg2/mod1.py b/tests/basics/pkg2/mod1.py deleted file mode 100644 index 03754a45f..000000000 --- a/tests/basics/pkg2/mod1.py +++ /dev/null @@ -1 +0,0 @@ -from pkg2 import mod2 diff --git a/tests/basics/pkg2/mod2.py b/tests/basics/pkg2/mod2.py deleted file mode 100644 index 97dadcde4..000000000 --- a/tests/basics/pkg2/mod2.py +++ /dev/null @@ -1 +0,0 @@ -print("in mod2") diff --git a/tests/basics/pkg3/__init__.py b/tests/basics/pkg3/__init__.py deleted file mode 100644 index 8b92fa996..000000000 --- a/tests/basics/pkg3/__init__.py +++ /dev/null @@ -1 +0,0 @@ -print("pkg __name__:", __name__) diff --git a/tests/basics/pkg3/mod1.py b/tests/basics/pkg3/mod1.py deleted file mode 100644 index 28a0f5bf1..000000000 --- a/tests/basics/pkg3/mod1.py +++ /dev/null @@ -1,2 +0,0 @@ -print("mod1 __name__:", __name__) -from . import mod2 diff --git a/tests/basics/pkg3/mod2.py b/tests/basics/pkg3/mod2.py deleted file mode 100644 index 67f43bad5..000000000 --- a/tests/basics/pkg3/mod2.py +++ /dev/null @@ -1,5 +0,0 @@ -print("mod2 __name__:", __name__) -print("in mod2") - -def foo(): - print("mod2.foo()") diff --git a/tests/basics/pkg3/subpkg1/__init__.py b/tests/basics/pkg3/subpkg1/__init__.py deleted file mode 100644 index 72b542395..000000000 --- a/tests/basics/pkg3/subpkg1/__init__.py +++ /dev/null @@ -1 +0,0 @@ -print("subpkg1 __name__:", __name__) diff --git a/tests/basics/pkg3/subpkg1/mod1.py b/tests/basics/pkg3/subpkg1/mod1.py deleted file mode 100644 index 7a2ae44b5..000000000 --- a/tests/basics/pkg3/subpkg1/mod1.py +++ /dev/null @@ -1,2 +0,0 @@ -print("subpkg1.mod1 __name__:", __name__) -from ..mod2 import foo diff --git a/tests/basics/string-format-modulo.py b/tests/basics/string-format-modulo.py index 8e58be18c..0e2c1d109 100644 --- a/tests/basics/string-format-modulo.py +++ b/tests/basics/string-format-modulo.py @@ -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) diff --git a/tests/basics/string-format.py b/tests/basics/string-format.py index 2d6d0cc72..84ea05475 100644 --- a/tests/basics/string-format.py +++ b/tests/basics/string-format.py @@ -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. - diff --git a/tests/basics/true-value.py b/tests/basics/true-value.py index 1dd547f32..9ec209fe8 100644 --- a/tests/basics/true-value.py +++ b/tests/basics/true-value.py @@ -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": diff --git a/tests/basics/try-module.py b/tests/basics/try-module.py deleted file mode 100644 index 03a9db15b..000000000 --- a/tests/basics/try-module.py +++ /dev/null @@ -1,15 +0,0 @@ -# Regression test for #290 - throwing exception in another module led to -# its namespace stick and namespace of current module not coming back. -import import1b - -def func1(): - print('func1') - -def func2(): - try: - import1b.throw() - except ValueError: - pass - func1() - -func2() diff --git a/tests/basics/types1.py b/tests/basics/types1.py index 57b33b842..38a20d680 100644 --- a/tests/basics/types1.py +++ b/tests/basics/types1.py @@ -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) diff --git a/tests/basics/types2.py b/tests/basics/types2.py index 83a69c918..ba7be6b15 100644 --- a/tests/basics/types2.py +++ b/tests/basics/types2.py @@ -9,4 +9,6 @@ print(int == int) print(int != list) d = {} -d[int] = float +d[int] = list +d[list] = int +print(len(d)) diff --git a/tests/float/float1.py b/tests/float/float1.py new file mode 100644 index 000000000..bf1305c3d --- /dev/null +++ b/tests/float/float1.py @@ -0,0 +1,16 @@ +# basic float +x = 1 / 2 +print(x) + +print(1.0 // 2) +print(2.0 // 2) + +try: + 1.0 / 0 +except ZeroDivisionError: + print("ZeroDivisionError") + +try: + 1.0 // 0 +except ZeroDivisionError: + print("ZeroDivisionError") diff --git a/tests/float/int-divzero.py b/tests/float/int-divzero.py new file mode 100644 index 000000000..b037dd8c7 --- /dev/null +++ b/tests/float/int-divzero.py @@ -0,0 +1,4 @@ +try: + 1 / 0 +except ZeroDivisionError: + print("ZeroDivisionError") diff --git a/tests/float/list-index.py b/tests/float/list-index.py new file mode 100644 index 000000000..13e4557af --- /dev/null +++ b/tests/float/list-index.py @@ -0,0 +1,8 @@ +x = [1, 2] + +print(x[1]) + +try: + print(x[1.0]) +except TypeError: + print("TypeError") diff --git a/tests/float/math-fun-bool.py b/tests/float/math-fun-bool.py new file mode 100644 index 000000000..cf718d4b8 --- /dev/null +++ b/tests/float/math-fun-bool.py @@ -0,0 +1,12 @@ +# Test the bool functions from math + +from math import isfinite, isnan, isinf + +test_values = [1, 0, -1, 1.0, 0.0, -1.0, float('NaN'), float('Inf'), + -float('NaN'), -float('Inf')] + +functions = [isfinite, isnan, isinf] + +for val in test_values: + for f in functions: + print(f(val)) diff --git a/tests/float/math-fun.py b/tests/float/math-fun.py new file mode 100644 index 000000000..7a37c5845 --- /dev/null +++ b/tests/float/math-fun.py @@ -0,0 +1,46 @@ +# Tests the functions imported from math + +from math import * + +test_values = [-100., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.] +p_test_values = [0.1, 0.5, 1.23456] +unit_range_test_values = [-1., -0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.] + +functions = [('sqrt', sqrt, p_test_values), + ('exp', exp, test_values), + ('expm1', expm1, test_values), + ('log', log, p_test_values), + ('log2', log2, p_test_values), + ('log10', log10, p_test_values), + ('cosh', cosh, test_values), + ('sinh', sinh, test_values), + ('tanh', tanh, test_values), + ('acosh', acosh, [1.0, 5.0, 1.0]), + ('asinh', asinh, test_values), + ('atanh', atanh, [-0.99, -0.5, 0.0, 0.5, 0.99]), + ('cos', cos, test_values), + ('sin', sin, test_values), + ('tan', tan, test_values), + ('acos', acos, unit_range_test_values), + ('asin', asin, unit_range_test_values), + ('atan', atan, test_values), + ('ceil', ceil, test_values), + ('fabs', fabs, test_values), + ('floor', floor, test_values), + #('frexp', frexp, test_values), + ('trunc', trunc, test_values) + ] + +for function_name, function, test_vals in functions: + print(function_name) + for value in test_vals: + print("{:.7g}".format(function(value))) + +binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.), + (-23., -42.), (1., 0.0), (1., -0.0)]) + ] + +for function_name, function, test_vals in binary_functions: + print(function_name) + for value1, value2 in test_vals: + print("{:.7g}".format(function(value1, value2))) diff --git a/tests/float/modulo.py b/tests/float/modulo.py new file mode 100644 index 000000000..911268513 --- /dev/null +++ b/tests/float/modulo.py @@ -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) diff --git a/tests/float/string-format-modulo.py b/tests/float/string-format-modulo.py new file mode 100644 index 000000000..f2117f356 --- /dev/null +++ b/tests/float/string-format-modulo.py @@ -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) diff --git a/tests/float/string-format.py b/tests/float/string-format.py new file mode 100644 index 000000000..b5ff68b8c --- /dev/null +++ b/tests/float/string-format.py @@ -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) diff --git a/tests/float/true-value.py b/tests/float/true-value.py new file mode 100644 index 000000000..df415f003 --- /dev/null +++ b/tests/float/true-value.py @@ -0,0 +1,7 @@ +# Test true-ish value handling + +if not 0.0: + print("float 0") + +if not 0+0j: + print("complex 0") diff --git a/tests/float/types.py b/tests/float/types.py new file mode 100644 index 000000000..75674c924 --- /dev/null +++ b/tests/float/types.py @@ -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)) diff --git a/tests/import/import-pkg1.py b/tests/import/import-pkg1.py new file mode 100644 index 000000000..8cd77af79 --- /dev/null +++ b/tests/import/import-pkg1.py @@ -0,0 +1,11 @@ +import pkg.mod + +print(pkg.__name__) +print(pkg.mod.__name__) +print(pkg.mod.foo()) + +# Import 2nd time, must be same module objects +pkg_ = __import__("pkg.mod") +print(pkg_ is not pkg.mod) +print(pkg_ is pkg) +print(pkg_.mod is pkg.mod) diff --git a/tests/import/import-pkg2.py b/tests/import/import-pkg2.py new file mode 100644 index 000000000..2e9f34121 --- /dev/null +++ b/tests/import/import-pkg2.py @@ -0,0 +1,18 @@ +from pkg.mod import foo + +try: + pkg +except NameError: + print("NameError") +try: + pkg.mod +except NameError: + print("NameError") +print(foo()) + +# Import few times, must be same module objects +mod_1 = __import__("pkg.mod", None, None, ("foo",)) +mod_2 = __import__("pkg.mod", None, None, ("foo",)) +print(mod_1 is mod_2) +print(mod_1.foo is mod_2.foo) +print(foo is mod_1.foo) diff --git a/tests/import/import-pkg3.py b/tests/import/import-pkg3.py new file mode 100644 index 000000000..0ee885b22 --- /dev/null +++ b/tests/import/import-pkg3.py @@ -0,0 +1,6 @@ +from pkg import mod + +print(mod.foo()) + +import pkg.mod +print(mod is pkg.mod) diff --git a/tests/import/import-pkg4.py b/tests/import/import-pkg4.py new file mode 100644 index 000000000..90b6f2e0e --- /dev/null +++ b/tests/import/import-pkg4.py @@ -0,0 +1,2 @@ +# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work +import pkg2 diff --git a/tests/import/import-pkg5.py b/tests/import/import-pkg5.py new file mode 100644 index 000000000..aa74bb45f --- /dev/null +++ b/tests/import/import-pkg5.py @@ -0,0 +1,6 @@ +# This tests relative imports as used in pkg3 +import pkg3 +import pkg3.mod1 +import pkg3.subpkg1.mod1 + +pkg3.subpkg1.mod1.foo() diff --git a/tests/import/import1a.py b/tests/import/import1a.py new file mode 100644 index 000000000..16b2d4d30 --- /dev/null +++ b/tests/import/import1a.py @@ -0,0 +1,2 @@ +import import1b +print(import1b.var) diff --git a/tests/import/import1b.py b/tests/import/import1b.py new file mode 100644 index 000000000..be74eca09 --- /dev/null +++ b/tests/import/import1b.py @@ -0,0 +1,4 @@ +var = 123 + +def throw(): + raise ValueError diff --git a/tests/import/import2a.py b/tests/import/import2a.py new file mode 100644 index 000000000..ce32b10b1 --- /dev/null +++ b/tests/import/import2a.py @@ -0,0 +1,2 @@ +from import1b import var +print(var) diff --git a/tests/import/import3a.py b/tests/import/import3a.py new file mode 100644 index 000000000..2e9d41f71 --- /dev/null +++ b/tests/import/import3a.py @@ -0,0 +1,2 @@ +from import1b import * +print(var) diff --git a/tests/import/pkg/__init__.py b/tests/import/pkg/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/import/pkg/mod.py b/tests/import/pkg/mod.py new file mode 100644 index 000000000..9e67bdd29 --- /dev/null +++ b/tests/import/pkg/mod.py @@ -0,0 +1,2 @@ +def foo(): + return 42 diff --git a/tests/import/pkg2/__init__.py b/tests/import/pkg2/__init__.py new file mode 100644 index 000000000..101ac7d40 --- /dev/null +++ b/tests/import/pkg2/__init__.py @@ -0,0 +1 @@ +from pkg2 import mod1 diff --git a/tests/import/pkg2/mod1.py b/tests/import/pkg2/mod1.py new file mode 100644 index 000000000..03754a45f --- /dev/null +++ b/tests/import/pkg2/mod1.py @@ -0,0 +1 @@ +from pkg2 import mod2 diff --git a/tests/import/pkg2/mod2.py b/tests/import/pkg2/mod2.py new file mode 100644 index 000000000..97dadcde4 --- /dev/null +++ b/tests/import/pkg2/mod2.py @@ -0,0 +1 @@ +print("in mod2") diff --git a/tests/import/pkg3/__init__.py b/tests/import/pkg3/__init__.py new file mode 100644 index 000000000..8b92fa996 --- /dev/null +++ b/tests/import/pkg3/__init__.py @@ -0,0 +1 @@ +print("pkg __name__:", __name__) diff --git a/tests/import/pkg3/mod1.py b/tests/import/pkg3/mod1.py new file mode 100644 index 000000000..28a0f5bf1 --- /dev/null +++ b/tests/import/pkg3/mod1.py @@ -0,0 +1,2 @@ +print("mod1 __name__:", __name__) +from . import mod2 diff --git a/tests/import/pkg3/mod2.py b/tests/import/pkg3/mod2.py new file mode 100644 index 000000000..67f43bad5 --- /dev/null +++ b/tests/import/pkg3/mod2.py @@ -0,0 +1,5 @@ +print("mod2 __name__:", __name__) +print("in mod2") + +def foo(): + print("mod2.foo()") diff --git a/tests/import/pkg3/subpkg1/__init__.py b/tests/import/pkg3/subpkg1/__init__.py new file mode 100644 index 000000000..72b542395 --- /dev/null +++ b/tests/import/pkg3/subpkg1/__init__.py @@ -0,0 +1 @@ +print("subpkg1 __name__:", __name__) diff --git a/tests/import/pkg3/subpkg1/mod1.py b/tests/import/pkg3/subpkg1/mod1.py new file mode 100644 index 000000000..7a2ae44b5 --- /dev/null +++ b/tests/import/pkg3/subpkg1/mod1.py @@ -0,0 +1,2 @@ +print("subpkg1.mod1 __name__:", __name__) +from ..mod2 import foo diff --git a/tests/import/try-module.py b/tests/import/try-module.py new file mode 100644 index 000000000..03a9db15b --- /dev/null +++ b/tests/import/try-module.py @@ -0,0 +1,15 @@ +# Regression test for #290 - throwing exception in another module led to +# its namespace stick and namespace of current module not coming back. +import import1b + +def func1(): + print('func1') + +def func2(): + try: + import1b.throw() + except ValueError: + pass + func1() + +func2() diff --git a/tests/run-tests b/tests/run-tests index bd6e50bbd..da18c6c76 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -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') + 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]) -- cgit v1.2.3