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
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
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
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
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
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
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=257)) with pytest.raises(ValueError): list(Record.split(BYTES, columns=253, tag=Record.TAG_TYPE.DATA_16)) with pytest.raises(ValueError): list(Record.split(BYTES, columns=252, tag=Record.TAG_TYPE.DATA_24)) with pytest.raises(ValueError): list(Record.split(BYTES, columns=251, tag=Record.TAG_TYPE.DATA_32)) 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
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
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
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
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
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)