233
|
1 import unittest
|
|
2 import io
|
244
|
3 from hexfile import HexFile, HexFileException
|
233
|
4
|
|
5
|
|
6 class testHexFile(unittest.TestCase):
|
244
|
7 def saveload(self, hf):
|
|
8 f = io.StringIO()
|
|
9 hf.save(f)
|
|
10 hf2 = HexFile()
|
|
11 hf2.load(io.StringIO(f.getvalue()))
|
|
12 self.assertEqual(hf, hf2)
|
|
13
|
233
|
14 def testSave(self):
|
244
|
15 hf = HexFile()
|
|
16 hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
|
|
17 self.saveload(hf)
|
|
18
|
|
19 def testEqual(self):
|
|
20 hf1 = HexFile()
|
|
21 hf2 = HexFile()
|
|
22 hf1.addRegion(10, bytes.fromhex('aabbcc'))
|
|
23 hf2.addRegion(10, bytes.fromhex('aabbcc'))
|
|
24 self.assertEqual(hf1, hf2)
|
|
25
|
|
26 def testNotEqual(self):
|
|
27 hf1 = HexFile()
|
|
28 hf2 = HexFile()
|
|
29 hf1.addRegion(10, bytes.fromhex('aabbcc'))
|
|
30 hf2.addRegion(10, bytes.fromhex('aabbdc'))
|
|
31 self.assertNotEqual(hf1, hf2)
|
|
32
|
|
33 def testNotEqual2(self):
|
|
34 hf1 = HexFile()
|
|
35 hf2 = HexFile()
|
|
36 hf1.addRegion(10, bytes.fromhex('aabbcc'))
|
|
37 hf2.addRegion(10, bytes.fromhex('aabbcc'))
|
|
38 hf2.addRegion(22, bytes.fromhex('aabbcc'))
|
|
39 self.assertNotEqual(hf1, hf2)
|
233
|
40
|
|
41 def testLoad(self):
|
244
|
42 hf = HexFile()
|
233
|
43 dummyhex = """
|
|
44 :01400000aa15
|
|
45 """
|
|
46 f = io.StringIO(dummyhex)
|
|
47 hf.load(f)
|
244
|
48 self.assertEqual(1, len(hf.regions))
|
|
49 self.assertEqual(0x4000, hf.regions[0].address)
|
|
50 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
|
233
|
51
|
|
52 def testIncorrectCrc(self):
|
244
|
53 hf = HexFile()
|
233
|
54 txt = ":01400000aabb"
|
|
55 f = io.StringIO(txt)
|
244
|
56 with self.assertRaisesRegex(HexFileException, 'crc'):
|
233
|
57 hf.load(f)
|
|
58
|
|
59 def testIncorrectLength(self):
|
244
|
60 hf = HexFile()
|
233
|
61 txt = ":0140002200aabb"
|
|
62 f = io.StringIO(txt)
|
244
|
63 with self.assertRaisesRegex(HexFileException, 'count'):
|
233
|
64 hf.load(f)
|
|
65
|
|
66 if __name__ == '__main__':
|
|
67 unittest.main()
|
|
68
|