From eaaabd0ecc9ef06eaf5734908f1476cbbc7565a5 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Wed, 17 Aug 2011 00:19:39 +0530 Subject: Fixes a silly error. --- rsa/common.py | 12 ++++++------ tests/test_common.py | 13 +++++++++++-- tox.ini | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/rsa/common.py b/rsa/common.py index 7801b59..eb95e82 100644 --- a/rsa/common.py +++ b/rsa/common.py @@ -22,6 +22,9 @@ def bit_size(num): Number of bits needed to represent a integer excluding any prefix 0 bits. + As per definition from http://wiki.python.org/moin/BitManipulation and + to match the behavior of the Python 3 API. + :param num: Integer value. If num is 0, returns 0. Only the absolute value of the number is considered. Therefore, signed integers will be abs(num) @@ -67,16 +70,13 @@ def _bit_size(number): def byte_size(number): - """Returns the number of bytes required to hold a specific long number. + """ + Returns the number of bytes required to hold a specific long number. The number of bytes is rounded up. """ - if number == 0: - return 0 - # Does not perform floating-point division and uses built-in divmod - # operator. quanta, mod = divmod(bit_size(number), 8) - if mod: + if mod or number == 0: quanta += 1 return quanta #return int(math.ceil(bit_size(number) / 8.0)) diff --git a/tests/test_common.py b/tests/test_common.py index 9980b15..9b69193 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -16,7 +16,6 @@ class Test_byte(unittest2.TestCase): self.assertRaises(struct.error, byte, 256) self.assertRaises(struct.error, byte, -1) - class Test_byte_size(unittest2.TestCase): def test_values(self): self.assertEqual(byte_size(1 << 1023), 128) @@ -24,7 +23,17 @@ class Test_byte_size(unittest2.TestCase): self.assertEqual(byte_size(1 << 1024), 129) def test_zero(self): - self.assertEqual(byte_size(0), 0) + self.assertEqual(byte_size(0), 1) + self.assertEqual(byte_size(255), 1) + self.assertEqual(byte_size(256), 2) + self.assertEqual(byte_size(0xffff), 2) + self.assertEqual(byte_size(0xffffff), 3) + self.assertEqual(byte_size(0xffffffff), 4) + self.assertEqual(byte_size(0xffffffffff), 5) + self.assertEqual(byte_size(0xffffffffffff), 6) + self.assertEqual(byte_size(0xffffffffffffff), 7) + self.assertEqual(byte_size(0xffffffffffffffff), 8) + def test_bad_type(self): self.assertRaises(TypeError, byte_size, []) diff --git a/tox.ini b/tox.ini index 26ab1c3..5d89f24 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,7 @@ envlist = py25,py26,py27,py32 #,pypy [pytest] -addopts = -n4 -v --cov rsa --cov-report term-missing --doctest-modules +addopts = -n4 -v --cov rsa --cov-report term-missing # --doctest-modules [testenv] commands=py.test [] -- cgit v1.2.1