annotate python/testhexfile.py @ 244:58155c7c4a8e

Add hexutil
author Windel Bouwman
date Wed, 24 Jul 2013 19:47:13 +0200
parents d3dccf12ca88
children 66912720d712
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)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
13
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
14 def testSave(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
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
19 def testEqual(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
20 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
21 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
22 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
23 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
24 self.assertEqual(hf1, hf2)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
25
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
26 def testNotEqual(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
27 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
28 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
29 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
30 hf2.addRegion(10, bytes.fromhex('aabbdc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
31 self.assertNotEqual(hf1, hf2)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
32
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
33 def testNotEqual2(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
34 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
35 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
36 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
37 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
38 hf2.addRegion(22, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
39 self.assertNotEqual(hf1, hf2)
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
40
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
41 def testLoad(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
42 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
43 dummyhex = """
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
44 :01400000aa15
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
45 """
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
46 f = io.StringIO(dummyhex)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
47 hf.load(f)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
48 self.assertEqual(1, len(hf.regions))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
49 self.assertEqual(0x4000, hf.regions[0].address)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
50 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
51
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
52 def testIncorrectCrc(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
53 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
54 txt = ":01400000aabb"
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
55 f = io.StringIO(txt)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
56 with self.assertRaisesRegex(HexFileException, 'crc'):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
57 hf.load(f)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
58
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
59 def testIncorrectLength(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
60 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
61 txt = ":0140002200aabb"
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
62 f = io.StringIO(txt)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
63 with self.assertRaisesRegex(HexFileException, 'count'):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
64 hf.load(f)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
65
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
66 if __name__ == '__main__':
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
67 unittest.main()
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
68