Add tests for jeb_utils.utils.int_to_bytes
This commit is contained in:
@@ -64,7 +64,8 @@ def int_to_bytes(n: int, signed: bool = False) -> bytes:
|
|||||||
:rtype: bytes
|
:rtype: bytes
|
||||||
'''
|
'''
|
||||||
n = int(n)
|
n = int(n)
|
||||||
length = (n.bit_length() + 7) // 8
|
signed_term = 1 if signed else 0
|
||||||
|
length = (n.bit_length() + 7 + signed_term) // 8
|
||||||
if length == 0:
|
if length == 0:
|
||||||
length = 1
|
length = 1
|
||||||
return n.to_bytes(length, signed=signed)
|
return n.to_bytes(length, signed=signed)
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from src.jeb_utils.utils import int_to_bytes
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('n,signed,expected', [
|
||||||
|
(0, False, b'\x00'),
|
||||||
|
(1, False, b'\x01'),
|
||||||
|
(2, False, b'\x02'),
|
||||||
|
(3, False, b'\x03'),
|
||||||
|
(42, False, b'\x2a'),
|
||||||
|
(254, False, b'\xfe'),
|
||||||
|
(255, False, b'\xff'),
|
||||||
|
(256, False, b'\x01\x00'),
|
||||||
|
(512, False, b'\x02\x00'),
|
||||||
|
(513, False, b'\x02\x01'),
|
||||||
|
|
||||||
|
(0, True, b'\x00'),
|
||||||
|
(1, True, b'\x01'),
|
||||||
|
(2, True, b'\x02'),
|
||||||
|
(3, True, b'\x03'),
|
||||||
|
(42, True, b'\x2a'),
|
||||||
|
(127, True, b'\x7f'),
|
||||||
|
(128, True, b'\x00\x80'),
|
||||||
|
(254, True, b'\x00\xfe'),
|
||||||
|
(255, True, b'\x00\xff'),
|
||||||
|
(256, True, b'\x01\x00'),
|
||||||
|
(512, True, b'\x02\x00'),
|
||||||
|
(513, True, b'\x02\x01'),
|
||||||
|
|
||||||
|
(-1, True, b'\xff'),
|
||||||
|
(-2, True, b'\xfe'),
|
||||||
|
(-3, True, b'\xfd'),
|
||||||
|
(-42, True, b'\xd6'),
|
||||||
|
(-127, True, b'\x81'),
|
||||||
|
(-128, True, b'\xff\x80'),
|
||||||
|
(-254, True, b'\xff\x02'),
|
||||||
|
(-255, True, b'\xff\x01'),
|
||||||
|
(-256, True, b'\xff\x00'),
|
||||||
|
(-510, True, b'\xfe\x02'),
|
||||||
|
(-511, True, b'\xfe\x01'),
|
||||||
|
(-512, True, b'\xfe\x00'),
|
||||||
|
(-513, True, b'\xfd\xff'),
|
||||||
|
])
|
||||||
|
def test_int_to_bytes(n, signed, expected):
|
||||||
|
assert int_to_bytes(n, signed) == expected
|
||||||
Reference in New Issue
Block a user