Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packtype/bitvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BitVector:

def __init__(self, width: int | None = None, value: int = 0) -> None:
self.__width = width
self.__value = 0
self.__value = value

@property
def width(self) -> int:
Expand Down
4 changes: 2 additions & 2 deletions packtype/union.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Union(Assembly):
_PT_WIDTH: int

def __init__(self,
value: int | None = 0,
value: int = 0,
_pt_bv: BitVector | BitVectorWindow | None = None) -> None:
super().__init__(_pt_bv=_pt_bv or BitVector(self._pt_width, value))
super().__init__(_pt_bv=_pt_bv or BitVector(self._pt_width, value=value))
for fname, ftype, fval in self._pt_definitions():
if isinstance(ftype, ArraySpec):
if isinstance(ftype.base, Primitive):
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,41 @@ def test_struct_constructor():
class TestPkg:
pass

@TestPkg.struct()
class InnerA:
ab: Scalar[6]
cd: Scalar[2]

@TestPkg.struct()
class InnerB:
ab: Scalar[2]
cd: Scalar[6]

@TestPkg.union()
class InnerUnion:
a: InnerA
b: InnerB

@TestPkg.struct()
class TestStruct:
ab: Scalar[12]
cd: 3 * Scalar[3]
ef: Scalar[9]
gh: InnerUnion

inst = TestStruct(ab=123, cd=[4, 5, 6], ef=41)
inst = TestStruct(ab=123, cd=[4, 5, 6], ef=41, gh=0x27)
assert int(inst.ab) == 123
assert int(inst.cd[0]) == 4
assert int(inst.cd[1]) == 5
assert int(inst.cd[2]) == 6
assert int(inst.ef) == 41
assert int(inst.gh) == 0x27
assert int(inst.gh.a) == 0x27
assert int(inst.gh.a.ab) == (0x27 & 0x3F)
assert int(inst.gh.a.cd) == (0x27 & 0xC0) >> 6
assert int(inst.gh.b) == 0x27
assert int(inst.gh.b.ab) == (0x27 & 0x03)
assert int(inst.gh.b.cd) == (0x27 & 0xFC) >> 2


def test_struct_bad_constructor():
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class UnionB:
assert int(inst.a.c) == 142
assert int(inst.b) == 142

# Implicit unpacking
inst = UnionB(142)
assert int(inst.a.a) == 142
assert int(inst.a.b) == 142
assert int(inst.a.c) == 142
assert int(inst.b) == 142

# Repacking
assert inst.a._pt_pack() == 142

Expand Down