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