191
|
1 #!/usr/bin/python
|
|
2
|
|
3 import unittest
|
|
4 import libasm
|
|
5 import ppci
|
195
|
6 from libasm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber
|
191
|
7
|
|
8 class AssemblerTestCase(unittest.TestCase):
|
|
9 """
|
|
10 Tests the assembler parts
|
|
11 """
|
|
12 def setUp(self):
|
|
13 pass
|
|
14
|
|
15 def testLex0(self):
|
|
16 """ Check if the lexer is OK """
|
|
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
|
|
18 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
|
|
19
|
|
20 def testLex1(self):
|
193
|
21 """ Test if lexer correctly maps some tokens """
|
191
|
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
|
|
23 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
|
|
24
|
193
|
25 def testLex1(self):
|
|
26 """ Test if lexer correctly maps some tokens """
|
|
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
|
|
28 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
|
|
29
|
191
|
30 def testLex2(self):
|
193
|
31 """ Test if lexer fails on a token that is invalid """
|
191
|
32 asmline = '0z4: mov rax, rbx $ '
|
|
33 with self.assertRaises(ppci.CompilerError):
|
|
34 list(libasm.tokenize(asmline))
|
|
35
|
|
36 def testParse(self):
|
|
37 asmline = 'lab1: mov rax, rbx'
|
|
38 a = libasm.Assembler()
|
194
|
39 a.parse_line(asmline)
|
191
|
40
|
193
|
41 def testParse2(self):
|
|
42 asmline = 'a: mov rax, [rbx + 2]'
|
|
43 a = libasm.Assembler()
|
194
|
44 a.parse_line(asmline)
|
195
|
45 output = []
|
|
46 output.append(ALabel('a'))
|
|
47 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
|
|
48 self.assertSequenceEqual(output, a.output)
|
194
|
49
|
|
50 def testParse3(self):
|
|
51 # A label must be optional:
|
|
52 asmline = 'mov rax, 1'
|
|
53 a = libasm.Assembler()
|
|
54 a.parse_line(asmline)
|
195
|
55 output = []
|
|
56 output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)]))
|
|
57 self.assertSequenceEqual(output, a.output)
|
|
58
|
|
59 def testParse4(self):
|
|
60 # Test 3 operands:
|
|
61 asmline = 'add rax, [4*rbx + 22], rcx'
|
|
62 a = libasm.Assembler()
|
|
63 a.parse_line(asmline)
|
|
64 output = []
|
|
65 ops = []
|
|
66 ops.append(ASymbol('rax'))
|
|
67 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
|
|
68 ops.append(ASymbol('rcx'))
|
|
69 output.append(AInstruction('add', ops))
|
|
70 self.assertSequenceEqual(output, a.output)
|
|
71
|
|
72 def testParse5(self):
|
|
73 # An instruction must be optional:
|
|
74 asmline = 'lab1:'
|
|
75 a = libasm.Assembler()
|
|
76 a.parse_line(asmline)
|
|
77 output = []
|
|
78 output.append(ALabel('lab1'))
|
|
79 self.assertSequenceEqual(output, a.output)
|
|
80
|
|
81 def testX86(self):
|
|
82 # TODO
|
|
83 pass
|
193
|
84
|
191
|
85 if __name__ == '__main__':
|
|
86 unittest.main()
|
|
87
|