changeset 233:d3dccf12ca88

Added hexfile tests
author Windel Bouwman
date Sun, 14 Jul 2013 12:28:23 +0200
parents e621e3ba78d2
children 83781bd10fdb
files python/c3/codegenerator.py python/hexfile.py python/testhexfile.py
diffstat 3 files changed, 84 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Sun Jul 14 11:50:58 2013 +0200
+++ b/python/c3/codegenerator.py	Sun Jul 14 12:28:23 2013 +0200
@@ -150,7 +150,7 @@
                 self.builder.addIns(ins)
                 return tmp
              else:
-                print('Unknown {0}'.format(expr))
+                raise NotImplementedError('Unknown {0}'.format(expr))
                 tmp = self.builder.newTmp()
                 # TODO
                 return tmp
@@ -162,7 +162,7 @@
                 return tmp
                 #self.builder.addIns(ins)
             else:
-                raise Exception('Unknown {0}'.format(expr))
+                raise NotImplementedError('Unknown {0}'.format(expr))
         elif type(expr) is astnodes.Constant:
             tmp = self.builder.newTmp()
             # TODO
@@ -204,7 +204,7 @@
                 else:
                     raise Exception()
             else:
-                raise Exception("not implemented")
+                raise NotImplementedError("not implemented")
         elif type(expr) is astnodes.FunctionCall:
              tmp = self.builder.newTmp("res")
              args = []
@@ -216,5 +216,5 @@
              self.builder.addIns(ins)
              return tmp
         else:
-             raise CompilerError('Unknown expr {}'.format(expr))
+             raise NotImplementedError('Unknown expr {}'.format(expr))
 
--- a/python/hexfile.py	Sun Jul 14 11:50:58 2013 +0200
+++ b/python/hexfile.py	Sun Jul 14 12:28:23 2013 +0200
@@ -1,34 +1,31 @@
 import os
 
 class HexFileException(Exception):
-   pass
+    pass
 
 def parseHexLine(line):
-   """ Parses a hexfile line into three parts """
-   line = line[1:] # Remove ':'
-   nums = bytes.fromhex(line)
-   bytecount = nums[0]
-   if len(nums) != bytecount + 5:
-      raise HexFileException('byte count field incorrect')
-   crc = sum(nums)
-   if (crc & 0xFF) != 0:
-      raise HexFileException('crc incorrect')
-   address = nums[1] * 256 + nums[2]
-   typ = nums[3]
-   data = nums[4:-1]
-   return (address, typ, data)
+    """ Parses a hexfile line into three parts """
+    line = line[1:] # Remove ':'
+    nums = bytes.fromhex(line)
+    bytecount = nums[0]
+    if len(nums) != bytecount + 5:
+        raise HexFileException('byte count field incorrect')
+    crc = sum(nums)
+    if (crc & 0xFF) != 0:
+        raise HexFileException('crc incorrect')
+    address = nums[1] * 256 + nums[2]
+    typ = nums[3]
+    data = nums[4:-1]
+    return (address, typ, data)
+
 
 class HexFile:
-   """ Represents an intel hexfile """
-   def __init__(self, filename=None):
+    """ Represents an intel hexfile """
+    def __init__(self, filename=None):
       self.regions = []
       self.startAddress = 0
-      self.filename = None
-      if filename:
-         self.load(filename)
 
-   def load(self, filename):
-      with open(filename, 'r') as f:
+    def load(self, f):
          endOfFile = False
          offset = 0
          startAddress = 0
@@ -66,21 +63,23 @@
          # After all lines:
          if curData:
             self.regions.append(HexFileRegion(startAddress, bytes(curData)))
-         # Store the filename:
-         self.filename = filename
-   def __repr__(self):
-      i = []
-      i.append(super().__repr__())
-      i.append('Start address {0}'.format(hex(self.startAddress)))
-      i.append('Filename: {0}'.format(self.filename))
-      for r in self.regions:
-         i.append(str(r))
-      return os.linesep.join(i)
-   def save(self, filename):
-      with open(filename, 'w') as f:
-         for line in f:
-            pass
-     
+
+    def __repr__(self):
+        i = []
+        i.append(super().__repr__())
+        i.append('Start address {0}'.format(hex(self.startAddress)))
+        for r in self.regions:
+            i.append(str(r))
+        return os.linesep.join(i)
+
+    def save(self, f):
+        for r in self.regions:
+            offset = 0
+            while offset < len(r.data):
+                f.write('a')
+                offset += 16
+
+
 class HexFileRegion:
    def __init__(self, address, data = bytes()):
       self.address = address
@@ -88,10 +87,13 @@
    def __repr__(self):
       return 'Region at 0x{0:X} of {1} bytes'.format(self.address, len(self.data))
 
+
 if __name__ == '__main__':
    h = HexFile()
    print(h)
    """ Test hexfile implementation with some hexfile """
-   h1 = HexFile('audio.hex')
+   h1 = HexFile()
+   with open('audio.hex', 'r') as f:
+        h1.load(f)
    print(h1)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/testhexfile.py	Sun Jul 14 12:28:23 2013 +0200
@@ -0,0 +1,41 @@
+import unittest
+import io
+import hexfile
+
+
+class testHexFile(unittest.TestCase):
+    def setUp(self):
+        pass
+
+    def testSave(self):
+        hf = hexfile.HexFile()
+        f = io.StringIO()
+        region = hexfile.HexFileRegion(0x8000, bytes.fromhex('aabbcc'))
+        hf.regions.append(region)
+        hf.save(f)
+
+    def testLoad(self):
+        hf = hexfile.HexFile()
+        dummyhex = """
+        :01400000aa15
+        """
+        f = io.StringIO(dummyhex)
+        hf.load(f)
+
+    def testIncorrectCrc(self):
+        hf = hexfile.HexFile()
+        txt = ":01400000aabb"
+        f = io.StringIO(txt)
+        with self.assertRaises(hexfile.HexFileException):
+            hf.load(f)
+
+    def testIncorrectLength(self):
+        hf = hexfile.HexFile()
+        txt = ":0140002200aabb"
+        f = io.StringIO(txt)
+        with self.assertRaises(hexfile.HexFileException):
+            hf.load(f)
+
+if __name__ == '__main__':
+    unittest.main()
+