diff options
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r-- | Lib/test/test_int.py | 81 |
1 files changed, 75 insertions, 6 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 51fd195..fa46212 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -2,6 +2,7 @@ import sys import unittest from test.test_support import run_unittest, have_unicode +import math L = [ ('0', 0), @@ -75,15 +76,15 @@ class IntTestCases(unittest.TestCase): s = repr(-1-sys.maxint) x = int(s) self.assertEqual(x+1, -sys.maxint) - self.assert_(isinstance(x, int)) + self.assertIsInstance(x, int) # should return long self.assertEqual(int(s[1:]), sys.maxint+1) # should return long x = int(1e100) - self.assert_(isinstance(x, long)) + self.assertIsInstance(x, long) x = int(-1e100) - self.assert_(isinstance(x, long)) + self.assertIsInstance(x, long) # SF bug 434186: 0x80000000/2 != 0x80000000>>1. @@ -101,11 +102,11 @@ class IntTestCases(unittest.TestCase): self.assertRaises(ValueError, int, '123\x00 245', 20) x = int('1' * 600) - self.assert_(isinstance(x, long)) + self.assertIsInstance(x, long) if have_unicode: x = int(unichr(0x661) * 600) - self.assert_(isinstance(x, long)) + self.assertIsInstance(x, long) self.assertRaises(TypeError, int, 1, 12) @@ -246,6 +247,74 @@ class IntTestCases(unittest.TestCase): self.assertEqual(int('2br45qc', 35), 4294967297L) self.assertEqual(int('1z141z5', 36), 4294967297L) + def test_bit_length(self): + tiny = 1e-10 + for x in xrange(-65000, 65000): + k = x.bit_length() + # Check equivalence with Python version + self.assertEqual(k, len(bin(x).lstrip('-0b'))) + # Behaviour as specified in the docs + if x != 0: + self.assertTrue(2**(k-1) <= abs(x) < 2**k) + else: + self.assertEqual(k, 0) + # Alternative definition: x.bit_length() == 1 + floor(log_2(x)) + if x != 0: + # When x is an exact power of 2, numeric errors can + # cause floor(log(x)/log(2)) to be one too small; for + # small x this can be fixed by adding a small quantity + # to the quotient before taking the floor. + self.assertEqual(k, 1 + math.floor( + math.log(abs(x))/math.log(2) + tiny)) + + self.assertEqual((0).bit_length(), 0) + self.assertEqual((1).bit_length(), 1) + self.assertEqual((-1).bit_length(), 1) + self.assertEqual((2).bit_length(), 2) + self.assertEqual((-2).bit_length(), 2) + for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64]: + a = 2**i + self.assertEqual((a-1).bit_length(), i) + self.assertEqual((1-a).bit_length(), i) + self.assertEqual((a).bit_length(), i+1) + self.assertEqual((-a).bit_length(), i+1) + self.assertEqual((a+1).bit_length(), i+1) + self.assertEqual((-a-1).bit_length(), i+1) + + @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), + "test requires IEEE 754 doubles") + def test_float_conversion(self): + # values exactly representable as floats + exact_values = [-2, -1, 0, 1, 2, 2**52, 2**53-1, 2**53, 2**53+2, + 2**53+4, 2**54-4, 2**54-2, 2**63, -2**63, 2**64, + -2**64, 10**20, 10**21, 10**22] + for value in exact_values: + self.assertEqual(int(float(int(value))), value) + + # test round-half-to-even + self.assertEqual(int(float(2**53+1)), 2**53) + self.assertEqual(int(float(2**53+2)), 2**53+2) + self.assertEqual(int(float(2**53+3)), 2**53+4) + self.assertEqual(int(float(2**53+5)), 2**53+4) + self.assertEqual(int(float(2**53+6)), 2**53+6) + self.assertEqual(int(float(2**53+7)), 2**53+8) + + self.assertEqual(int(float(-2**53-1)), -2**53) + self.assertEqual(int(float(-2**53-2)), -2**53-2) + self.assertEqual(int(float(-2**53-3)), -2**53-4) + self.assertEqual(int(float(-2**53-5)), -2**53-4) + self.assertEqual(int(float(-2**53-6)), -2**53-6) + self.assertEqual(int(float(-2**53-7)), -2**53-8) + + self.assertEqual(int(float(2**54-2)), 2**54-2) + self.assertEqual(int(float(2**54-1)), 2**54) + self.assertEqual(int(float(2**54+2)), 2**54) + self.assertEqual(int(float(2**54+3)), 2**54+4) + self.assertEqual(int(float(2**54+5)), 2**54+4) + self.assertEqual(int(float(2**54+6)), 2**54+8) + self.assertEqual(int(float(2**54+10)), 2**54+8) + self.assertEqual(int(float(2**54+11)), 2**54+12) + def test_intconversion(self): # Test __int__() class ClassicMissingMethods: @@ -323,7 +392,7 @@ class IntTestCases(unittest.TestCase): try: int(TruncReturnsNonIntegral()) except TypeError as e: - self.assertEquals(str(e), + self.assertEqual(str(e), "__trunc__ returned non-Integral" " (type NonIntegral)") else: |