diff options
Diffstat (limited to 'tests/float')
20 files changed, 212 insertions, 48 deletions
diff --git a/tests/float/array_construct.py b/tests/float/array_construct.py index a25cc72c8..7e01fd476 100644 --- a/tests/float/array_construct.py +++ b/tests/float/array_construct.py @@ -1,6 +1,11 @@ # test construction of array from array with float type -from array import array +try: + from array import array +except ImportError: + import sys + print("SKIP") + sys.exit() 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..42cfa6382 100644 --- a/tests/float/builtin_float_minmax.py +++ b/tests/float/builtin_float_minmax.py @@ -1,4 +1,11 @@ # test builtin min and max functions with float args +try: + min + max +except: + import sys + print("SKIP") + sys.exit() print(min(0,1.0)) print(min(1.0,0)) 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..db946a99d 100644 --- a/tests/float/bytearray_construct.py +++ b/tests/float/bytearray_construct.py @@ -1,5 +1,10 @@ # test construction of bytearray from array with float type -from array import array +try: + from array import array +except ImportError: + import sys + print("SKIP") + sys.exit() print(bytearray(array('f', [1, 2.3]))) diff --git a/tests/float/bytes_construct.py b/tests/float/bytes_construct.py index 0a57e08a2..8664d7296 100644 --- a/tests/float/bytes_construct.py +++ b/tests/float/bytes_construct.py @@ -1,5 +1,10 @@ # test construction of bytearray from array with float type -from array import array +try: + from array import array +except ImportError: + import sys + print("SKIP") + sys.exit() print(bytes(array('f', [1, 2.3]))) 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..8bc963444 100644 --- a/tests/float/float_array.py +++ b/tests/float/float_array.py @@ -1,4 +1,9 @@ -from array import array +try: + from array import array +except ImportError: + import sys + print("SKIP") + sys.exit() def test(a): print(a) diff --git a/tests/float/float_struct.py b/tests/float/float_struct.py index e55890a2c..a36ccce38 100644 --- a/tests/float/float_struct.py +++ b/tests/float/float_struct.py @@ -1,9 +1,13 @@ # test struct package with floats - try: - import ustruct as struct -except: - import struct + try: + import ustruct as struct + except: + import struct +except ImportError: + import sys + print("SKIP") + sys.exit() 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_int.py b/tests/float/math_fun_int.py new file mode 100644 index 000000000..ee54f0995 --- /dev/null +++ b/tests/float/math_fun_int.py @@ -0,0 +1,15 @@ +# test the math functions that return ints + +try: + import math +except ImportError: + print("SKIP") + import sys + sys.exit() + +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..962c10daa --- /dev/null +++ b/tests/float/math_fun_intbig.py @@ -0,0 +1,12 @@ +# test the math functions that return ints, with very large results + +try: + import math +except ImportError: + print("SKIP") + import sys + sys.exit() + +for fun in (math.ceil, math.floor, math.trunc): + for x in (-1e25, 1e25): + print('%.3g' % fun(x)) 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) |