191
|
1 #!/usr/bin/python
|
|
2
|
346
|
3 import unittest
|
200
|
4 from ppci import CompilerError
|
382
|
5 from ppci.assembler import AsmLexer
|
335
|
6 from ppci.objectfile import ObjectFile
|
342
|
7 from ppci.outstream import BinaryOutputStream
|
|
8 from ppci.target.basetarget import Label
|
381
|
9 from ppci.buildfunctions import link
|
191
|
10
|
290
|
11
|
198
|
12 class AssemblerLexingCase(unittest.TestCase):
|
|
13 """ Tests the assemblers lexer """
|
191
|
14
|
382
|
15 def setUp(self):
|
|
16 self.lexer = AsmLexer([])
|
|
17
|
|
18 def do(self, asmline, toks):
|
|
19 output = []
|
|
20 self.lexer.feed(asmline)
|
|
21 while 'EOF' not in output:
|
|
22 output.append(self.lexer.next_token().typ)
|
|
23 self.assertSequenceEqual(toks, output)
|
|
24
|
191
|
25 def testLex0(self):
|
|
26 """ Check if the lexer is OK """
|
382
|
27 asmline = 'mov rax, rbx '
|
|
28 toks = ['ID', 'ID', ',', 'ID', 'EOF']
|
|
29 self.do(asmline, toks)
|
191
|
30
|
|
31 def testLex1(self):
|
193
|
32 """ Test if lexer correctly maps some tokens """
|
382
|
33 asmline = 'lab1: mov rax, rbx '
|
|
34 toks = ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF']
|
|
35 self.do(asmline, toks)
|
193
|
36
|
191
|
37 def testLex2(self):
|
318
|
38 """ Test if lexer correctly maps some tokens """
|
341
|
39 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
|
382
|
40 self.do(asmline, toks)
|
318
|
41
|
|
42 def testLex3(self):
|
193
|
43 """ Test if lexer fails on a token that is invalid """
|
191
|
44 asmline = '0z4: mov rax, rbx $ '
|
200
|
45 with self.assertRaises(CompilerError):
|
382
|
46 self.do(asmline, [])
|
198
|
47
|
290
|
48
|
236
|
49 class OustreamTestCase(unittest.TestCase):
|
|
50 def test1(self):
|
335
|
51 obj = ObjectFile()
|
342
|
52 o = BinaryOutputStream(obj)
|
348
|
53 o.select_section('.text')
|
236
|
54 o.emit(Label('a'))
|
335
|
55 self.assertSequenceEqual(bytes(), obj.get_section('.text').data)
|
236
|
56
|
|
57
|
|
58 class AsmTestCaseBase(unittest.TestCase):
|
292
|
59 """ Base testcase for assembly """
|
236
|
60 def feed(self, line):
|
381
|
61 self.assembler.assemble(line, self.ostream)
|
236
|
62
|
381
|
63 def check(self, hexstr, layout={}):
|
|
64 self.assembler.flush()
|
|
65 self.obj = link([self.obj], layout)
|
335
|
66 data = bytes(self.obj.get_section('.text').data)
|
|
67 self.assertSequenceEqual(bytes.fromhex(hexstr), data)
|
236
|
68
|
|
69
|
191
|
70 if __name__ == '__main__':
|
|
71 unittest.main()
|