MCPcopy Index your code
hub / github.com/python/cpython / test_bit_length

Method test_bit_length

Lib/test/test_long.py:1143–1175  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1141 self.assertIs(0 * i, 0)
1142
1143 def test_bit_length(self):
1144 tiny = 1e-10
1145 for x in range(-65000, 65000):
1146 k = x.bit_length()
1147 # Check equivalence with Python version
1148 self.assertEqual(k, len(bin(x).lstrip('-0b')))
1149 # Behaviour as specified in the docs
1150 if x != 0:
1151 self.assertTrue(2**(k-1) <= abs(x) < 2**k)
1152 else:
1153 self.assertEqual(k, 0)
1154 # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
1155 if x != 0:
1156 # When x is an exact power of 2, numeric errors can
1157 # cause floor(log(x)/log(2)) to be one too small; for
1158 # small x this can be fixed by adding a small quantity
1159 # to the quotient before taking the floor.
1160 self.assertEqual(k, 1 + math.floor(
1161 math.log(abs(x))/math.log(2) + tiny))
1162
1163 self.assertEqual((0).bit_length(), 0)
1164 self.assertEqual((1).bit_length(), 1)
1165 self.assertEqual((-1).bit_length(), 1)
1166 self.assertEqual((2).bit_length(), 2)
1167 self.assertEqual((-2).bit_length(), 2)
1168 for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]:
1169 a = 2**i
1170 self.assertEqual((a-1).bit_length(), i)
1171 self.assertEqual((1-a).bit_length(), i)
1172 self.assertEqual((a).bit_length(), i+1)
1173 self.assertEqual((-a).bit_length(), i+1)
1174 self.assertEqual((a+1).bit_length(), i+1)
1175 self.assertEqual((-a-1).bit_length(), i+1)
1176
1177 def test_bit_count(self):
1178 for a in range(-1000, 1000):

Callers

nothing calls this directly

Calls 7

absFunction · 0.85
bit_lengthMethod · 0.80
assertTrueMethod · 0.80
binFunction · 0.50
assertEqualMethod · 0.45
lstripMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected