Back to index

python3.2  3.2.2
Public Member Functions
test.test_int.IntTestCases Class Reference

List of all members.

Public Member Functions

def test_basic
def test_intconversion
def test_error_message

Detailed Description

Definition at line 27 of file test_int.py.


Member Function Documentation

Definition at line 29 of file test_int.py.

00029 
00030     def test_basic(self):
00031         self.assertEqual(int(314), 314)
00032         self.assertEqual(int(3.14), 3)
00033         # Check that conversion from float truncates towards zero
00034         self.assertEqual(int(-3.14), -3)
00035         self.assertEqual(int(3.9), 3)
00036         self.assertEqual(int(-3.9), -3)
00037         self.assertEqual(int(3.5), 3)
00038         self.assertEqual(int(-3.5), -3)
00039         self.assertEqual(int("-3"), -3)
00040         self.assertEqual(int(" -3 "), -3)
00041         self.assertEqual(int("\N{EM SPACE}-3\N{EN SPACE}"), -3)
00042         # Different base:
00043         self.assertEqual(int("10",16), 16)
00044         # Test conversion from strings and various anomalies
00045         for s, v in L:
00046             for sign in "", "+", "-":
00047                 for prefix in "", " ", "\t", "  \t\t  ":
00048                     ss = prefix + sign + s
00049                     vv = v
00050                     if sign == "-" and v is not ValueError:
00051                         vv = -v
00052                     try:
00053                         self.assertEqual(int(ss), vv)
00054                     except ValueError:
00055                         pass
00056 
00057         s = repr(-1-sys.maxsize)
00058         x = int(s)
00059         self.assertEqual(x+1, -sys.maxsize)
00060         self.assertIsInstance(x, int)
00061         # should return int
00062         self.assertEqual(int(s[1:]), sys.maxsize+1)
00063 
00064         # should return int
00065         x = int(1e100)
00066         self.assertIsInstance(x, int)
00067         x = int(-1e100)
00068         self.assertIsInstance(x, int)
00069 
00070 
00071         # SF bug 434186:  0x80000000/2 != 0x80000000>>1.
00072         # Worked by accident in Windows release build, but failed in debug build.
00073         # Failed in all Linux builds.
00074         x = -1-sys.maxsize
00075         self.assertEqual(x >> 1, x//2)
00076 
00077         self.assertRaises(ValueError, int, '123\0')
00078         self.assertRaises(ValueError, int, '53', 40)
00079 
00080         # SF bug 1545497: embedded NULs were not detected with
00081         # explicit base
00082         self.assertRaises(ValueError, int, '123\0', 10)
00083         self.assertRaises(ValueError, int, '123\x00 245', 20)
00084 
00085         x = int('1' * 600)
00086         self.assertIsInstance(x, int)
00087 
00088 
00089         self.assertRaises(TypeError, int, 1, 12)
00090 
00091         self.assertEqual(int('0o123', 0), 83)
00092         self.assertEqual(int('0x123', 16), 291)
00093 
00094         # Bug 1679: "0x" is not a valid hex literal
00095         self.assertRaises(ValueError, int, "0x", 16)
00096         self.assertRaises(ValueError, int, "0x", 0)
00097 
00098         self.assertRaises(ValueError, int, "0o", 8)
00099         self.assertRaises(ValueError, int, "0o", 0)
00100 
00101         self.assertRaises(ValueError, int, "0b", 2)
00102         self.assertRaises(ValueError, int, "0b", 0)
00103 
00104         # Bug #3236: Return small longs from PyLong_FromString
00105         self.assertTrue(int("10") is 10)
00106         self.assertTrue(int("-1") is -1)
00107 
00108         # SF bug 1334662: int(string, base) wrong answers
00109         # Various representations of 2**32 evaluated to 0
00110         # rather than 2**32 in previous versions
00111 
00112         self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296)
00113         self.assertEqual(int('102002022201221111211', 3), 4294967296)
00114         self.assertEqual(int('10000000000000000', 4), 4294967296)
00115         self.assertEqual(int('32244002423141', 5), 4294967296)
00116         self.assertEqual(int('1550104015504', 6), 4294967296)
00117         self.assertEqual(int('211301422354', 7), 4294967296)
00118         self.assertEqual(int('40000000000', 8), 4294967296)
00119         self.assertEqual(int('12068657454', 9), 4294967296)
00120         self.assertEqual(int('4294967296', 10), 4294967296)
00121         self.assertEqual(int('1904440554', 11), 4294967296)
00122         self.assertEqual(int('9ba461594', 12), 4294967296)
00123         self.assertEqual(int('535a79889', 13), 4294967296)
00124         self.assertEqual(int('2ca5b7464', 14), 4294967296)
00125         self.assertEqual(int('1a20dcd81', 15), 4294967296)
00126         self.assertEqual(int('100000000', 16), 4294967296)
00127         self.assertEqual(int('a7ffda91', 17), 4294967296)
00128         self.assertEqual(int('704he7g4', 18), 4294967296)
00129         self.assertEqual(int('4f5aff66', 19), 4294967296)
00130         self.assertEqual(int('3723ai4g', 20), 4294967296)
00131         self.assertEqual(int('281d55i4', 21), 4294967296)
00132         self.assertEqual(int('1fj8b184', 22), 4294967296)
00133         self.assertEqual(int('1606k7ic', 23), 4294967296)
00134         self.assertEqual(int('mb994ag', 24), 4294967296)
00135         self.assertEqual(int('hek2mgl', 25), 4294967296)
00136         self.assertEqual(int('dnchbnm', 26), 4294967296)
00137         self.assertEqual(int('b28jpdm', 27), 4294967296)
00138         self.assertEqual(int('8pfgih4', 28), 4294967296)
00139         self.assertEqual(int('76beigg', 29), 4294967296)
00140         self.assertEqual(int('5qmcpqg', 30), 4294967296)
00141         self.assertEqual(int('4q0jto4', 31), 4294967296)
00142         self.assertEqual(int('4000000', 32), 4294967296)
00143         self.assertEqual(int('3aokq94', 33), 4294967296)
00144         self.assertEqual(int('2qhxjli', 34), 4294967296)
00145         self.assertEqual(int('2br45qb', 35), 4294967296)
00146         self.assertEqual(int('1z141z4', 36), 4294967296)
00147 
00148         # tests with base 0
00149         # this fails on 3.0, but in 2.x the old octal syntax is allowed
00150         self.assertEqual(int(' 0o123  ', 0), 83)
00151         self.assertEqual(int(' 0o123  ', 0), 83)
00152         self.assertEqual(int('000', 0), 0)
00153         self.assertEqual(int('0o123', 0), 83)
00154         self.assertEqual(int('0x123', 0), 291)
00155         self.assertEqual(int('0b100', 0), 4)
00156         self.assertEqual(int(' 0O123   ', 0), 83)
00157         self.assertEqual(int(' 0X123  ', 0), 291)
00158         self.assertEqual(int(' 0B100 ', 0), 4)
00159 
00160         # without base still base 10
00161         self.assertEqual(int('0123'), 123)
00162         self.assertEqual(int('0123', 10), 123)
00163 
00164         # tests with prefix and base != 0
00165         self.assertEqual(int('0x123', 16), 291)
00166         self.assertEqual(int('0o123', 8), 83)
00167         self.assertEqual(int('0b100', 2), 4)
00168         self.assertEqual(int('0X123', 16), 291)
00169         self.assertEqual(int('0O123', 8), 83)
00170         self.assertEqual(int('0B100', 2), 4)
00171 
00172         # the code has special checks for the first character after the
00173         #  type prefix
00174         self.assertRaises(ValueError, int, '0b2', 2)
00175         self.assertRaises(ValueError, int, '0b02', 2)
00176         self.assertRaises(ValueError, int, '0B2', 2)
00177         self.assertRaises(ValueError, int, '0B02', 2)
00178         self.assertRaises(ValueError, int, '0o8', 8)
00179         self.assertRaises(ValueError, int, '0o08', 8)
00180         self.assertRaises(ValueError, int, '0O8', 8)
00181         self.assertRaises(ValueError, int, '0O08', 8)
00182         self.assertRaises(ValueError, int, '0xg', 16)
00183         self.assertRaises(ValueError, int, '0x0g', 16)
00184         self.assertRaises(ValueError, int, '0Xg', 16)
00185         self.assertRaises(ValueError, int, '0X0g', 16)
00186 
00187         # SF bug 1334662: int(string, base) wrong answers
00188         # Checks for proper evaluation of 2**32 + 1
00189         self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297)
00190         self.assertEqual(int('102002022201221111212', 3), 4294967297)
00191         self.assertEqual(int('10000000000000001', 4), 4294967297)
00192         self.assertEqual(int('32244002423142', 5), 4294967297)
00193         self.assertEqual(int('1550104015505', 6), 4294967297)
00194         self.assertEqual(int('211301422355', 7), 4294967297)
00195         self.assertEqual(int('40000000001', 8), 4294967297)
00196         self.assertEqual(int('12068657455', 9), 4294967297)
00197         self.assertEqual(int('4294967297', 10), 4294967297)
00198         self.assertEqual(int('1904440555', 11), 4294967297)
00199         self.assertEqual(int('9ba461595', 12), 4294967297)
00200         self.assertEqual(int('535a7988a', 13), 4294967297)
00201         self.assertEqual(int('2ca5b7465', 14), 4294967297)
00202         self.assertEqual(int('1a20dcd82', 15), 4294967297)
00203         self.assertEqual(int('100000001', 16), 4294967297)
00204         self.assertEqual(int('a7ffda92', 17), 4294967297)
00205         self.assertEqual(int('704he7g5', 18), 4294967297)
00206         self.assertEqual(int('4f5aff67', 19), 4294967297)
00207         self.assertEqual(int('3723ai4h', 20), 4294967297)
00208         self.assertEqual(int('281d55i5', 21), 4294967297)
00209         self.assertEqual(int('1fj8b185', 22), 4294967297)
00210         self.assertEqual(int('1606k7id', 23), 4294967297)
00211         self.assertEqual(int('mb994ah', 24), 4294967297)
00212         self.assertEqual(int('hek2mgm', 25), 4294967297)
00213         self.assertEqual(int('dnchbnn', 26), 4294967297)
00214         self.assertEqual(int('b28jpdn', 27), 4294967297)
00215         self.assertEqual(int('8pfgih5', 28), 4294967297)
00216         self.assertEqual(int('76beigh', 29), 4294967297)
00217         self.assertEqual(int('5qmcpqh', 30), 4294967297)
00218         self.assertEqual(int('4q0jto5', 31), 4294967297)
00219         self.assertEqual(int('4000001', 32), 4294967297)
00220         self.assertEqual(int('3aokq95', 33), 4294967297)
00221         self.assertEqual(int('2qhxjlj', 34), 4294967297)
00222         self.assertEqual(int('2br45qc', 35), 4294967297)
00223         self.assertEqual(int('1z141z5', 36), 4294967297)

Here is the call graph for this function:

Definition at line 308 of file test_int.py.

00308 
00309     def test_error_message(self):
00310         testlist = ('\xbd', '123\xbd', '  123 456  ')
00311         for s in testlist:
00312             try:
00313                 int(s)
00314             except ValueError as e:
00315                 self.assertIn(s.strip(), e.args[0])
00316             else:
00317                 self.fail("Expected int(%r) to raise a ValueError", s)

Here is the call graph for this function:

Definition at line 224 of file test_int.py.

00224 
00225     def test_intconversion(self):
00226         # Test __int__()
00227         class ClassicMissingMethods:
00228             pass
00229         self.assertRaises(TypeError, int, ClassicMissingMethods())
00230 
00231         class MissingMethods(object):
00232             pass
00233         self.assertRaises(TypeError, int, MissingMethods())
00234 
00235         class Foo0:
00236             def __int__(self):
00237                 return 42
00238 
00239         class Foo1(object):
00240             def __int__(self):
00241                 return 42
00242 
00243         class Foo2(int):
00244             def __int__(self):
00245                 return 42
00246 
00247         class Foo3(int):
00248             def __int__(self):
00249                 return self
00250 
00251         class Foo4(int):
00252             def __int__(self):
00253                 return 42
00254 
00255         class Foo5(int):
00256             def __int__(self):
00257                 return 42.
00258 
00259         self.assertEqual(int(Foo0()), 42)
00260         self.assertEqual(int(Foo1()), 42)
00261         self.assertEqual(int(Foo2()), 42)
00262         self.assertEqual(int(Foo3()), 0)
00263         self.assertEqual(int(Foo4()), 42)
00264         self.assertRaises(TypeError, int, Foo5())
00265 
00266         class Classic:
00267             pass
00268         for base in (object, Classic):
00269             class IntOverridesTrunc(base):
00270                 def __int__(self):
00271                     return 42
00272                 def __trunc__(self):
00273                     return -12
00274             self.assertEqual(int(IntOverridesTrunc()), 42)
00275 
00276             class JustTrunc(base):
00277                 def __trunc__(self):
00278                     return 42
00279             self.assertEqual(int(JustTrunc()), 42)
00280 
00281             for trunc_result_base in (object, Classic):
00282                 class Integral(trunc_result_base):
00283                     def __int__(self):
00284                         return 42
00285 
00286                 class TruncReturnsNonInt(base):
00287                     def __trunc__(self):
00288                         return Integral()
00289                 self.assertEqual(int(TruncReturnsNonInt()), 42)
00290 
00291                 class NonIntegral(trunc_result_base):
00292                     def __trunc__(self):
00293                         # Check that we avoid infinite recursion.
00294                         return NonIntegral()
00295 
00296                 class TruncReturnsNonIntegral(base):
00297                     def __trunc__(self):
00298                         return NonIntegral()
00299                 try:
00300                     int(TruncReturnsNonIntegral())
00301                 except TypeError as e:
00302                     self.assertEqual(str(e),
00303                                       "__trunc__ returned non-Integral"
00304                                       " (type NonIntegral)")
00305                 else:
00306                     self.fail("Failed to raise TypeError with %s" %
00307                               ((base, trunc_result_base),))


The documentation for this class was generated from the following file: