Skip to content

Commit

Permalink
increased utils test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fchorney committed Feb 25, 2025
1 parent c81d905 commit 1b25663
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/pystructtype/structtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@

from pystructtype import structdataclass

# X = TypeVar("X", int, float, default=int)
#
# @dataclass(frozen=True)
# class Foo[X]:
# foo: int = 1
# bar: X | None = None
#
# a = Foo(bar=2)
# b = Foo[int](foo=2, bar=3)
# c = Foo[float](foo=3, bar=1.2)
# z: Annotated[int, Foo(foo=2)]
# x: Annotated[int, Foo[int](foo=2, bar=3)]
#
# class Bar[X]:
# def __init__(self, foo: int = 1, bar: X | None = None):
# self.foo = foo
# self.bar = bar
#
# d = Bar(bar=2)
# e = Bar[int](foo=2, bar=3)
# f = Bar[float](foo=3, bar=1.2)
#
# y: Annotated[int, Bar(foo=2)]
# u: Annotated[int, Bar[int](foo=2, bar=3)]

T = TypeVar("T", int, float, bytes, default=int)
"""Generic Data Type for TypeMeta Contents"""

Expand Down
7 changes: 4 additions & 3 deletions src/pystructtype/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ def list_chunks(_list: list, n: int) -> Generator[list]:

def int_to_bool_list(data: int | list[int], byte_length: int) -> list[bool]:
"""
Converts an integer or a list of integers into a list of bools representing the bits
Converts an integer or a list of integers into a list of bools representing the bits.
The result is in reverse order so that the Least Significant Bit is in list position 0.
ex. ord("A") or 0b01000001 = [False, True, False, False, False, False, False, True]
ex. ord("A") or 0b01000001 = [True, False, False, False, False, False, True, False]
ex. [ord("A"), ord("B")] = [False, True, False, False, False, False, False, True,
ex. [ord("A"), ord("B")] or [0b01000001, 0b01000010]= [False, True, False, False, False, False, False, True,
False, True, False, False, False, False, True, False]
:param data: Integer(s) to be converted
Expand Down
31 changes: 31 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pystructtype.utils import int_to_bool_list, list_chunks


def test_list_chunks():
data = [1, 2, 3, 4, 5, 6]

assert list(list_chunks(data, 2)) == [[1, 2], [3, 4], [5, 6]]
assert list(list_chunks(data, 3)) == [[1, 2, 3], [4, 5, 6]]
assert list(list_chunks(data, 4)) == [[1, 2, 3, 4], [5, 6]]


def test_int_to_bool_list():
assert int_to_bool_list(ord("A"), 1) == [True, False, False, False, False, False, True, False]
assert int_to_bool_list([ord("A"), ord("B")], 2) == [
False,
True,
False,
False,
False,
False,
True,
False,
True,
False,
False,
False,
False,
False,
True,
False,
]

0 comments on commit 1b25663

Please sign in to comment.