Skip to content

Commit

Permalink
Merge pull request #31 from quentinlampin/bugfix-buffer-add-2
Browse files Browse the repository at this point in the history
[bugfix] buffer concatenation fix
  • Loading branch information
quentinlampin authored Jan 14, 2025
2 parents 685f7e7 + 1bf74c9 commit 8bddf0f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
23 changes: 12 additions & 11 deletions microschc/binary/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,17 @@ def __add__(self, other: 'Buffer') -> 'Buffer':
new_content = left.content[0:-1] + (left.content[-1] + right.content[0]).to_bytes(1, 'big') + right.content[1:]
else:
# right padding is not aligned with left padding
if right.padding_length > left.padding_length:
if 8 - left.padding_length < right.padding_length:
bit_shift: int = abs(right.padding_length - left.padding_length)
# shift right to the left, careful with the carry that will spill over right's left boundary
new_content: bytes = b''
carry: int = 0
for b in right.content[::-1]:
sb:int = ((b << bit_shift) & 0xff) + carry
new_content = sb.to_bytes(1, 'big') + new_content
carry = b >> (8-bit_shift)
new_content = left.content[0:-1] + (left.content[-1] + carry).to_bytes(1, 'big') + new_content
else:
bit_shift: int = 8 - left.padding_length - right.padding_length
new_content: bytes = b''
# shift right to the right
Expand All @@ -329,16 +339,7 @@ def __add__(self, other: 'Buffer') -> 'Buffer':
carry = (b & carry_mask) << (8 - bit_shift)
new_content += sb.to_bytes(1, 'big')
new_content = left.content[0:-1] + (left.content[-1] + new_content[0]).to_bytes(1, 'big') + new_content[1:] + carry.to_bytes(1, 'big')
else:
bit_shift: int = abs(right.padding_length - left.padding_length)
# shift right to the left, careful with the carry that will spill over right's left boundary
new_content: bytes = b''
carry: int = 0
for b in right.content[::-1]:
sb:int = ((b << bit_shift) & 0xff) + carry
new_content = sb.to_bytes(1, 'big') + new_content
carry = b >> (8-bit_shift)
new_content = left.content[0:-1] + (left.content[-1] + carry).to_bytes(1, 'big') + new_content

else:
# right is right padded
# shift right to the right
Expand Down
32 changes: 13 additions & 19 deletions tests/binary/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ def test_add():
expected: Buffer = Buffer(content=b'\xff\xff\xff\xf8', length=29, padding=Padding.RIGHT)
assert left_right == expected

left: Buffer = Buffer(content=b'\x40', length=5, padding=Padding.RIGHT)
right: Buffer = Buffer(content=b'\x00', length=1, padding=Padding.LEFT)
left_right: Buffer = left + right
expected: Buffer = Buffer(content=b'\x40', length=6, padding=Padding.RIGHT)
assert left_right == expected

left: Buffer = Buffer(content=b'\x00\x8a\xf8', length=23, padding=Padding.RIGHT)
right: Buffer = Buffer(content=b'\x22', length=6, padding=Padding.LEFT)
left_right: Buffer = left + right
expected: Buffer = Buffer(content=b'\x00\x8a\xf9\x10', length=29, padding=Padding.RIGHT)
assert left_right == expected

def test_or():
# 0x08 0x68
Expand Down Expand Up @@ -371,25 +382,8 @@ def test_and():
buffer_1_and_1_left: Buffer = buffer_1 & buffer_1_pad_left
expected_1_and_1_left: Buffer = Buffer(content=b'\x08\x68', length=13, padding=Padding.RIGHT)
assert buffer_1_and_1_left == expected_1_and_1_left

def test_complement():
buffer: Buffer = Buffer(content=b'\x01\xff\xf0', length=24)
complement = ~buffer
expected_buffer = Buffer(content=b'\xfe\x00\x0f', length=24)
assert complement == expected_buffer

def test_complement_padding_left():
buffer: Buffer = Buffer(content=b'\x0f\xff\xf0', length=20)
complement = ~buffer
expected_buffer = Buffer(content=b'\x00\x00\x0f', length=20)
assert complement == expected_buffer

def test_complement_padding_right():
buffer: Buffer = Buffer(content=b'\x0f\xff\x80', length=20, padding=Padding.RIGHT)
complement = ~buffer
expected_buffer = Buffer(content=b'\xf0\x00\x70', length=20, padding=Padding.RIGHT)
assert complement == expected_buffer



def test_value():

buffer: Buffer = Buffer(content=b'\x00\x01', length=16)
Expand Down

0 comments on commit 8bddf0f

Please sign in to comment.