Mercurial > lcfOS
view test/testasm.py @ 381:6df89163e114
Fix section and ldr pseudo instruction
author | Windel Bouwman |
---|---|
date | Sat, 26 Apr 2014 17:41:56 +0200 |
parents | 442fb043d149 |
children | 0c44e494ef58 |
line wrap: on
line source
#!/usr/bin/python import unittest from ppci import CompilerError from ppci.assembler import tokenize from ppci.objectfile import ObjectFile from ppci.outstream import BinaryOutputStream from ppci.target.basetarget import Label from ppci.buildfunctions import link class AssemblerLexingCase(unittest.TestCase): """ Tests the assemblers lexer """ def testLex0(self): """ Check if the lexer is OK """ asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID', 'EOF'] self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks) def testLex1(self): """ Test if lexer correctly maps some tokens """ asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF'] self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks) def testLex2(self): """ Test if lexer correctly maps some tokens """ asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF'] self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks) def testLex3(self): """ Test if lexer fails on a token that is invalid """ asmline = '0z4: mov rax, rbx $ ' with self.assertRaises(CompilerError): list(tokenize(asmline, [])) class OustreamTestCase(unittest.TestCase): def test1(self): obj = ObjectFile() o = BinaryOutputStream(obj) o.select_section('.text') o.emit(Label('a')) self.assertSequenceEqual(bytes(), obj.get_section('.text').data) class AsmTestCaseBase(unittest.TestCase): """ Base testcase for assembly """ def feed(self, line): self.assembler.assemble(line, self.ostream) def check(self, hexstr, layout={}): self.assembler.flush() self.obj = link([self.obj], layout) data = bytes(self.obj.get_section('.text').data) self.assertSequenceEqual(bytes.fromhex(hexstr), data) if __name__ == '__main__': unittest.main()