diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2008-12-17 16:19:07 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2008-12-17 16:19:07 (GMT) |
commit | 54bc1ec4c7689ceab900f453fdd4c8cf5a308e59 (patch) | |
tree | 87dd2371d042f3b8e63a32a4bda589ccce0b2303 /Lib/test | |
parent | 81c93fb45c4076506a5ab3d89aeec1f42e0e6be2 (diff) | |
download | cpython-54bc1ec4c7689ceab900f453fdd4c8cf5a308e59.zip cpython-54bc1ec4c7689ceab900f453fdd4c8cf5a308e59.tar.gz cpython-54bc1ec4c7689ceab900f453fdd4c8cf5a308e59.tar.bz2 |
Forward merge of r67822 to py3k: add bit_length method to int.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_long.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index a1db26a..cbd0b2b 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -3,6 +3,7 @@ from test import support import sys import random +import math # Used for lazy formatting of failure messages class Frm(object): @@ -836,6 +837,41 @@ class LongTest(unittest.TestCase): self.assertTrue(i - i is 0) self.assertTrue(0 * i is 0) + def test_bit_length(self): + tiny = 1e-10 + for x in range(-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.assert_(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, 234]: + 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) + + def test_main(): support.run_unittest(LongTest) |