Skip to content

Commit 94bd18b

Browse files
fixed bug in BitVector __repr__ und __str__
1 parent 8088dc5 commit 94bd18b

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎src/science_mode_4/utils/bit_vector.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ def get_bytes(self) -> bytes:
9595

9696

9797
def __repr__(self) -> str:
98-
return f"{type(self).__name__}(0b{self._data: _b})"
98+
value = int.from_bytes(self.get_bytes(), "little")
99+
return f"{type(self).__name__}(0b{value:_b})"
99100

100101

101102
def __str__(self) -> str:
102-
return "0b" + format(self._data, "_b")
103+
value = int.from_bytes(self.get_bytes(), "little")
104+
return "0b" + format(value, "_b")

‎tests/test_bit_vector.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ def test_get_bytes_pads_partial_last_byte():
101101
def test_iter_yields_all_bits_in_order():
102102
bv = BitVector.init_from_int(0b0110, 4)
103103
assert list(iter(bv)) == [0, 1, 1, 0]
104+
105+
106+
def test_repr_shows_value_as_binary():
107+
bv = BitVector.init_from_int(0x1234, 16)
108+
assert repr(bv) == "BitVector(0b1_0010_0011_0100)"
109+
110+
111+
def test_str_shows_value_as_binary():
112+
bv = BitVector.init_from_int(0b1011, 4)
113+
assert str(bv) == "0b1011"
114+
115+
116+
def test_repr_and_str_of_empty_bitvector():
117+
bv = BitVector()
118+
assert repr(bv) == "BitVector(0b0)"
119+
assert str(bv) == "0b0"

0 commit comments

Comments
 (0)