mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #4635 from youknowone/relocate-tests
Relocate and format extra_tests/snippets/builtin_{int,float.complex}.py
This commit is contained in:
@@ -9,6 +9,7 @@ assert abs(complex(1.5, 2.5)) == 2.9154759474226504
|
||||
|
||||
# __eq__
|
||||
|
||||
assert 3 + 02j == 3 + 2j
|
||||
assert complex(1, -1) == complex(1, -1)
|
||||
assert complex(1, 0) == 1
|
||||
assert 1 == complex(1, 0)
|
||||
@@ -22,9 +23,9 @@ assert complex(1, 0) != 1.5
|
||||
assert not 1.0 != complex(1, 0)
|
||||
assert bool(complex(1, 0))
|
||||
assert complex(1, 2) != complex(1, 1)
|
||||
assert complex(1, 2) != 'foo'
|
||||
assert complex(1, 2).__eq__('foo') == NotImplemented
|
||||
assert 1j != 10 ** 1000
|
||||
assert complex(1, 2) != "foo"
|
||||
assert complex(1, 2).__eq__("foo") == NotImplemented
|
||||
assert 1j != 10**1000
|
||||
|
||||
# __mul__, __rmul__
|
||||
|
||||
@@ -81,7 +82,7 @@ assert bool(complex(1, 0)) is True
|
||||
assert hash(complex(1)) == hash(float(1)) == hash(int(1))
|
||||
assert hash(complex(-1)) == hash(float(-1)) == hash(int(-1))
|
||||
assert hash(complex(3.14)) == hash(float(3.14))
|
||||
assert hash(complex(-float('inf'))) == hash(-float('inf'))
|
||||
assert hash(complex(-float("inf"))) == hash(-float("inf"))
|
||||
assert hash(1j) != hash(1)
|
||||
|
||||
# TODO: Find a way to test platform dependent values
|
||||
@@ -119,28 +120,28 @@ assert 1j - 1 == complex(-1, 1)
|
||||
assert 2j - 1j == complex(0, 1)
|
||||
|
||||
# type error addition
|
||||
assert_raises(TypeError, lambda: 1j + 'str')
|
||||
assert_raises(TypeError, lambda: 1j - 'str')
|
||||
assert_raises(TypeError, lambda: 'str' + 1j)
|
||||
assert_raises(TypeError, lambda: 'str' - 1j)
|
||||
assert_raises(TypeError, lambda: 1j + "str")
|
||||
assert_raises(TypeError, lambda: 1j - "str")
|
||||
assert_raises(TypeError, lambda: "str" + 1j)
|
||||
assert_raises(TypeError, lambda: "str" - 1j)
|
||||
|
||||
# overflow
|
||||
assert_raises(OverflowError, lambda: complex(10 ** 1000, 0))
|
||||
assert_raises(OverflowError, lambda: complex(0, 10 ** 1000))
|
||||
assert_raises(OverflowError, lambda: 0j + 10 ** 1000)
|
||||
assert_raises(OverflowError, lambda: complex(10**1000, 0))
|
||||
assert_raises(OverflowError, lambda: complex(0, 10**1000))
|
||||
assert_raises(OverflowError, lambda: 0j + 10**1000)
|
||||
|
||||
# str/repr
|
||||
assert '(1+1j)' == str(1+1j)
|
||||
assert '(1-1j)' == str(1-1j)
|
||||
assert '(1+1j)' == repr(1+1j)
|
||||
assert '(1-1j)' == repr(1-1j)
|
||||
assert "(1+1j)" == str(1 + 1j)
|
||||
assert "(1-1j)" == str(1 - 1j)
|
||||
assert "(1+1j)" == repr(1 + 1j)
|
||||
assert "(1-1j)" == repr(1 - 1j)
|
||||
|
||||
# __getnewargs__
|
||||
assert (3 + 5j).__getnewargs__() == (3.0, 5.0)
|
||||
assert (5j).__getnewargs__() == (0.0, 5.0)
|
||||
|
||||
|
||||
class Complex():
|
||||
class Complex:
|
||||
def __init__(self, real, imag):
|
||||
self.real = real
|
||||
self.imag = imag
|
||||
@@ -157,6 +158,7 @@ class Complex():
|
||||
def __eq__(self, other):
|
||||
return self.real == other.real and self.imag == other.imag
|
||||
|
||||
|
||||
assert Complex(4, 5) - 3 == Complex(1, 5)
|
||||
assert 7 - Complex(4, 5) == Complex(3, -5)
|
||||
|
||||
@@ -166,62 +168,71 @@ assert complex("-2j") == -2j
|
||||
assert_raises(TypeError, lambda: complex("5+2j", 1))
|
||||
assert_raises(ValueError, lambda: complex("abc"))
|
||||
|
||||
assert complex("1+10j") == 1+10j
|
||||
assert complex(10) == 10+0j
|
||||
assert complex(10.0) == 10+0j
|
||||
assert complex(10) == 10+0j
|
||||
assert complex(10+0j) == 10+0j
|
||||
assert complex(1, 10) == 1+10j
|
||||
assert complex(1, 10) == 1+10j
|
||||
assert complex(1, 10.0) == 1+10j
|
||||
assert complex(1, 10) == 1+10j
|
||||
assert complex(1, 10) == 1+10j
|
||||
assert complex(1, 10.0) == 1+10j
|
||||
assert complex(1.0, 10) == 1+10j
|
||||
assert complex(1.0, 10) == 1+10j
|
||||
assert complex(1.0, 10.0) == 1+10j
|
||||
assert complex(3.14+0j) == 3.14+0j
|
||||
assert complex(3.14) == 3.14+0j
|
||||
assert complex(314) == 314.0+0j
|
||||
assert complex(314) == 314.0+0j
|
||||
assert complex(3.14+0j, 0j) == 3.14+0j
|
||||
assert complex(3.14, 0.0) == 3.14+0j
|
||||
assert complex(314, 0) == 314.0+0j
|
||||
assert complex(314, 0) == 314.0+0j
|
||||
assert complex(0j, 3.14j) == -3.14+0j
|
||||
assert complex(0.0, 3.14j) == -3.14+0j
|
||||
assert complex("1+10j") == 1 + 10j
|
||||
assert complex(10) == 10 + 0j
|
||||
assert complex(10.0) == 10 + 0j
|
||||
assert complex(10) == 10 + 0j
|
||||
assert complex(10 + 0j) == 10 + 0j
|
||||
assert complex(1, 10) == 1 + 10j
|
||||
assert complex(1, 10) == 1 + 10j
|
||||
assert complex(1, 10.0) == 1 + 10j
|
||||
assert complex(1, 10) == 1 + 10j
|
||||
assert complex(1, 10) == 1 + 10j
|
||||
assert complex(1, 10.0) == 1 + 10j
|
||||
assert complex(1.0, 10) == 1 + 10j
|
||||
assert complex(1.0, 10) == 1 + 10j
|
||||
assert complex(1.0, 10.0) == 1 + 10j
|
||||
assert complex(3.14 + 0j) == 3.14 + 0j
|
||||
assert complex(3.14) == 3.14 + 0j
|
||||
assert complex(314) == 314.0 + 0j
|
||||
assert complex(314) == 314.0 + 0j
|
||||
assert complex(3.14 + 0j, 0j) == 3.14 + 0j
|
||||
assert complex(3.14, 0.0) == 3.14 + 0j
|
||||
assert complex(314, 0) == 314.0 + 0j
|
||||
assert complex(314, 0) == 314.0 + 0j
|
||||
assert complex(0j, 3.14j) == -3.14 + 0j
|
||||
assert complex(0.0, 3.14j) == -3.14 + 0j
|
||||
assert complex(0j, 3.14) == 3.14j
|
||||
assert complex(0.0, 3.14) == 3.14j
|
||||
assert complex("1") == 1+0j
|
||||
assert complex("1") == 1 + 0j
|
||||
assert complex("1j") == 1j
|
||||
assert complex() == 0
|
||||
assert complex("-1") == -1
|
||||
assert complex("+1") == +1
|
||||
assert complex("(1+2j)") == 1+2j
|
||||
assert complex("(1.3+2.2j)") == 1.3+2.2j
|
||||
assert complex("3.14+1J") == 3.14+1j
|
||||
assert complex(" ( +3.14-6J )") == 3.14-6j
|
||||
assert complex(" ( +3.14-J )") == 3.14-1j
|
||||
assert complex(" ( +3.14+j )") == 3.14+1j
|
||||
assert complex("(1+2j)") == 1 + 2j
|
||||
assert complex("(1.3+2.2j)") == 1.3 + 2.2j
|
||||
assert complex("3.14+1J") == 3.14 + 1j
|
||||
assert complex(" ( +3.14-6J )") == 3.14 - 6j
|
||||
assert complex(" ( +3.14-J )") == 3.14 - 1j
|
||||
assert complex(" ( +3.14+j )") == 3.14 + 1j
|
||||
assert complex("J") == 1j
|
||||
assert complex("( j )") == 1j
|
||||
assert complex("+J") == 1j
|
||||
assert complex("( -j)") == -1j
|
||||
assert complex('1e-500') == 0.0 + 0.0j
|
||||
assert complex('-1e-500j') == 0.0 - 0.0j
|
||||
assert complex('-1e-500+1e-500j') == -0.0 + 0.0j
|
||||
assert complex("1e-500") == 0.0 + 0.0j
|
||||
assert complex("-1e-500j") == 0.0 - 0.0j
|
||||
assert complex("-1e-500+1e-500j") == -0.0 + 0.0j
|
||||
|
||||
|
||||
# Invalid syntax:
|
||||
src = """
|
||||
b = 03 + 2j
|
||||
"""
|
||||
|
||||
with assert_raises(SyntaxError):
|
||||
exec(src)
|
||||
|
||||
|
||||
# __complex__
|
||||
def test__complex__():
|
||||
z = 3 + 4j
|
||||
assert z.__complex__() == z
|
||||
assert type(z.__complex__()) == complex
|
||||
|
||||
class complex_subclass(complex):
|
||||
pass
|
||||
z = complex_subclass(3 + 4j)
|
||||
assert z.__complex__() == 3 + 4j
|
||||
assert type(z.__complex__()) == complex
|
||||
z = 3 + 4j
|
||||
assert z.__complex__() == z
|
||||
assert type(z.__complex__()) == complex
|
||||
|
||||
testutils.skip_if_unsupported(3, 11, test__complex__)
|
||||
|
||||
class complex_subclass(complex):
|
||||
pass
|
||||
|
||||
|
||||
z = complex_subclass(3 + 4j)
|
||||
assert z.__complex__() == 3 + 4j
|
||||
assert type(z.__complex__()) == complex
|
||||
|
||||
@@ -2,9 +2,9 @@ import math
|
||||
|
||||
from testutils import assert_raises
|
||||
|
||||
NAN = float('nan')
|
||||
INF = float('inf')
|
||||
NINF = float('-inf')
|
||||
NAN = float("nan")
|
||||
INF = float("inf")
|
||||
NINF = float("-inf")
|
||||
|
||||
1 + 1.1
|
||||
|
||||
@@ -12,7 +12,7 @@ a = 1.2
|
||||
b = 1.3
|
||||
c = 1.2
|
||||
z = 2
|
||||
ov = 10 ** 1000
|
||||
ov = 10**1000
|
||||
|
||||
assert -a == -1.2
|
||||
|
||||
@@ -48,7 +48,7 @@ assert_raises(OverflowError, lambda: a * ov)
|
||||
assert_raises(OverflowError, lambda: a / ov)
|
||||
assert_raises(OverflowError, lambda: a // ov)
|
||||
assert_raises(OverflowError, lambda: a % ov)
|
||||
assert_raises(OverflowError, lambda: a ** ov)
|
||||
assert_raises(OverflowError, lambda: a**ov)
|
||||
assert_raises(OverflowError, lambda: ov + a)
|
||||
assert_raises(OverflowError, lambda: ov - a)
|
||||
assert_raises(OverflowError, lambda: ov * a)
|
||||
@@ -62,46 +62,46 @@ assert a <= 5
|
||||
assert a < 5.5
|
||||
assert a <= 5.5
|
||||
try:
|
||||
assert a < 'a'
|
||||
assert a < "a"
|
||||
except TypeError:
|
||||
pass
|
||||
try:
|
||||
assert a <= 'a'
|
||||
assert a <= "a"
|
||||
except TypeError:
|
||||
pass
|
||||
assert a > 1
|
||||
assert a >= 1
|
||||
try:
|
||||
assert a > 'a'
|
||||
assert a > "a"
|
||||
except TypeError:
|
||||
pass
|
||||
try:
|
||||
assert a >= 'a'
|
||||
assert a >= "a"
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
assert math.isnan(float('nan'))
|
||||
assert math.isnan(float('NaN'))
|
||||
assert math.isnan(float('+NaN'))
|
||||
assert math.isnan(float('-NaN'))
|
||||
assert math.isnan(float("nan"))
|
||||
assert math.isnan(float("NaN"))
|
||||
assert math.isnan(float("+NaN"))
|
||||
assert math.isnan(float("-NaN"))
|
||||
|
||||
assert math.isinf(float('inf'))
|
||||
assert math.isinf(float('Inf'))
|
||||
assert math.isinf(float('+Inf'))
|
||||
assert math.isinf(float('-Inf'))
|
||||
assert math.isinf(float("inf"))
|
||||
assert math.isinf(float("Inf"))
|
||||
assert math.isinf(float("+Inf"))
|
||||
assert math.isinf(float("-Inf"))
|
||||
|
||||
assert float() == 0
|
||||
|
||||
assert float('+Inf') > 0
|
||||
assert float('-Inf') < 0
|
||||
assert float("+Inf") > 0
|
||||
assert float("-Inf") < 0
|
||||
|
||||
assert float('3.14') == 3.14
|
||||
assert float('2.99e-23') == 2.99e-23
|
||||
assert float("3.14") == 3.14
|
||||
assert float("2.99e-23") == 2.99e-23
|
||||
|
||||
assert float(b'3.14') == 3.14
|
||||
assert float(b'2.99e-23') == 2.99e-23
|
||||
assert float(b"3.14") == 3.14
|
||||
assert float(b"2.99e-23") == 2.99e-23
|
||||
|
||||
assert_raises(ValueError, float, 'foo')
|
||||
assert_raises(ValueError, float, "foo")
|
||||
assert_raises(OverflowError, float, 2**10000)
|
||||
|
||||
# check eq and hash for small numbers
|
||||
@@ -116,30 +116,31 @@ assert hash(0.0) == hash(0)
|
||||
assert hash(0.0) == hash(False)
|
||||
assert hash(1.0) != hash(1.0000000001)
|
||||
|
||||
assert 03.2 == 3.2
|
||||
assert 5.0 in {3, 4, 5}
|
||||
assert {-1: 2}[-1.0] == 2
|
||||
|
||||
# check that magic methods are implemented for ints and floats
|
||||
|
||||
assert 1.0.__add__(1.0) == 2.0
|
||||
assert 1.0.__radd__(1.0) == 2.0
|
||||
assert 2.0.__sub__(1.0) == 1.0
|
||||
assert 2.0.__rmul__(1.0) == 2.0
|
||||
assert 1.0.__truediv__(2.0) == 0.5
|
||||
assert 1.0.__rtruediv__(2.0) == 2.0
|
||||
assert 2.5.__divmod__(2.0) == (1.0, 0.5)
|
||||
assert 2.0.__rdivmod__(2.5) == (1.0, 0.5)
|
||||
assert (1.0).__add__(1.0) == 2.0
|
||||
assert (1.0).__radd__(1.0) == 2.0
|
||||
assert (2.0).__sub__(1.0) == 1.0
|
||||
assert (2.0).__rmul__(1.0) == 2.0
|
||||
assert (1.0).__truediv__(2.0) == 0.5
|
||||
assert (1.0).__rtruediv__(2.0) == 2.0
|
||||
assert (2.5).__divmod__(2.0) == (1.0, 0.5)
|
||||
assert (2.0).__rdivmod__(2.5) == (1.0, 0.5)
|
||||
|
||||
assert 1.0.__add__(1) == 2.0
|
||||
assert 1.0.__radd__(1) == 2.0
|
||||
assert 2.0.__sub__(1) == 1.0
|
||||
assert 2.0.__rmul__(1) == 2.0
|
||||
assert 1.0.__truediv__(2) == 0.5
|
||||
assert 1.0.__rtruediv__(2) == 2.0
|
||||
assert 2.0.__mul__(1) == 2.0
|
||||
assert 2.0.__rsub__(1) == -1.0
|
||||
assert 2.0.__mod__(2) == 0.0
|
||||
assert 2.0.__rmod__(2) == 0.0
|
||||
assert (1.0).__add__(1) == 2.0
|
||||
assert (1.0).__radd__(1) == 2.0
|
||||
assert (2.0).__sub__(1) == 1.0
|
||||
assert (2.0).__rmul__(1) == 2.0
|
||||
assert (1.0).__truediv__(2) == 0.5
|
||||
assert (1.0).__rtruediv__(2) == 2.0
|
||||
assert (2.0).__mul__(1) == 2.0
|
||||
assert (2.0).__rsub__(1) == -1.0
|
||||
assert (2.0).__mod__(2) == 0.0
|
||||
assert (2.0).__rmod__(2) == 0.0
|
||||
assert_raises(ZeroDivisionError, lambda: 2.0 / 0)
|
||||
assert_raises(ZeroDivisionError, lambda: 2.0 // 0)
|
||||
assert_raises(ZeroDivisionError, lambda: 2.0 % 0)
|
||||
@@ -153,49 +154,49 @@ assert_raises(ZeroDivisionError, lambda: 2.0 // 0.0)
|
||||
assert_raises(ZeroDivisionError, lambda: 2.0 % 0.0)
|
||||
assert_raises(ZeroDivisionError, divmod, 2.0, 0.0)
|
||||
|
||||
assert 1.2.__int__() == 1
|
||||
assert 1.2.__float__() == 1.2
|
||||
assert 1.2.__trunc__() == 1
|
||||
assert (1.2).__int__() == 1
|
||||
assert (1.2).__float__() == 1.2
|
||||
assert (1.2).__trunc__() == 1
|
||||
assert int(1.2) == 1
|
||||
assert float(1.2) == 1.2
|
||||
assert math.trunc(1.2) == 1
|
||||
assert_raises(OverflowError, float('inf').__trunc__)
|
||||
assert_raises(ValueError, float('nan').__trunc__)
|
||||
assert isinstance(0.5.__round__(), int)
|
||||
assert isinstance(1.5.__round__(), int)
|
||||
assert 0.5.__round__() == 0
|
||||
assert 1.5.__round__() == 2
|
||||
assert isinstance(0.5.__round__(0), float)
|
||||
assert isinstance(1.5.__round__(0), float)
|
||||
assert 0.5.__round__(0) == 0.0
|
||||
assert 1.5.__round__(0) == 2.0
|
||||
assert isinstance(0.5.__round__(None), int)
|
||||
assert isinstance(1.5.__round__(None), int)
|
||||
assert 0.5.__round__(None) == 0
|
||||
assert 1.5.__round__(None) == 2
|
||||
assert 1.234.__round__(1) == 1.2
|
||||
assert 1.23456.__round__(4) == 1.2346
|
||||
assert 1.00000000001.__round__(10) == 1.0
|
||||
assert 1234.5.__round__(-2) == 1200
|
||||
assert 1.234.__round__(-1) == 0
|
||||
assert 1.23456789.__round__(15) == 1.23456789
|
||||
assert 1.2e300.__round__(-500) == 0
|
||||
assert 1.234.__round__(500) == 1.234
|
||||
assert 1.2e-300.__round__(299) == 0
|
||||
assert_raises(TypeError, lambda: 0.5.__round__(0.0))
|
||||
assert_raises(TypeError, lambda: 1.5.__round__(0.0))
|
||||
assert_raises(OverflowError, float('inf').__round__)
|
||||
assert_raises(ValueError, float('nan').__round__)
|
||||
assert_raises(OverflowError, float("inf").__trunc__)
|
||||
assert_raises(ValueError, float("nan").__trunc__)
|
||||
assert isinstance((0.5).__round__(), int)
|
||||
assert isinstance((1.5).__round__(), int)
|
||||
assert (0.5).__round__() == 0
|
||||
assert (1.5).__round__() == 2
|
||||
assert isinstance((0.5).__round__(0), float)
|
||||
assert isinstance((1.5).__round__(0), float)
|
||||
assert (0.5).__round__(0) == 0.0
|
||||
assert (1.5).__round__(0) == 2.0
|
||||
assert isinstance((0.5).__round__(None), int)
|
||||
assert isinstance((1.5).__round__(None), int)
|
||||
assert (0.5).__round__(None) == 0
|
||||
assert (1.5).__round__(None) == 2
|
||||
assert (1.234).__round__(1) == 1.2
|
||||
assert (1.23456).__round__(4) == 1.2346
|
||||
assert (1.00000000001).__round__(10) == 1.0
|
||||
assert (1234.5).__round__(-2) == 1200
|
||||
assert (1.234).__round__(-1) == 0
|
||||
assert (1.23456789).__round__(15) == 1.23456789
|
||||
assert (1.2e300).__round__(-500) == 0
|
||||
assert (1.234).__round__(500) == 1.234
|
||||
assert (1.2e-300).__round__(299) == 0
|
||||
assert_raises(TypeError, lambda: (0.5).__round__(0.0))
|
||||
assert_raises(TypeError, lambda: (1.5).__round__(0.0))
|
||||
assert_raises(OverflowError, float("inf").__round__)
|
||||
assert_raises(ValueError, float("nan").__round__)
|
||||
|
||||
assert 1.2 ** 2 == 1.44
|
||||
assert_raises(OverflowError, lambda: 1.2 ** (10 ** 1000))
|
||||
assert 3 ** 2.0 == 9.0
|
||||
assert 1.2**2 == 1.44
|
||||
assert_raises(OverflowError, lambda: 1.2 ** (10**1000))
|
||||
assert 3**2.0 == 9.0
|
||||
|
||||
assert (1.7).real == 1.7
|
||||
assert (1.7).imag == 0.0
|
||||
assert (1.7).conjugate() == 1.7
|
||||
assert (1.3).is_integer() == False
|
||||
assert (1.0).is_integer() == True
|
||||
assert (1.0).is_integer() == True
|
||||
|
||||
assert (0.875).as_integer_ratio() == (7, 8)
|
||||
assert (-0.875).as_integer_ratio() == (-7, 8)
|
||||
@@ -208,22 +209,28 @@ assert (2.1).as_integer_ratio() == (4728779608739021, 2251799813685248)
|
||||
assert (-2.1).as_integer_ratio() == (-4728779608739021, 2251799813685248)
|
||||
assert (-2100.0).as_integer_ratio() == (-2100, 1)
|
||||
assert (2.220446049250313e-16).as_integer_ratio() == (1, 4503599627370496)
|
||||
assert (1.7976931348623157e+308).as_integer_ratio() == (179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368, 1)
|
||||
assert (2.2250738585072014e-308).as_integer_ratio() == (1, 44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304)
|
||||
assert (1.7976931348623157e308).as_integer_ratio() == (
|
||||
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368,
|
||||
1,
|
||||
)
|
||||
assert (2.2250738585072014e-308).as_integer_ratio() == (
|
||||
1,
|
||||
44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304,
|
||||
)
|
||||
|
||||
assert_raises(OverflowError, float('inf').as_integer_ratio)
|
||||
assert_raises(OverflowError, float('-inf').as_integer_ratio)
|
||||
assert_raises(ValueError, float('nan').as_integer_ratio)
|
||||
assert_raises(OverflowError, float("inf").as_integer_ratio)
|
||||
assert_raises(OverflowError, float("-inf").as_integer_ratio)
|
||||
assert_raises(ValueError, float("nan").as_integer_ratio)
|
||||
|
||||
assert str(1.0) == '1.0'
|
||||
assert str(0.0) == '0.0'
|
||||
assert str(-0.0) == '-0.0'
|
||||
assert str(1.123456789) == '1.123456789'
|
||||
assert str(1.0) == "1.0"
|
||||
assert str(0.0) == "0.0"
|
||||
assert str(-0.0) == "-0.0"
|
||||
assert str(1.123456789) == "1.123456789"
|
||||
|
||||
# Test special case for lexer, float starts with a dot:
|
||||
a = .5
|
||||
a = 0.5
|
||||
assert a == 0.5
|
||||
assert 3.14 == float('3.14')
|
||||
assert 3.14 == float("3.14")
|
||||
src = """
|
||||
a = 3._14
|
||||
"""
|
||||
@@ -245,6 +252,8 @@ with assert_raises(SyntaxError):
|
||||
exec(src)
|
||||
|
||||
fromHex = float.fromhex
|
||||
|
||||
|
||||
def identical(x, y):
|
||||
if math.isnan(x) or math.isnan(y):
|
||||
if math.isnan(x) == math.isnan(y):
|
||||
@@ -253,6 +262,7 @@ def identical(x, y):
|
||||
return
|
||||
raise SyntaxError(f"{x} not identical to {y}")
|
||||
|
||||
|
||||
invalid_inputs = [
|
||||
"infi", # misspelt infinities and nans
|
||||
"-Infinit",
|
||||
@@ -318,16 +328,7 @@ value_pairs = [
|
||||
("-0x.2", -0.125),
|
||||
("-0.0", -0.0),
|
||||
]
|
||||
whitespace = [
|
||||
"",
|
||||
" ",
|
||||
"\t",
|
||||
"\n",
|
||||
"\n \t",
|
||||
"\f",
|
||||
"\v",
|
||||
"\r"
|
||||
]
|
||||
whitespace = ["", " ", "\t", "\n", "\n \t", "\f", "\v", "\r"]
|
||||
|
||||
for inp, expected in value_pairs:
|
||||
for lead in whitespace:
|
||||
@@ -335,120 +336,120 @@ for inp, expected in value_pairs:
|
||||
got = fromHex(lead + inp + trail)
|
||||
identical(got, expected)
|
||||
|
||||
MAX = fromHex('0x.fffffffffffff8p+1024') # max normal
|
||||
MIN = fromHex('0x1p-1022') # min normal
|
||||
TINY = fromHex('0x0.0000000000001p-1022') # min subnormal
|
||||
EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up
|
||||
MAX = fromHex("0x.fffffffffffff8p+1024") # max normal
|
||||
MIN = fromHex("0x1p-1022") # min normal
|
||||
TINY = fromHex("0x0.0000000000001p-1022") # min subnormal
|
||||
EPS = fromHex("0x0.0000000000001p0") # diff between 1.0 and next float up
|
||||
|
||||
# two spellings of infinity, with optional signs; case-insensitive
|
||||
identical(fromHex('inf'), INF)
|
||||
identical(fromHex('+Inf'), INF)
|
||||
identical(fromHex('-INF'), -INF)
|
||||
identical(fromHex('iNf'), INF)
|
||||
identical(fromHex('Infinity'), INF)
|
||||
identical(fromHex('+INFINITY'), INF)
|
||||
identical(fromHex('-infinity'), -INF)
|
||||
identical(fromHex('-iNFiNitY'), -INF)
|
||||
identical(fromHex("inf"), INF)
|
||||
identical(fromHex("+Inf"), INF)
|
||||
identical(fromHex("-INF"), -INF)
|
||||
identical(fromHex("iNf"), INF)
|
||||
identical(fromHex("Infinity"), INF)
|
||||
identical(fromHex("+INFINITY"), INF)
|
||||
identical(fromHex("-infinity"), -INF)
|
||||
identical(fromHex("-iNFiNitY"), -INF)
|
||||
|
||||
# nans with optional sign; case insensitive
|
||||
identical(fromHex('nan'), NAN)
|
||||
identical(fromHex('+NaN'), NAN)
|
||||
identical(fromHex('-NaN'), NAN)
|
||||
identical(fromHex('-nAN'), NAN)
|
||||
identical(fromHex("nan"), NAN)
|
||||
identical(fromHex("+NaN"), NAN)
|
||||
identical(fromHex("-NaN"), NAN)
|
||||
identical(fromHex("-nAN"), NAN)
|
||||
|
||||
# variations in input format
|
||||
identical(fromHex('1'), 1.0)
|
||||
identical(fromHex('+1'), 1.0)
|
||||
identical(fromHex('1.'), 1.0)
|
||||
identical(fromHex('1.0'), 1.0)
|
||||
identical(fromHex('1.0p0'), 1.0)
|
||||
identical(fromHex('01'), 1.0)
|
||||
identical(fromHex('01.'), 1.0)
|
||||
identical(fromHex('0x1'), 1.0)
|
||||
identical(fromHex('0x1.'), 1.0)
|
||||
identical(fromHex('0x1.0'), 1.0)
|
||||
identical(fromHex('+0x1.0'), 1.0)
|
||||
identical(fromHex('0x1p0'), 1.0)
|
||||
identical(fromHex('0X1p0'), 1.0)
|
||||
identical(fromHex('0X1P0'), 1.0)
|
||||
identical(fromHex('0x1P0'), 1.0)
|
||||
identical(fromHex('0x1.p0'), 1.0)
|
||||
identical(fromHex('0x1.0p0'), 1.0)
|
||||
identical(fromHex('0x.1p4'), 1.0)
|
||||
identical(fromHex('0x.1p04'), 1.0)
|
||||
identical(fromHex('0x.1p004'), 1.0)
|
||||
identical(fromHex('0x1p+0'), 1.0)
|
||||
identical(fromHex('0x1P-0'), 1.0)
|
||||
identical(fromHex('+0x1p0'), 1.0)
|
||||
identical(fromHex('0x01p0'), 1.0)
|
||||
identical(fromHex('0x1p00'), 1.0)
|
||||
identical(fromHex(' 0x1p0 '), 1.0)
|
||||
identical(fromHex('\n 0x1p0'), 1.0)
|
||||
identical(fromHex('0x1p0 \t'), 1.0)
|
||||
identical(fromHex('0xap0'), 10.0)
|
||||
identical(fromHex('0xAp0'), 10.0)
|
||||
identical(fromHex('0xaP0'), 10.0)
|
||||
identical(fromHex('0xAP0'), 10.0)
|
||||
identical(fromHex('0xbep0'), 190.0)
|
||||
identical(fromHex('0xBep0'), 190.0)
|
||||
identical(fromHex('0xbEp0'), 190.0)
|
||||
identical(fromHex('0XBE0P-4'), 190.0)
|
||||
identical(fromHex('0xBEp0'), 190.0)
|
||||
identical(fromHex('0xB.Ep4'), 190.0)
|
||||
identical(fromHex('0x.BEp8'), 190.0)
|
||||
identical(fromHex('0x.0BEp12'), 190.0)
|
||||
identical(fromHex("1"), 1.0)
|
||||
identical(fromHex("+1"), 1.0)
|
||||
identical(fromHex("1."), 1.0)
|
||||
identical(fromHex("1.0"), 1.0)
|
||||
identical(fromHex("1.0p0"), 1.0)
|
||||
identical(fromHex("01"), 1.0)
|
||||
identical(fromHex("01."), 1.0)
|
||||
identical(fromHex("0x1"), 1.0)
|
||||
identical(fromHex("0x1."), 1.0)
|
||||
identical(fromHex("0x1.0"), 1.0)
|
||||
identical(fromHex("+0x1.0"), 1.0)
|
||||
identical(fromHex("0x1p0"), 1.0)
|
||||
identical(fromHex("0X1p0"), 1.0)
|
||||
identical(fromHex("0X1P0"), 1.0)
|
||||
identical(fromHex("0x1P0"), 1.0)
|
||||
identical(fromHex("0x1.p0"), 1.0)
|
||||
identical(fromHex("0x1.0p0"), 1.0)
|
||||
identical(fromHex("0x.1p4"), 1.0)
|
||||
identical(fromHex("0x.1p04"), 1.0)
|
||||
identical(fromHex("0x.1p004"), 1.0)
|
||||
identical(fromHex("0x1p+0"), 1.0)
|
||||
identical(fromHex("0x1P-0"), 1.0)
|
||||
identical(fromHex("+0x1p0"), 1.0)
|
||||
identical(fromHex("0x01p0"), 1.0)
|
||||
identical(fromHex("0x1p00"), 1.0)
|
||||
identical(fromHex(" 0x1p0 "), 1.0)
|
||||
identical(fromHex("\n 0x1p0"), 1.0)
|
||||
identical(fromHex("0x1p0 \t"), 1.0)
|
||||
identical(fromHex("0xap0"), 10.0)
|
||||
identical(fromHex("0xAp0"), 10.0)
|
||||
identical(fromHex("0xaP0"), 10.0)
|
||||
identical(fromHex("0xAP0"), 10.0)
|
||||
identical(fromHex("0xbep0"), 190.0)
|
||||
identical(fromHex("0xBep0"), 190.0)
|
||||
identical(fromHex("0xbEp0"), 190.0)
|
||||
identical(fromHex("0XBE0P-4"), 190.0)
|
||||
identical(fromHex("0xBEp0"), 190.0)
|
||||
identical(fromHex("0xB.Ep4"), 190.0)
|
||||
identical(fromHex("0x.BEp8"), 190.0)
|
||||
identical(fromHex("0x.0BEp12"), 190.0)
|
||||
|
||||
# moving the point around
|
||||
pi = fromHex('0x1.921fb54442d18p1')
|
||||
identical(fromHex('0x.006487ed5110b46p11'), pi)
|
||||
identical(fromHex('0x.00c90fdaa22168cp10'), pi)
|
||||
identical(fromHex('0x.01921fb54442d18p9'), pi)
|
||||
identical(fromHex('0x.03243f6a8885a3p8'), pi)
|
||||
identical(fromHex('0x.06487ed5110b46p7'), pi)
|
||||
identical(fromHex('0x.0c90fdaa22168cp6'), pi)
|
||||
identical(fromHex('0x.1921fb54442d18p5'), pi)
|
||||
identical(fromHex('0x.3243f6a8885a3p4'), pi)
|
||||
identical(fromHex('0x.6487ed5110b46p3'), pi)
|
||||
identical(fromHex('0x.c90fdaa22168cp2'), pi)
|
||||
identical(fromHex('0x1.921fb54442d18p1'), pi)
|
||||
identical(fromHex('0x3.243f6a8885a3p0'), pi)
|
||||
identical(fromHex('0x6.487ed5110b46p-1'), pi)
|
||||
identical(fromHex('0xc.90fdaa22168cp-2'), pi)
|
||||
identical(fromHex('0x19.21fb54442d18p-3'), pi)
|
||||
identical(fromHex('0x32.43f6a8885a3p-4'), pi)
|
||||
identical(fromHex('0x64.87ed5110b46p-5'), pi)
|
||||
identical(fromHex('0xc9.0fdaa22168cp-6'), pi)
|
||||
identical(fromHex('0x192.1fb54442d18p-7'), pi)
|
||||
identical(fromHex('0x324.3f6a8885a3p-8'), pi)
|
||||
identical(fromHex('0x648.7ed5110b46p-9'), pi)
|
||||
identical(fromHex('0xc90.fdaa22168cp-10'), pi)
|
||||
identical(fromHex('0x1921.fb54442d18p-11'), pi)
|
||||
identical(fromHex('0x1921fb54442d1.8p-47'), pi)
|
||||
identical(fromHex('0x3243f6a8885a3p-48'), pi)
|
||||
identical(fromHex('0x6487ed5110b46p-49'), pi)
|
||||
identical(fromHex('0xc90fdaa22168cp-50'), pi)
|
||||
identical(fromHex('0x1921fb54442d18p-51'), pi)
|
||||
identical(fromHex('0x3243f6a8885a30p-52'), pi)
|
||||
identical(fromHex('0x6487ed5110b460p-53'), pi)
|
||||
identical(fromHex('0xc90fdaa22168c0p-54'), pi)
|
||||
identical(fromHex('0x1921fb54442d180p-55'), pi)
|
||||
pi = fromHex("0x1.921fb54442d18p1")
|
||||
identical(fromHex("0x.006487ed5110b46p11"), pi)
|
||||
identical(fromHex("0x.00c90fdaa22168cp10"), pi)
|
||||
identical(fromHex("0x.01921fb54442d18p9"), pi)
|
||||
identical(fromHex("0x.03243f6a8885a3p8"), pi)
|
||||
identical(fromHex("0x.06487ed5110b46p7"), pi)
|
||||
identical(fromHex("0x.0c90fdaa22168cp6"), pi)
|
||||
identical(fromHex("0x.1921fb54442d18p5"), pi)
|
||||
identical(fromHex("0x.3243f6a8885a3p4"), pi)
|
||||
identical(fromHex("0x.6487ed5110b46p3"), pi)
|
||||
identical(fromHex("0x.c90fdaa22168cp2"), pi)
|
||||
identical(fromHex("0x1.921fb54442d18p1"), pi)
|
||||
identical(fromHex("0x3.243f6a8885a3p0"), pi)
|
||||
identical(fromHex("0x6.487ed5110b46p-1"), pi)
|
||||
identical(fromHex("0xc.90fdaa22168cp-2"), pi)
|
||||
identical(fromHex("0x19.21fb54442d18p-3"), pi)
|
||||
identical(fromHex("0x32.43f6a8885a3p-4"), pi)
|
||||
identical(fromHex("0x64.87ed5110b46p-5"), pi)
|
||||
identical(fromHex("0xc9.0fdaa22168cp-6"), pi)
|
||||
identical(fromHex("0x192.1fb54442d18p-7"), pi)
|
||||
identical(fromHex("0x324.3f6a8885a3p-8"), pi)
|
||||
identical(fromHex("0x648.7ed5110b46p-9"), pi)
|
||||
identical(fromHex("0xc90.fdaa22168cp-10"), pi)
|
||||
identical(fromHex("0x1921.fb54442d18p-11"), pi)
|
||||
identical(fromHex("0x1921fb54442d1.8p-47"), pi)
|
||||
identical(fromHex("0x3243f6a8885a3p-48"), pi)
|
||||
identical(fromHex("0x6487ed5110b46p-49"), pi)
|
||||
identical(fromHex("0xc90fdaa22168cp-50"), pi)
|
||||
identical(fromHex("0x1921fb54442d18p-51"), pi)
|
||||
identical(fromHex("0x3243f6a8885a30p-52"), pi)
|
||||
identical(fromHex("0x6487ed5110b460p-53"), pi)
|
||||
identical(fromHex("0xc90fdaa22168c0p-54"), pi)
|
||||
identical(fromHex("0x1921fb54442d180p-55"), pi)
|
||||
|
||||
assert (0.0).hex() == '0x0.0p+0'
|
||||
assert (-0.0).hex() == '-0x0.0p+0'
|
||||
assert (1.0).hex() == '0x1.0000000000000p+0'
|
||||
assert (-1.5).hex() == '-0x1.8000000000000p+0'
|
||||
assert float('inf').hex() == 'inf'
|
||||
assert float('-inf').hex() == '-inf'
|
||||
assert float('nan').hex() == 'nan'
|
||||
assert (0.0).hex() == "0x0.0p+0"
|
||||
assert (-0.0).hex() == "-0x0.0p+0"
|
||||
assert (1.0).hex() == "0x1.0000000000000p+0"
|
||||
assert (-1.5).hex() == "-0x1.8000000000000p+0"
|
||||
assert float("inf").hex() == "inf"
|
||||
assert float("-inf").hex() == "-inf"
|
||||
assert float("nan").hex() == "nan"
|
||||
|
||||
assert float(math.nan) is float(math.nan)
|
||||
|
||||
# Test float exponent:
|
||||
assert 1 if 1else 0 == 1
|
||||
assert 1 if 1 else 0 == 1
|
||||
|
||||
a = 3.
|
||||
a = 3.0
|
||||
assert a.__eq__(3) is True
|
||||
assert a.__eq__(3.) is True
|
||||
assert a.__eq__(3.0) is True
|
||||
assert a.__eq__(3.00000) is True
|
||||
assert a.__eq__(3.01) is False
|
||||
|
||||
@@ -457,26 +458,26 @@ assert pi.__eq__(3.14) is True
|
||||
assert pi.__ne__(3.14) is False
|
||||
assert pi.__eq__(3) is False
|
||||
assert pi.__ne__(3) is True
|
||||
assert pi.__eq__('pi') is NotImplemented
|
||||
assert pi.__ne__('pi') is NotImplemented
|
||||
assert pi.__eq__("pi") is NotImplemented
|
||||
assert pi.__ne__("pi") is NotImplemented
|
||||
|
||||
assert pi.__eq__(float('inf')) is False
|
||||
assert pi.__ne__(float('inf')) is True
|
||||
assert float('inf').__eq__(pi) is False
|
||||
assert float('inf').__ne__(pi) is True
|
||||
assert float('inf').__eq__(float('inf')) is True
|
||||
assert float('inf').__ne__(float('inf')) is False
|
||||
assert float('inf').__eq__(float('nan')) is False
|
||||
assert float('inf').__ne__(float('nan')) is True
|
||||
assert pi.__eq__(float("inf")) is False
|
||||
assert pi.__ne__(float("inf")) is True
|
||||
assert float("inf").__eq__(pi) is False
|
||||
assert float("inf").__ne__(pi) is True
|
||||
assert float("inf").__eq__(float("inf")) is True
|
||||
assert float("inf").__ne__(float("inf")) is False
|
||||
assert float("inf").__eq__(float("nan")) is False
|
||||
assert float("inf").__ne__(float("nan")) is True
|
||||
|
||||
assert pi.__eq__(float('nan')) is False
|
||||
assert pi.__ne__(float('nan')) is True
|
||||
assert float('nan').__eq__(pi) is False
|
||||
assert float('nan').__ne__(pi) is True
|
||||
assert float('nan').__eq__(float('nan')) is False
|
||||
assert float('nan').__ne__(float('nan')) is True
|
||||
assert float('nan').__eq__(float('inf')) is False
|
||||
assert float('nan').__ne__(float('inf')) is True
|
||||
assert pi.__eq__(float("nan")) is False
|
||||
assert pi.__ne__(float("nan")) is True
|
||||
assert float("nan").__eq__(pi) is False
|
||||
assert float("nan").__ne__(pi) is True
|
||||
assert float("nan").__eq__(float("nan")) is False
|
||||
assert float("nan").__ne__(float("nan")) is True
|
||||
assert float("nan").__eq__(float("inf")) is False
|
||||
assert float("nan").__ne__(float("inf")) is True
|
||||
|
||||
assert float(1e15).__repr__() == "1000000000000000.0"
|
||||
assert float(1e16).__repr__() == "1e+16"
|
||||
@@ -500,19 +501,19 @@ assert format(1e-4) == "0.0001"
|
||||
assert format(1.2345678901234567890) == "1.2345678901234567"
|
||||
assert format(1.2345678901234567890e308) == "1.2345678901234567e+308"
|
||||
|
||||
assert float('0_0') == 0.0
|
||||
assert float('.0') == 0.0
|
||||
assert float('0.') == 0.0
|
||||
assert float('-.0') == 0.0
|
||||
assert float('+.0') == 0.0
|
||||
assert float("0_0") == 0.0
|
||||
assert float(".0") == 0.0
|
||||
assert float("0.") == 0.0
|
||||
assert float("-.0") == 0.0
|
||||
assert float("+.0") == 0.0
|
||||
|
||||
assert_raises(ValueError, lambda: float('0._0'))
|
||||
assert_raises(ValueError, lambda: float('0_.0'))
|
||||
assert_raises(ValueError, lambda: float('._0'))
|
||||
assert_raises(ValueError, lambda: float('0_'))
|
||||
assert_raises(ValueError, lambda: float('0._'))
|
||||
assert_raises(ValueError, lambda: float('_.0'))
|
||||
assert_raises(ValueError, lambda: float('._0'))
|
||||
assert_raises(ValueError, lambda: float("0._0"))
|
||||
assert_raises(ValueError, lambda: float("0_.0"))
|
||||
assert_raises(ValueError, lambda: float("._0"))
|
||||
assert_raises(ValueError, lambda: float("0_"))
|
||||
assert_raises(ValueError, lambda: float("0._"))
|
||||
assert_raises(ValueError, lambda: float("_.0"))
|
||||
assert_raises(ValueError, lambda: float("._0"))
|
||||
|
||||
assert 3.0 % -2.0 == -1.0
|
||||
assert -3.0 % 2.0 == 1.0
|
||||
|
||||
@@ -78,7 +78,7 @@ assert (10).denominator == 1
|
||||
assert (-10).numerator == -10
|
||||
assert (-10).denominator == 1
|
||||
|
||||
assert_raises(OverflowError, lambda: 1 << 10 ** 100000)
|
||||
assert_raises(OverflowError, lambda: 1 << 10**100000)
|
||||
|
||||
assert (1).__eq__(1.0) == NotImplemented
|
||||
assert (1).__ne__(1.0) == NotImplemented
|
||||
@@ -110,34 +110,34 @@ assert int("101", 2) == 5
|
||||
assert int("101", base=2) == 5
|
||||
|
||||
# implied base
|
||||
assert int('1', base=0) == 1
|
||||
assert int('123', base=0) == 123
|
||||
assert int('0b101', base=0) == 5
|
||||
assert int('0B101', base=0) == 5
|
||||
assert int('0o100', base=0) == 64
|
||||
assert int('0O100', base=0) == 64
|
||||
assert int('0xFF', base=0) == 255
|
||||
assert int('0XFF', base=0) == 255
|
||||
assert int("1", base=0) == 1
|
||||
assert int("123", base=0) == 123
|
||||
assert int("0b101", base=0) == 5
|
||||
assert int("0B101", base=0) == 5
|
||||
assert int("0o100", base=0) == 64
|
||||
assert int("0O100", base=0) == 64
|
||||
assert int("0xFF", base=0) == 255
|
||||
assert int("0XFF", base=0) == 255
|
||||
with assert_raises(ValueError):
|
||||
int('0xFF', base=10)
|
||||
int("0xFF", base=10)
|
||||
with assert_raises(ValueError):
|
||||
int('0oFF', base=10)
|
||||
int("0oFF", base=10)
|
||||
with assert_raises(ValueError):
|
||||
int('0bFF', base=10)
|
||||
int("0bFF", base=10)
|
||||
with assert_raises(ValueError):
|
||||
int('0bFF', base=10)
|
||||
int("0bFF", base=10)
|
||||
with assert_raises(ValueError):
|
||||
int(b"F\xc3\xb8\xc3\xb6\xbbB\xc3\xa5r")
|
||||
with assert_raises(ValueError):
|
||||
int(b"F\xc3\xb8\xc3\xb6\xbbB\xc3\xa5r")
|
||||
|
||||
# string looks like radix
|
||||
assert int('0b1', base=12) == 133
|
||||
assert int('0o1', base=25) == 601
|
||||
assert int('0x1', base=34) == 1123
|
||||
assert int("0b1", base=12) == 133
|
||||
assert int("0o1", base=25) == 601
|
||||
assert int("0x1", base=34) == 1123
|
||||
|
||||
# underscore
|
||||
assert int('0xFF_FF_FF', base=16) == 16_777_215
|
||||
assert int("0xFF_FF_FF", base=16) == 16_777_215
|
||||
with assert_raises(ValueError):
|
||||
int("_123_")
|
||||
with assert_raises(ValueError):
|
||||
@@ -147,93 +147,105 @@ with assert_raises(ValueError):
|
||||
with assert_raises(ValueError):
|
||||
int("1__23")
|
||||
|
||||
assert int('0x_10', base=0) == 16
|
||||
assert int("0x_10", base=0) == 16
|
||||
|
||||
# signed
|
||||
assert int('-123') == -123
|
||||
assert int('+0b101', base=2) == +5
|
||||
assert int("-123") == -123
|
||||
assert int("+0b101", base=2) == +5
|
||||
|
||||
# trailing spaces
|
||||
assert int(' 1') == 1
|
||||
assert int('1 ') == 1
|
||||
assert int(' 1 ') == 1
|
||||
assert int('10', base=0) == 10
|
||||
assert int(" 1") == 1
|
||||
assert int("1 ") == 1
|
||||
assert int(" 1 ") == 1
|
||||
assert int("10", base=0) == 10
|
||||
|
||||
# type byte, signed, implied base
|
||||
assert int(b' -0XFF ', base=0) == -255
|
||||
assert int(b" -0XFF ", base=0) == -255
|
||||
|
||||
assert int.from_bytes(b'\x00\x10', 'big') == 16
|
||||
assert int.from_bytes(b'\x00\x10', 'little') == 4096
|
||||
assert int.from_bytes(b'\x00\x10', byteorder='big') == 16
|
||||
assert int.from_bytes(b'\x00\x10', byteorder='little') == 4096
|
||||
assert int.from_bytes(bytes=b'\x00\x10', byteorder='big') == 16
|
||||
assert int.from_bytes(bytes=b'\x00\x10', byteorder='little') == 4096
|
||||
assert int.from_bytes(b"\x00\x10", "big") == 16
|
||||
assert int.from_bytes(b"\x00\x10", "little") == 4096
|
||||
assert int.from_bytes(b"\x00\x10", byteorder="big") == 16
|
||||
assert int.from_bytes(b"\x00\x10", byteorder="little") == 4096
|
||||
assert int.from_bytes(bytes=b"\x00\x10", byteorder="big") == 16
|
||||
assert int.from_bytes(bytes=b"\x00\x10", byteorder="little") == 4096
|
||||
|
||||
assert int.from_bytes(b'\xfc\x00', 'big', signed=True) == -1024
|
||||
assert int.from_bytes(b'\xfc\x00', 'big', signed=False) == 64512
|
||||
assert int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) == -1024
|
||||
assert int.from_bytes(b'\xfc\x00', byteorder='big', signed=False) == 64512
|
||||
assert int.from_bytes(bytes=b'\xfc\x00', byteorder='big', signed=True) == -1024
|
||||
assert int.from_bytes(bytes=b'\xfc\x00', byteorder='big', signed=False) == 64512
|
||||
assert int.from_bytes(b"\xfc\x00", "big", signed=True) == -1024
|
||||
assert int.from_bytes(b"\xfc\x00", "big", signed=False) == 64512
|
||||
assert int.from_bytes(b"\xfc\x00", byteorder="big", signed=True) == -1024
|
||||
assert int.from_bytes(b"\xfc\x00", byteorder="big", signed=False) == 64512
|
||||
assert int.from_bytes(bytes=b"\xfc\x00", byteorder="big", signed=True) == -1024
|
||||
assert int.from_bytes(bytes=b"\xfc\x00", byteorder="big", signed=False) == 64512
|
||||
|
||||
assert int.from_bytes([255, 0, 0], 'big') == 16711680
|
||||
assert int.from_bytes([255, 0, 0], 'little') == 255
|
||||
assert int.from_bytes([255, 0, 0], 'big', signed=False) == 16711680
|
||||
assert int.from_bytes([255, 0, 0], 'big', signed=True) == -65536
|
||||
assert int.from_bytes([255, 0, 0], "big") == 16711680
|
||||
assert int.from_bytes([255, 0, 0], "little") == 255
|
||||
assert int.from_bytes([255, 0, 0], "big", signed=False) == 16711680
|
||||
assert int.from_bytes([255, 0, 0], "big", signed=True) == -65536
|
||||
|
||||
with assert_raises(ValueError):
|
||||
int.from_bytes(b'\x00\x10', 'something')
|
||||
int.from_bytes(b"\x00\x10", "something")
|
||||
|
||||
with assert_raises(ValueError):
|
||||
int.from_bytes([256, 0, 0], 'big')
|
||||
int.from_bytes([256, 0, 0], "big")
|
||||
|
||||
with assert_raises(TypeError):
|
||||
int.from_bytes(['something', 0, 0], 'big')
|
||||
int.from_bytes(["something", 0, 0], "big")
|
||||
|
||||
assert (1024).to_bytes(4, 'big') == b'\x00\x00\x04\x00'
|
||||
assert (1024).to_bytes(2, 'little') == b'\x00\x04'
|
||||
assert (1024).to_bytes(4, byteorder='big') == b'\x00\x00\x04\x00'
|
||||
assert (1024).to_bytes(2, byteorder='little') == b'\x00\x04'
|
||||
assert (1024).to_bytes(length=4, byteorder='big') == b'\x00\x00\x04\x00'
|
||||
assert (1024).to_bytes(length=2, byteorder='little') == b'\x00\x04'
|
||||
assert (1024).to_bytes(4, "big") == b"\x00\x00\x04\x00"
|
||||
assert (1024).to_bytes(2, "little") == b"\x00\x04"
|
||||
assert (1024).to_bytes(4, byteorder="big") == b"\x00\x00\x04\x00"
|
||||
assert (1024).to_bytes(2, byteorder="little") == b"\x00\x04"
|
||||
assert (1024).to_bytes(length=4, byteorder="big") == b"\x00\x00\x04\x00"
|
||||
assert (1024).to_bytes(length=2, byteorder="little") == b"\x00\x04"
|
||||
|
||||
assert (-1024).to_bytes(4, 'big', signed=True) == b'\xff\xff\xfc\x00'
|
||||
assert (-1024).to_bytes(4, 'little', signed=True) == b'\x00\xfc\xff\xff'
|
||||
assert (-1024).to_bytes(4, "big", signed=True) == b"\xff\xff\xfc\x00"
|
||||
assert (-1024).to_bytes(4, "little", signed=True) == b"\x00\xfc\xff\xff"
|
||||
|
||||
assert (2147483647).to_bytes(8, 'big', signed=False) == b'\x00\x00\x00\x00\x7f\xff\xff\xff'
|
||||
assert (-2147483648).to_bytes(8, 'little', signed=True) == b'\x00\x00\x00\x80\xff\xff\xff\xff'
|
||||
assert (2147483647).to_bytes(8, byteorder='big', signed=False) == b'\x00\x00\x00\x00\x7f\xff\xff\xff'
|
||||
assert (-2147483648).to_bytes(8, byteorder='little', signed=True) == b'\x00\x00\x00\x80\xff\xff\xff\xff'
|
||||
assert (2147483647).to_bytes(length=8, byteorder='big', signed=False) == b'\x00\x00\x00\x00\x7f\xff\xff\xff'
|
||||
assert (-2147483648).to_bytes(length=8, byteorder='little', signed=True) == b'\x00\x00\x00\x80\xff\xff\xff\xff'
|
||||
assert (2147483647).to_bytes(
|
||||
8, "big", signed=False
|
||||
) == b"\x00\x00\x00\x00\x7f\xff\xff\xff"
|
||||
assert (-2147483648).to_bytes(
|
||||
8, "little", signed=True
|
||||
) == b"\x00\x00\x00\x80\xff\xff\xff\xff"
|
||||
assert (2147483647).to_bytes(
|
||||
8, byteorder="big", signed=False
|
||||
) == b"\x00\x00\x00\x00\x7f\xff\xff\xff"
|
||||
assert (-2147483648).to_bytes(
|
||||
8, byteorder="little", signed=True
|
||||
) == b"\x00\x00\x00\x80\xff\xff\xff\xff"
|
||||
assert (2147483647).to_bytes(
|
||||
length=8, byteorder="big", signed=False
|
||||
) == b"\x00\x00\x00\x00\x7f\xff\xff\xff"
|
||||
assert (-2147483648).to_bytes(
|
||||
length=8, byteorder="little", signed=True
|
||||
) == b"\x00\x00\x00\x80\xff\xff\xff\xff"
|
||||
|
||||
with assert_raises(ValueError):
|
||||
(1024).to_bytes(4, 'something')
|
||||
(1024).to_bytes(4, "something")
|
||||
|
||||
with assert_raises(OverflowError):
|
||||
(-1024).to_bytes(4, 'big')
|
||||
(-1024).to_bytes(4, "big")
|
||||
|
||||
with assert_raises(OverflowError):
|
||||
(1024).to_bytes(10000000000000000000000, 'big')
|
||||
(1024).to_bytes(10000000000000000000000, "big")
|
||||
|
||||
with assert_raises(OverflowError):
|
||||
(1024).to_bytes(1, 'big')
|
||||
(1024).to_bytes(1, "big")
|
||||
|
||||
with assert_raises(ValueError):
|
||||
# check base first
|
||||
int(' 1 ', base=1)
|
||||
int(" 1 ", base=1)
|
||||
|
||||
with assert_raises(ValueError):
|
||||
int(' 1 ', base=37)
|
||||
int(" 1 ", base=37)
|
||||
|
||||
with assert_raises(ValueError):
|
||||
int(' 1 ', base=-1)
|
||||
int(" 1 ", base=-1)
|
||||
|
||||
with assert_raises(ValueError):
|
||||
int(' 1 ', base=1000000000000000)
|
||||
int(" 1 ", base=1000000000000000)
|
||||
|
||||
with assert_raises(ValueError):
|
||||
int(' 1 ', base=-1000000000000000)
|
||||
int(" 1 ", base=-1000000000000000)
|
||||
|
||||
with assert_raises(TypeError):
|
||||
int(base=2)
|
||||
@@ -245,44 +257,56 @@ with assert_raises(TypeError):
|
||||
# check that first parameter is truly positional only
|
||||
int(val_options=1)
|
||||
|
||||
|
||||
class A(object):
|
||||
def __int__(self):
|
||||
return 10
|
||||
|
||||
|
||||
assert int(A()) == 10
|
||||
|
||||
|
||||
class B(object):
|
||||
pass
|
||||
|
||||
|
||||
b = B()
|
||||
b.__int__ = lambda: 20
|
||||
|
||||
with assert_raises(TypeError):
|
||||
assert int(b) == 20
|
||||
|
||||
|
||||
class C(object):
|
||||
def __int__(self):
|
||||
return 'str'
|
||||
return "str"
|
||||
|
||||
|
||||
with assert_raises(TypeError):
|
||||
int(C())
|
||||
|
||||
|
||||
class I(int):
|
||||
def __int__(self):
|
||||
return 3
|
||||
|
||||
|
||||
assert int(I(1)) == 3
|
||||
|
||||
|
||||
class F(float):
|
||||
def __int__(self):
|
||||
return 3
|
||||
|
||||
|
||||
assert int(F(1.2)) == 3
|
||||
|
||||
|
||||
class BadInt(int):
|
||||
def __int__(self):
|
||||
return 42.0
|
||||
|
||||
|
||||
with assert_raises(TypeError):
|
||||
int(BadInt())
|
||||
|
||||
@@ -301,33 +325,24 @@ assert_raises(TypeError, lambda: (1).__round__(0.0))
|
||||
|
||||
assert 00 == 0
|
||||
assert 0_0 == 0
|
||||
assert 03.2 == 3.2
|
||||
assert 3+02j == 3+2j
|
||||
|
||||
# Invalid syntax:
|
||||
src = """
|
||||
b = 02
|
||||
"""
|
||||
|
||||
with assert_raises(SyntaxError):
|
||||
exec(src)
|
||||
|
||||
# Invalid syntax:
|
||||
src = """
|
||||
b = 03 + 2j
|
||||
"""
|
||||
|
||||
with assert_raises(SyntaxError):
|
||||
exec(src)
|
||||
|
||||
# Small int cache in [-5..256]
|
||||
assert 1 is 1 # noqa
|
||||
x = 6
|
||||
assert 5 is (x-1) # noqa
|
||||
assert 5 is (x - 1) # noqa
|
||||
|
||||
|
||||
# test issue3687
|
||||
import math
|
||||
|
||||
big_int = 1000000
|
||||
small_int = 3
|
||||
assert big_int.real is big_int
|
||||
@@ -341,8 +356,12 @@ assert math.trunc(small_int) is small_int
|
||||
assert math.floor(small_int) is small_int
|
||||
assert math.ceil(small_int) is small_int
|
||||
|
||||
|
||||
# test subclassing int
|
||||
class SubInt(int): pass
|
||||
class SubInt(int):
|
||||
pass
|
||||
|
||||
|
||||
subint = int.__new__(SubInt, 11)
|
||||
assert subint.real is not subint
|
||||
assert type(subint.real) is int
|
||||
|
||||
Reference in New Issue
Block a user