Skip to content

Commit

Permalink
Fix fill for all word sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Dec 18, 2019
1 parent 810814f commit e359d7d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
24 changes: 15 additions & 9 deletions bincopy.py
Expand Up @@ -21,7 +21,7 @@


__author__ = 'Erik Moqvist'
__version__ = '16.1.3'
__version__ = '16.2.0'


DEFAULT_WORD_SIZE_BITS = 8
Expand Down Expand Up @@ -1331,26 +1331,32 @@ def format_line(address, data):

return '\n'.join(lines) + '\n'

def fill(self, value=b'\xff', max_words=None):
"""Fill all empty space between segments with given value `value`.
`value` value which is used to fill the empty space.
`max_words` maximal number of words to fill between the segments. Empty space
which is larger than this is not touched. If `None` is given, all empty space
is filled.
def fill(self, value=None, max_words=None):
"""Fill all empty space between segments.
`value` is the value which is used to fill the empty space. By
default the value is ``b'\\xff' * word_size_bytes``.
`max_words` is the maximum number of words to fill between the
segments. Empty space which larger than this is not
touched. If ``None``, all empty space is filled.
"""

if value is None:
value = b'\xff' * self.word_size_bytes

previous_segment_maximum_address = None
fill_segments = []

for address, data in self._segments:
address *= self.word_size_bytes
maximum_address = address + len(data)

if previous_segment_maximum_address is not None:
fill_size = address - previous_segment_maximum_address
fill_size_words = fill_size // self.word_size_bytes

if max_words is None or fill_size_words <= max_words:
fill_segments.append(_Segment(
previous_segment_maximum_address,
Expand Down
36 changes: 31 additions & 5 deletions tests/test_bincopy.py
Expand Up @@ -1105,22 +1105,48 @@ def test_fill(self):
binfile.fill()
self.assertEqual(binfile.as_binary(), b'')

# Add some data and fill again
# Add some data and fill again.
binfile.add_binary(b'\x01\x02\x03\x04', address=0)
binfile.add_binary(b'\x01\x02\x03\x04', address=8)
binfile.fill()
self.assertEqual(binfile.as_binary(),
b'\x01\x02\x03\x04\xff\xff\xff\xff\x01\x02\x03\x04')

# Fill with max words
def test_fill_max_words(self):
binfile = bincopy.BinFile()
binfile.add_binary(b'\x01', address=0)
binfile.add_binary(b'\x02', address=2)
binfile.add_binary(b'\x03', address=5)
binfile.add_binary(b'\x04', address=9)
binfile.fill(value=b'\xaa', max_words=2)
self.assertEqual(binfile.as_binary(),
b'\x01\xaa\x02\xaa\xaa\x03\xff\xff\xff\x04')
binfile.fill(b'\xaa', max_words=2)
self.assertEqual(len(binfile.segments), 2)
self.assertEqual(binfile.segments[0].address, 0)
self.assertEqual(binfile.segments[0].data, b'\x01\xaa\x02\xaa\xaa\x03')
self.assertEqual(binfile.segments[1].address, 9)
self.assertEqual(binfile.segments[1].data, b'\x04')

def test_fill_word_size_16(self):
binfile = bincopy.BinFile(word_size_bits=16)
binfile.add_binary(b'\x01\x02', address=0)
binfile.add_binary(b'\x03\x04', address=2)
binfile.add_binary(b'\x05\x06', address=5)
binfile.add_binary(b'\x07\x08', address=9)
binfile.fill(b'\xaa\xaa', max_words=2)
self.assertEqual(len(binfile.segments), 2)
self.assertEqual(binfile.segments[0].address, 0)
self.assertEqual(binfile.segments[0].data,
b'\x01\x02\xaa\xaa\x03\x04\xaa\xaa\xaa\xaa\x05\x06')
self.assertEqual(binfile.segments[1].address, 9)
self.assertEqual(binfile.segments[1].data,
b'\x07\x08')

# Fill the rest with the default value.
binfile.fill()
self.assertEqual(len(binfile.segments), 1)
self.assertEqual(
binfile.as_binary(),
(b'\x01\x02\xaa\xaa\x03\x04\xaa\xaa\xaa\xaa\x05\x06\xff\xff\xff\xff'
b'\xff\xff\x07\x08'))

def test_set_get_item(self):
binfile = bincopy.BinFile()
Expand Down

0 comments on commit e359d7d

Please sign in to comment.