comparison python/testasm.py @ 198:33d50727a23c

Fixup testscript
author Windel Bouwman
date Sat, 01 Jun 2013 13:11:22 +0200
parents ec2b423cdbea
children a690473b79e2
comparison
equal deleted inserted replaced
197:4a1ca1271241 198:33d50727a23c
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import unittest 3 import unittest
4 import libasm
5 import ppci 4 import ppci
6 from libasm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber 5 from asm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber, tokenize, Assembler
7 6
8 class AssemblerTestCase(unittest.TestCase): 7 class AssemblerLexingCase(unittest.TestCase):
9 """ 8 """ Tests the assemblers lexer """
10 Tests the assembler parts
11 """
12 def setUp(self):
13 pass
14 9
15 def testLex0(self): 10 def testLex0(self):
16 """ Check if the lexer is OK """ 11 """ Check if the lexer is OK """
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID'] 12 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
18 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks) 13 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
19 14
20 def testLex1(self): 15 def testLex1(self):
21 """ Test if lexer correctly maps some tokens """ 16 """ Test if lexer correctly maps some tokens """
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID'] 17 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
23 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks) 18 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
24 19
25 def testLex1(self): 20 def testLex1(self):
26 """ Test if lexer correctly maps some tokens """ 21 """ Test if lexer correctly maps some tokens """
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER'] 22 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
28 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks) 23 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
29 24
30 def testLex2(self): 25 def testLex2(self):
31 """ Test if lexer fails on a token that is invalid """ 26 """ Test if lexer fails on a token that is invalid """
32 asmline = '0z4: mov rax, rbx $ ' 27 asmline = '0z4: mov rax, rbx $ '
33 with self.assertRaises(ppci.CompilerError): 28 with self.assertRaises(ppci.CompilerError):
34 list(libasm.tokenize(asmline)) 29 list(tokenize(asmline))
30
31 class AssemblerParsingTestCase(unittest.TestCase):
32 """
33 Tests the assembler parts
34 """
35 35
36 def testParse(self): 36 def testParse(self):
37 asmline = 'lab1: mov rax, rbx' 37 asmline = 'lab1: mov rax, rbx'
38 a = libasm.Assembler() 38 a = Assembler()
39 a.parse_line(asmline) 39 a.parse_line(asmline)
40 40
41 def testParse2(self): 41 def testParse2(self):
42 asmline = 'a: mov rax, [rbx + 2]' 42 asmline = 'a: mov rax, [rbx + 2]'
43 a = libasm.Assembler() 43 a = Assembler()
44 a.parse_line(asmline) 44 a.parse_line(asmline)
45 output = [] 45 output = []
46 output.append(ALabel('a')) 46 output.append(ALabel('a'))
47 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))])) 47 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
48 self.assertSequenceEqual(output, a.output) 48 self.assertSequenceEqual(output, a.output)
49 49
50 def testParse3(self): 50 def testParse3(self):
51 # A label must be optional: 51 # A label must be optional:
52 asmline = 'mov rax, 1' 52 asmline = 'mov rax, 1'
53 a = libasm.Assembler() 53 a = Assembler()
54 a.parse_line(asmline) 54 a.parse_line(asmline)
55 output = [] 55 output = []
56 output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)])) 56 output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)]))
57 self.assertSequenceEqual(output, a.output) 57 self.assertSequenceEqual(output, a.output)
58 58
59 def testParse4(self): 59 def testParse4(self):
60 # Test 3 operands: 60 # Test 3 operands:
61 asmline = 'add rax, [4*rbx + 22], rcx' 61 asmline = 'add rax, [4*rbx + 22], rcx'
62 a = libasm.Assembler() 62 a = Assembler()
63 a.parse_line(asmline) 63 a.parse_line(asmline)
64 output = [] 64 output = []
65 ops = [] 65 ops = []
66 ops.append(ASymbol('rax')) 66 ops.append(ASymbol('rax'))
67 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22))) 67 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
70 self.assertSequenceEqual(output, a.output) 70 self.assertSequenceEqual(output, a.output)
71 71
72 def testParse5(self): 72 def testParse5(self):
73 # An instruction must be optional: 73 # An instruction must be optional:
74 asmline = 'lab1:' 74 asmline = 'lab1:'
75 a = libasm.Assembler() 75 a = Assembler()
76 a.parse_line(asmline) 76 a.parse_line(asmline)
77 output = [] 77 output = []
78 output.append(ALabel('lab1')) 78 output.append(ALabel('lab1'))
79 self.assertSequenceEqual(output, a.output) 79 self.assertSequenceEqual(output, a.output)
80 80
81 def testParse6(self): 81 def testParse6(self):
82 # A line can be empty 82 # A line can be empty
83 a = libasm.Assembler() 83 a = Assembler()
84 a.parse_line('') 84 a.parse_line('')
85 85
86 class AssemblerOtherTestCase(unittest.TestCase):
86 def testX86(self): 87 def testX86(self):
87 testsrc = """ 88 testsrc = """ ; tst
88 begin: 89 begin:
89 mov rax, rbx 90 mov rax, rbx ; 0x48, 0x89, 0xd8
90 xor rcx, rbx 91 xor rcx, rbx ; 0x48, 0x31, 0xd9
91 inc rcx 92 inc rcx ; 0x48 0xff 0xc1
92 """ 93 """
93 a = libasm.Assembler() 94 a = Assembler()
94 a.assemble(testsrc) 95 a.assemble(testsrc)
95 # Compare with nasm output: 96 # Compare with nasm output:
96 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1] 97 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
97 98
98 if __name__ == '__main__': 99 if __name__ == '__main__':