4 from test
.test_support
import run_unittest
, have_unicode
17 (' \t\t 314 \t\t ', 314),
18 (repr(sys
.maxint
), sys
.maxint
),
21 (' 1\02 ', ValueError),
24 (' \t\t ', ValueError)
33 (unicode('100'), 100),
34 (unicode('314'), 314),
35 (unicode(' 314'), 314),
36 (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
37 (unicode(' \t\t 314 \t\t '), 314),
38 (unicode(' 1x'), ValueError),
40 (unicode(' 1\02 '), ValueError),
41 (unicode(''), ValueError),
42 (unicode(' '), ValueError),
43 (unicode(' \t\t '), ValueError),
44 (unichr(0x200), ValueError),
47 class IntTestCases(unittest
.TestCase
):
50 self
.assertEqual(int(314), 314)
51 self
.assertEqual(int(3.14), 3)
52 self
.assertEqual(int(314L), 314)
53 # Check that conversion from float truncates towards zero
54 self
.assertEqual(int(-3.14), -3)
55 self
.assertEqual(int(3.9), 3)
56 self
.assertEqual(int(-3.9), -3)
57 self
.assertEqual(int(3.5), 3)
58 self
.assertEqual(int(-3.5), -3)
60 self
.assertEqual(int("10",16), 16L)
62 self
.assertEqual(int(unicode("10"),16), 16L)
63 # Test conversion from strings and various anomalies
65 for sign
in "", "+", "-":
66 for prefix
in "", " ", "\t", " \t\t ":
67 ss
= prefix
+ sign
+ s
69 if sign
== "-" and v
is not ValueError:
72 self
.assertEqual(int(ss
), vv
)
76 s
= repr(-1-sys
.maxint
)
78 self
.assertEqual(x
+1, -sys
.maxint
)
79 self
.assertIsInstance(x
, int)
81 self
.assertEqual(int(s
[1:]), sys
.maxint
+1)
85 self
.assertIsInstance(x
, long)
87 self
.assertIsInstance(x
, long)
90 # SF bug 434186: 0x80000000/2 != 0x80000000>>1.
91 # Worked by accident in Windows release build, but failed in debug build.
92 # Failed in all Linux builds.
94 self
.assertEqual(x
>> 1, x
//2)
96 self
.assertRaises(ValueError, int, '123\0')
97 self
.assertRaises(ValueError, int, '53', 40)
99 # SF bug 1545497: embedded NULs were not detected with
101 self
.assertRaises(ValueError, int, '123\0', 10)
102 self
.assertRaises(ValueError, int, '123\x00 245', 20)
105 self
.assertIsInstance(x
, long)
108 x
= int(unichr(0x661) * 600)
109 self
.assertIsInstance(x
, long)
111 self
.assertRaises(TypeError, int, 1, 12)
113 self
.assertEqual(int('0123', 0), 83)
114 self
.assertEqual(int('0x123', 16), 291)
116 # Bug 1679: "0x" is not a valid hex literal
117 self
.assertRaises(ValueError, int, "0x", 16)
118 self
.assertRaises(ValueError, int, "0x", 0)
120 self
.assertRaises(ValueError, int, "0o", 8)
121 self
.assertRaises(ValueError, int, "0o", 0)
123 self
.assertRaises(ValueError, int, "0b", 2)
124 self
.assertRaises(ValueError, int, "0b", 0)
127 # SF bug 1334662: int(string, base) wrong answers
128 # Various representations of 2**32 evaluated to 0
129 # rather than 2**32 in previous versions
131 self
.assertEqual(int('100000000000000000000000000000000', 2), 4294967296L)
132 self
.assertEqual(int('102002022201221111211', 3), 4294967296L)
133 self
.assertEqual(int('10000000000000000', 4), 4294967296L)
134 self
.assertEqual(int('32244002423141', 5), 4294967296L)
135 self
.assertEqual(int('1550104015504', 6), 4294967296L)
136 self
.assertEqual(int('211301422354', 7), 4294967296L)
137 self
.assertEqual(int('40000000000', 8), 4294967296L)
138 self
.assertEqual(int('12068657454', 9), 4294967296L)
139 self
.assertEqual(int('4294967296', 10), 4294967296L)
140 self
.assertEqual(int('1904440554', 11), 4294967296L)
141 self
.assertEqual(int('9ba461594', 12), 4294967296L)
142 self
.assertEqual(int('535a79889', 13), 4294967296L)
143 self
.assertEqual(int('2ca5b7464', 14), 4294967296L)
144 self
.assertEqual(int('1a20dcd81', 15), 4294967296L)
145 self
.assertEqual(int('100000000', 16), 4294967296L)
146 self
.assertEqual(int('a7ffda91', 17), 4294967296L)
147 self
.assertEqual(int('704he7g4', 18), 4294967296L)
148 self
.assertEqual(int('4f5aff66', 19), 4294967296L)
149 self
.assertEqual(int('3723ai4g', 20), 4294967296L)
150 self
.assertEqual(int('281d55i4', 21), 4294967296L)
151 self
.assertEqual(int('1fj8b184', 22), 4294967296L)
152 self
.assertEqual(int('1606k7ic', 23), 4294967296L)
153 self
.assertEqual(int('mb994ag', 24), 4294967296L)
154 self
.assertEqual(int('hek2mgl', 25), 4294967296L)
155 self
.assertEqual(int('dnchbnm', 26), 4294967296L)
156 self
.assertEqual(int('b28jpdm', 27), 4294967296L)
157 self
.assertEqual(int('8pfgih4', 28), 4294967296L)
158 self
.assertEqual(int('76beigg', 29), 4294967296L)
159 self
.assertEqual(int('5qmcpqg', 30), 4294967296L)
160 self
.assertEqual(int('4q0jto4', 31), 4294967296L)
161 self
.assertEqual(int('4000000', 32), 4294967296L)
162 self
.assertEqual(int('3aokq94', 33), 4294967296L)
163 self
.assertEqual(int('2qhxjli', 34), 4294967296L)
164 self
.assertEqual(int('2br45qb', 35), 4294967296L)
165 self
.assertEqual(int('1z141z4', 36), 4294967296L)
168 # this fails on 3.0, but in 2.x the old octal syntax is allowed
169 self
.assertEqual(int(' 0123 ', 0), 83)
170 self
.assertEqual(int(' 0123 ', 0), 83)
171 self
.assertEqual(int('000', 0), 0)
172 self
.assertEqual(int('0o123', 0), 83)
173 self
.assertEqual(int('0x123', 0), 291)
174 self
.assertEqual(int('0b100', 0), 4)
175 self
.assertEqual(int(' 0O123 ', 0), 83)
176 self
.assertEqual(int(' 0X123 ', 0), 291)
177 self
.assertEqual(int(' 0B100 ', 0), 4)
178 self
.assertEqual(int('0', 0), 0)
179 self
.assertEqual(int('+0', 0), 0)
180 self
.assertEqual(int('-0', 0), 0)
181 self
.assertEqual(int('00', 0), 0)
182 self
.assertRaises(ValueError, int, '08', 0)
183 self
.assertRaises(ValueError, int, '-012395', 0)
185 # without base still base 10
186 self
.assertEqual(int('0123'), 123)
187 self
.assertEqual(int('0123', 10), 123)
189 # tests with prefix and base != 0
190 self
.assertEqual(int('0x123', 16), 291)
191 self
.assertEqual(int('0o123', 8), 83)
192 self
.assertEqual(int('0b100', 2), 4)
193 self
.assertEqual(int('0X123', 16), 291)
194 self
.assertEqual(int('0O123', 8), 83)
195 self
.assertEqual(int('0B100', 2), 4)
197 # the code has special checks for the first character after the
199 self
.assertRaises(ValueError, int, '0b2', 2)
200 self
.assertRaises(ValueError, int, '0b02', 2)
201 self
.assertRaises(ValueError, int, '0B2', 2)
202 self
.assertRaises(ValueError, int, '0B02', 2)
203 self
.assertRaises(ValueError, int, '0o8', 8)
204 self
.assertRaises(ValueError, int, '0o08', 8)
205 self
.assertRaises(ValueError, int, '0O8', 8)
206 self
.assertRaises(ValueError, int, '0O08', 8)
207 self
.assertRaises(ValueError, int, '0xg', 16)
208 self
.assertRaises(ValueError, int, '0x0g', 16)
209 self
.assertRaises(ValueError, int, '0Xg', 16)
210 self
.assertRaises(ValueError, int, '0X0g', 16)
212 # SF bug 1334662: int(string, base) wrong answers
213 # Checks for proper evaluation of 2**32 + 1
214 self
.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L)
215 self
.assertEqual(int('102002022201221111212', 3), 4294967297L)
216 self
.assertEqual(int('10000000000000001', 4), 4294967297L)
217 self
.assertEqual(int('32244002423142', 5), 4294967297L)
218 self
.assertEqual(int('1550104015505', 6), 4294967297L)
219 self
.assertEqual(int('211301422355', 7), 4294967297L)
220 self
.assertEqual(int('40000000001', 8), 4294967297L)
221 self
.assertEqual(int('12068657455', 9), 4294967297L)
222 self
.assertEqual(int('4294967297', 10), 4294967297L)
223 self
.assertEqual(int('1904440555', 11), 4294967297L)
224 self
.assertEqual(int('9ba461595', 12), 4294967297L)
225 self
.assertEqual(int('535a7988a', 13), 4294967297L)
226 self
.assertEqual(int('2ca5b7465', 14), 4294967297L)
227 self
.assertEqual(int('1a20dcd82', 15), 4294967297L)
228 self
.assertEqual(int('100000001', 16), 4294967297L)
229 self
.assertEqual(int('a7ffda92', 17), 4294967297L)
230 self
.assertEqual(int('704he7g5', 18), 4294967297L)
231 self
.assertEqual(int('4f5aff67', 19), 4294967297L)
232 self
.assertEqual(int('3723ai4h', 20), 4294967297L)
233 self
.assertEqual(int('281d55i5', 21), 4294967297L)
234 self
.assertEqual(int('1fj8b185', 22), 4294967297L)
235 self
.assertEqual(int('1606k7id', 23), 4294967297L)
236 self
.assertEqual(int('mb994ah', 24), 4294967297L)
237 self
.assertEqual(int('hek2mgm', 25), 4294967297L)
238 self
.assertEqual(int('dnchbnn', 26), 4294967297L)
239 self
.assertEqual(int('b28jpdn', 27), 4294967297L)
240 self
.assertEqual(int('8pfgih5', 28), 4294967297L)
241 self
.assertEqual(int('76beigh', 29), 4294967297L)
242 self
.assertEqual(int('5qmcpqh', 30), 4294967297L)
243 self
.assertEqual(int('4q0jto5', 31), 4294967297L)
244 self
.assertEqual(int('4000001', 32), 4294967297L)
245 self
.assertEqual(int('3aokq95', 33), 4294967297L)
246 self
.assertEqual(int('2qhxjlj', 34), 4294967297L)
247 self
.assertEqual(int('2br45qc', 35), 4294967297L)
248 self
.assertEqual(int('1z141z5', 36), 4294967297L)
250 def test_bit_length(self
):
252 for x
in xrange(-65000, 65000):
254 # Check equivalence with Python version
255 self
.assertEqual(k
, len(bin(x
).lstrip('-0b')))
256 # Behaviour as specified in the docs
258 self
.assertTrue(2**(k
-1) <= abs(x
) < 2**k
)
260 self
.assertEqual(k
, 0)
261 # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
263 # When x is an exact power of 2, numeric errors can
264 # cause floor(log(x)/log(2)) to be one too small; for
265 # small x this can be fixed by adding a small quantity
266 # to the quotient before taking the floor.
267 self
.assertEqual(k
, 1 + math
.floor(
268 math
.log(abs(x
))/math
.log(2) + tiny
))
270 self
.assertEqual((0).bit_length(), 0)
271 self
.assertEqual((1).bit_length(), 1)
272 self
.assertEqual((-1).bit_length(), 1)
273 self
.assertEqual((2).bit_length(), 2)
274 self
.assertEqual((-2).bit_length(), 2)
275 for i
in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64]:
277 self
.assertEqual((a
-1).bit_length(), i
)
278 self
.assertEqual((1-a
).bit_length(), i
)
279 self
.assertEqual((a
).bit_length(), i
+1)
280 self
.assertEqual((-a
).bit_length(), i
+1)
281 self
.assertEqual((a
+1).bit_length(), i
+1)
282 self
.assertEqual((-a
-1).bit_length(), i
+1)
284 @unittest.skipUnless(float.__getformat
__("double").startswith("IEEE"),
285 "test requires IEEE 754 doubles")
286 def test_float_conversion(self
):
287 # values exactly representable as floats
288 exact_values
= [-2, -1, 0, 1, 2, 2**52, 2**53-1, 2**53, 2**53+2,
289 2**53+4, 2**54-4, 2**54-2, 2**63, -2**63, 2**64,
290 -2**64, 10**20, 10**21, 10**22]
291 for value
in exact_values
:
292 self
.assertEqual(int(float(int(value
))), value
)
294 # test round-half-to-even
295 self
.assertEqual(int(float(2**53+1)), 2**53)
296 self
.assertEqual(int(float(2**53+2)), 2**53+2)
297 self
.assertEqual(int(float(2**53+3)), 2**53+4)
298 self
.assertEqual(int(float(2**53+5)), 2**53+4)
299 self
.assertEqual(int(float(2**53+6)), 2**53+6)
300 self
.assertEqual(int(float(2**53+7)), 2**53+8)
302 self
.assertEqual(int(float(-2**53-1)), -2**53)
303 self
.assertEqual(int(float(-2**53-2)), -2**53-2)
304 self
.assertEqual(int(float(-2**53-3)), -2**53-4)
305 self
.assertEqual(int(float(-2**53-5)), -2**53-4)
306 self
.assertEqual(int(float(-2**53-6)), -2**53-6)
307 self
.assertEqual(int(float(-2**53-7)), -2**53-8)
309 self
.assertEqual(int(float(2**54-2)), 2**54-2)
310 self
.assertEqual(int(float(2**54-1)), 2**54)
311 self
.assertEqual(int(float(2**54+2)), 2**54)
312 self
.assertEqual(int(float(2**54+3)), 2**54+4)
313 self
.assertEqual(int(float(2**54+5)), 2**54+4)
314 self
.assertEqual(int(float(2**54+6)), 2**54+8)
315 self
.assertEqual(int(float(2**54+10)), 2**54+8)
316 self
.assertEqual(int(float(2**54+11)), 2**54+12)
318 def test_intconversion(self
):
320 class ClassicMissingMethods
:
322 self
.assertRaises(AttributeError, int, ClassicMissingMethods())
324 class MissingMethods(object):
326 self
.assertRaises(TypeError, int, MissingMethods())
352 self
.assertEqual(int(Foo0()), 42)
353 self
.assertEqual(int(Foo1()), 42)
354 self
.assertEqual(int(Foo2()), 42)
355 self
.assertEqual(int(Foo3()), 0)
356 self
.assertEqual(int(Foo4()), 42L)
357 self
.assertRaises(TypeError, int, Foo5())
361 for base
in (object, Classic
):
362 class IntOverridesTrunc(base
):
367 self
.assertEqual(int(IntOverridesTrunc()), 42)
369 class JustTrunc(base
):
372 self
.assertEqual(int(JustTrunc()), 42)
374 for trunc_result_base
in (object, Classic
):
375 class Integral(trunc_result_base
):
379 class TruncReturnsNonInt(base
):
382 self
.assertEqual(int(TruncReturnsNonInt()), 42)
384 class NonIntegral(trunc_result_base
):
386 # Check that we avoid infinite recursion.
389 class TruncReturnsNonIntegral(base
):
393 int(TruncReturnsNonIntegral())
394 except TypeError as e
:
395 self
.assertEquals(str(e
),
396 "__trunc__ returned non-Integral"
397 " (type NonIntegral)")
399 self
.fail("Failed to raise TypeError with %s" %
400 ((base
, trunc_result_base
),))
403 run_unittest(IntTestCases
)
405 if __name__
== "__main__":