comparison python/testhexfile.py @ 244:58155c7c4a8e

Add hexutil
author Windel Bouwman
date Wed, 24 Jul 2013 19:47:13 +0200
parents d3dccf12ca88
children 66912720d712
comparison
equal deleted inserted replaced
243:ef683881c64e 244:58155c7c4a8e
1 import unittest 1 import unittest
2 import io 2 import io
3 import hexfile 3 from hexfile import HexFile, HexFileException
4 4
5 5
6 class testHexFile(unittest.TestCase): 6 class testHexFile(unittest.TestCase):
7 def setUp(self): 7 def saveload(self, hf):
8 pass 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 testSave(self):
15 hf = HexFile()
16 hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
17 self.saveload(hf)
9 18
10 def testSave(self): 19 def testEqual(self):
11 hf = hexfile.HexFile() 20 hf1 = HexFile()
12 f = io.StringIO() 21 hf2 = HexFile()
13 region = hexfile.HexFileRegion(0x8000, bytes.fromhex('aabbcc')) 22 hf1.addRegion(10, bytes.fromhex('aabbcc'))
14 hf.regions.append(region) 23 hf2.addRegion(10, bytes.fromhex('aabbcc'))
15 hf.save(f) 24 self.assertEqual(hf1, hf2)
25
26 def testNotEqual(self):
27 hf1 = HexFile()
28 hf2 = HexFile()
29 hf1.addRegion(10, bytes.fromhex('aabbcc'))
30 hf2.addRegion(10, bytes.fromhex('aabbdc'))
31 self.assertNotEqual(hf1, hf2)
32
33 def testNotEqual2(self):
34 hf1 = HexFile()
35 hf2 = HexFile()
36 hf1.addRegion(10, bytes.fromhex('aabbcc'))
37 hf2.addRegion(10, bytes.fromhex('aabbcc'))
38 hf2.addRegion(22, bytes.fromhex('aabbcc'))
39 self.assertNotEqual(hf1, hf2)
16 40
17 def testLoad(self): 41 def testLoad(self):
18 hf = hexfile.HexFile() 42 hf = HexFile()
19 dummyhex = """ 43 dummyhex = """
20 :01400000aa15 44 :01400000aa15
21 """ 45 """
22 f = io.StringIO(dummyhex) 46 f = io.StringIO(dummyhex)
23 hf.load(f) 47 hf.load(f)
48 self.assertEqual(1, len(hf.regions))
49 self.assertEqual(0x4000, hf.regions[0].address)
50 self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
24 51
25 def testIncorrectCrc(self): 52 def testIncorrectCrc(self):
26 hf = hexfile.HexFile() 53 hf = HexFile()
27 txt = ":01400000aabb" 54 txt = ":01400000aabb"
28 f = io.StringIO(txt) 55 f = io.StringIO(txt)
29 with self.assertRaises(hexfile.HexFileException): 56 with self.assertRaisesRegex(HexFileException, 'crc'):
30 hf.load(f) 57 hf.load(f)
31 58
32 def testIncorrectLength(self): 59 def testIncorrectLength(self):
33 hf = hexfile.HexFile() 60 hf = HexFile()
34 txt = ":0140002200aabb" 61 txt = ":0140002200aabb"
35 f = io.StringIO(txt) 62 f = io.StringIO(txt)
36 with self.assertRaises(hexfile.HexFileException): 63 with self.assertRaisesRegex(HexFileException, 'count'):
37 hf.load(f) 64 hf.load(f)
38 65
39 if __name__ == '__main__': 66 if __name__ == '__main__':
40 unittest.main() 67 unittest.main()
41 68