Mercurial > lcfOS
view test/testir.py @ 350:2b02bd286fe9
Fixed A9 hello worle
author | Windel Bouwman |
---|---|
date | Sat, 08 Mar 2014 16:29:03 +0100 |
parents | 86b02c98a717 |
children | 988f3fb861e4 |
line wrap: on
line source
import unittest import os import sys import io import ppci from ppci import ir from ppci import irutils from ppci.transform import ConstantFolder class IrCodeTestCase(unittest.TestCase): def testAdd(self): v = ir.Add(ir.Const(1), ir.Const(2)) class IrBuilderTestCase(unittest.TestCase): def setUp(self): self.b = irutils.Builder() self.m = ir.Module('test') self.b.setModule(self.m) def testBuilder(self): f = self.b.newFunction('add') self.b.setFunction(f) bb = self.b.newBlock() self.b.emit(ir.Jump(bb)) self.b.setBlock(bb) self.b.emit(ir.Exp(ir.Const(0))) self.b.emit(ir.Jump(f.epiloog)) # Run interpreter: # r = self.m.getFunction('add').call(1, 2) #self.assertEqual(3, r) class PatternMatchTestCase(unittest.TestCase): @unittest.skip('Not yet implemented') def testSimpleTree(self): t = ir.Term('x') pat = ir.Binop(ir.Const(2), '+', t) res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', 3), pat) self.assertTrue(res) self.assertIn(t, mp) self.assertEqual(3, mp[t]) @unittest.skip('Not yet implemented') def testSimpleTree2(self): t = ir.Term('x') t2 = ir.Term('y') pat = ir.Binop(ir.Const(2), '+', ir.Binop(t, '-', t2)) res, mp = ir.match_tree(ir.Binop(ir.Const(2), '+', ir.Binop(2,'-',3)), pat) self.assertTrue(res) self.assertIn(t, mp) self.assertEqual(2, mp[t]) self.assertIn(t2, mp) self.assertEqual(3, mp[t2]) res, mp = ir.match_tree(ir.Const(2), pat) self.assertFalse(res) class ConstantFolderTestCase(unittest.TestCase): def setUp(self): self.b = irutils.Builder() self.cf = ConstantFolder() self.m = ir.Module('test') self.b.setModule(self.m) def testBuilder(self): f = self.b.newFunction('test') self.b.setFunction(f) bb = self.b.newBlock() self.b.emit(ir.Jump(bb)) self.b.setBlock(bb) v1 = ir.Const(5) v2 = ir.Const(7) v3 = ir.Add(v1, v2) self.b.emit(ir.Jump(f.epiloog)) self.cf.run(self.m) def testAdd0(self): f = self.b.newFunction('test') self.b.setFunction(f) self.b.setBlock(self.b.newBlock()) v1 = ir.Const(0) v3 = ir.Add(v1, ir.Const(0)) class TestWriter(unittest.TestCase): def testWrite(self): writer = irutils.Writer() module = ir.Module('mod1') function = ir.Function('func1', module) f = io.StringIO() writer.write(module, f) #print(f.getvalue()) f2 = io.StringIO(f.getvalue()) reader = irutils.Reader() module2 = reader.read(f2) f = io.StringIO() writer.write(module2, f) #print(f.getvalue()) class TestReader(unittest.TestCase): def testAddExample(self): reader = irutils.Reader() with open('../examples/pi/add.pi') as f: m = reader.read(f) self.assertTrue(m) #print(m) if __name__ == '__main__': unittest.main() sys.exit()