191
|
1 #!/usr/bin/python
|
|
2
|
346
|
3 import unittest
|
200
|
4 from ppci import CompilerError
|
381
|
5 from ppci.assembler import tokenize
|
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
|
|
15 def testLex0(self):
|
|
16 """ Check if the lexer is OK """
|
318
|
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID', 'EOF']
|
341
|
18 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
|
191
|
19
|
|
20 def testLex1(self):
|
193
|
21 """ Test if lexer correctly maps some tokens """
|
318
|
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF']
|
341
|
23 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
|
193
|
24
|
191
|
25 def testLex2(self):
|
318
|
26 """ Test if lexer correctly maps some tokens """
|
341
|
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
|
|
28 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
|
318
|
29
|
|
30 def testLex3(self):
|
193
|
31 """ Test if lexer fails on a token that is invalid """
|
191
|
32 asmline = '0z4: mov rax, rbx $ '
|
200
|
33 with self.assertRaises(CompilerError):
|
341
|
34 list(tokenize(asmline, []))
|
198
|
35
|
290
|
36
|
236
|
37 class OustreamTestCase(unittest.TestCase):
|
|
38 def test1(self):
|
335
|
39 obj = ObjectFile()
|
342
|
40 o = BinaryOutputStream(obj)
|
348
|
41 o.select_section('.text')
|
236
|
42 o.emit(Label('a'))
|
335
|
43 self.assertSequenceEqual(bytes(), obj.get_section('.text').data)
|
236
|
44
|
|
45
|
|
46 class AsmTestCaseBase(unittest.TestCase):
|
292
|
47 """ Base testcase for assembly """
|
236
|
48 def feed(self, line):
|
381
|
49 self.assembler.assemble(line, self.ostream)
|
236
|
50
|
381
|
51 def check(self, hexstr, layout={}):
|
|
52 self.assembler.flush()
|
|
53 self.obj = link([self.obj], layout)
|
335
|
54 data = bytes(self.obj.get_section('.text').data)
|
|
55 self.assertSequenceEqual(bytes.fromhex(hexstr), data)
|
236
|
56
|
|
57
|
191
|
58 if __name__ == '__main__':
|
|
59 unittest.main()
|