mirror of https://github.com/python/cpython
Moved testing of builtin types out of test_builtin and into type specific modules
This commit is contained in:
parent
761b9c6a53
commit
979395b7a8
|
@ -53,45 +53,6 @@ class BitBucket:
|
||||||
def write(self, line):
|
def write(self, line):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
L = [
|
|
||||||
('0', 0),
|
|
||||||
('1', 1),
|
|
||||||
('9', 9),
|
|
||||||
('10', 10),
|
|
||||||
('99', 99),
|
|
||||||
('100', 100),
|
|
||||||
('314', 314),
|
|
||||||
(' 314', 314),
|
|
||||||
('314 ', 314),
|
|
||||||
(' \t\t 314 \t\t ', 314),
|
|
||||||
(repr(sys.maxint), sys.maxint),
|
|
||||||
(' 1x', ValueError),
|
|
||||||
(' 1 ', 1),
|
|
||||||
(' 1\02 ', ValueError),
|
|
||||||
('', ValueError),
|
|
||||||
(' ', ValueError),
|
|
||||||
(' \t\t ', ValueError)
|
|
||||||
]
|
|
||||||
if have_unicode:
|
|
||||||
L += [
|
|
||||||
(unicode('0'), 0),
|
|
||||||
(unicode('1'), 1),
|
|
||||||
(unicode('9'), 9),
|
|
||||||
(unicode('10'), 10),
|
|
||||||
(unicode('99'), 99),
|
|
||||||
(unicode('100'), 100),
|
|
||||||
(unicode('314'), 314),
|
|
||||||
(unicode(' 314'), 314),
|
|
||||||
(unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
|
|
||||||
(unicode(' \t\t 314 \t\t '), 314),
|
|
||||||
(unicode(' 1x'), ValueError),
|
|
||||||
(unicode(' 1 '), 1),
|
|
||||||
(unicode(' 1\02 '), ValueError),
|
|
||||||
(unicode(''), ValueError),
|
|
||||||
(unicode(' '), ValueError),
|
|
||||||
(unicode(' \t\t '), ValueError),
|
|
||||||
(unichr(0x200), ValueError),
|
|
||||||
]
|
|
||||||
|
|
||||||
class TestFailingBool:
|
class TestFailingBool:
|
||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
|
@ -612,115 +573,6 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertEqual(outp, exp)
|
self.assertEqual(outp, exp)
|
||||||
self.assert_(not isinstance(outp, cls))
|
self.assert_(not isinstance(outp, cls))
|
||||||
|
|
||||||
def test_float(self):
|
|
||||||
self.assertEqual(float(3.14), 3.14)
|
|
||||||
self.assertEqual(float(314), 314.0)
|
|
||||||
self.assertEqual(float(314L), 314.0)
|
|
||||||
self.assertEqual(float(" 3.14 "), 3.14)
|
|
||||||
self.assertRaises(ValueError, float, " 0x3.1 ")
|
|
||||||
self.assertRaises(ValueError, float, " -0x3.p-1 ")
|
|
||||||
self.assertRaises(ValueError, float, " +0x3.p-1 ")
|
|
||||||
self.assertRaises(ValueError, float, "++3.14")
|
|
||||||
self.assertRaises(ValueError, float, "+-3.14")
|
|
||||||
self.assertRaises(ValueError, float, "-+3.14")
|
|
||||||
self.assertRaises(ValueError, float, "--3.14")
|
|
||||||
if have_unicode:
|
|
||||||
self.assertEqual(float(unicode(" 3.14 ")), 3.14)
|
|
||||||
self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14)
|
|
||||||
# Implementation limitation in PyFloat_FromString()
|
|
||||||
self.assertRaises(ValueError, float, unicode("1"*10000))
|
|
||||||
|
|
||||||
@run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
|
|
||||||
def test_float_with_comma(self):
|
|
||||||
# set locale to something that doesn't use '.' for the decimal point
|
|
||||||
# float must not accept the locale specific decimal point but
|
|
||||||
# it still has to accept the normal python syntac
|
|
||||||
import locale
|
|
||||||
if not locale.localeconv()['decimal_point'] == ',':
|
|
||||||
return
|
|
||||||
|
|
||||||
self.assertEqual(float(" 3.14 "), 3.14)
|
|
||||||
self.assertEqual(float("+3.14 "), 3.14)
|
|
||||||
self.assertEqual(float("-3.14 "), -3.14)
|
|
||||||
self.assertEqual(float(".14 "), .14)
|
|
||||||
self.assertEqual(float("3. "), 3.0)
|
|
||||||
self.assertEqual(float("3.e3 "), 3000.0)
|
|
||||||
self.assertEqual(float("3.2e3 "), 3200.0)
|
|
||||||
self.assertEqual(float("2.5e-1 "), 0.25)
|
|
||||||
self.assertEqual(float("5e-1"), 0.5)
|
|
||||||
self.assertRaises(ValueError, float, " 3,14 ")
|
|
||||||
self.assertRaises(ValueError, float, " +3,14 ")
|
|
||||||
self.assertRaises(ValueError, float, " -3,14 ")
|
|
||||||
self.assertRaises(ValueError, float, " 0x3.1 ")
|
|
||||||
self.assertRaises(ValueError, float, " -0x3.p-1 ")
|
|
||||||
self.assertRaises(ValueError, float, " +0x3.p-1 ")
|
|
||||||
self.assertEqual(float(" 25.e-1 "), 2.5)
|
|
||||||
self.assertEqual(fcmp(float(" .25e-1 "), .025), 0)
|
|
||||||
|
|
||||||
def test_floatconversion(self):
|
|
||||||
# Make sure that calls to __float__() work properly
|
|
||||||
class Foo0:
|
|
||||||
def __float__(self):
|
|
||||||
return 42.
|
|
||||||
|
|
||||||
class Foo1(object):
|
|
||||||
def __float__(self):
|
|
||||||
return 42.
|
|
||||||
|
|
||||||
class Foo2(float):
|
|
||||||
def __float__(self):
|
|
||||||
return 42.
|
|
||||||
|
|
||||||
class Foo3(float):
|
|
||||||
def __new__(cls, value=0.):
|
|
||||||
return float.__new__(cls, 2*value)
|
|
||||||
|
|
||||||
def __float__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
class Foo4(float):
|
|
||||||
def __float__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
self.assertAlmostEqual(float(Foo0()), 42.)
|
|
||||||
self.assertAlmostEqual(float(Foo1()), 42.)
|
|
||||||
self.assertAlmostEqual(float(Foo2()), 42.)
|
|
||||||
self.assertAlmostEqual(float(Foo3(21)), 42.)
|
|
||||||
self.assertRaises(TypeError, float, Foo4(42))
|
|
||||||
|
|
||||||
def test_floatasratio(self):
|
|
||||||
for f, ratio in [
|
|
||||||
(0.875, (7, 8)),
|
|
||||||
(-0.875, (-7, 8)),
|
|
||||||
(0.0, (0, 1)),
|
|
||||||
(11.5, (23, 2)),
|
|
||||||
]:
|
|
||||||
self.assertEqual(f.as_integer_ratio(), ratio)
|
|
||||||
|
|
||||||
for i in range(10000):
|
|
||||||
f = random.random()
|
|
||||||
f *= 10 ** random.randint(-100, 100)
|
|
||||||
n, d = f.as_integer_ratio()
|
|
||||||
self.assertEqual(float(n).__truediv__(d), f)
|
|
||||||
|
|
||||||
R = fractions.Fraction
|
|
||||||
self.assertEqual(R(0, 1),
|
|
||||||
R(*float(0.0).as_integer_ratio()))
|
|
||||||
self.assertEqual(R(5, 2),
|
|
||||||
R(*float(2.5).as_integer_ratio()))
|
|
||||||
self.assertEqual(R(1, 2),
|
|
||||||
R(*float(0.5).as_integer_ratio()))
|
|
||||||
self.assertEqual(R(4728779608739021, 2251799813685248),
|
|
||||||
R(*float(2.1).as_integer_ratio()))
|
|
||||||
self.assertEqual(R(-4728779608739021, 2251799813685248),
|
|
||||||
R(*float(-2.1).as_integer_ratio()))
|
|
||||||
self.assertEqual(R(-2100, 1),
|
|
||||||
R(*float(-2100.0).as_integer_ratio()))
|
|
||||||
|
|
||||||
self.assertRaises(OverflowError, float('inf').as_integer_ratio)
|
|
||||||
self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
|
|
||||||
self.assertRaises(ValueError, float('nan').as_integer_ratio)
|
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
import sys
|
import sys
|
||||||
self.assert_(getattr(sys, 'stdout') is sys.stdout)
|
self.assert_(getattr(sys, 'stdout') is sys.stdout)
|
||||||
|
@ -782,285 +634,6 @@ class BuiltinTest(unittest.TestCase):
|
||||||
|
|
||||||
# Test input() later, together with raw_input
|
# Test input() later, together with raw_input
|
||||||
|
|
||||||
def test_int(self):
|
|
||||||
self.assertEqual(int(314), 314)
|
|
||||||
self.assertEqual(int(3.14), 3)
|
|
||||||
self.assertEqual(int(314L), 314)
|
|
||||||
# Check that conversion from float truncates towards zero
|
|
||||||
self.assertEqual(int(-3.14), -3)
|
|
||||||
self.assertEqual(int(3.9), 3)
|
|
||||||
self.assertEqual(int(-3.9), -3)
|
|
||||||
self.assertEqual(int(3.5), 3)
|
|
||||||
self.assertEqual(int(-3.5), -3)
|
|
||||||
# Different base:
|
|
||||||
self.assertEqual(int("10",16), 16L)
|
|
||||||
if have_unicode:
|
|
||||||
self.assertEqual(int(unicode("10"),16), 16L)
|
|
||||||
# Test conversion from strings and various anomalies
|
|
||||||
for s, v in L:
|
|
||||||
for sign in "", "+", "-":
|
|
||||||
for prefix in "", " ", "\t", " \t\t ":
|
|
||||||
ss = prefix + sign + s
|
|
||||||
vv = v
|
|
||||||
if sign == "-" and v is not ValueError:
|
|
||||||
vv = -v
|
|
||||||
try:
|
|
||||||
self.assertEqual(int(ss), vv)
|
|
||||||
except v:
|
|
||||||
pass
|
|
||||||
|
|
||||||
s = repr(-1-sys.maxint)
|
|
||||||
x = int(s)
|
|
||||||
self.assertEqual(x+1, -sys.maxint)
|
|
||||||
self.assert_(isinstance(x, int))
|
|
||||||
# should return long
|
|
||||||
self.assertEqual(int(s[1:]), sys.maxint+1)
|
|
||||||
|
|
||||||
# should return long
|
|
||||||
x = int(1e100)
|
|
||||||
self.assert_(isinstance(x, long))
|
|
||||||
x = int(-1e100)
|
|
||||||
self.assert_(isinstance(x, long))
|
|
||||||
|
|
||||||
|
|
||||||
# SF bug 434186: 0x80000000/2 != 0x80000000>>1.
|
|
||||||
# Worked by accident in Windows release build, but failed in debug build.
|
|
||||||
# Failed in all Linux builds.
|
|
||||||
x = -1-sys.maxint
|
|
||||||
self.assertEqual(x >> 1, x//2)
|
|
||||||
|
|
||||||
self.assertRaises(ValueError, int, '123\0')
|
|
||||||
self.assertRaises(ValueError, int, '53', 40)
|
|
||||||
|
|
||||||
# SF bug 1545497: embedded NULs were not detected with
|
|
||||||
# explicit base
|
|
||||||
self.assertRaises(ValueError, int, '123\0', 10)
|
|
||||||
self.assertRaises(ValueError, int, '123\x00 245', 20)
|
|
||||||
|
|
||||||
x = int('1' * 600)
|
|
||||||
self.assert_(isinstance(x, long))
|
|
||||||
|
|
||||||
if have_unicode:
|
|
||||||
x = int(unichr(0x661) * 600)
|
|
||||||
self.assert_(isinstance(x, long))
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, int, 1, 12)
|
|
||||||
|
|
||||||
self.assertEqual(int('0123', 0), 83)
|
|
||||||
self.assertEqual(int('0x123', 16), 291)
|
|
||||||
|
|
||||||
# Bug 1679: "0x" is not a valid hex literal
|
|
||||||
self.assertRaises(ValueError, int, "0x", 16)
|
|
||||||
self.assertRaises(ValueError, int, "0x", 0)
|
|
||||||
|
|
||||||
self.assertRaises(ValueError, int, "0o", 8)
|
|
||||||
self.assertRaises(ValueError, int, "0o", 0)
|
|
||||||
|
|
||||||
self.assertRaises(ValueError, int, "0b", 2)
|
|
||||||
self.assertRaises(ValueError, int, "0b", 0)
|
|
||||||
|
|
||||||
|
|
||||||
# SF bug 1334662: int(string, base) wrong answers
|
|
||||||
# Various representations of 2**32 evaluated to 0
|
|
||||||
# rather than 2**32 in previous versions
|
|
||||||
|
|
||||||
self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296L)
|
|
||||||
self.assertEqual(int('102002022201221111211', 3), 4294967296L)
|
|
||||||
self.assertEqual(int('10000000000000000', 4), 4294967296L)
|
|
||||||
self.assertEqual(int('32244002423141', 5), 4294967296L)
|
|
||||||
self.assertEqual(int('1550104015504', 6), 4294967296L)
|
|
||||||
self.assertEqual(int('211301422354', 7), 4294967296L)
|
|
||||||
self.assertEqual(int('40000000000', 8), 4294967296L)
|
|
||||||
self.assertEqual(int('12068657454', 9), 4294967296L)
|
|
||||||
self.assertEqual(int('4294967296', 10), 4294967296L)
|
|
||||||
self.assertEqual(int('1904440554', 11), 4294967296L)
|
|
||||||
self.assertEqual(int('9ba461594', 12), 4294967296L)
|
|
||||||
self.assertEqual(int('535a79889', 13), 4294967296L)
|
|
||||||
self.assertEqual(int('2ca5b7464', 14), 4294967296L)
|
|
||||||
self.assertEqual(int('1a20dcd81', 15), 4294967296L)
|
|
||||||
self.assertEqual(int('100000000', 16), 4294967296L)
|
|
||||||
self.assertEqual(int('a7ffda91', 17), 4294967296L)
|
|
||||||
self.assertEqual(int('704he7g4', 18), 4294967296L)
|
|
||||||
self.assertEqual(int('4f5aff66', 19), 4294967296L)
|
|
||||||
self.assertEqual(int('3723ai4g', 20), 4294967296L)
|
|
||||||
self.assertEqual(int('281d55i4', 21), 4294967296L)
|
|
||||||
self.assertEqual(int('1fj8b184', 22), 4294967296L)
|
|
||||||
self.assertEqual(int('1606k7ic', 23), 4294967296L)
|
|
||||||
self.assertEqual(int('mb994ag', 24), 4294967296L)
|
|
||||||
self.assertEqual(int('hek2mgl', 25), 4294967296L)
|
|
||||||
self.assertEqual(int('dnchbnm', 26), 4294967296L)
|
|
||||||
self.assertEqual(int('b28jpdm', 27), 4294967296L)
|
|
||||||
self.assertEqual(int('8pfgih4', 28), 4294967296L)
|
|
||||||
self.assertEqual(int('76beigg', 29), 4294967296L)
|
|
||||||
self.assertEqual(int('5qmcpqg', 30), 4294967296L)
|
|
||||||
self.assertEqual(int('4q0jto4', 31), 4294967296L)
|
|
||||||
self.assertEqual(int('4000000', 32), 4294967296L)
|
|
||||||
self.assertEqual(int('3aokq94', 33), 4294967296L)
|
|
||||||
self.assertEqual(int('2qhxjli', 34), 4294967296L)
|
|
||||||
self.assertEqual(int('2br45qb', 35), 4294967296L)
|
|
||||||
self.assertEqual(int('1z141z4', 36), 4294967296L)
|
|
||||||
|
|
||||||
# tests with base 0
|
|
||||||
# this fails on 3.0, but in 2.x the old octal syntax is allowed
|
|
||||||
self.assertEqual(int(' 0123 ', 0), 83)
|
|
||||||
self.assertEqual(int(' 0123 ', 0), 83)
|
|
||||||
self.assertEqual(int('000', 0), 0)
|
|
||||||
self.assertEqual(int('0o123', 0), 83)
|
|
||||||
self.assertEqual(int('0x123', 0), 291)
|
|
||||||
self.assertEqual(int('0b100', 0), 4)
|
|
||||||
self.assertEqual(int(' 0O123 ', 0), 83)
|
|
||||||
self.assertEqual(int(' 0X123 ', 0), 291)
|
|
||||||
self.assertEqual(int(' 0B100 ', 0), 4)
|
|
||||||
|
|
||||||
# without base still base 10
|
|
||||||
self.assertEqual(int('0123'), 123)
|
|
||||||
self.assertEqual(int('0123', 10), 123)
|
|
||||||
|
|
||||||
# tests with prefix and base != 0
|
|
||||||
self.assertEqual(int('0x123', 16), 291)
|
|
||||||
self.assertEqual(int('0o123', 8), 83)
|
|
||||||
self.assertEqual(int('0b100', 2), 4)
|
|
||||||
self.assertEqual(int('0X123', 16), 291)
|
|
||||||
self.assertEqual(int('0O123', 8), 83)
|
|
||||||
self.assertEqual(int('0B100', 2), 4)
|
|
||||||
|
|
||||||
# the code has special checks for the first character after the
|
|
||||||
# type prefix
|
|
||||||
self.assertRaises(ValueError, int, '0b2', 2)
|
|
||||||
self.assertRaises(ValueError, int, '0b02', 2)
|
|
||||||
self.assertRaises(ValueError, int, '0B2', 2)
|
|
||||||
self.assertRaises(ValueError, int, '0B02', 2)
|
|
||||||
self.assertRaises(ValueError, int, '0o8', 8)
|
|
||||||
self.assertRaises(ValueError, int, '0o08', 8)
|
|
||||||
self.assertRaises(ValueError, int, '0O8', 8)
|
|
||||||
self.assertRaises(ValueError, int, '0O08', 8)
|
|
||||||
self.assertRaises(ValueError, int, '0xg', 16)
|
|
||||||
self.assertRaises(ValueError, int, '0x0g', 16)
|
|
||||||
self.assertRaises(ValueError, int, '0Xg', 16)
|
|
||||||
self.assertRaises(ValueError, int, '0X0g', 16)
|
|
||||||
|
|
||||||
# SF bug 1334662: int(string, base) wrong answers
|
|
||||||
# Checks for proper evaluation of 2**32 + 1
|
|
||||||
self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L)
|
|
||||||
self.assertEqual(int('102002022201221111212', 3), 4294967297L)
|
|
||||||
self.assertEqual(int('10000000000000001', 4), 4294967297L)
|
|
||||||
self.assertEqual(int('32244002423142', 5), 4294967297L)
|
|
||||||
self.assertEqual(int('1550104015505', 6), 4294967297L)
|
|
||||||
self.assertEqual(int('211301422355', 7), 4294967297L)
|
|
||||||
self.assertEqual(int('40000000001', 8), 4294967297L)
|
|
||||||
self.assertEqual(int('12068657455', 9), 4294967297L)
|
|
||||||
self.assertEqual(int('4294967297', 10), 4294967297L)
|
|
||||||
self.assertEqual(int('1904440555', 11), 4294967297L)
|
|
||||||
self.assertEqual(int('9ba461595', 12), 4294967297L)
|
|
||||||
self.assertEqual(int('535a7988a', 13), 4294967297L)
|
|
||||||
self.assertEqual(int('2ca5b7465', 14), 4294967297L)
|
|
||||||
self.assertEqual(int('1a20dcd82', 15), 4294967297L)
|
|
||||||
self.assertEqual(int('100000001', 16), 4294967297L)
|
|
||||||
self.assertEqual(int('a7ffda92', 17), 4294967297L)
|
|
||||||
self.assertEqual(int('704he7g5', 18), 4294967297L)
|
|
||||||
self.assertEqual(int('4f5aff67', 19), 4294967297L)
|
|
||||||
self.assertEqual(int('3723ai4h', 20), 4294967297L)
|
|
||||||
self.assertEqual(int('281d55i5', 21), 4294967297L)
|
|
||||||
self.assertEqual(int('1fj8b185', 22), 4294967297L)
|
|
||||||
self.assertEqual(int('1606k7id', 23), 4294967297L)
|
|
||||||
self.assertEqual(int('mb994ah', 24), 4294967297L)
|
|
||||||
self.assertEqual(int('hek2mgm', 25), 4294967297L)
|
|
||||||
self.assertEqual(int('dnchbnn', 26), 4294967297L)
|
|
||||||
self.assertEqual(int('b28jpdn', 27), 4294967297L)
|
|
||||||
self.assertEqual(int('8pfgih5', 28), 4294967297L)
|
|
||||||
self.assertEqual(int('76beigh', 29), 4294967297L)
|
|
||||||
self.assertEqual(int('5qmcpqh', 30), 4294967297L)
|
|
||||||
self.assertEqual(int('4q0jto5', 31), 4294967297L)
|
|
||||||
self.assertEqual(int('4000001', 32), 4294967297L)
|
|
||||||
self.assertEqual(int('3aokq95', 33), 4294967297L)
|
|
||||||
self.assertEqual(int('2qhxjlj', 34), 4294967297L)
|
|
||||||
self.assertEqual(int('2br45qc', 35), 4294967297L)
|
|
||||||
self.assertEqual(int('1z141z5', 36), 4294967297L)
|
|
||||||
|
|
||||||
def test_intconversion(self):
|
|
||||||
# Test __int__()
|
|
||||||
class ClassicMissingMethods:
|
|
||||||
pass
|
|
||||||
self.assertRaises(AttributeError, int, ClassicMissingMethods())
|
|
||||||
|
|
||||||
class MissingMethods(object):
|
|
||||||
pass
|
|
||||||
self.assertRaises(TypeError, int, MissingMethods())
|
|
||||||
|
|
||||||
class Foo0:
|
|
||||||
def __int__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
class Foo1(object):
|
|
||||||
def __int__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
class Foo2(int):
|
|
||||||
def __int__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
class Foo3(int):
|
|
||||||
def __int__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
class Foo4(int):
|
|
||||||
def __int__(self):
|
|
||||||
return 42L
|
|
||||||
|
|
||||||
class Foo5(int):
|
|
||||||
def __int__(self):
|
|
||||||
return 42.
|
|
||||||
|
|
||||||
self.assertEqual(int(Foo0()), 42)
|
|
||||||
self.assertEqual(int(Foo1()), 42)
|
|
||||||
self.assertEqual(int(Foo2()), 42)
|
|
||||||
self.assertEqual(int(Foo3()), 0)
|
|
||||||
self.assertEqual(int(Foo4()), 42L)
|
|
||||||
self.assertRaises(TypeError, int, Foo5())
|
|
||||||
|
|
||||||
class Classic:
|
|
||||||
pass
|
|
||||||
for base in (object, Classic):
|
|
||||||
class IntOverridesTrunc(base):
|
|
||||||
def __int__(self):
|
|
||||||
return 42
|
|
||||||
def __trunc__(self):
|
|
||||||
return -12
|
|
||||||
self.assertEqual(int(IntOverridesTrunc()), 42)
|
|
||||||
|
|
||||||
class JustTrunc(base):
|
|
||||||
def __trunc__(self):
|
|
||||||
return 42
|
|
||||||
self.assertEqual(int(JustTrunc()), 42)
|
|
||||||
|
|
||||||
for trunc_result_base in (object, Classic):
|
|
||||||
class Integral(trunc_result_base):
|
|
||||||
def __int__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
class TruncReturnsNonInt(base):
|
|
||||||
def __trunc__(self):
|
|
||||||
return Integral()
|
|
||||||
self.assertEqual(int(TruncReturnsNonInt()), 42)
|
|
||||||
|
|
||||||
class NonIntegral(trunc_result_base):
|
|
||||||
def __trunc__(self):
|
|
||||||
# Check that we avoid infinite recursion.
|
|
||||||
return NonIntegral()
|
|
||||||
|
|
||||||
class TruncReturnsNonIntegral(base):
|
|
||||||
def __trunc__(self):
|
|
||||||
return NonIntegral()
|
|
||||||
try:
|
|
||||||
int(TruncReturnsNonIntegral())
|
|
||||||
except TypeError as e:
|
|
||||||
self.assertEquals(str(e),
|
|
||||||
"__trunc__ returned non-Integral"
|
|
||||||
" (type NonIntegral)")
|
|
||||||
else:
|
|
||||||
self.fail("Failed to raise TypeError with %s" %
|
|
||||||
((base, trunc_result_base),))
|
|
||||||
|
|
||||||
def test_intern(self):
|
def test_intern(self):
|
||||||
self.assertRaises(TypeError, intern)
|
self.assertRaises(TypeError, intern)
|
||||||
s = "never interned before"
|
s = "never interned before"
|
||||||
|
@ -1144,247 +717,6 @@ class BuiltinTest(unittest.TestCase):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
self.assertRaises(ValueError, len, BadSeq())
|
self.assertRaises(ValueError, len, BadSeq())
|
||||||
|
|
||||||
def test_list(self):
|
|
||||||
self.assertEqual(list([]), [])
|
|
||||||
l0_3 = [0, 1, 2, 3]
|
|
||||||
l0_3_bis = list(l0_3)
|
|
||||||
self.assertEqual(l0_3, l0_3_bis)
|
|
||||||
self.assert_(l0_3 is not l0_3_bis)
|
|
||||||
self.assertEqual(list(()), [])
|
|
||||||
self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
|
|
||||||
self.assertEqual(list(''), [])
|
|
||||||
self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
|
|
||||||
|
|
||||||
if sys.maxint == 0x7fffffff:
|
|
||||||
# This test can currently only work on 32-bit machines.
|
|
||||||
# XXX If/when PySequence_Length() returns a ssize_t, it should be
|
|
||||||
# XXX re-enabled.
|
|
||||||
# Verify clearing of bug #556025.
|
|
||||||
# This assumes that the max data size (sys.maxint) == max
|
|
||||||
# address size this also assumes that the address size is at
|
|
||||||
# least 4 bytes with 8 byte addresses, the bug is not well
|
|
||||||
# tested
|
|
||||||
#
|
|
||||||
# Note: This test is expected to SEGV under Cygwin 1.3.12 or
|
|
||||||
# earlier due to a newlib bug. See the following mailing list
|
|
||||||
# thread for the details:
|
|
||||||
|
|
||||||
# http://sources.redhat.com/ml/newlib/2002/msg00369.html
|
|
||||||
self.assertRaises(MemoryError, list, xrange(sys.maxint // 2))
|
|
||||||
|
|
||||||
# This code used to segfault in Py2.4a3
|
|
||||||
x = []
|
|
||||||
x.extend(-y for y in x)
|
|
||||||
self.assertEqual(x, [])
|
|
||||||
|
|
||||||
def test_long(self):
|
|
||||||
self.assertEqual(long(314), 314L)
|
|
||||||
self.assertEqual(long(3.14), 3L)
|
|
||||||
self.assertEqual(long(314L), 314L)
|
|
||||||
# Check that conversion from float truncates towards zero
|
|
||||||
self.assertEqual(long(-3.14), -3L)
|
|
||||||
self.assertEqual(long(3.9), 3L)
|
|
||||||
self.assertEqual(long(-3.9), -3L)
|
|
||||||
self.assertEqual(long(3.5), 3L)
|
|
||||||
self.assertEqual(long(-3.5), -3L)
|
|
||||||
self.assertEqual(long("-3"), -3L)
|
|
||||||
if have_unicode:
|
|
||||||
self.assertEqual(long(unicode("-3")), -3L)
|
|
||||||
# Different base:
|
|
||||||
self.assertEqual(long("10",16), 16L)
|
|
||||||
if have_unicode:
|
|
||||||
self.assertEqual(long(unicode("10"),16), 16L)
|
|
||||||
# Check conversions from string (same test set as for int(), and then some)
|
|
||||||
LL = [
|
|
||||||
('1' + '0'*20, 10L**20),
|
|
||||||
('1' + '0'*100, 10L**100)
|
|
||||||
]
|
|
||||||
L2 = L[:]
|
|
||||||
if have_unicode:
|
|
||||||
L2 += [
|
|
||||||
(unicode('1') + unicode('0')*20, 10L**20),
|
|
||||||
(unicode('1') + unicode('0')*100, 10L**100),
|
|
||||||
]
|
|
||||||
for s, v in L2 + LL:
|
|
||||||
for sign in "", "+", "-":
|
|
||||||
for prefix in "", " ", "\t", " \t\t ":
|
|
||||||
ss = prefix + sign + s
|
|
||||||
vv = v
|
|
||||||
if sign == "-" and v is not ValueError:
|
|
||||||
vv = -v
|
|
||||||
try:
|
|
||||||
self.assertEqual(long(ss), long(vv))
|
|
||||||
except v:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.assertRaises(ValueError, long, '123\0')
|
|
||||||
self.assertRaises(ValueError, long, '53', 40)
|
|
||||||
self.assertRaises(TypeError, long, 1, 12)
|
|
||||||
|
|
||||||
# SF patch #1638879: embedded NULs were not detected with
|
|
||||||
# explicit base
|
|
||||||
self.assertRaises(ValueError, long, '123\0', 10)
|
|
||||||
self.assertRaises(ValueError, long, '123\x00 245', 20)
|
|
||||||
|
|
||||||
self.assertEqual(long('100000000000000000000000000000000', 2),
|
|
||||||
4294967296)
|
|
||||||
self.assertEqual(long('102002022201221111211', 3), 4294967296)
|
|
||||||
self.assertEqual(long('10000000000000000', 4), 4294967296)
|
|
||||||
self.assertEqual(long('32244002423141', 5), 4294967296)
|
|
||||||
self.assertEqual(long('1550104015504', 6), 4294967296)
|
|
||||||
self.assertEqual(long('211301422354', 7), 4294967296)
|
|
||||||
self.assertEqual(long('40000000000', 8), 4294967296)
|
|
||||||
self.assertEqual(long('12068657454', 9), 4294967296)
|
|
||||||
self.assertEqual(long('4294967296', 10), 4294967296)
|
|
||||||
self.assertEqual(long('1904440554', 11), 4294967296)
|
|
||||||
self.assertEqual(long('9ba461594', 12), 4294967296)
|
|
||||||
self.assertEqual(long('535a79889', 13), 4294967296)
|
|
||||||
self.assertEqual(long('2ca5b7464', 14), 4294967296)
|
|
||||||
self.assertEqual(long('1a20dcd81', 15), 4294967296)
|
|
||||||
self.assertEqual(long('100000000', 16), 4294967296)
|
|
||||||
self.assertEqual(long('a7ffda91', 17), 4294967296)
|
|
||||||
self.assertEqual(long('704he7g4', 18), 4294967296)
|
|
||||||
self.assertEqual(long('4f5aff66', 19), 4294967296)
|
|
||||||
self.assertEqual(long('3723ai4g', 20), 4294967296)
|
|
||||||
self.assertEqual(long('281d55i4', 21), 4294967296)
|
|
||||||
self.assertEqual(long('1fj8b184', 22), 4294967296)
|
|
||||||
self.assertEqual(long('1606k7ic', 23), 4294967296)
|
|
||||||
self.assertEqual(long('mb994ag', 24), 4294967296)
|
|
||||||
self.assertEqual(long('hek2mgl', 25), 4294967296)
|
|
||||||
self.assertEqual(long('dnchbnm', 26), 4294967296)
|
|
||||||
self.assertEqual(long('b28jpdm', 27), 4294967296)
|
|
||||||
self.assertEqual(long('8pfgih4', 28), 4294967296)
|
|
||||||
self.assertEqual(long('76beigg', 29), 4294967296)
|
|
||||||
self.assertEqual(long('5qmcpqg', 30), 4294967296)
|
|
||||||
self.assertEqual(long('4q0jto4', 31), 4294967296)
|
|
||||||
self.assertEqual(long('4000000', 32), 4294967296)
|
|
||||||
self.assertEqual(long('3aokq94', 33), 4294967296)
|
|
||||||
self.assertEqual(long('2qhxjli', 34), 4294967296)
|
|
||||||
self.assertEqual(long('2br45qb', 35), 4294967296)
|
|
||||||
self.assertEqual(long('1z141z4', 36), 4294967296)
|
|
||||||
|
|
||||||
self.assertEqual(long('100000000000000000000000000000001', 2),
|
|
||||||
4294967297)
|
|
||||||
self.assertEqual(long('102002022201221111212', 3), 4294967297)
|
|
||||||
self.assertEqual(long('10000000000000001', 4), 4294967297)
|
|
||||||
self.assertEqual(long('32244002423142', 5), 4294967297)
|
|
||||||
self.assertEqual(long('1550104015505', 6), 4294967297)
|
|
||||||
self.assertEqual(long('211301422355', 7), 4294967297)
|
|
||||||
self.assertEqual(long('40000000001', 8), 4294967297)
|
|
||||||
self.assertEqual(long('12068657455', 9), 4294967297)
|
|
||||||
self.assertEqual(long('4294967297', 10), 4294967297)
|
|
||||||
self.assertEqual(long('1904440555', 11), 4294967297)
|
|
||||||
self.assertEqual(long('9ba461595', 12), 4294967297)
|
|
||||||
self.assertEqual(long('535a7988a', 13), 4294967297)
|
|
||||||
self.assertEqual(long('2ca5b7465', 14), 4294967297)
|
|
||||||
self.assertEqual(long('1a20dcd82', 15), 4294967297)
|
|
||||||
self.assertEqual(long('100000001', 16), 4294967297)
|
|
||||||
self.assertEqual(long('a7ffda92', 17), 4294967297)
|
|
||||||
self.assertEqual(long('704he7g5', 18), 4294967297)
|
|
||||||
self.assertEqual(long('4f5aff67', 19), 4294967297)
|
|
||||||
self.assertEqual(long('3723ai4h', 20), 4294967297)
|
|
||||||
self.assertEqual(long('281d55i5', 21), 4294967297)
|
|
||||||
self.assertEqual(long('1fj8b185', 22), 4294967297)
|
|
||||||
self.assertEqual(long('1606k7id', 23), 4294967297)
|
|
||||||
self.assertEqual(long('mb994ah', 24), 4294967297)
|
|
||||||
self.assertEqual(long('hek2mgm', 25), 4294967297)
|
|
||||||
self.assertEqual(long('dnchbnn', 26), 4294967297)
|
|
||||||
self.assertEqual(long('b28jpdn', 27), 4294967297)
|
|
||||||
self.assertEqual(long('8pfgih5', 28), 4294967297)
|
|
||||||
self.assertEqual(long('76beigh', 29), 4294967297)
|
|
||||||
self.assertEqual(long('5qmcpqh', 30), 4294967297)
|
|
||||||
self.assertEqual(long('4q0jto5', 31), 4294967297)
|
|
||||||
self.assertEqual(long('4000001', 32), 4294967297)
|
|
||||||
self.assertEqual(long('3aokq95', 33), 4294967297)
|
|
||||||
self.assertEqual(long('2qhxjlj', 34), 4294967297)
|
|
||||||
self.assertEqual(long('2br45qc', 35), 4294967297)
|
|
||||||
self.assertEqual(long('1z141z5', 36), 4294967297)
|
|
||||||
|
|
||||||
|
|
||||||
def test_longconversion(self):
|
|
||||||
# Test __long__()
|
|
||||||
class ClassicMissingMethods:
|
|
||||||
pass
|
|
||||||
self.assertRaises(AttributeError, long, ClassicMissingMethods())
|
|
||||||
|
|
||||||
class MissingMethods(object):
|
|
||||||
pass
|
|
||||||
self.assertRaises(TypeError, long, MissingMethods())
|
|
||||||
|
|
||||||
class Foo0:
|
|
||||||
def __long__(self):
|
|
||||||
return 42L
|
|
||||||
|
|
||||||
class Foo1(object):
|
|
||||||
def __long__(self):
|
|
||||||
return 42L
|
|
||||||
|
|
||||||
class Foo2(long):
|
|
||||||
def __long__(self):
|
|
||||||
return 42L
|
|
||||||
|
|
||||||
class Foo3(long):
|
|
||||||
def __long__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
class Foo4(long):
|
|
||||||
def __long__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
class Foo5(long):
|
|
||||||
def __long__(self):
|
|
||||||
return 42.
|
|
||||||
|
|
||||||
self.assertEqual(long(Foo0()), 42L)
|
|
||||||
self.assertEqual(long(Foo1()), 42L)
|
|
||||||
self.assertEqual(long(Foo2()), 42L)
|
|
||||||
self.assertEqual(long(Foo3()), 0)
|
|
||||||
self.assertEqual(long(Foo4()), 42)
|
|
||||||
self.assertRaises(TypeError, long, Foo5())
|
|
||||||
|
|
||||||
class Classic:
|
|
||||||
pass
|
|
||||||
for base in (object, Classic):
|
|
||||||
class LongOverridesTrunc(base):
|
|
||||||
def __long__(self):
|
|
||||||
return 42
|
|
||||||
def __trunc__(self):
|
|
||||||
return -12
|
|
||||||
self.assertEqual(long(LongOverridesTrunc()), 42)
|
|
||||||
|
|
||||||
class JustTrunc(base):
|
|
||||||
def __trunc__(self):
|
|
||||||
return 42
|
|
||||||
self.assertEqual(long(JustTrunc()), 42)
|
|
||||||
|
|
||||||
for trunc_result_base in (object, Classic):
|
|
||||||
class Integral(trunc_result_base):
|
|
||||||
def __int__(self):
|
|
||||||
return 42
|
|
||||||
|
|
||||||
class TruncReturnsNonLong(base):
|
|
||||||
def __trunc__(self):
|
|
||||||
return Integral()
|
|
||||||
self.assertEqual(long(TruncReturnsNonLong()), 42)
|
|
||||||
|
|
||||||
class NonIntegral(trunc_result_base):
|
|
||||||
def __trunc__(self):
|
|
||||||
# Check that we avoid infinite recursion.
|
|
||||||
return NonIntegral()
|
|
||||||
|
|
||||||
class TruncReturnsNonIntegral(base):
|
|
||||||
def __trunc__(self):
|
|
||||||
return NonIntegral()
|
|
||||||
try:
|
|
||||||
long(TruncReturnsNonIntegral())
|
|
||||||
except TypeError as e:
|
|
||||||
self.assertEquals(str(e),
|
|
||||||
"__trunc__ returned non-Integral"
|
|
||||||
" (type NonIntegral)")
|
|
||||||
else:
|
|
||||||
self.fail("Failed to raise TypeError with %s" %
|
|
||||||
((base, trunc_result_base),))
|
|
||||||
|
|
||||||
def test_map(self):
|
def test_map(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
map(None, 'hello world'),
|
map(None, 'hello world'),
|
||||||
|
@ -1919,20 +1251,6 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, setattr, sys, 1, 'spam')
|
self.assertRaises(TypeError, setattr, sys, 1, 'spam')
|
||||||
self.assertRaises(TypeError, setattr)
|
self.assertRaises(TypeError, setattr)
|
||||||
|
|
||||||
def test_str(self):
|
|
||||||
self.assertEqual(str(''), '')
|
|
||||||
self.assertEqual(str(0), '0')
|
|
||||||
self.assertEqual(str(0L), '0')
|
|
||||||
self.assertEqual(str(()), '()')
|
|
||||||
self.assertEqual(str([]), '[]')
|
|
||||||
self.assertEqual(str({}), '{}')
|
|
||||||
a = []
|
|
||||||
a.append(a)
|
|
||||||
self.assertEqual(str(a), '[[...]]')
|
|
||||||
a = {}
|
|
||||||
a[0] = a
|
|
||||||
self.assertEqual(str(a), '{0: {...}}')
|
|
||||||
|
|
||||||
def test_sum(self):
|
def test_sum(self):
|
||||||
self.assertEqual(sum([]), 0)
|
self.assertEqual(sum([]), 0)
|
||||||
self.assertEqual(sum(range(2,8)), 27)
|
self.assertEqual(sum(range(2,8)), 27)
|
||||||
|
@ -1954,16 +1272,6 @@ class BuiltinTest(unittest.TestCase):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
self.assertRaises(ValueError, sum, BadSeq())
|
self.assertRaises(ValueError, sum, BadSeq())
|
||||||
|
|
||||||
def test_tuple(self):
|
|
||||||
self.assertEqual(tuple(()), ())
|
|
||||||
t0_3 = (0, 1, 2, 3)
|
|
||||||
t0_3_bis = tuple(t0_3)
|
|
||||||
self.assert_(t0_3 is t0_3_bis)
|
|
||||||
self.assertEqual(tuple([]), ())
|
|
||||||
self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
|
|
||||||
self.assertEqual(tuple(''), ())
|
|
||||||
self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
|
|
||||||
|
|
||||||
def test_type(self):
|
def test_type(self):
|
||||||
self.assertEqual(type(''), type('123'))
|
self.assertEqual(type(''), type('123'))
|
||||||
self.assertNotEqual(type(''), type(()))
|
self.assertNotEqual(type(''), type(()))
|
||||||
|
|
|
@ -9,6 +9,117 @@ import operator
|
||||||
INF = float("inf")
|
INF = float("inf")
|
||||||
NAN = float("nan")
|
NAN = float("nan")
|
||||||
|
|
||||||
|
class GeneralFloatCases(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_float(self):
|
||||||
|
self.assertEqual(float(3.14), 3.14)
|
||||||
|
self.assertEqual(float(314), 314.0)
|
||||||
|
self.assertEqual(float(314L), 314.0)
|
||||||
|
self.assertEqual(float(" 3.14 "), 3.14)
|
||||||
|
self.assertRaises(ValueError, float, " 0x3.1 ")
|
||||||
|
self.assertRaises(ValueError, float, " -0x3.p-1 ")
|
||||||
|
self.assertRaises(ValueError, float, " +0x3.p-1 ")
|
||||||
|
self.assertRaises(ValueError, float, "++3.14")
|
||||||
|
self.assertRaises(ValueError, float, "+-3.14")
|
||||||
|
self.assertRaises(ValueError, float, "-+3.14")
|
||||||
|
self.assertRaises(ValueError, float, "--3.14")
|
||||||
|
if have_unicode:
|
||||||
|
self.assertEqual(float(unicode(" 3.14 ")), 3.14)
|
||||||
|
self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14)
|
||||||
|
# Implementation limitation in PyFloat_FromString()
|
||||||
|
self.assertRaises(ValueError, float, unicode("1"*10000))
|
||||||
|
|
||||||
|
@test_support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
|
||||||
|
def test_float_with_comma(self):
|
||||||
|
# set locale to something that doesn't use '.' for the decimal point
|
||||||
|
# float must not accept the locale specific decimal point but
|
||||||
|
# it still has to accept the normal python syntac
|
||||||
|
import locale
|
||||||
|
if not locale.localeconv()['decimal_point'] == ',':
|
||||||
|
return
|
||||||
|
|
||||||
|
self.assertEqual(float(" 3.14 "), 3.14)
|
||||||
|
self.assertEqual(float("+3.14 "), 3.14)
|
||||||
|
self.assertEqual(float("-3.14 "), -3.14)
|
||||||
|
self.assertEqual(float(".14 "), .14)
|
||||||
|
self.assertEqual(float("3. "), 3.0)
|
||||||
|
self.assertEqual(float("3.e3 "), 3000.0)
|
||||||
|
self.assertEqual(float("3.2e3 "), 3200.0)
|
||||||
|
self.assertEqual(float("2.5e-1 "), 0.25)
|
||||||
|
self.assertEqual(float("5e-1"), 0.5)
|
||||||
|
self.assertRaises(ValueError, float, " 3,14 ")
|
||||||
|
self.assertRaises(ValueError, float, " +3,14 ")
|
||||||
|
self.assertRaises(ValueError, float, " -3,14 ")
|
||||||
|
self.assertRaises(ValueError, float, " 0x3.1 ")
|
||||||
|
self.assertRaises(ValueError, float, " -0x3.p-1 ")
|
||||||
|
self.assertRaises(ValueError, float, " +0x3.p-1 ")
|
||||||
|
self.assertEqual(float(" 25.e-1 "), 2.5)
|
||||||
|
self.assertEqual(fcmp(float(" .25e-1 "), .025), 0)
|
||||||
|
|
||||||
|
def test_floatconversion(self):
|
||||||
|
# Make sure that calls to __float__() work properly
|
||||||
|
class Foo0:
|
||||||
|
def __float__(self):
|
||||||
|
return 42.
|
||||||
|
|
||||||
|
class Foo1(object):
|
||||||
|
def __float__(self):
|
||||||
|
return 42.
|
||||||
|
|
||||||
|
class Foo2(float):
|
||||||
|
def __float__(self):
|
||||||
|
return 42.
|
||||||
|
|
||||||
|
class Foo3(float):
|
||||||
|
def __new__(cls, value=0.):
|
||||||
|
return float.__new__(cls, 2*value)
|
||||||
|
|
||||||
|
def __float__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
class Foo4(float):
|
||||||
|
def __float__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
self.assertAlmostEqual(float(Foo0()), 42.)
|
||||||
|
self.assertAlmostEqual(float(Foo1()), 42.)
|
||||||
|
self.assertAlmostEqual(float(Foo2()), 42.)
|
||||||
|
self.assertAlmostEqual(float(Foo3(21)), 42.)
|
||||||
|
self.assertRaises(TypeError, float, Foo4(42))
|
||||||
|
|
||||||
|
def test_floatasratio(self):
|
||||||
|
for f, ratio in [
|
||||||
|
(0.875, (7, 8)),
|
||||||
|
(-0.875, (-7, 8)),
|
||||||
|
(0.0, (0, 1)),
|
||||||
|
(11.5, (23, 2)),
|
||||||
|
]:
|
||||||
|
self.assertEqual(f.as_integer_ratio(), ratio)
|
||||||
|
|
||||||
|
for i in range(10000):
|
||||||
|
f = random.random()
|
||||||
|
f *= 10 ** random.randint(-100, 100)
|
||||||
|
n, d = f.as_integer_ratio()
|
||||||
|
self.assertEqual(float(n).__truediv__(d), f)
|
||||||
|
|
||||||
|
R = fractions.Fraction
|
||||||
|
self.assertEqual(R(0, 1),
|
||||||
|
R(*float(0.0).as_integer_ratio()))
|
||||||
|
self.assertEqual(R(5, 2),
|
||||||
|
R(*float(2.5).as_integer_ratio()))
|
||||||
|
self.assertEqual(R(1, 2),
|
||||||
|
R(*float(0.5).as_integer_ratio()))
|
||||||
|
self.assertEqual(R(4728779608739021, 2251799813685248),
|
||||||
|
R(*float(2.1).as_integer_ratio()))
|
||||||
|
self.assertEqual(R(-4728779608739021, 2251799813685248),
|
||||||
|
R(*float(-2.1).as_integer_ratio()))
|
||||||
|
self.assertEqual(R(-2100, 1),
|
||||||
|
R(*float(-2100.0).as_integer_ratio()))
|
||||||
|
|
||||||
|
self.assertRaises(OverflowError, float('inf').as_integer_ratio)
|
||||||
|
self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
|
||||||
|
self.assertRaises(ValueError, float('nan').as_integer_ratio)
|
||||||
|
|
||||||
class FormatFunctionsTestCase(unittest.TestCase):
|
class FormatFunctionsTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -0,0 +1,331 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from test.test_support import run_unittest, have_unicode
|
||||||
|
|
||||||
|
L = [
|
||||||
|
('0', 0),
|
||||||
|
('1', 1),
|
||||||
|
('9', 9),
|
||||||
|
('10', 10),
|
||||||
|
('99', 99),
|
||||||
|
('100', 100),
|
||||||
|
('314', 314),
|
||||||
|
(' 314', 314),
|
||||||
|
('314 ', 314),
|
||||||
|
(' \t\t 314 \t\t ', 314),
|
||||||
|
(repr(sys.maxint), sys.maxint),
|
||||||
|
(' 1x', ValueError),
|
||||||
|
(' 1 ', 1),
|
||||||
|
(' 1\02 ', ValueError),
|
||||||
|
('', ValueError),
|
||||||
|
(' ', ValueError),
|
||||||
|
(' \t\t ', ValueError)
|
||||||
|
]
|
||||||
|
if have_unicode:
|
||||||
|
L += [
|
||||||
|
(unicode('0'), 0),
|
||||||
|
(unicode('1'), 1),
|
||||||
|
(unicode('9'), 9),
|
||||||
|
(unicode('10'), 10),
|
||||||
|
(unicode('99'), 99),
|
||||||
|
(unicode('100'), 100),
|
||||||
|
(unicode('314'), 314),
|
||||||
|
(unicode(' 314'), 314),
|
||||||
|
(unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
|
||||||
|
(unicode(' \t\t 314 \t\t '), 314),
|
||||||
|
(unicode(' 1x'), ValueError),
|
||||||
|
(unicode(' 1 '), 1),
|
||||||
|
(unicode(' 1\02 '), ValueError),
|
||||||
|
(unicode(''), ValueError),
|
||||||
|
(unicode(' '), ValueError),
|
||||||
|
(unicode(' \t\t '), ValueError),
|
||||||
|
(unichr(0x200), ValueError),
|
||||||
|
]
|
||||||
|
|
||||||
|
class IntTestCases(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_basic(self):
|
||||||
|
self.assertEqual(int(314), 314)
|
||||||
|
self.assertEqual(int(3.14), 3)
|
||||||
|
self.assertEqual(int(314L), 314)
|
||||||
|
# Check that conversion from float truncates towards zero
|
||||||
|
self.assertEqual(int(-3.14), -3)
|
||||||
|
self.assertEqual(int(3.9), 3)
|
||||||
|
self.assertEqual(int(-3.9), -3)
|
||||||
|
self.assertEqual(int(3.5), 3)
|
||||||
|
self.assertEqual(int(-3.5), -3)
|
||||||
|
# Different base:
|
||||||
|
self.assertEqual(int("10",16), 16L)
|
||||||
|
if have_unicode:
|
||||||
|
self.assertEqual(int(unicode("10"),16), 16L)
|
||||||
|
# Test conversion from strings and various anomalies
|
||||||
|
for s, v in L:
|
||||||
|
for sign in "", "+", "-":
|
||||||
|
for prefix in "", " ", "\t", " \t\t ":
|
||||||
|
ss = prefix + sign + s
|
||||||
|
vv = v
|
||||||
|
if sign == "-" and v is not ValueError:
|
||||||
|
vv = -v
|
||||||
|
try:
|
||||||
|
self.assertEqual(int(ss), vv)
|
||||||
|
except v:
|
||||||
|
pass
|
||||||
|
|
||||||
|
s = repr(-1-sys.maxint)
|
||||||
|
x = int(s)
|
||||||
|
self.assertEqual(x+1, -sys.maxint)
|
||||||
|
self.assert_(isinstance(x, int))
|
||||||
|
# should return long
|
||||||
|
self.assertEqual(int(s[1:]), sys.maxint+1)
|
||||||
|
|
||||||
|
# should return long
|
||||||
|
x = int(1e100)
|
||||||
|
self.assert_(isinstance(x, long))
|
||||||
|
x = int(-1e100)
|
||||||
|
self.assert_(isinstance(x, long))
|
||||||
|
|
||||||
|
|
||||||
|
# SF bug 434186: 0x80000000/2 != 0x80000000>>1.
|
||||||
|
# Worked by accident in Windows release build, but failed in debug build.
|
||||||
|
# Failed in all Linux builds.
|
||||||
|
x = -1-sys.maxint
|
||||||
|
self.assertEqual(x >> 1, x//2)
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, int, '123\0')
|
||||||
|
self.assertRaises(ValueError, int, '53', 40)
|
||||||
|
|
||||||
|
# SF bug 1545497: embedded NULs were not detected with
|
||||||
|
# explicit base
|
||||||
|
self.assertRaises(ValueError, int, '123\0', 10)
|
||||||
|
self.assertRaises(ValueError, int, '123\x00 245', 20)
|
||||||
|
|
||||||
|
x = int('1' * 600)
|
||||||
|
self.assert_(isinstance(x, long))
|
||||||
|
|
||||||
|
if have_unicode:
|
||||||
|
x = int(unichr(0x661) * 600)
|
||||||
|
self.assert_(isinstance(x, long))
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, int, 1, 12)
|
||||||
|
|
||||||
|
self.assertEqual(int('0123', 0), 83)
|
||||||
|
self.assertEqual(int('0x123', 16), 291)
|
||||||
|
|
||||||
|
# Bug 1679: "0x" is not a valid hex literal
|
||||||
|
self.assertRaises(ValueError, int, "0x", 16)
|
||||||
|
self.assertRaises(ValueError, int, "0x", 0)
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, int, "0o", 8)
|
||||||
|
self.assertRaises(ValueError, int, "0o", 0)
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, int, "0b", 2)
|
||||||
|
self.assertRaises(ValueError, int, "0b", 0)
|
||||||
|
|
||||||
|
|
||||||
|
# SF bug 1334662: int(string, base) wrong answers
|
||||||
|
# Various representations of 2**32 evaluated to 0
|
||||||
|
# rather than 2**32 in previous versions
|
||||||
|
|
||||||
|
self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296L)
|
||||||
|
self.assertEqual(int('102002022201221111211', 3), 4294967296L)
|
||||||
|
self.assertEqual(int('10000000000000000', 4), 4294967296L)
|
||||||
|
self.assertEqual(int('32244002423141', 5), 4294967296L)
|
||||||
|
self.assertEqual(int('1550104015504', 6), 4294967296L)
|
||||||
|
self.assertEqual(int('211301422354', 7), 4294967296L)
|
||||||
|
self.assertEqual(int('40000000000', 8), 4294967296L)
|
||||||
|
self.assertEqual(int('12068657454', 9), 4294967296L)
|
||||||
|
self.assertEqual(int('4294967296', 10), 4294967296L)
|
||||||
|
self.assertEqual(int('1904440554', 11), 4294967296L)
|
||||||
|
self.assertEqual(int('9ba461594', 12), 4294967296L)
|
||||||
|
self.assertEqual(int('535a79889', 13), 4294967296L)
|
||||||
|
self.assertEqual(int('2ca5b7464', 14), 4294967296L)
|
||||||
|
self.assertEqual(int('1a20dcd81', 15), 4294967296L)
|
||||||
|
self.assertEqual(int('100000000', 16), 4294967296L)
|
||||||
|
self.assertEqual(int('a7ffda91', 17), 4294967296L)
|
||||||
|
self.assertEqual(int('704he7g4', 18), 4294967296L)
|
||||||
|
self.assertEqual(int('4f5aff66', 19), 4294967296L)
|
||||||
|
self.assertEqual(int('3723ai4g', 20), 4294967296L)
|
||||||
|
self.assertEqual(int('281d55i4', 21), 4294967296L)
|
||||||
|
self.assertEqual(int('1fj8b184', 22), 4294967296L)
|
||||||
|
self.assertEqual(int('1606k7ic', 23), 4294967296L)
|
||||||
|
self.assertEqual(int('mb994ag', 24), 4294967296L)
|
||||||
|
self.assertEqual(int('hek2mgl', 25), 4294967296L)
|
||||||
|
self.assertEqual(int('dnchbnm', 26), 4294967296L)
|
||||||
|
self.assertEqual(int('b28jpdm', 27), 4294967296L)
|
||||||
|
self.assertEqual(int('8pfgih4', 28), 4294967296L)
|
||||||
|
self.assertEqual(int('76beigg', 29), 4294967296L)
|
||||||
|
self.assertEqual(int('5qmcpqg', 30), 4294967296L)
|
||||||
|
self.assertEqual(int('4q0jto4', 31), 4294967296L)
|
||||||
|
self.assertEqual(int('4000000', 32), 4294967296L)
|
||||||
|
self.assertEqual(int('3aokq94', 33), 4294967296L)
|
||||||
|
self.assertEqual(int('2qhxjli', 34), 4294967296L)
|
||||||
|
self.assertEqual(int('2br45qb', 35), 4294967296L)
|
||||||
|
self.assertEqual(int('1z141z4', 36), 4294967296L)
|
||||||
|
|
||||||
|
# tests with base 0
|
||||||
|
# this fails on 3.0, but in 2.x the old octal syntax is allowed
|
||||||
|
self.assertEqual(int(' 0123 ', 0), 83)
|
||||||
|
self.assertEqual(int(' 0123 ', 0), 83)
|
||||||
|
self.assertEqual(int('000', 0), 0)
|
||||||
|
self.assertEqual(int('0o123', 0), 83)
|
||||||
|
self.assertEqual(int('0x123', 0), 291)
|
||||||
|
self.assertEqual(int('0b100', 0), 4)
|
||||||
|
self.assertEqual(int(' 0O123 ', 0), 83)
|
||||||
|
self.assertEqual(int(' 0X123 ', 0), 291)
|
||||||
|
self.assertEqual(int(' 0B100 ', 0), 4)
|
||||||
|
|
||||||
|
# without base still base 10
|
||||||
|
self.assertEqual(int('0123'), 123)
|
||||||
|
self.assertEqual(int('0123', 10), 123)
|
||||||
|
|
||||||
|
# tests with prefix and base != 0
|
||||||
|
self.assertEqual(int('0x123', 16), 291)
|
||||||
|
self.assertEqual(int('0o123', 8), 83)
|
||||||
|
self.assertEqual(int('0b100', 2), 4)
|
||||||
|
self.assertEqual(int('0X123', 16), 291)
|
||||||
|
self.assertEqual(int('0O123', 8), 83)
|
||||||
|
self.assertEqual(int('0B100', 2), 4)
|
||||||
|
|
||||||
|
# the code has special checks for the first character after the
|
||||||
|
# type prefix
|
||||||
|
self.assertRaises(ValueError, int, '0b2', 2)
|
||||||
|
self.assertRaises(ValueError, int, '0b02', 2)
|
||||||
|
self.assertRaises(ValueError, int, '0B2', 2)
|
||||||
|
self.assertRaises(ValueError, int, '0B02', 2)
|
||||||
|
self.assertRaises(ValueError, int, '0o8', 8)
|
||||||
|
self.assertRaises(ValueError, int, '0o08', 8)
|
||||||
|
self.assertRaises(ValueError, int, '0O8', 8)
|
||||||
|
self.assertRaises(ValueError, int, '0O08', 8)
|
||||||
|
self.assertRaises(ValueError, int, '0xg', 16)
|
||||||
|
self.assertRaises(ValueError, int, '0x0g', 16)
|
||||||
|
self.assertRaises(ValueError, int, '0Xg', 16)
|
||||||
|
self.assertRaises(ValueError, int, '0X0g', 16)
|
||||||
|
|
||||||
|
# SF bug 1334662: int(string, base) wrong answers
|
||||||
|
# Checks for proper evaluation of 2**32 + 1
|
||||||
|
self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L)
|
||||||
|
self.assertEqual(int('102002022201221111212', 3), 4294967297L)
|
||||||
|
self.assertEqual(int('10000000000000001', 4), 4294967297L)
|
||||||
|
self.assertEqual(int('32244002423142', 5), 4294967297L)
|
||||||
|
self.assertEqual(int('1550104015505', 6), 4294967297L)
|
||||||
|
self.assertEqual(int('211301422355', 7), 4294967297L)
|
||||||
|
self.assertEqual(int('40000000001', 8), 4294967297L)
|
||||||
|
self.assertEqual(int('12068657455', 9), 4294967297L)
|
||||||
|
self.assertEqual(int('4294967297', 10), 4294967297L)
|
||||||
|
self.assertEqual(int('1904440555', 11), 4294967297L)
|
||||||
|
self.assertEqual(int('9ba461595', 12), 4294967297L)
|
||||||
|
self.assertEqual(int('535a7988a', 13), 4294967297L)
|
||||||
|
self.assertEqual(int('2ca5b7465', 14), 4294967297L)
|
||||||
|
self.assertEqual(int('1a20dcd82', 15), 4294967297L)
|
||||||
|
self.assertEqual(int('100000001', 16), 4294967297L)
|
||||||
|
self.assertEqual(int('a7ffda92', 17), 4294967297L)
|
||||||
|
self.assertEqual(int('704he7g5', 18), 4294967297L)
|
||||||
|
self.assertEqual(int('4f5aff67', 19), 4294967297L)
|
||||||
|
self.assertEqual(int('3723ai4h', 20), 4294967297L)
|
||||||
|
self.assertEqual(int('281d55i5', 21), 4294967297L)
|
||||||
|
self.assertEqual(int('1fj8b185', 22), 4294967297L)
|
||||||
|
self.assertEqual(int('1606k7id', 23), 4294967297L)
|
||||||
|
self.assertEqual(int('mb994ah', 24), 4294967297L)
|
||||||
|
self.assertEqual(int('hek2mgm', 25), 4294967297L)
|
||||||
|
self.assertEqual(int('dnchbnn', 26), 4294967297L)
|
||||||
|
self.assertEqual(int('b28jpdn', 27), 4294967297L)
|
||||||
|
self.assertEqual(int('8pfgih5', 28), 4294967297L)
|
||||||
|
self.assertEqual(int('76beigh', 29), 4294967297L)
|
||||||
|
self.assertEqual(int('5qmcpqh', 30), 4294967297L)
|
||||||
|
self.assertEqual(int('4q0jto5', 31), 4294967297L)
|
||||||
|
self.assertEqual(int('4000001', 32), 4294967297L)
|
||||||
|
self.assertEqual(int('3aokq95', 33), 4294967297L)
|
||||||
|
self.assertEqual(int('2qhxjlj', 34), 4294967297L)
|
||||||
|
self.assertEqual(int('2br45qc', 35), 4294967297L)
|
||||||
|
self.assertEqual(int('1z141z5', 36), 4294967297L)
|
||||||
|
|
||||||
|
def test_intconversion(self):
|
||||||
|
# Test __int__()
|
||||||
|
class ClassicMissingMethods:
|
||||||
|
pass
|
||||||
|
self.assertRaises(AttributeError, int, ClassicMissingMethods())
|
||||||
|
|
||||||
|
class MissingMethods(object):
|
||||||
|
pass
|
||||||
|
self.assertRaises(TypeError, int, MissingMethods())
|
||||||
|
|
||||||
|
class Foo0:
|
||||||
|
def __int__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
class Foo1(object):
|
||||||
|
def __int__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
class Foo2(int):
|
||||||
|
def __int__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
class Foo3(int):
|
||||||
|
def __int__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
class Foo4(int):
|
||||||
|
def __int__(self):
|
||||||
|
return 42L
|
||||||
|
|
||||||
|
class Foo5(int):
|
||||||
|
def __int__(self):
|
||||||
|
return 42.
|
||||||
|
|
||||||
|
self.assertEqual(int(Foo0()), 42)
|
||||||
|
self.assertEqual(int(Foo1()), 42)
|
||||||
|
self.assertEqual(int(Foo2()), 42)
|
||||||
|
self.assertEqual(int(Foo3()), 0)
|
||||||
|
self.assertEqual(int(Foo4()), 42L)
|
||||||
|
self.assertRaises(TypeError, int, Foo5())
|
||||||
|
|
||||||
|
class Classic:
|
||||||
|
pass
|
||||||
|
for base in (object, Classic):
|
||||||
|
class IntOverridesTrunc(base):
|
||||||
|
def __int__(self):
|
||||||
|
return 42
|
||||||
|
def __trunc__(self):
|
||||||
|
return -12
|
||||||
|
self.assertEqual(int(IntOverridesTrunc()), 42)
|
||||||
|
|
||||||
|
class JustTrunc(base):
|
||||||
|
def __trunc__(self):
|
||||||
|
return 42
|
||||||
|
self.assertEqual(int(JustTrunc()), 42)
|
||||||
|
|
||||||
|
for trunc_result_base in (object, Classic):
|
||||||
|
class Integral(trunc_result_base):
|
||||||
|
def __int__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
class TruncReturnsNonInt(base):
|
||||||
|
def __trunc__(self):
|
||||||
|
return Integral()
|
||||||
|
self.assertEqual(int(TruncReturnsNonInt()), 42)
|
||||||
|
|
||||||
|
class NonIntegral(trunc_result_base):
|
||||||
|
def __trunc__(self):
|
||||||
|
# Check that we avoid infinite recursion.
|
||||||
|
return NonIntegral()
|
||||||
|
|
||||||
|
class TruncReturnsNonIntegral(base):
|
||||||
|
def __trunc__(self):
|
||||||
|
return NonIntegral()
|
||||||
|
try:
|
||||||
|
int(TruncReturnsNonIntegral())
|
||||||
|
except TypeError as e:
|
||||||
|
self.assertEquals(str(e),
|
||||||
|
"__trunc__ returned non-Integral"
|
||||||
|
" (type NonIntegral)")
|
||||||
|
else:
|
||||||
|
self.fail("Failed to raise TypeError with %s" %
|
||||||
|
((base, trunc_result_base),))
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
run_unittest(IntTestCases)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
|
@ -4,6 +4,39 @@ from test import test_support, list_tests
|
||||||
class ListTest(list_tests.CommonTest):
|
class ListTest(list_tests.CommonTest):
|
||||||
type2test = list
|
type2test = list
|
||||||
|
|
||||||
|
def test_basic(self):
|
||||||
|
self.assertEqual(list([]), [])
|
||||||
|
l0_3 = [0, 1, 2, 3]
|
||||||
|
l0_3_bis = list(l0_3)
|
||||||
|
self.assertEqual(l0_3, l0_3_bis)
|
||||||
|
self.assert_(l0_3 is not l0_3_bis)
|
||||||
|
self.assertEqual(list(()), [])
|
||||||
|
self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
|
||||||
|
self.assertEqual(list(''), [])
|
||||||
|
self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
|
||||||
|
|
||||||
|
if sys.maxint == 0x7fffffff:
|
||||||
|
# This test can currently only work on 32-bit machines.
|
||||||
|
# XXX If/when PySequence_Length() returns a ssize_t, it should be
|
||||||
|
# XXX re-enabled.
|
||||||
|
# Verify clearing of bug #556025.
|
||||||
|
# This assumes that the max data size (sys.maxint) == max
|
||||||
|
# address size this also assumes that the address size is at
|
||||||
|
# least 4 bytes with 8 byte addresses, the bug is not well
|
||||||
|
# tested
|
||||||
|
#
|
||||||
|
# Note: This test is expected to SEGV under Cygwin 1.3.12 or
|
||||||
|
# earlier due to a newlib bug. See the following mailing list
|
||||||
|
# thread for the details:
|
||||||
|
|
||||||
|
# http://sources.redhat.com/ml/newlib/2002/msg00369.html
|
||||||
|
self.assertRaises(MemoryError, list, xrange(sys.maxint // 2))
|
||||||
|
|
||||||
|
# This code used to segfault in Py2.4a3
|
||||||
|
x = []
|
||||||
|
x.extend(-y for y in x)
|
||||||
|
self.assertEqual(x, [])
|
||||||
|
|
||||||
def test_truth(self):
|
def test_truth(self):
|
||||||
super(ListTest, self).test_truth()
|
super(ListTest, self).test_truth()
|
||||||
self.assert_(not [])
|
self.assert_(not [])
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import unittest
|
import unittest
|
||||||
from test import test_support
|
from test import test_support
|
||||||
|
import sys
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -36,6 +37,46 @@ del p2
|
||||||
special = special + map(lambda x: ~x, special) + \
|
special = special + map(lambda x: ~x, special) + \
|
||||||
map(lambda x: -x, special)
|
map(lambda x: -x, special)
|
||||||
|
|
||||||
|
L = [
|
||||||
|
('0', 0),
|
||||||
|
('1', 1),
|
||||||
|
('9', 9),
|
||||||
|
('10', 10),
|
||||||
|
('99', 99),
|
||||||
|
('100', 100),
|
||||||
|
('314', 314),
|
||||||
|
(' 314', 314),
|
||||||
|
('314 ', 314),
|
||||||
|
(' \t\t 314 \t\t ', 314),
|
||||||
|
(repr(sys.maxint), sys.maxint),
|
||||||
|
(' 1x', ValueError),
|
||||||
|
(' 1 ', 1),
|
||||||
|
(' 1\02 ', ValueError),
|
||||||
|
('', ValueError),
|
||||||
|
(' ', ValueError),
|
||||||
|
(' \t\t ', ValueError)
|
||||||
|
]
|
||||||
|
if test_support.have_unicode:
|
||||||
|
L += [
|
||||||
|
(unicode('0'), 0),
|
||||||
|
(unicode('1'), 1),
|
||||||
|
(unicode('9'), 9),
|
||||||
|
(unicode('10'), 10),
|
||||||
|
(unicode('99'), 99),
|
||||||
|
(unicode('100'), 100),
|
||||||
|
(unicode('314'), 314),
|
||||||
|
(unicode(' 314'), 314),
|
||||||
|
(unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
|
||||||
|
(unicode(' \t\t 314 \t\t '), 314),
|
||||||
|
(unicode(' 1x'), ValueError),
|
||||||
|
(unicode(' 1 '), 1),
|
||||||
|
(unicode(' 1\02 '), ValueError),
|
||||||
|
(unicode(''), ValueError),
|
||||||
|
(unicode(' '), ValueError),
|
||||||
|
(unicode(' \t\t '), ValueError),
|
||||||
|
(unichr(0x200), ValueError),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class LongTest(unittest.TestCase):
|
class LongTest(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -234,8 +275,215 @@ class LongTest(unittest.TestCase):
|
||||||
x = self.getran(lenx)
|
x = self.getran(lenx)
|
||||||
self.check_format_1(x)
|
self.check_format_1(x)
|
||||||
|
|
||||||
|
def test_long(self):
|
||||||
|
self.assertEqual(long(314), 314L)
|
||||||
|
self.assertEqual(long(3.14), 3L)
|
||||||
|
self.assertEqual(long(314L), 314L)
|
||||||
|
# Check that conversion from float truncates towards zero
|
||||||
|
self.assertEqual(long(-3.14), -3L)
|
||||||
|
self.assertEqual(long(3.9), 3L)
|
||||||
|
self.assertEqual(long(-3.9), -3L)
|
||||||
|
self.assertEqual(long(3.5), 3L)
|
||||||
|
self.assertEqual(long(-3.5), -3L)
|
||||||
|
self.assertEqual(long("-3"), -3L)
|
||||||
|
if test_support.have_unicode:
|
||||||
|
self.assertEqual(long(unicode("-3")), -3L)
|
||||||
|
# Different base:
|
||||||
|
self.assertEqual(long("10",16), 16L)
|
||||||
|
if test_support.have_unicode:
|
||||||
|
self.assertEqual(long(unicode("10"),16), 16L)
|
||||||
|
# Check conversions from string (same test set as for int(), and then some)
|
||||||
|
LL = [
|
||||||
|
('1' + '0'*20, 10L**20),
|
||||||
|
('1' + '0'*100, 10L**100)
|
||||||
|
]
|
||||||
|
L2 = L[:]
|
||||||
|
if test_support.have_unicode:
|
||||||
|
L2 += [
|
||||||
|
(unicode('1') + unicode('0')*20, 10L**20),
|
||||||
|
(unicode('1') + unicode('0')*100, 10L**100),
|
||||||
|
]
|
||||||
|
for s, v in L2 + LL:
|
||||||
|
for sign in "", "+", "-":
|
||||||
|
for prefix in "", " ", "\t", " \t\t ":
|
||||||
|
ss = prefix + sign + s
|
||||||
|
vv = v
|
||||||
|
if sign == "-" and v is not ValueError:
|
||||||
|
vv = -v
|
||||||
|
try:
|
||||||
|
self.assertEqual(long(ss), long(vv))
|
||||||
|
except v:
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, long, '123\0')
|
||||||
|
self.assertRaises(ValueError, long, '53', 40)
|
||||||
|
self.assertRaises(TypeError, long, 1, 12)
|
||||||
|
|
||||||
|
# SF patch #1638879: embedded NULs were not detected with
|
||||||
|
# explicit base
|
||||||
|
self.assertRaises(ValueError, long, '123\0', 10)
|
||||||
|
self.assertRaises(ValueError, long, '123\x00 245', 20)
|
||||||
|
|
||||||
|
self.assertEqual(long('100000000000000000000000000000000', 2),
|
||||||
|
4294967296)
|
||||||
|
self.assertEqual(long('102002022201221111211', 3), 4294967296)
|
||||||
|
self.assertEqual(long('10000000000000000', 4), 4294967296)
|
||||||
|
self.assertEqual(long('32244002423141', 5), 4294967296)
|
||||||
|
self.assertEqual(long('1550104015504', 6), 4294967296)
|
||||||
|
self.assertEqual(long('211301422354', 7), 4294967296)
|
||||||
|
self.assertEqual(long('40000000000', 8), 4294967296)
|
||||||
|
self.assertEqual(long('12068657454', 9), 4294967296)
|
||||||
|
self.assertEqual(long('4294967296', 10), 4294967296)
|
||||||
|
self.assertEqual(long('1904440554', 11), 4294967296)
|
||||||
|
self.assertEqual(long('9ba461594', 12), 4294967296)
|
||||||
|
self.assertEqual(long('535a79889', 13), 4294967296)
|
||||||
|
self.assertEqual(long('2ca5b7464', 14), 4294967296)
|
||||||
|
self.assertEqual(long('1a20dcd81', 15), 4294967296)
|
||||||
|
self.assertEqual(long('100000000', 16), 4294967296)
|
||||||
|
self.assertEqual(long('a7ffda91', 17), 4294967296)
|
||||||
|
self.assertEqual(long('704he7g4', 18), 4294967296)
|
||||||
|
self.assertEqual(long('4f5aff66', 19), 4294967296)
|
||||||
|
self.assertEqual(long('3723ai4g', 20), 4294967296)
|
||||||
|
self.assertEqual(long('281d55i4', 21), 4294967296)
|
||||||
|
self.assertEqual(long('1fj8b184', 22), 4294967296)
|
||||||
|
self.assertEqual(long('1606k7ic', 23), 4294967296)
|
||||||
|
self.assertEqual(long('mb994ag', 24), 4294967296)
|
||||||
|
self.assertEqual(long('hek2mgl', 25), 4294967296)
|
||||||
|
self.assertEqual(long('dnchbnm', 26), 4294967296)
|
||||||
|
self.assertEqual(long('b28jpdm', 27), 4294967296)
|
||||||
|
self.assertEqual(long('8pfgih4', 28), 4294967296)
|
||||||
|
self.assertEqual(long('76beigg', 29), 4294967296)
|
||||||
|
self.assertEqual(long('5qmcpqg', 30), 4294967296)
|
||||||
|
self.assertEqual(long('4q0jto4', 31), 4294967296)
|
||||||
|
self.assertEqual(long('4000000', 32), 4294967296)
|
||||||
|
self.assertEqual(long('3aokq94', 33), 4294967296)
|
||||||
|
self.assertEqual(long('2qhxjli', 34), 4294967296)
|
||||||
|
self.assertEqual(long('2br45qb', 35), 4294967296)
|
||||||
|
self.assertEqual(long('1z141z4', 36), 4294967296)
|
||||||
|
|
||||||
|
self.assertEqual(long('100000000000000000000000000000001', 2),
|
||||||
|
4294967297)
|
||||||
|
self.assertEqual(long('102002022201221111212', 3), 4294967297)
|
||||||
|
self.assertEqual(long('10000000000000001', 4), 4294967297)
|
||||||
|
self.assertEqual(long('32244002423142', 5), 4294967297)
|
||||||
|
self.assertEqual(long('1550104015505', 6), 4294967297)
|
||||||
|
self.assertEqual(long('211301422355', 7), 4294967297)
|
||||||
|
self.assertEqual(long('40000000001', 8), 4294967297)
|
||||||
|
self.assertEqual(long('12068657455', 9), 4294967297)
|
||||||
|
self.assertEqual(long('4294967297', 10), 4294967297)
|
||||||
|
self.assertEqual(long('1904440555', 11), 4294967297)
|
||||||
|
self.assertEqual(long('9ba461595', 12), 4294967297)
|
||||||
|
self.assertEqual(long('535a7988a', 13), 4294967297)
|
||||||
|
self.assertEqual(long('2ca5b7465', 14), 4294967297)
|
||||||
|
self.assertEqual(long('1a20dcd82', 15), 4294967297)
|
||||||
|
self.assertEqual(long('100000001', 16), 4294967297)
|
||||||
|
self.assertEqual(long('a7ffda92', 17), 4294967297)
|
||||||
|
self.assertEqual(long('704he7g5', 18), 4294967297)
|
||||||
|
self.assertEqual(long('4f5aff67', 19), 4294967297)
|
||||||
|
self.assertEqual(long('3723ai4h', 20), 4294967297)
|
||||||
|
self.assertEqual(long('281d55i5', 21), 4294967297)
|
||||||
|
self.assertEqual(long('1fj8b185', 22), 4294967297)
|
||||||
|
self.assertEqual(long('1606k7id', 23), 4294967297)
|
||||||
|
self.assertEqual(long('mb994ah', 24), 4294967297)
|
||||||
|
self.assertEqual(long('hek2mgm', 25), 4294967297)
|
||||||
|
self.assertEqual(long('dnchbnn', 26), 4294967297)
|
||||||
|
self.assertEqual(long('b28jpdn', 27), 4294967297)
|
||||||
|
self.assertEqual(long('8pfgih5', 28), 4294967297)
|
||||||
|
self.assertEqual(long('76beigh', 29), 4294967297)
|
||||||
|
self.assertEqual(long('5qmcpqh', 30), 4294967297)
|
||||||
|
self.assertEqual(long('4q0jto5', 31), 4294967297)
|
||||||
|
self.assertEqual(long('4000001', 32), 4294967297)
|
||||||
|
self.assertEqual(long('3aokq95', 33), 4294967297)
|
||||||
|
self.assertEqual(long('2qhxjlj', 34), 4294967297)
|
||||||
|
self.assertEqual(long('2br45qc', 35), 4294967297)
|
||||||
|
self.assertEqual(long('1z141z5', 36), 4294967297)
|
||||||
|
|
||||||
|
|
||||||
|
def test_conversion(self):
|
||||||
|
# Test __long__()
|
||||||
|
class ClassicMissingMethods:
|
||||||
|
pass
|
||||||
|
self.assertRaises(AttributeError, long, ClassicMissingMethods())
|
||||||
|
|
||||||
|
class MissingMethods(object):
|
||||||
|
pass
|
||||||
|
self.assertRaises(TypeError, long, MissingMethods())
|
||||||
|
|
||||||
|
class Foo0:
|
||||||
|
def __long__(self):
|
||||||
|
return 42L
|
||||||
|
|
||||||
|
class Foo1(object):
|
||||||
|
def __long__(self):
|
||||||
|
return 42L
|
||||||
|
|
||||||
|
class Foo2(long):
|
||||||
|
def __long__(self):
|
||||||
|
return 42L
|
||||||
|
|
||||||
|
class Foo3(long):
|
||||||
|
def __long__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
class Foo4(long):
|
||||||
|
def __long__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
class Foo5(long):
|
||||||
|
def __long__(self):
|
||||||
|
return 42.
|
||||||
|
|
||||||
|
self.assertEqual(long(Foo0()), 42L)
|
||||||
|
self.assertEqual(long(Foo1()), 42L)
|
||||||
|
self.assertEqual(long(Foo2()), 42L)
|
||||||
|
self.assertEqual(long(Foo3()), 0)
|
||||||
|
self.assertEqual(long(Foo4()), 42)
|
||||||
|
self.assertRaises(TypeError, long, Foo5())
|
||||||
|
|
||||||
|
class Classic:
|
||||||
|
pass
|
||||||
|
for base in (object, Classic):
|
||||||
|
class LongOverridesTrunc(base):
|
||||||
|
def __long__(self):
|
||||||
|
return 42
|
||||||
|
def __trunc__(self):
|
||||||
|
return -12
|
||||||
|
self.assertEqual(long(LongOverridesTrunc()), 42)
|
||||||
|
|
||||||
|
class JustTrunc(base):
|
||||||
|
def __trunc__(self):
|
||||||
|
return 42
|
||||||
|
self.assertEqual(long(JustTrunc()), 42)
|
||||||
|
|
||||||
|
for trunc_result_base in (object, Classic):
|
||||||
|
class Integral(trunc_result_base):
|
||||||
|
def __int__(self):
|
||||||
|
return 42
|
||||||
|
|
||||||
|
class TruncReturnsNonLong(base):
|
||||||
|
def __trunc__(self):
|
||||||
|
return Integral()
|
||||||
|
self.assertEqual(long(TruncReturnsNonLong()), 42)
|
||||||
|
|
||||||
|
class NonIntegral(trunc_result_base):
|
||||||
|
def __trunc__(self):
|
||||||
|
# Check that we avoid infinite recursion.
|
||||||
|
return NonIntegral()
|
||||||
|
|
||||||
|
class TruncReturnsNonIntegral(base):
|
||||||
|
def __trunc__(self):
|
||||||
|
return NonIntegral()
|
||||||
|
try:
|
||||||
|
long(TruncReturnsNonIntegral())
|
||||||
|
except TypeError as e:
|
||||||
|
self.assertEquals(str(e),
|
||||||
|
"__trunc__ returned non-Integral"
|
||||||
|
" (type NonIntegral)")
|
||||||
|
else:
|
||||||
|
self.fail("Failed to raise TypeError with %s" %
|
||||||
|
((base, trunc_result_base),))
|
||||||
|
|
||||||
def test_misc(self):
|
def test_misc(self):
|
||||||
import sys
|
|
||||||
|
|
||||||
# check the extremes in int<->long conversion
|
# check the extremes in int<->long conversion
|
||||||
hugepos = sys.maxint
|
hugepos = sys.maxint
|
||||||
|
@ -417,7 +665,6 @@ class LongTest(unittest.TestCase):
|
||||||
def test_mixed_compares(self):
|
def test_mixed_compares(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
import math
|
import math
|
||||||
import sys
|
|
||||||
|
|
||||||
# We're mostly concerned with that mixing floats and longs does the
|
# We're mostly concerned with that mixing floats and longs does the
|
||||||
# right stuff, even when longs are too large to fit in a float.
|
# right stuff, even when longs are too large to fit in a float.
|
||||||
|
|
|
@ -17,6 +17,20 @@ class StrTest(
|
||||||
def fixtype(self, obj):
|
def fixtype(self, obj):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
def test_basic_creation(self):
|
||||||
|
self.assertEqual(str(''), '')
|
||||||
|
self.assertEqual(str(0), '0')
|
||||||
|
self.assertEqual(str(0L), '0')
|
||||||
|
self.assertEqual(str(()), '()')
|
||||||
|
self.assertEqual(str([]), '[]')
|
||||||
|
self.assertEqual(str({}), '{}')
|
||||||
|
a = []
|
||||||
|
a.append(a)
|
||||||
|
self.assertEqual(str(a), '[[...]]')
|
||||||
|
a = {}
|
||||||
|
a[0] = a
|
||||||
|
self.assertEqual(str(a), '{0: {...}}')
|
||||||
|
|
||||||
def test_formatting(self):
|
def test_formatting(self):
|
||||||
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
|
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
|
||||||
self.assertRaises(OverflowError, '%c'.__mod__, 0x1234)
|
self.assertRaises(OverflowError, '%c'.__mod__, 0x1234)
|
||||||
|
|
|
@ -7,6 +7,13 @@ class TupleTest(seq_tests.CommonTest):
|
||||||
super(TupleTest, self).test_len()
|
super(TupleTest, self).test_len()
|
||||||
# calling built-in types without argument must return empty
|
# calling built-in types without argument must return empty
|
||||||
self.assertEqual(tuple(), ())
|
self.assertEqual(tuple(), ())
|
||||||
|
t0_3 = (0, 1, 2, 3)
|
||||||
|
t0_3_bis = tuple(t0_3)
|
||||||
|
self.assert_(t0_3 is t0_3_bis)
|
||||||
|
self.assertEqual(tuple([]), ())
|
||||||
|
self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
|
||||||
|
self.assertEqual(tuple(''), ())
|
||||||
|
self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
|
||||||
|
|
||||||
def test_truth(self):
|
def test_truth(self):
|
||||||
super(TupleTest, self).test_truth()
|
super(TupleTest, self).test_truth()
|
||||||
|
|
Loading…
Reference in New Issue