Skip to content

Commit

Permalink
style: remove asserts from bytes_utils.int_to_bytes (#72)
Browse files Browse the repository at this point in the history
* style: remove `assert`s from `bytes_utils.int_to_bytes` 

* style: tidy-up things

* chore: bump version to `0.11.0`

---------

Co-authored-by: Piotr Idzik <[email protected]>
  • Loading branch information
muddi900 and vil02 authored Aug 18, 2024
1 parent be0459c commit 99e7f7f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
16 changes: 10 additions & 6 deletions puzzle_generator/bytes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
BYTEORDER: typing.Literal["little", "big"] = "little"


def int_to_bytes(in_value: int) -> bytes:
assert in_value >= 0 # nosec B101
def byte_length(in_value: int) -> int:
if in_value < 0:
raise ValueError("in_value must be non-negative")
number_of_bytes = in_value.bit_length() // 8
if 8 * number_of_bytes < in_value.bit_length():
number_of_bytes += 1
assert number_of_bytes <= 256 # nosec B101
assert (
number_of_bytes - 1 <= in_value.bit_length() // 8 <= number_of_bytes
) # nosec B101
return number_of_bytes


def int_to_bytes(in_value: int) -> bytes:
number_of_bytes = byte_length(in_value)
if number_of_bytes > 255:
raise ValueError("in_value must be 255 bytes or less")
return number_of_bytes.to_bytes(length=1, byteorder=BYTEORDER) + in_value.to_bytes(
length=number_of_bytes, byteorder=BYTEORDER
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "puzzle-generator"
version = "0.10.2"
version = "0.11.0"
description = "Generates python code representing a puzzle"
authors = ["piotr.idzik <[email protected]>"]
readme = "./puzzle_generator/README.md"
Expand Down
30 changes: 29 additions & 1 deletion tests/test_bytes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,40 @@


@pytest.mark.parametrize(
"in_value", [0, 1, 2, 3, 255, 256, 257, 3239949409384, 10**570]
("in_value", "expected"),
[
(0, 0),
(1, 1),
(2, 1),
(3, 1),
(255, 1),
(256, 2),
(257, 2),
(2 ** (8 * 255) - 1, 255),
(2 ** (8 * 255), 256),
],
)
def test_byte_length(in_value, expected):
assert bu.byte_length(in_value) == expected


def test_byte_length_raises_for_negative_input():
with pytest.raises(ValueError, match="in_value must be non-negative"):
bu.byte_length(-1)


@pytest.mark.parametrize(
"in_value", [0, 1, 2, 3, 255, 256, 257, 3239949409384, 10**570, 2 ** (8 * 255) - 1]
)
def test_int_to_bytes(in_value):
assert bu.bytes_to_int(bu.int_to_bytes(in_value)) == in_value


def test_int_to_bytes_raises_when_input_is_too_big():
with pytest.raises(ValueError, match="in_value must be 255 bytes or less"):
bu.int_to_bytes(2 ** (8 * 255))


@pytest.mark.parametrize(
("in_str", "in_bytes"), itertools.product(utils.STRS, utils.BYTES_LIST)
)
Expand Down

0 comments on commit 99e7f7f

Please sign in to comment.