annotate test/testhexfile.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 534b94b40aa8
children e609d5296ee9
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
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
3 from utils 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
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
41 def testTwoRegions(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
42 hf = HexFile()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
43 hf2 = HexFile()
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
44 hf.addRegion(0x100, bytes.fromhex('abcd'))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
45 hf.addRegion(0x200, bytes.fromhex('beef'))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
46 hf2.addRegion(0x200, bytes.fromhex('beef'))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
47 hf2.addRegion(0x100, bytes.fromhex('abcd'))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
48 self.assertEqual(hf, hf2)
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
49
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
50 def testMerge(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
51 hf = HexFile()
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
52 hf.addRegion(0x10, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
53 hf.addRegion(0x13, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
54 self.assertEqual(1, len(hf.regions))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
55
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
56 def testOverlapped(self):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
57 hf = HexFile()
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
58 hf.addRegion(0x10, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
59 with self.assertRaisesRegex(HexFileException, 'verlap'):
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
60 hf.addRegion(0x12, bytes.fromhex('abcdab'))
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
61
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
62 def testEqual(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
63 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
64 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
65 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
66 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
67 self.assertEqual(hf1, hf2)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
68
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
69 def testNotEqual(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
70 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
71 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
72 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
73 hf2.addRegion(10, bytes.fromhex('aabbdc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
74 self.assertNotEqual(hf1, hf2)
245
66912720d712 Added grinder
Windel Bouwman
parents: 244
diff changeset
75
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
76 def testNotEqual2(self):
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
77 hf1 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
78 hf2 = HexFile()
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
79 hf1.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
80 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
81 hf2.addRegion(22, bytes.fromhex('aabbcc'))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
82 self.assertNotEqual(hf1, hf2)
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
83
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
84 def testLoad(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
85 hf = HexFile()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 284
diff changeset
86 dummyhex = """:01400000aa15"""
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
87 f = io.StringIO(dummyhex)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
88 hf.load(f)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
89 self.assertEqual(1, len(hf.regions))
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
90 self.assertEqual(0x4000, hf.regions[0].address)
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
91 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
92
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
93 def testIncorrectCrc(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 = ":01400000aabb"
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, 'crc'):
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 def testIncorrectLength(self):
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
101 hf = HexFile()
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
102 txt = ":0140002200aabb"
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
103 f = io.StringIO(txt)
244
58155c7c4a8e Add hexutil
Windel Bouwman
parents: 233
diff changeset
104 with self.assertRaisesRegex(HexFileException, 'count'):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
105 hf.load(f)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
106
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
107 if __name__ == '__main__':
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents:
diff changeset
108 unittest.main()