diff options
Diffstat (limited to 'tests/float')
25 files changed, 227 insertions, 76 deletions
diff --git a/tests/float/array_construct.py b/tests/float/array_construct.py index a25cc72c8..938675835 100644 --- a/tests/float/array_construct.py +++ b/tests/float/array_construct.py @@ -1,6 +1,10 @@ # test construction of array from array with float type -from array import array +try: + from array import array +except ImportError: + print("SKIP") + raise SystemExit print(array('f', array('h', [1, 2]))) print(array('d', array('f', [1, 2]))) diff --git a/tests/float/builtin_float_hash.py b/tests/float/builtin_float_hash.py new file mode 100644 index 000000000..ba6b63907 --- /dev/null +++ b/tests/float/builtin_float_hash.py @@ -0,0 +1,22 @@ +# test builtin hash function with float args + +# these should hash to an integer with a specific value +for val in ( + '0.0', + '1.0', + '2.0', + '-12.0', + '12345.0', + ): + print(val, hash(float(val))) + +# just check that these values are hashable +for val in ( + '0.1', + '-0.1', + '10.3', + 'inf', + '-inf', + 'nan', + ): + print(val, type(hash(float(val)))) diff --git a/tests/float/builtin_float_minmax.py b/tests/float/builtin_float_minmax.py index ce45a768a..266ed133d 100644 --- a/tests/float/builtin_float_minmax.py +++ b/tests/float/builtin_float_minmax.py @@ -1,26 +1,32 @@ # test builtin min and max functions with float args +try: + min + max +except: + print("SKIP") + raise SystemExit -print(min(0,1.0)) -print(min(1.0,0)) -print(min(0,-1.0)) -print(min(-1.0,0)) +print(min(0, 1.0)) +print(min(1.0, 0)) +print(min(0, -1.0)) +print(min(-1.0, 0)) -print(max(0,1.0)) -print(max(1.0,0)) -print(max(0,-1.0)) -print(max(-1.0,0)) +print(max(0, 1.0)) +print(max(1.0, 0)) +print(max(0, -1.0)) +print(max(-1.0, 0)) -print(min(1.5,-1.5)) -print(min(-1.5,1.5)) +print(min(1.5, -1.5)) +print(min(-1.5, 1.5)) -print(max(1.5,-1.5)) -print(max(-1.5,1.5)) +print(max(1.5, -1.5)) +print(max(-1.5, 1.5)) -print(min([1,2.9,4,0,-1,2])) -print(max([1,2.9,4,0,-1,2])) +print(min([1, 2.9, 4, 0, -1, 2])) +print(max([1, 2.9, 4, 0, -1, 2])) -print(min([1,2.9,4,6.5,-1,2])) -print(max([1,2.9,4,6.5,-1,2])) -print(min([1,2.9,4,-6.5,-1,2])) -print(max([1,2.9,4,-6.5,-1,2])) +print(min([1, 2.9, 4, 6.5, -1, 2])) +print(max([1, 2.9, 4, 6.5, -1, 2])) +print(min([1, 2.9, 4, -6.5, -1, 2])) +print(max([1, 2.9, 4, -6.5, -1, 2])) diff --git a/tests/float/builtin_float_round.py b/tests/float/builtin_float_round.py index de72514db..63cb39aa3 100644 --- a/tests/float/builtin_float_round.py +++ b/tests/float/builtin_float_round.py @@ -15,3 +15,10 @@ for i in range(11): # test second arg for i in range(-1, 3): print(round(1.47, i)) + +# test inf and nan +for val in (float('inf'), float('nan')): + try: + round(val) + except (ValueError, OverflowError) as e: + print(type(e)) diff --git a/tests/float/builtin_float_round_intbig.py b/tests/float/builtin_float_round_intbig.py new file mode 100644 index 000000000..2083e3ea3 --- /dev/null +++ b/tests/float/builtin_float_round_intbig.py @@ -0,0 +1,4 @@ +# test round() with floats that return large integers + +for x in (-1e25, 1e25): + print('%.3g' % round(x)) diff --git a/tests/float/bytearray_construct.py b/tests/float/bytearray_construct.py index f72926635..e960d624e 100644 --- a/tests/float/bytearray_construct.py +++ b/tests/float/bytearray_construct.py @@ -1,5 +1,9 @@ # test construction of bytearray from array with float type -from array import array +try: + from array import array +except ImportError: + print("SKIP") + raise SystemExit print(bytearray(array('f', [1, 2.3]))) diff --git a/tests/float/bytes_construct.py b/tests/float/bytes_construct.py index 0a57e08a2..0e4482e43 100644 --- a/tests/float/bytes_construct.py +++ b/tests/float/bytes_construct.py @@ -1,5 +1,9 @@ # test construction of bytearray from array with float type -from array import array +try: + from array import array +except ImportError: + print("SKIP") + raise SystemExit print(bytes(array('f', [1, 2.3]))) diff --git a/tests/float/cmath_fun.py b/tests/float/cmath_fun.py index 3ebcf5918..ae5921c30 100644 --- a/tests/float/cmath_fun.py +++ b/tests/float/cmath_fun.py @@ -4,8 +4,7 @@ try: from cmath import * except ImportError: print("SKIP") - import sys - sys.exit() + raise SystemExit # make sure these constants exist in cmath print("%.5g" % e) diff --git a/tests/float/cmath_fun_special.py b/tests/float/cmath_fun_special.py index 422964dd7..471fda8c0 100644 --- a/tests/float/cmath_fun_special.py +++ b/tests/float/cmath_fun_special.py @@ -5,8 +5,7 @@ try: log10 except (ImportError, NameError): print("SKIP") - import sys - sys.exit() + raise SystemExit test_values_non_zero = [] base_values = (0.0, 0.5, 1.2345, 10.) diff --git a/tests/float/complex1.py b/tests/float/complex1.py index 027d12583..a6038de04 100644 --- a/tests/float/complex1.py +++ b/tests/float/complex1.py @@ -28,6 +28,7 @@ print(1j / 2) print((1j / 2j).real) print(1j / (1 + 2j)) ans = 0j ** 0; print("%.5g %.5g" % (ans.real, ans.imag)) +ans = 0j ** 1; print("%.5g %.5g" % (ans.real, ans.imag)) ans = 0j ** 0j; print("%.5g %.5g" % (ans.real, ans.imag)) ans = 1j ** 2.5; print("%.5g %.5g" % (ans.real, ans.imag)) ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag)) @@ -40,6 +41,10 @@ print(1j == 1j) print(abs(1j)) print("%.5g" % abs(1j + 2)) +# builtin hash +print(hash(1 + 0j)) +print(type(hash(1j))) + # float on lhs should delegate to complex print(1.2 + 3j) @@ -48,8 +53,11 @@ print(float('nan') * 1j) print(float('inf') * (1 + 1j)) print(float('-inf') * (1 + 1j)) -# convert bignum to complex on rhs -ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag)) +# can't assign to attributes +try: + (1j).imag = 0 +except AttributeError: + print('AttributeError') # can't convert rhs to complex try: @@ -89,6 +97,10 @@ except ZeroDivisionError: # zero division via power try: + 0j ** -1 +except ZeroDivisionError: + print("ZeroDivisionError") +try: 0j ** 1j except ZeroDivisionError: print("ZeroDivisionError") diff --git a/tests/float/complex1_intbig.py b/tests/float/complex1_intbig.py new file mode 100644 index 000000000..ed2390bba --- /dev/null +++ b/tests/float/complex1_intbig.py @@ -0,0 +1,4 @@ +# test basic complex number functionality + +# convert bignum to complex on rhs +ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag)) diff --git a/tests/float/float1.py b/tests/float/float1.py index 0e115032b..93f6f014c 100644 --- a/tests/float/float1.py +++ b/tests/float/float1.py @@ -75,6 +75,11 @@ try: except ZeroDivisionError: print("ZeroDivisionError") +try: + 0.0 ** -1 +except ZeroDivisionError: + print("ZeroDivisionError") + # unsupported unary ops try: diff --git a/tests/float/float2int_doubleprec.py b/tests/float/float2int_doubleprec_intbig.py index acdc8c69c..de2137d66 100644 --- a/tests/float/float2int_doubleprec.py +++ b/tests/float/float2int_doubleprec_intbig.py @@ -5,21 +5,29 @@ try: except: import struct +import sys +maxsize_bits = 0 +maxsize = sys.maxsize +while maxsize: + maxsize >>= 1 + maxsize_bits += 1 + # work out configuration values -is_64bit = struct.calcsize("P") == 8 +is_64bit = maxsize_bits > 32 # 0 = none, 1 = long long, 2 = mpz -try: - dummy = 0x7fffffffffffffff - try: - if (0xffffffffffffffff + 1) > 0: - ll_type = 2 - else: - ll_type = 1 - except: - # in case the sum in the if statement above changes to raising an exception on overflow +ll_type = None +if is_64bit: + if maxsize_bits < 63: + ll_type = 0 +else: + if maxsize_bits < 31: + ll_type = 0 +if ll_type is None: + one = 1 + if one << 65 < one << 62: ll_type = 1 -except: - ll_type = 0 + else: + ll_type = 2 # This case occurs with time.time() values if ll_type != 0: diff --git a/tests/float/float2int_fp30.py b/tests/float/float2int_fp30_intbig.py index bad9c31e9..fbb94a4cc 100644 --- a/tests/float/float2int_fp30.py +++ b/tests/float/float2int_fp30_intbig.py @@ -5,21 +5,29 @@ try: except: import struct +import sys +maxsize_bits = 0 +maxsize = sys.maxsize +while maxsize: + maxsize >>= 1 + maxsize_bits += 1 + # work out configuration values -is_64bit = struct.calcsize("P") == 8 +is_64bit = maxsize_bits > 32 # 0 = none, 1 = long long, 2 = mpz -try: - dummy = 0x7fffffffffffffff - try: - if (0xffffffffffffffff + 1) > 0: - ll_type = 2 - else: - ll_type = 1 - except: - # in case the sum in the if statement above changes to raising an exception on overflow +ll_type = None +if is_64bit: + if maxsize_bits < 63: + ll_type = 0 +else: + if maxsize_bits < 31: + ll_type = 0 +if ll_type is None: + one = 1 + if one << 65 < one << 62: ll_type = 1 -except: - ll_type = 0 + else: + ll_type = 2 # basic conversion print(int(14187744.)) diff --git a/tests/float/float2int.py b/tests/float/float2int_intbig.py index da530cee6..3596d2f73 100644 --- a/tests/float/float2int.py +++ b/tests/float/float2int_intbig.py @@ -5,21 +5,31 @@ try: except: import struct +import sys + +maxsize_bits = 0 +maxsize = sys.maxsize +while maxsize: + maxsize >>= 1 + maxsize_bits += 1 + # work out configuration values -is_64bit = struct.calcsize("P") == 8 +is_64bit = maxsize_bits > 32 # 0 = none, 1 = long long, 2 = mpz -try: - dummy = 0x7fffffffffffffff - try: - if (0xffffffffffffffff + 1) > 0: - ll_type = 2 - else: - ll_type = 1 - except: - # in case the sum in the if statement above changes to raising an exception on overflow +ll_type = None +if is_64bit: + if maxsize_bits < 63: + ll_type = 0 +else: + if maxsize_bits < 31: + ll_type = 0 +if ll_type is None: + one = 1 + if one << 65 < one << 62: ll_type = 1 -except: - ll_type = 0 + else: + ll_type = 2 + # basic conversion print(int(14187745.)) diff --git a/tests/float/float_array.py b/tests/float/float_array.py index 033877db3..8c8edcff7 100644 --- a/tests/float/float_array.py +++ b/tests/float/float_array.py @@ -1,4 +1,8 @@ -from array import array +try: + from array import array +except ImportError: + print("SKIP") + raise SystemExit def test(a): print(a) diff --git a/tests/float/float_struct.py b/tests/float/float_struct.py index e55890a2c..c4c186b89 100644 --- a/tests/float/float_struct.py +++ b/tests/float/float_struct.py @@ -1,9 +1,12 @@ # test struct package with floats - try: - import ustruct as struct -except: - import struct + try: + import ustruct as struct + except: + import struct +except ImportError: + print("SKIP") + raise SystemExit i = 1. + 1/2 # TODO: it looks like '=' format modifier is not yet supported diff --git a/tests/float/int_divzero.py b/tests/float/int_divzero.py index b037dd8c7..b311a1dbc 100644 --- a/tests/float/int_divzero.py +++ b/tests/float/int_divzero.py @@ -2,3 +2,8 @@ try: 1 / 0 except ZeroDivisionError: print("ZeroDivisionError") + +try: + 0 ** -1 +except ZeroDivisionError: + print("ZeroDivisionError") diff --git a/tests/float/math_fun.py b/tests/float/math_fun.py index d9f179587..80d20bd8a 100644 --- a/tests/float/math_fun.py +++ b/tests/float/math_fun.py @@ -4,8 +4,7 @@ try: from math import * except ImportError: print("SKIP") - import sys - sys.exit() + raise SystemExit test_values = [-100., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.] test_values_small = [-10., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 10.] # so we don't overflow 32-bit precision diff --git a/tests/float/math_fun_bool.py b/tests/float/math_fun_bool.py index 57232857a..30ab14a52 100644 --- a/tests/float/math_fun_bool.py +++ b/tests/float/math_fun_bool.py @@ -4,8 +4,7 @@ try: from math import isfinite, isnan, isinf except ImportError: print("SKIP") - import sys - sys.exit() + raise SystemExit test_values = [1, 0, -1, 1.0, 0.0, -1.0, float('NaN'), float('Inf'), -float('NaN'), -float('Inf')] diff --git a/tests/float/math_fun_int.py b/tests/float/math_fun_int.py new file mode 100644 index 000000000..5cadbb1e5 --- /dev/null +++ b/tests/float/math_fun_int.py @@ -0,0 +1,14 @@ +# test the math functions that return ints + +try: + import math +except ImportError: + print("SKIP") + raise SystemExit + +for fun in (math.ceil, math.floor, math.trunc): + for x in (-1.6, -0.2, 0, 0.6, 1.4, float('inf'), float('nan')): + try: + print(fun(x)) + except (ValueError, OverflowError) as e: + print(type(e)) diff --git a/tests/float/math_fun_intbig.py b/tests/float/math_fun_intbig.py new file mode 100644 index 000000000..697ca7a6d --- /dev/null +++ b/tests/float/math_fun_intbig.py @@ -0,0 +1,11 @@ +# test the math functions that return ints, with very large results + +try: + import math +except ImportError: + print("SKIP") + raise SystemExit + +for fun in (math.ceil, math.floor, math.trunc): + for x in (-1e25, 1e25): + print('%.3g' % fun(x)) diff --git a/tests/float/math_fun_special.py b/tests/float/math_fun_special.py index 32249b423..c3665a7cd 100644 --- a/tests/float/math_fun_special.py +++ b/tests/float/math_fun_special.py @@ -5,8 +5,7 @@ try: erf except (ImportError, NameError): print("SKIP") - import sys - sys.exit() + raise SystemExit test_values = [-8., -2.5, -1, -0.5, 0.0, 0.5, 2.5, 8.,] pos_test_values = [0.001, 0.1, 0.5, 1.0, 1.5, 10.,] diff --git a/tests/float/string_format_modulo2.py b/tests/float/string_format_modulo2.py index d35f2a2c0..f6b1ae537 100644 --- a/tests/float/string_format_modulo2.py +++ b/tests/float/string_format_modulo2.py @@ -18,7 +18,7 @@ def test(num, num_str): # check pure zero test(0.0, '0.0') -# check most powers of 10, making sure to include exponents with 3 digits -for e in range(-101, 102): +# check some powers of 10, making sure to include exponents with 3 digits +for e in range(-8, 8): num = pow(10, e) test(num, '1e%d' % e) diff --git a/tests/float/string_format_modulo2_intbig.py b/tests/float/string_format_modulo2_intbig.py new file mode 100644 index 000000000..9992ba65d --- /dev/null +++ b/tests/float/string_format_modulo2_intbig.py @@ -0,0 +1,21 @@ +# test formatting floats with large precision, that it doesn't overflow the buffer + +def test(num, num_str): + if num == float('inf') or num == 0.0 and num_str != '0.0': + # skip numbers that overflow or underflow the FP precision + return + for kind in ('e', 'f', 'g'): + # check precision either side of the size of the buffer (32 bytes) + for prec in range(23, 36, 2): + fmt = '%.' + '%d' % prec + kind + s = fmt % num + check = abs(float(s) - num) + if num > 1: + check /= num + if check > 1e-6: + print('FAIL', num_str, fmt, s, len(s), check) + +# check most powers of 10, making sure to include exponents with 3 digits +for e in range(-101, 102): + num = pow(10, e) + test(num, '1e%d' % e) |
