PageRenderTime 630ms CodeModel.GetById 169ms app.highlight 325ms RepoModel.GetById 131ms app.codeStats 0ms

/Lib/test/test_int.py

http://unladen-swallow.googlecode.com/
Python | 331 lines | 329 code | 2 blank | 0 comment | 0 complexity | 4639ae84b039077b0a2796732fcd7e30 MD5 | raw file
  1import sys
  2
  3import unittest
  4from test.test_support import run_unittest, have_unicode
  5
  6L = [
  7        ('0', 0),
  8        ('1', 1),
  9        ('9', 9),
 10        ('10', 10),
 11        ('99', 99),
 12        ('100', 100),
 13        ('314', 314),
 14        (' 314', 314),
 15        ('314 ', 314),
 16        ('  \t\t  314  \t\t  ', 314),
 17        (repr(sys.maxint), sys.maxint),
 18        ('  1x', ValueError),
 19        ('  1  ', 1),
 20        ('  1\02  ', ValueError),
 21        ('', ValueError),
 22        (' ', ValueError),
 23        ('  \t\t  ', ValueError)
 24]
 25if have_unicode:
 26    L += [
 27        (unicode('0'), 0),
 28        (unicode('1'), 1),
 29        (unicode('9'), 9),
 30        (unicode('10'), 10),
 31        (unicode('99'), 99),
 32        (unicode('100'), 100),
 33        (unicode('314'), 314),
 34        (unicode(' 314'), 314),
 35        (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
 36        (unicode('  \t\t  314  \t\t  '), 314),
 37        (unicode('  1x'), ValueError),
 38        (unicode('  1  '), 1),
 39        (unicode('  1\02  '), ValueError),
 40        (unicode(''), ValueError),
 41        (unicode(' '), ValueError),
 42        (unicode('  \t\t  '), ValueError),
 43        (unichr(0x200), ValueError),
 44]
 45
 46class IntTestCases(unittest.TestCase):
 47
 48    def test_basic(self):
 49        self.assertEqual(int(314), 314)
 50        self.assertEqual(int(3.14), 3)
 51        self.assertEqual(int(314L), 314)
 52        # Check that conversion from float truncates towards zero
 53        self.assertEqual(int(-3.14), -3)
 54        self.assertEqual(int(3.9), 3)
 55        self.assertEqual(int(-3.9), -3)
 56        self.assertEqual(int(3.5), 3)
 57        self.assertEqual(int(-3.5), -3)
 58        # Different base:
 59        self.assertEqual(int("10",16), 16L)
 60        if have_unicode:
 61            self.assertEqual(int(unicode("10"),16), 16L)
 62        # Test conversion from strings and various anomalies
 63        for s, v in L:
 64            for sign in "", "+", "-":
 65                for prefix in "", " ", "\t", "  \t\t  ":
 66                    ss = prefix + sign + s
 67                    vv = v
 68                    if sign == "-" and v is not ValueError:
 69                        vv = -v
 70                    try:
 71                        self.assertEqual(int(ss), vv)
 72                    except v:
 73                        pass
 74
 75        s = repr(-1-sys.maxint)
 76        x = int(s)
 77        self.assertEqual(x+1, -sys.maxint)
 78        self.assert_(isinstance(x, int))
 79        # should return long
 80        self.assertEqual(int(s[1:]), sys.maxint+1)
 81
 82        # should return long
 83        x = int(1e100)
 84        self.assert_(isinstance(x, long))
 85        x = int(-1e100)
 86        self.assert_(isinstance(x, long))
 87
 88
 89        # SF bug 434186:  0x80000000/2 != 0x80000000>>1.
 90        # Worked by accident in Windows release build, but failed in debug build.
 91        # Failed in all Linux builds.
 92        x = -1-sys.maxint
 93        self.assertEqual(x >> 1, x//2)
 94
 95        self.assertRaises(ValueError, int, '123\0')
 96        self.assertRaises(ValueError, int, '53', 40)
 97
 98        # SF bug 1545497: embedded NULs were not detected with
 99        # explicit base
100        self.assertRaises(ValueError, int, '123\0', 10)
101        self.assertRaises(ValueError, int, '123\x00 245', 20)
102
103        x = int('1' * 600)
104        self.assert_(isinstance(x, long))
105
106        if have_unicode:
107            x = int(unichr(0x661) * 600)
108            self.assert_(isinstance(x, long))
109
110        self.assertRaises(TypeError, int, 1, 12)
111
112        self.assertEqual(int('0123', 0), 83)
113        self.assertEqual(int('0x123', 16), 291)
114
115        # Bug 1679: "0x" is not a valid hex literal
116        self.assertRaises(ValueError, int, "0x", 16)
117        self.assertRaises(ValueError, int, "0x", 0)
118
119        self.assertRaises(ValueError, int, "0o", 8)
120        self.assertRaises(ValueError, int, "0o", 0)
121
122        self.assertRaises(ValueError, int, "0b", 2)
123        self.assertRaises(ValueError, int, "0b", 0)
124
125
126        # SF bug 1334662: int(string, base) wrong answers
127        # Various representations of 2**32 evaluated to 0
128        # rather than 2**32 in previous versions
129
130        self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296L)
131        self.assertEqual(int('102002022201221111211', 3), 4294967296L)
132        self.assertEqual(int('10000000000000000', 4), 4294967296L)
133        self.assertEqual(int('32244002423141', 5), 4294967296L)
134        self.assertEqual(int('1550104015504', 6), 4294967296L)
135        self.assertEqual(int('211301422354', 7), 4294967296L)
136        self.assertEqual(int('40000000000', 8), 4294967296L)
137        self.assertEqual(int('12068657454', 9), 4294967296L)
138        self.assertEqual(int('4294967296', 10), 4294967296L)
139        self.assertEqual(int('1904440554', 11), 4294967296L)
140        self.assertEqual(int('9ba461594', 12), 4294967296L)
141        self.assertEqual(int('535a79889', 13), 4294967296L)
142        self.assertEqual(int('2ca5b7464', 14), 4294967296L)
143        self.assertEqual(int('1a20dcd81', 15), 4294967296L)
144        self.assertEqual(int('100000000', 16), 4294967296L)
145        self.assertEqual(int('a7ffda91', 17), 4294967296L)
146        self.assertEqual(int('704he7g4', 18), 4294967296L)
147        self.assertEqual(int('4f5aff66', 19), 4294967296L)
148        self.assertEqual(int('3723ai4g', 20), 4294967296L)
149        self.assertEqual(int('281d55i4', 21), 4294967296L)
150        self.assertEqual(int('1fj8b184', 22), 4294967296L)
151        self.assertEqual(int('1606k7ic', 23), 4294967296L)
152        self.assertEqual(int('mb994ag', 24), 4294967296L)
153        self.assertEqual(int('hek2mgl', 25), 4294967296L)
154        self.assertEqual(int('dnchbnm', 26), 4294967296L)
155        self.assertEqual(int('b28jpdm', 27), 4294967296L)
156        self.assertEqual(int('8pfgih4', 28), 4294967296L)
157        self.assertEqual(int('76beigg', 29), 4294967296L)
158        self.assertEqual(int('5qmcpqg', 30), 4294967296L)
159        self.assertEqual(int('4q0jto4', 31), 4294967296L)
160        self.assertEqual(int('4000000', 32), 4294967296L)
161        self.assertEqual(int('3aokq94', 33), 4294967296L)
162        self.assertEqual(int('2qhxjli', 34), 4294967296L)
163        self.assertEqual(int('2br45qb', 35), 4294967296L)
164        self.assertEqual(int('1z141z4', 36), 4294967296L)
165
166        # tests with base 0
167        # this fails on 3.0, but in 2.x the old octal syntax is allowed
168        self.assertEqual(int(' 0123  ', 0), 83)
169        self.assertEqual(int(' 0123  ', 0), 83)
170        self.assertEqual(int('000', 0), 0)
171        self.assertEqual(int('0o123', 0), 83)
172        self.assertEqual(int('0x123', 0), 291)
173        self.assertEqual(int('0b100', 0), 4)
174        self.assertEqual(int(' 0O123   ', 0), 83)
175        self.assertEqual(int(' 0X123  ', 0), 291)
176        self.assertEqual(int(' 0B100 ', 0), 4)
177
178        # without base still base 10
179        self.assertEqual(int('0123'), 123)
180        self.assertEqual(int('0123', 10), 123)
181
182        # tests with prefix and base != 0
183        self.assertEqual(int('0x123', 16), 291)
184        self.assertEqual(int('0o123', 8), 83)
185        self.assertEqual(int('0b100', 2), 4)
186        self.assertEqual(int('0X123', 16), 291)
187        self.assertEqual(int('0O123', 8), 83)
188        self.assertEqual(int('0B100', 2), 4)
189
190        # the code has special checks for the first character after the
191        #  type prefix
192        self.assertRaises(ValueError, int, '0b2', 2)
193        self.assertRaises(ValueError, int, '0b02', 2)
194        self.assertRaises(ValueError, int, '0B2', 2)
195        self.assertRaises(ValueError, int, '0B02', 2)
196        self.assertRaises(ValueError, int, '0o8', 8)
197        self.assertRaises(ValueError, int, '0o08', 8)
198        self.assertRaises(ValueError, int, '0O8', 8)
199        self.assertRaises(ValueError, int, '0O08', 8)
200        self.assertRaises(ValueError, int, '0xg', 16)
201        self.assertRaises(ValueError, int, '0x0g', 16)
202        self.assertRaises(ValueError, int, '0Xg', 16)
203        self.assertRaises(ValueError, int, '0X0g', 16)
204
205        # SF bug 1334662: int(string, base) wrong answers
206        # Checks for proper evaluation of 2**32 + 1
207        self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L)
208        self.assertEqual(int('102002022201221111212', 3), 4294967297L)
209        self.assertEqual(int('10000000000000001', 4), 4294967297L)
210        self.assertEqual(int('32244002423142', 5), 4294967297L)
211        self.assertEqual(int('1550104015505', 6), 4294967297L)
212        self.assertEqual(int('211301422355', 7), 4294967297L)
213        self.assertEqual(int('40000000001', 8), 4294967297L)
214        self.assertEqual(int('12068657455', 9), 4294967297L)
215        self.assertEqual(int('4294967297', 10), 4294967297L)
216        self.assertEqual(int('1904440555', 11), 4294967297L)
217        self.assertEqual(int('9ba461595', 12), 4294967297L)
218        self.assertEqual(int('535a7988a', 13), 4294967297L)
219        self.assertEqual(int('2ca5b7465', 14), 4294967297L)
220        self.assertEqual(int('1a20dcd82', 15), 4294967297L)
221        self.assertEqual(int('100000001', 16), 4294967297L)
222        self.assertEqual(int('a7ffda92', 17), 4294967297L)
223        self.assertEqual(int('704he7g5', 18), 4294967297L)
224        self.assertEqual(int('4f5aff67', 19), 4294967297L)
225        self.assertEqual(int('3723ai4h', 20), 4294967297L)
226        self.assertEqual(int('281d55i5', 21), 4294967297L)
227        self.assertEqual(int('1fj8b185', 22), 4294967297L)
228        self.assertEqual(int('1606k7id', 23), 4294967297L)
229        self.assertEqual(int('mb994ah', 24), 4294967297L)
230        self.assertEqual(int('hek2mgm', 25), 4294967297L)
231        self.assertEqual(int('dnchbnn', 26), 4294967297L)
232        self.assertEqual(int('b28jpdn', 27), 4294967297L)
233        self.assertEqual(int('8pfgih5', 28), 4294967297L)
234        self.assertEqual(int('76beigh', 29), 4294967297L)
235        self.assertEqual(int('5qmcpqh', 30), 4294967297L)
236        self.assertEqual(int('4q0jto5', 31), 4294967297L)
237        self.assertEqual(int('4000001', 32), 4294967297L)
238        self.assertEqual(int('3aokq95', 33), 4294967297L)
239        self.assertEqual(int('2qhxjlj', 34), 4294967297L)
240        self.assertEqual(int('2br45qc', 35), 4294967297L)
241        self.assertEqual(int('1z141z5', 36), 4294967297L)
242
243    def test_intconversion(self):
244        # Test __int__()
245        class ClassicMissingMethods:
246            pass
247        self.assertRaises(AttributeError, int, ClassicMissingMethods())
248
249        class MissingMethods(object):
250            pass
251        self.assertRaises(TypeError, int, MissingMethods())
252
253        class Foo0:
254            def __int__(self):
255                return 42
256
257        class Foo1(object):
258            def __int__(self):
259                return 42
260
261        class Foo2(int):
262            def __int__(self):
263                return 42
264
265        class Foo3(int):
266            def __int__(self):
267                return self
268
269        class Foo4(int):
270            def __int__(self):
271                return 42L
272
273        class Foo5(int):
274            def __int__(self):
275                return 42.
276
277        self.assertEqual(int(Foo0()), 42)
278        self.assertEqual(int(Foo1()), 42)
279        self.assertEqual(int(Foo2()), 42)
280        self.assertEqual(int(Foo3()), 0)
281        self.assertEqual(int(Foo4()), 42L)
282        self.assertRaises(TypeError, int, Foo5())
283
284        class Classic:
285            pass
286        for base in (object, Classic):
287            class IntOverridesTrunc(base):
288                def __int__(self):
289                    return 42
290                def __trunc__(self):
291                    return -12
292            self.assertEqual(int(IntOverridesTrunc()), 42)
293
294            class JustTrunc(base):
295                def __trunc__(self):
296                    return 42
297            self.assertEqual(int(JustTrunc()), 42)
298
299            for trunc_result_base in (object, Classic):
300                class Integral(trunc_result_base):
301                    def __int__(self):
302                        return 42
303
304                class TruncReturnsNonInt(base):
305                    def __trunc__(self):
306                        return Integral()
307                self.assertEqual(int(TruncReturnsNonInt()), 42)
308
309                class NonIntegral(trunc_result_base):
310                    def __trunc__(self):
311                        # Check that we avoid infinite recursion.
312                        return NonIntegral()
313
314                class TruncReturnsNonIntegral(base):
315                    def __trunc__(self):
316                        return NonIntegral()
317                try:
318                    int(TruncReturnsNonIntegral())
319                except TypeError as e:
320                    self.assertEquals(str(e),
321                                      "__trunc__ returned non-Integral"
322                                      " (type NonIntegral)")
323                else:
324                    self.fail("Failed to raise TypeError with %s" %
325                              ((base, trunc_result_base),))
326
327def test_main():
328    run_unittest(IntTestCases)
329
330if __name__ == "__main__":
331    test_main()