Mercurial > lcfOS
annotate test/testhexfile.py @ 369:5333318ee33d
Python 3.2 compatible issues
author | Windel Bouwman |
---|---|
date | Fri, 21 Mar 2014 11:54:50 +0100 |
parents | b4ac28efcdf4 |
children |
rev | line source |
---|---|
233 | 1 import unittest |
2 import io | |
292 | 3 from utils 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) | |
245 | 13 |
14 def testSave1(self): | |
244 | 15 hf = HexFile() |
16 hf.addRegion(0x8000, bytes.fromhex('aabbcc')) | |
17 self.saveload(hf) | |
18 | |
245 | 19 def testSave2(self): |
20 hf = HexFile() | |
21 hf.addRegion(0x8000, bytes.fromhex('aabbcc')) | |
22 hf.addRegion(0x118000, bytes.fromhex('aabbcc')) | |
23 self.saveload(hf) | |
24 | |
25 def testSave3(self): | |
26 hf = HexFile() | |
27 hf.addRegion(0x8000, bytes.fromhex('aabbcc')) | |
28 hf.addRegion(0xFFFE, bytes.fromhex('aabbcc')) | |
29 self.saveload(hf) | |
30 | |
31 def testSave4(self): | |
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 | 34 self.saveload(hf) |
35 | |
36 def testSave5(self): | |
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 | 39 self.saveload(hf) |
40 | |
292 | 41 def testTwoRegions(self): |
42 hf = HexFile() | |
43 hf2 = HexFile() | |
44 hf.addRegion(0x100, bytes.fromhex('abcd')) | |
45 hf.addRegion(0x200, bytes.fromhex('beef')) | |
46 hf2.addRegion(0x200, bytes.fromhex('beef')) | |
47 hf2.addRegion(0x100, bytes.fromhex('abcd')) | |
48 self.assertEqual(hf, hf2) | |
49 | |
245 | 50 def testMerge(self): |
51 hf = HexFile() | |
52 hf.addRegion(0x10, bytes.fromhex('abcdab')) | |
53 hf.addRegion(0x13, bytes.fromhex('abcdab')) | |
54 self.assertEqual(1, len(hf.regions)) | |
55 | |
56 def testOverlapped(self): | |
57 hf = HexFile() | |
58 hf.addRegion(0x10, bytes.fromhex('abcdab')) | |
59 with self.assertRaisesRegex(HexFileException, 'verlap'): | |
60 hf.addRegion(0x12, bytes.fromhex('abcdab')) | |
61 | |
244 | 62 def testEqual(self): |
63 hf1 = HexFile() | |
64 hf2 = HexFile() | |
65 hf1.addRegion(10, bytes.fromhex('aabbcc')) | |
66 hf2.addRegion(10, bytes.fromhex('aabbcc')) | |
67 self.assertEqual(hf1, hf2) | |
68 | |
69 def testNotEqual(self): | |
70 hf1 = HexFile() | |
71 hf2 = HexFile() | |
72 hf1.addRegion(10, bytes.fromhex('aabbcc')) | |
73 hf2.addRegion(10, bytes.fromhex('aabbdc')) | |
74 self.assertNotEqual(hf1, hf2) | |
245 | 75 |
244 | 76 def testNotEqual2(self): |
77 hf1 = HexFile() | |
78 hf2 = HexFile() | |
79 hf1.addRegion(10, bytes.fromhex('aabbcc')) | |
80 hf2.addRegion(10, bytes.fromhex('aabbcc')) | |
81 hf2.addRegion(22, bytes.fromhex('aabbcc')) | |
82 self.assertNotEqual(hf1, hf2) | |
233 | 83 |
84 def testLoad(self): | |
244 | 85 hf = HexFile() |
292 | 86 dummyhex = """:01400000aa15""" |
233 | 87 f = io.StringIO(dummyhex) |
88 hf.load(f) | |
244 | 89 self.assertEqual(1, len(hf.regions)) |
90 self.assertEqual(0x4000, hf.regions[0].address) | |
91 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data) | |
233 | 92 |
93 def testIncorrectCrc(self): | |
244 | 94 hf = HexFile() |
233 | 95 txt = ":01400000aabb" |
96 f = io.StringIO(txt) | |
244 | 97 with self.assertRaisesRegex(HexFileException, 'crc'): |
233 | 98 hf.load(f) |
99 | |
100 def testIncorrectLength(self): | |
244 | 101 hf = HexFile() |
233 | 102 txt = ":0140002200aabb" |
103 f = io.StringIO(txt) | |
244 | 104 with self.assertRaisesRegex(HexFileException, 'count'): |
233 | 105 hf.load(f) |
106 | |
107 if __name__ == '__main__': | |
108 unittest.main() |