view python/testasm.py @ 198:33d50727a23c

Fixup testscript
author Windel Bouwman
date Sat, 01 Jun 2013 13:11:22 +0200
parents ec2b423cdbea
children a690473b79e2
line wrap: on
line source

#!/usr/bin/python

import unittest
import ppci
from asm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber, tokenize, Assembler

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']
        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']
        self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)

    def testLex1(self):
        """ Test if lexer correctly maps some tokens """
        asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
        self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)

    def testLex2(self):
        """ Test if lexer fails on a token that is invalid """
        asmline = '0z4: mov rax, rbx $ '
        with self.assertRaises(ppci.CompilerError):
            list(tokenize(asmline))

class AssemblerParsingTestCase(unittest.TestCase):
    """ 
        Tests the assembler parts
    """

    def testParse(self):
        asmline = 'lab1: mov rax, rbx'
        a = Assembler()
        a.parse_line(asmline)

    def testParse2(self):
        asmline = 'a: mov rax, [rbx + 2]'
        a = Assembler()
        a.parse_line(asmline)
        output = []
        output.append(ALabel('a'))
        output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
        self.assertSequenceEqual(output, a.output)

    def testParse3(self):
        # A label must be optional:
        asmline = 'mov rax, 1'
        a = Assembler()
        a.parse_line(asmline)
        output = []
        output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)]))
        self.assertSequenceEqual(output, a.output)

    def testParse4(self):
        # Test 3 operands:
        asmline = 'add rax, [4*rbx + 22], rcx'
        a = Assembler()
        a.parse_line(asmline)
        output = []
        ops = []
        ops.append(ASymbol('rax'))
        ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
        ops.append(ASymbol('rcx'))
        output.append(AInstruction('add', ops))
        self.assertSequenceEqual(output, a.output)

    def testParse5(self):
        # An instruction must be optional:
        asmline = 'lab1:'
        a = Assembler()
        a.parse_line(asmline)
        output = []
        output.append(ALabel('lab1'))
        self.assertSequenceEqual(output, a.output)

    def testParse6(self):
        # A line can be empty
        a = Assembler()
        a.parse_line('')
    
class AssemblerOtherTestCase(unittest.TestCase):
    def testX86(self):
        testsrc = """ ; tst
        begin:
        mov rax, rbx ; 0x48, 0x89, 0xd8
        xor rcx, rbx ; 0x48, 0x31, 0xd9
        inc rcx ; 0x48 0xff 0xc1
        """
        a = Assembler()
        a.assemble(testsrc)
        # Compare with nasm output:
        nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]

if __name__ == '__main__':
    unittest.main()