comparison test/testhexfile.py @ 284:05184b95fa16

Moved tests to seperate folder
author Windel Bouwman
date Fri, 15 Nov 2013 13:43:22 +0100
parents python/testhexfile.py@046017431c6a
children 534b94b40aa8
comparison
equal deleted inserted replaced
283:c9781c73e7e2 284:05184b95fa16
1 import unittest
2 import io
3 from hexfile import HexFile, HexFileException
4
5
6 class testHexFile(unittest.TestCase):
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
14 def testSave1(self):
15 hf = HexFile()
16 hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
17 self.saveload(hf)
18
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()
33 hf.addRegion(0xF000, bytes.fromhex('ab')*0x10000)
34 self.saveload(hf)
35
36 def testSave5(self):
37 hf = HexFile()
38 hf.addRegion(0xF003, bytes.fromhex('ab')*0x10000)
39 self.saveload(hf)
40
41 def testMerge(self):
42 hf = HexFile()
43 hf.addRegion(0x10, bytes.fromhex('abcdab'))
44 hf.addRegion(0x13, bytes.fromhex('abcdab'))
45 self.assertEqual(1, len(hf.regions))
46
47 def testOverlapped(self):
48 hf = HexFile()
49 hf.addRegion(0x10, bytes.fromhex('abcdab'))
50 with self.assertRaisesRegex(HexFileException, 'verlap'):
51 hf.addRegion(0x12, bytes.fromhex('abcdab'))
52
53 def testEqual(self):
54 hf1 = HexFile()
55 hf2 = HexFile()
56 hf1.addRegion(10, bytes.fromhex('aabbcc'))
57 hf2.addRegion(10, bytes.fromhex('aabbcc'))
58 self.assertEqual(hf1, hf2)
59
60 def testNotEqual(self):
61 hf1 = HexFile()
62 hf2 = HexFile()
63 hf1.addRegion(10, bytes.fromhex('aabbcc'))
64 hf2.addRegion(10, bytes.fromhex('aabbdc'))
65 self.assertNotEqual(hf1, hf2)
66
67 def testNotEqual2(self):
68 hf1 = HexFile()
69 hf2 = HexFile()
70 hf1.addRegion(10, bytes.fromhex('aabbcc'))
71 hf2.addRegion(10, bytes.fromhex('aabbcc'))
72 hf2.addRegion(22, bytes.fromhex('aabbcc'))
73 self.assertNotEqual(hf1, hf2)
74
75 def testLoad(self):
76 hf = HexFile()
77 dummyhex = """
78 :01400000aa15
79 """
80 f = io.StringIO(dummyhex)
81 hf.load(f)
82 self.assertEqual(1, len(hf.regions))
83 self.assertEqual(0x4000, hf.regions[0].address)
84 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
85
86 def testIncorrectCrc(self):
87 hf = HexFile()
88 txt = ":01400000aabb"
89 f = io.StringIO(txt)
90 with self.assertRaisesRegex(HexFileException, 'crc'):
91 hf.load(f)
92
93 def testIncorrectLength(self):
94 hf = HexFile()
95 txt = ":0140002200aabb"
96 f = io.StringIO(txt)
97 with self.assertRaisesRegex(HexFileException, 'count'):
98 hf.load(f)
99
100 if __name__ == '__main__':
101 unittest.main()
102