diff options
Diffstat (limited to 'tests/float')
-rw-r--r-- | tests/float/float1.py | 16 | ||||
-rw-r--r-- | tests/float/int-divzero.py | 4 | ||||
-rw-r--r-- | tests/float/list-index.py | 8 | ||||
-rw-r--r-- | tests/float/math-fun-bool.py | 12 | ||||
-rw-r--r-- | tests/float/math-fun.py | 46 | ||||
-rw-r--r-- | tests/float/modulo.py | 14 | ||||
-rw-r--r-- | tests/float/string-format-modulo.py | 16 | ||||
-rw-r--r-- | tests/float/string-format.py | 123 | ||||
-rw-r--r-- | tests/float/true-value.py | 7 | ||||
-rw-r--r-- | tests/float/types.py | 17 |
10 files changed, 263 insertions, 0 deletions
diff --git a/tests/float/float1.py b/tests/float/float1.py new file mode 100644 index 0000000000..bf1305c3d5 --- /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 0000000000..b037dd8c7b --- /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 0000000000..13e4557af7 --- /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 0000000000..cf718d4b80 --- /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 0000000000..7a37c58454 --- /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 0000000000..911268513a --- /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 0000000000..f2117f3565 --- /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 0000000000..b5ff68b8ca --- /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 0000000000..df415f0031 --- /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 0000000000..75674c9246 --- /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)) |