Example #1
0
 def test_read_blocks_doctest(self):
     blocks = [(0, b'abc'), (16, b'def')]
     stream = io.StringIO()
     MotorolaRecord.write_blocks(stream, blocks)
     stream.seek(0, io.SEEK_SET)
     ans_out = MotorolaRecord.read_blocks(stream)
     ans_ref = blocks
     assert ans_out == ans_ref
Example #2
0
    def test_build_count_doctest(self):
        ans_out = str(Record.build_count(0x1234))
        ans_ref = 'S5031234B6'
        assert ans_out == ans_ref

        ans_out = str(Record.build_count(0x123456))
        ans_ref = 'S6041234565F'
        assert ans_out == ans_ref
Example #3
0
 def test_save_records(self, tmppath, datapath):
     path_out = tmppath / 'bytes.mot'
     path_ref = datapath / 'bytes.mot'
     records = list(Record.split(BYTES))
     Record.save_records(str(path_out), records)
     ans_out = read_text(path_out)
     ans_ref = read_text(path_ref)
     assert ans_out == ans_ref
Example #4
0
 def test_get_metadata(self):
     records = list(Record.split(BYTES, header=b'header', start=0x1234))
     ans_out = Record.get_metadata(records)
     ans_ref = {
         'header': b'header',
         'columns': 16,
         'count': 16,
         'start': 0x1234,
     }
     assert ans_out == ans_ref
Example #5
0
def test_convert_records():
    motorola1 = list(MotorolaRecord.split(BYTES))
    motorola2 = list(MotorolaRecord.split(BYTES))
    converted = convert_records(motorola1, input_type=MotorolaRecord)
    assert converted == motorola2

    intel1 = list(IntelRecord.split(BYTES))
    intel2 = list(IntelRecord.split(BYTES))
    converted = convert_records(intel1, output_type=IntelRecord)
    assert converted == intel2
Example #6
0
def test_convert_records_doctest():
    motorola = list(MotorolaRecord.split(BYTES))
    intel = list(IntelRecord.split(BYTES))
    converted = convert_records(motorola, output_type=IntelRecord)
    assert converted == intel

    motorola = list(MotorolaRecord.split(BYTES))
    intel = list(IntelRecord.split(BYTES))
    converted = convert_records(intel, output_type=MotorolaRecord)
    assert converted == motorola
Example #7
0
 def test_write_memory_doctest(self):
     memory = Memory(blocks=[(0, b'abc'), (16, b'def')])
     stream = io.StringIO()
     MotorolaRecord.write_memory(stream, memory)
     ans_out = stream.getvalue()
     ans_ref = ('S0030000FC\n'
                'S1060000616263D3\n'
                'S1060010646566BA\n'
                'S5030002FA\n'
                'S9030000FC\n')
     assert ans_out == ans_ref
Example #8
0
 def test_write_records_doctest(self):
     blocks = [(0, b'abc'), (16, b'def')]
     records = blocks_to_records(blocks, MotorolaRecord)
     stream = io.StringIO()
     MotorolaRecord.write_records(stream, records)
     ans_out = stream.getvalue()
     ans_ref = ('S0030000FC\n'
                'S1060000616263D3\n'
                'S1060010646566BA\n'
                'S5030002FA\n'
                'S9030000FC\n')
     assert ans_out == ans_ref
Example #9
0
 def test_read_records_doctest(self):
     blocks = [(0, b'abc'), (16, b'def')]
     stream = io.StringIO()
     MotorolaRecord.write_blocks(stream, blocks)
     stream.seek(0, io.SEEK_SET)
     records = MotorolaRecord.read_records(stream)
     ans_out = list(map(str, records))
     ans_ref = [
         'S0030000FC', 'S1060000616263D3', 'S1060010646566BA', 'S5030002FA',
         'S9030000FC'
     ]
     assert ans_out == ans_ref
Example #10
0
 def test_save_memory_doctest(self, tmppath):
     path = str(tmppath / 'save_memory.mot')
     blocks = [(0, b'abc'), (16, b'def')]
     MotorolaRecord.save_blocks(path, blocks)
     with open(path, 'rt') as f:
         ans_out = f.read()
     ans_ref = ('S0030000FC\n'
                'S1060000616263D3\n'
                'S1060010646566BA\n'
                'S5030002FA\n'
                'S9030000FC\n')
     assert ans_out == ans_ref
Example #11
0
    def test_build_standalone(self):
        ans_out = list(
            Record.build_standalone(
                [],
                tag=Tag.DATA_32,
                header=b'Hello, World!',
                start=0,
            ))
        ans_ref = [
            Record(0, Tag.HEADER, b'Hello, World!'),
            Record(0, Tag.COUNT_16, b'\x00\x00'),
            Record(0, Tag.START_32, b''),
        ]
        assert ans_out == ans_ref

        ans_out = list(
            Record.build_standalone(
                [],
                tag=Tag.DATA_32,
                header=b'Hello, World!',
            ))
        ans_ref = [
            Record(0, Tag.HEADER, b'Hello, World!'),
            Record(0, Tag.COUNT_16, b'\x00\x00'),
            Record(0, Tag.START_32, b''),
        ]
        assert ans_out == ans_ref
Example #12
0
    def test___eq___doctest(self):
        record1 = BinaryRecord.build_data(0, b'Hello, World!')
        record2 = BinaryRecord.build_data(0, b'Hello, World!')
        assert record1 == record2

        record1 = BinaryRecord.build_data(0, b'Hello, World!')
        record2 = BinaryRecord.build_data(1, b'Hello, World!')
        assert record1 != record2

        record1 = BinaryRecord.build_data(0, b'Hello, World!')
        record2 = BinaryRecord.build_data(0, b'hello, world!')
        assert record1 != record2

        record1 = MotorolaRecord.build_header(b'Hello, World!')
        record2 = MotorolaRecord.build_data(0, b'hello, world!')
        assert record1 != record2
Example #13
0
def test_load_records(tmppath):
    path = str(tmppath / 'bytes.mot')
    records = list(MotorolaRecord.split(BYTES))
    save_records(path, records)
    ans_out = load_records(path, MotorolaRecord)
    ans_ref = records
    assert ans_out == ans_ref
Example #14
0
 def test_build_data_max_columns(self):
     for tag in (1, 2, 3):
         tag = Tag(tag)
         max_columns = Record.TAG_TO_COLUMN_SIZE[tag]
         record = Record.build_data(0x1234, bytes(max_columns), tag=tag)
         assert len(record.data) == max_columns
         assert record.count == 255
Example #15
0
    def test_fit_count_tag_doctest(self):
        ans_out = repr(Record.fit_count_tag(0x0000000))
        ans_ref = '<Tag.COUNT_16: 5>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_count_tag(0x00FFFF))
        ans_ref = '<Tag.COUNT_16: 5>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_count_tag(0x010000))
        ans_ref = '<Tag.COUNT_24: 6>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_count_tag(0xFFFFFF))
        ans_ref = '<Tag.COUNT_24: 6>'
        assert ans_out == ans_ref
Example #16
0
    def test_build_terminator_doctest(self):
        ans_out = str(Record.build_terminator(0x1234))
        ans_ref = 'S9031234B6'
        assert ans_out == ans_ref

        ans_out = str(Record.build_terminator(0x1234, Tag.DATA_16))
        ans_ref = 'S9031234B6'
        assert ans_out == ans_ref

        ans_out = str(Record.build_terminator(0x123456, Tag.DATA_24))
        ans_ref = 'S8041234565F'
        assert ans_out == ans_ref

        ans_out = str(Record.build_terminator(0x12345678, Tag.DATA_32))
        ans_ref = 'S70512345678E6'
        assert ans_out == ans_ref
Example #17
0
    def test_fit_data_tag_doctest(self):
        ans_out = repr(Record.fit_data_tag(0x00000000))
        ans_ref = '<Tag.DATA_16: 1>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_data_tag(0x0000FFFF))
        ans_ref = '<Tag.DATA_16: 1>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_data_tag(0x00010000))
        ans_ref = '<Tag.DATA_16: 1>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_data_tag(0x00FFFFFF))
        ans_ref = '<Tag.DATA_24: 2>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_data_tag(0x01000000))
        ans_ref = '<Tag.DATA_24: 2>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_data_tag(0xFFFFFFFF))
        ans_ref = '<Tag.DATA_32: 3>'
        assert ans_out == ans_ref

        ans_out = repr(Record.fit_data_tag(0x100000000))
        ans_ref = '<Tag.DATA_32: 3>'
        assert ans_out == ans_ref
Example #18
0
def test_find_corrupted_records_doctest():
    data = bytes(range(256))
    records = list(MotorolaRecord.split(data))
    records[3].checksum ^= 0xFF
    records[5].checksum ^= 0xFF
    records[7].checksum ^= 0xFF
    ans_out = find_corrupted_records(records)
    ans_ref = [3, 5, 7]
    assert ans_out == ans_ref
Example #19
0
def test_convert_file_doctest(tmppath):
    path_mot = str(tmppath / 'bytes.mot')
    path_hex = str(tmppath / 'bytes.hex')
    motorola = list(MotorolaRecord.split(BYTES))
    intel = list(IntelRecord.split(BYTES))
    save_records(path_mot, motorola)
    convert_file(path_mot, path_hex)
    ans_out = load_records(path_hex)
    ans_ref = intel
    assert ans_out == ans_ref
Example #20
0
    def test_get_metadata(self):
        ans_out = Record.get_metadata([])
        ans_ref = dict(columns=0)
        assert ans_out == ans_ref

        data = bytes(range(256))
        records = list(MotorolaRecord.split(data))
        ans_out = Record.get_metadata(records)
        ans_ref = dict(columns=16)
        assert ans_out == ans_ref
Example #21
0
    def test_build_data_doctest(self):
        ans_out = str(Record.build_data(0x1234, b'Hello, World!'))
        ans_ref = 'S110123448656C6C6F2C20576F726C642140'
        assert ans_out == ans_ref

        ans_out = str(
            Record.build_data(0x1234, b'Hello, World!', tag=Tag.DATA_16))
        ans_ref = 'S110123448656C6C6F2C20576F726C642140'
        assert ans_out == ans_ref

        ans_out = str(
            Record.build_data(0x123456, b'Hello, World!', tag=Tag.DATA_24))
        ans_ref = 'S21112345648656C6C6F2C20576F726C6421E9'
        assert ans_out == ans_ref

        ans_out = str(
            Record.build_data(0x12345678, b'Hello, World!', tag=Tag.DATA_32))
        ans_ref = 'S3121234567848656C6C6F2C20576F726C642170'
        assert ans_out == ans_ref
Example #22
0
 def test_load_blocks_doctest(self, tmppath):
     path = str(tmppath / 'load_blocks.mot')
     with open(path, 'wt') as f:
         f.write('S0030000FC\n')
         f.write('S1060000616263D3\n')
         f.write('S1060010646566BA\n')
         f.write('S5030002FA\n')
         f.write('S9030000FC\n')
     ans_out = MotorolaRecord.load_blocks(path)
     ans_ref = [(0, b'abc'), (16, b'def')]
     assert ans_out == ans_ref
Example #23
0
 def test_load_records_doctest(self, tmppath):
     path = str(tmppath / 'load_records.mot')
     with open(path, 'wt') as f:
         f.write('S0030000FC\n')
         f.write('S1060000616263D3\n')
         f.write('S1060010646566BA\n')
         f.write('S5030002FA\n')
         f.write('S9030000FC\n')
     records = MotorolaRecord.load_records(path)
     ans_out = list(map(str, records))
     ans_ref = [
         'S0030000FC', 'S1060000616263D3', 'S1060010646566BA', 'S5030002FA',
         'S9030000FC'
     ]
     assert ans_out == ans_ref
Example #24
0
    def test_check(self):
        record = Record.build_data(0, b'')
        for tag in range(len(Tag), 256):
            record.tag = tag
            record.update_checksum()
            with pytest.raises(ValueError):
                record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.check()
        record.tag = len(Tag)
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_header(b'')
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError, match='address error'):
            record.check()

        record = Record.build_count(0x1234)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError, match='address error'):
            record.check()

        record = Record.build_count(0x123456)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError, match='address error'):
            record.check()

        record = Record.build_data(0, bytearray(252))
        record.data.append(1)
        record.update_count()
        record.update_checksum()
        with pytest.raises(ValueError, match='count overflow'):
            record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.count += 1
        record.update_checksum()
        with pytest.raises(ValueError, match='count error'):
            record.check()
Example #25
0
def test_save_records(tmppath):
    path = str(tmppath / 'bytes.hex')
    records = list(IntelRecord.split(BYTES))
    save_records(path, records, IntelRecord)
    ans_out = load_records(path)
    ans_ref = records
    assert ans_out == ans_ref

    records = []
    save_records(path, records, IntelRecord)
    ans_out = load_records(path)
    ans_ref = records
    assert ans_out == ans_ref

    path = str(tmppath / 'bytes.mot')
    intel = list(IntelRecord.split(BYTES))
    motorola = list(MotorolaRecord.split(BYTES))
    save_records(path, intel, MotorolaRecord)
    ans_out = load_records(path)
    ans_ref = motorola
    assert ans_out == ans_ref
Example #26
0
    def test_check(self):
        record = Record.build_data(0, b'')
        for tag in range(len(Tag), 256):
            record.tag = tag
            record.update_checksum()
            with pytest.raises(ValueError):
                record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.check()
        record.tag = len(Tag)
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_header(b'')
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_count(0x1234)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_count(0x123456)
        record.address = 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()

        record = Record.build_data(0, b'Hello, World!')
        record.count += 1
        record.update_checksum()
        with pytest.raises(ValueError):
            record.check()
Example #27
0
 def test_load_records(self, datapath):
     path_ref = datapath / 'bytes.mot'
     ans_out = list(Record.load_records(str(path_ref)))
     ans_ref = list(Record.split(BYTES))
     assert ans_out == ans_ref
Example #28
0
    def test_fix_tags(self):
        ans_out = []
        Record.fix_tags(ans_out)
        assert ans_out == []

        ans_out = [
            Record(0, Tag.HEADER, b'Hello, World!'),
            Record(0x12345678, Tag.DATA_16, HEXBYTES),
            Record(0, Tag.COUNT_16, b'\x12\x34'),
            Record(0, Tag.COUNT_16, b'\x12\x34\x56'),
            Record(0x1234, Tag.START_16, b''),
        ]
        Record.fix_tags(ans_out)
        ans_ref = [
            Record(0, Tag.HEADER, b'Hello, World!'),
            Record(0x12345678, Tag.DATA_32, HEXBYTES),
            Record(0, Tag.COUNT_16, b'\x12\x34'),
            Record(0, Tag.COUNT_24, b'\x12\x34\x56'),
            Record(0x1234, Tag.START_32, b''),
        ]
        assert ans_out == ans_ref
Example #29
0
    def test_split(self):
        with pytest.raises(ValueError):
            list(Record.split(BYTES, address=-1))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, address=(1 << 32)))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, address=((1 << 32) - 128)))

        with pytest.raises(ValueError):
            list(Record.split(BYTES, columns=129))

        ans_out = list(
            Record.split(HEXBYTES,
                         header=b'Hello, World!',
                         start=0,
                         tag=Tag.DATA_16))
        ans_ref = [
            Record(0, Tag.HEADER, b'Hello, World!'),
            Record(0, Tag.DATA_16, HEXBYTES),
            Record(0, Tag.COUNT_16, b'\x00\x01'),
            Record(0, Tag.START_16, b''),
        ]
        assert ans_out == ans_ref

        ans_out = list(
            Record.split(HEXBYTES,
                         standalone=False,
                         address=7,
                         columns=5,
                         align=3))
        ans_ref = [
            Record.build_data(7, HEXBYTES[:4]),
            Record.build_data(11, HEXBYTES[4:9]),
            Record.build_data(16, HEXBYTES[9:14]),
            Record.build_data(21, HEXBYTES[14:]),
        ]
        assert ans_out == ans_ref
Example #30
0
    def test_check_sequence(self):
        records = list(Record.split(BYTES, header=b'Hello, World!'))
        Record.check_sequence(records)

        records = list(Record.split(BYTES, header=b'Hello, World!'))
        assert records[0].tag == Tag.HEADER
        del records[0]
        with pytest.raises(ValueError, match='missing header'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES, header=b'Hello, World!'))
        assert records[0].tag == Tag.HEADER
        records.insert(1, records[0])
        with pytest.raises(ValueError, match='header error'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES, header=b'Hello, World!'))
        assert records[0].tag == Tag.HEADER
        records.insert(1, Record(0, Tag._RESERVED, b''))
        with pytest.raises(ValueError, match='missing count'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES, header=b'Hello, World!'))
        assert records[2].tag == Tag.DATA_16
        records[2].tag = Tag.DATA_24
        records[2].update_count()
        records[2].update_checksum()
        with pytest.raises(ValueError, match='tag error'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[2].tag == Tag.DATA_16
        records[2].address -= 1
        records[2].update_count()
        records[2].update_checksum()
        with pytest.raises(ValueError, match='overlapping records'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[2].tag == Tag.DATA_16
        assert records[-2].tag == Tag.COUNT_16
        del records[2]
        with pytest.raises(ValueError, match='record count error'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-2].tag == Tag.COUNT_16
        del records[-2]
        with pytest.raises(ValueError, match='missing count'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-2].tag == Tag.COUNT_16
        records.insert(-2, records[-2])
        with pytest.raises(ValueError, match='misplaced count'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-2].tag == Tag.COUNT_16
        records[-2].tag = Tag.COUNT_24
        records[-2].data = b'\x00' + records[-2].data
        records[-2].update_count()
        records[-2].update_checksum()
        Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-2].tag == Tag.COUNT_16
        records[-2].tag = Tag.COUNT_24
        records[-2].data = b'\x00' + records[-2].data
        records[-2].update_count()
        records[-2].update_checksum()
        records.insert(-2, records[-2])
        with pytest.raises(ValueError, match='misplaced count'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-2].tag == Tag.COUNT_16
        records[-2].tag = Tag.COUNT_24
        records[-2].data = b'\x00' + records[-2].data
        records[-2].update_count()
        records[-2].update_checksum()
        assert records[2].tag == Tag.DATA_16
        del records[2]
        with pytest.raises(ValueError, match='record count error'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[1].tag == Tag.DATA_16
        assert records[-1].tag == Tag.START_16
        records[-1].tag = Tag.START_24
        records[-1].update_count()
        records[-1].update_checksum()
        with pytest.raises(ValueError):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-1].tag == Tag.START_16
        del records[-1]
        with pytest.raises(ValueError, match='missing start'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-1].tag == Tag.START_16
        records.insert(-1, Record(0, Tag._RESERVED, b''))
        with pytest.raises(ValueError, match='tag error'):
            Record.check_sequence(records)

        records = list(Record.split(BYTES))
        assert records[-1].tag == Tag.START_16
        records.append(Record(0, Tag._RESERVED, b''))
        with pytest.raises(ValueError, match='sequence length error'):
            Record.check_sequence(records)