annotate python/testhexfile.py @ 280:02385f62f250

Rework from str interface to Instruction interface
author Windel Bouwman
date Sat, 02 Nov 2013 10:03:26 +0100
parents 046017431c6a
children
rev   line source
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
1 import unittest
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
2 import io
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
3 from hexfile import HexFile, HexFileException
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
4
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
5
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
6 class testHexFile(unittest.TestCase):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
7 def saveload(self, hf):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
8 f = io.StringIO()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
9 hf.save(f)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
10 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
11 hf2.load(io.StringIO(f.getvalue()))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
12 self.assertEqual(hf, hf2)
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
13
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
14 def testSave1(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
15 hf = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
16 hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
17 self.saveload(hf)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
18
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
19 def testSave2(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
20 hf = HexFile()
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
21 hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
22 hf.addRegion(0x118000, bytes.fromhex('aabbcc'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
23 self.saveload(hf)
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
24
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
25 def testSave3(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
26 hf = HexFile()
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
27 hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
28 hf.addRegion(0xFFFE, bytes.fromhex('aabbcc'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
29 self.saveload(hf)
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
30
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
31 def testSave4(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
32 hf = HexFile()
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 245
diff changeset
33 hf.addRegion(0xF000, bytes.fromhex('ab')*0x10000)
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
34 self.saveload(hf)
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
35
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
36 def testSave5(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
37 hf = HexFile()
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 245
diff changeset
38 hf.addRegion(0xF003, bytes.fromhex('ab')*0x10000)
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
39 self.saveload(hf)
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
40
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
41 def testMerge(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
42 hf = HexFile()
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
43 hf.addRegion(0x10, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
44 hf.addRegion(0x13, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
45 self.assertEqual(1, len(hf.regions))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
46
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
47 def testOverlapped(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
48 hf = HexFile()
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
49 hf.addRegion(0x10, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
50 with self.assertRaisesRegex(HexFileException, 'verlap'):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
51 hf.addRegion(0x12, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
52
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
53 def testEqual(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
54 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
55 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
56 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
57 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
58 self.assertEqual(hf1, hf2)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
59
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
60 def testNotEqual(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
61 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
62 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
63 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
64 hf2.addRegion(10, bytes.fromhex('aabbdc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
65 self.assertNotEqual(hf1, hf2)
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
66
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
67 def testNotEqual2(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
68 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
69 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
70 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
71 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
72 hf2.addRegion(22, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
73 self.assertNotEqual(hf1, hf2)
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
74
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
75 def testLoad(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
76 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
77 dummyhex = """
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
78 :01400000aa15
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
79 """
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
80 f = io.StringIO(dummyhex)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
81 hf.load(f)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
82 self.assertEqual(1, len(hf.regions))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
83 self.assertEqual(0x4000, hf.regions[0].address)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
84 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
85
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
86 def testIncorrectCrc(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
87 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
88 txt = ":01400000aabb"
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
89 f = io.StringIO(txt)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
90 with self.assertRaisesRegex(HexFileException, 'crc'):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
91 hf.load(f)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
92
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
93 def testIncorrectLength(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
94 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
95 txt = ":0140002200aabb"
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
96 f = io.StringIO(txt)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
97 with self.assertRaisesRegex(HexFileException, 'count'):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
98 hf.load(f)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
99
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
100 if __name__ == '__main__':
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
101 unittest.main()
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
102