Example #1
0
 def test_save_records(self, tmppath, datapath):
     path_out = tmppath / 'hexbytes.bin'
     path_ref = datapath / 'hexbytes.bin'
     records = [Record.build_data(0, HEXBYTES)]
     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 #2
0
    def test_overlaps_doctest(self):
        record1 = BinaryRecord(0, None, b'abc')
        record2 = BinaryRecord(1, None, b'def')
        assert record1.overlaps(record2)

        record1 = BinaryRecord(0, None, b'abc')
        record2 = BinaryRecord(3, None, b'def')
        assert not record1.overlaps(record2)
Example #3
0
    def test_build_standalone(self):
        data = bytes(range(256))
        blocks = list(chop_blocks(data, 16))
        records = blocks_to_records(blocks, BinaryRecord)
        data_records = get_data_records(records)
        data_records2 = list(BinaryRecord.build_standalone(records))
        assert data_records == data_records2

        with pytest.raises(ValueError, match='unexpected positional arg'):
            next(BinaryRecord.build_standalone((), Ellipsis))

        with pytest.raises(ValueError, match='unexpected keyword arg'):
            next(BinaryRecord.build_standalone((), _=Ellipsis))
Example #4
0
    def test_is_data_doctest(self):
        record = BinaryRecord(0, None, b'Hello, World!')
        assert record.is_data()

        record = MotorolaRecord(0, MotorolaTag.DATA_16, b'Hello, World!')
        assert record.is_data()

        record = MotorolaRecord(0, MotorolaTag.HEADER, b'Hello, World!')
        assert not record.is_data()

        record = IntelRecord(0, IntelTag.DATA, b'Hello, World!')
        assert record.is_data()

        record = IntelRecord(0, IntelTag.END_OF_FILE, b'')
        assert not record.is_data()
Example #5
0
 def test_build_data_doctest(self):
     record = Record.build_data(0x1234, b'Hello, World!')
     ans_out = normalize_whitespace(repr(record))
     ans_ref = normalize_whitespace('''
     Record(address=0x00001234, tag=None, count=13,
            data=b'Hello, World!', checksum=0x69)
     ''')
     assert ans_out == ans_ref
Example #6
0
    def test_overlaps(self):
        record1 = BinaryRecord(0, None, b'abc')
        record1.address = None
        record2 = BinaryRecord(3, None, b'def')
        assert not record1.overlaps(record2)

        record1 = BinaryRecord(0, None, b'abc')
        record2 = BinaryRecord(3, None, b'def')
        record2.address = None
        assert not record1.overlaps(record2)
Example #7
0
 def test_parse_doctest(self):
     line = '48656C6C 6F2C2057 6F726C64 21'
     record = Record.parse_record(line)
     ans_out = normalize_whitespace(repr(record))
     ans_ref = normalize_whitespace('''
     Record(address=0x00000000, tag=None, count=13,
            data=b'Hello, World!', checksum=0x69)
     ''')
     assert ans_out == ans_ref
Example #8
0
    def test_compute_checksum_doctest(self):
        record = BinaryRecord(0, None, b'Hello, World!')
        assert str(record) == '48656C6C6F2C20576F726C6421'
        assert hex(record.compute_checksum()) == '0x69'

        record = MotorolaRecord(0, MotorolaTag.DATA_16, b'Hello, World!')
        assert str(record) == 'S110000048656C6C6F2C20576F726C642186'
        assert hex(record.compute_checksum()) == '0x86'

        record = IntelRecord(0, IntelTag.DATA, b'Hello, World!')
        assert str(record) == ':0D00000048656C6C6F2C20576F726C64218A'
        assert hex(record.compute_checksum()) == '0x8a'
Example #9
0
    def test_compute_count_doctest(self):
        record = BinaryRecord(0, None, b'Hello, World!')
        assert str(record) == '48656C6C6F2C20576F726C6421'
        assert record.compute_count() == 13

        record = MotorolaRecord(0, MotorolaTag.DATA_16, b'Hello, World!')
        assert str(record) == 'S110000048656C6C6F2C20576F726C642186'
        assert record.compute_count() == 16

        record = IntelRecord(0, IntelTag.DATA, b'Hello, World!')
        assert str(record) == ':0D00000048656C6C6F2C20576F726C64218A'
        assert record.compute_count() == 13
Example #10
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 #11
0
    def test_check(self):
        with pytest.raises(ValueError):
            Record(-1, None, b'Hello, World!').check()

        record = Record(0, None, b'')
        record.data = None
        with pytest.raises(ValueError):
            record.check()

        record = Record(0, None, b'Hello, World!')
        record.checksum = None
        record.check()

        record = Record(0, None, b'Hello, World!')
        record.checksum = -1
        with pytest.raises(ValueError):
            record.check()

        record = Record(0, None, b'Hello, World!')
        record.checksum = 256
        with pytest.raises(ValueError):
            record.check()

        record = Record(0, None, b'')
        record.checksum ^= 0xFF
        with pytest.raises(ValueError):
            record.check()

        record = Record(0, None, b'')
        record.tag = -1
        with pytest.raises(ValueError):
            record.check()

        record = Record(0, None, b'')
        record.count = -1
        with pytest.raises(ValueError):
            record.check()
Example #12
0
 def test_is_data(self):
     assert Record.build_data(0, b'').is_data()
Example #13
0
 def test_load_records(self, datapath):
     path_ref = datapath / 'hexbytes.bin'
     ans_out = list(Record.load_records(str(path_ref)))
     ans_ref = [Record.build_data(0, HEXBYTES)]
     assert ans_out == ans_ref
Example #14
0
    def test_split(self):
        with pytest.raises(ValueError):
            list(Record.split(b'', -1))

        with pytest.raises(ValueError):
            list(Record.split(b'', 1 << 32))

        with pytest.raises(ValueError):
            list(Record.split(b'abc', (1 << 32) - 1))

        ans_out = list(Record.split(BYTES))
        ans_ref = [Record.build_data(0, BYTES)]
        assert ans_out == ans_ref

        ans_out = list(Record.split(BYTES, columns=8))
        ans_ref = [
            Record.build_data(offset, BYTES[offset:(offset + 8)])
            for offset in range(0, 256, 8)
        ]
        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 #15
0
    def test_check_sequence(self):
        record1 = BinaryRecord(0, None, b'abc')
        record2 = BinaryRecord(3, None, b'def')
        records = [record1, record2]
        BinaryRecord.check_sequence(records)

        record2.address = 1
        record2.update_checksum()
        with pytest.raises(ValueError):
            BinaryRecord.check_sequence(records)

        record1.address = 3
        record2.address = 0
        record1.update_checksum()
        record2.update_checksum()
        with pytest.raises(ValueError):
            BinaryRecord.check_sequence(records)
Example #16
0
    def test__get_checksum(self):
        record = BinaryRecord(0, None, b'Hello, World!')
        assert hex(record._get_checksum()) == '0x69'
        record.checksum = None
        assert hex(record._get_checksum()) == '0x69'

        record = MotorolaRecord(0, MotorolaTag.DATA_16, b'Hello, World!')
        assert hex(record._get_checksum()) == '0x86'
        record.checksum = None
        assert hex(record._get_checksum()) == '0x86'

        record = IntelRecord(0, IntelTag.DATA, b'Hello, World!')
        assert hex(record._get_checksum()) == '0x8a'
        record.checksum = None
        assert hex(record._get_checksum()) == '0x8a'