comparison python/runtests.py @ 100:fe145e42259d

Fixes after movage
author windel
date Mon, 24 Dec 2012 16:35:22 +0100
parents python/tests/runtests.py@32078200cdd6
children 6b2bec5653f1
comparison
equal deleted inserted replaced
99:6efbeb903777 100:fe145e42259d
1 #!/usr/bin/python
2
3 import unittest
4 import os
5
6 from compiler.compiler import Compiler
7 from compiler.errors import CompilerException, printError
8 from compiler import lexer
9 from compiler.parser import Parser
10 from compiler import assembler
11 from compiler.codegenerator import CodeGenerator
12 from project import Project
13
14 class CompilerTestCase(unittest.TestCase):
15 """ test methods start with 'test*' """
16 def testSource1(self):
17 source = """
18 module lcfos;
19 var
20 a : integer;
21
22 procedure putchar(num : integer);
23 begin
24 end putchar;
25
26 procedure WriteNum( num: integer);
27 var
28 d, base : integer;
29 dgt : integer;
30 begin
31 d := 1;
32 base := 10;
33 while num div d >= base do
34 d := d * base
35 end;
36 while d <> 0 do
37 dgt := num div d;
38 num := num mod d;
39 d := d div base;
40 putchar(48 + dgt)
41 end
42 end WriteNum;
43
44 begin
45 a := 1;
46 while a < 26
47 do
48 putchar(65+a);
49 a := a * 2
50 end;
51 end lcfos.
52 """
53 pc = Compiler()
54 pc.compilesource(source)
55 def testSource2(self):
56 source = """
57 module lcfos;
58 var
59 a, b : integer;
60 arr: array 30 of integer;
61 arr2: array 10, 12 of integer;
62 procedure t2*() : integer;
63 begin
64 a := 2;
65 while a < 5 do
66 b := arr[a-1] + arr[a-2];
67 arr2[a,2] := b;
68 arr2[a,3] := arr2[a,2] + arr2[a,2]*3 + b;
69 arr[a] := b;
70 a := a + 1;
71 end;
72 return b
73 end t2;
74 begin
75 b := 12;
76 arr[0] := 1;
77 arr[1] := 1;
78 end lcfos.
79 """
80 pc = Compiler()
81 mod = pc.compilesource(source)
82 def testSource5(self):
83 source = """
84 module lcfos;
85 procedure WriteLn() : integer;
86 const zzz = 13;
87 var
88 a, b, c: integer;
89 begin
90 a := 2;
91 b := 7;
92 c := 10 * a + b*10*a;
93 return c
94 end WriteLn;
95 begin end lcfos.
96 """
97 pc = Compiler()
98 pc.compilesource(source)
99 def tstForStatement(self):
100 source = """
101 module fortest;
102 var
103 a,b,c : integer;
104 begin
105 c := 0;
106 for a := 1 to 10 by 1 do
107 b := a + 15;
108 c := c + b * a;
109 end;
110 end fortest.
111 """
112 pc = Compiler()
113 pc.compilesource(source)
114 def testSourceIfAndWhilePattern(self):
115 source = """
116 module lcfos;
117 procedure WriteLn() : integer;
118 const zzz = 13;
119 var
120 a, b, c: integer;
121 begin
122 a := 1;
123 b := 2;
124 if a * 3 > b then
125 c := 10*a + b*10*a*a*a*b;
126 else
127 c := 13;
128 end;
129 while a < 101 do
130 a := a + 1;
131 c := c + 2;
132 end;
133 return c
134 end WriteLn;
135 begin end lcfos.
136 """
137 pc = Compiler()
138 pc.compilesource(source)
139
140 def testPattern1(self):
141 """ Test if expression can be compiled into byte code """
142 src = "12*13+33-12*2*3"
143 tokens = lexer.tokenize(src)
144 ast = Parser(tokens).parseExpression()
145 code = CodeGenerator().genexprcode(ast)
146
147 def testAssembler(self):
148 """ Check all kind of assembler cases """
149 assert(assembler.shortjump(5) == [0xeb, 0x5])
150 assert(assembler.shortjump(-2) == [0xeb, 0xfc])
151 assert(assembler.shortjump(10,'GE') == [0x7d, 0xa])
152 assert(assembler.nearjump(5) == [0xe9, 0x5,0x0,0x0,0x0])
153 assert(assembler.nearjump(-2) == [0xe9, 0xf9, 0xff,0xff,0xff])
154 assert(assembler.nearjump(10,'LE') == [0x0f, 0x8e, 0xa,0x0,0x0,0x0])
155
156 def testCall(self):
157 assert(assembler.call('r10') == [0x41, 0xff, 0xd2])
158 assert(assembler.call('rcx') == [0xff, 0xd1])
159 def testXOR(self):
160 assert(assembler.xorreg64('rax', 'rax') == [0x48, 0x31, 0xc0])
161 assert(assembler.xorreg64('r9', 'r8') == [0x4d, 0x31, 0xc1])
162 assert(assembler.xorreg64('rbx', 'r11') == [0x4c, 0x31, 0xdb])
163
164 def testINC(self):
165 assert(assembler.increg64('r11') == [0x49, 0xff, 0xc3])
166 assert(assembler.increg64('rcx') == [0x48, 0xff, 0xc1])
167
168 def testPush(self):
169 assert(assembler.push('rbp') == [0x55])
170 assert(assembler.push('rbx') == [0x53])
171 assert(assembler.push('r12') == [0x41, 0x54])
172 def testPop(self):
173 assert(assembler.pop('rbx') == [0x5b])
174 assert(assembler.pop('rbp') == [0x5d])
175 assert(assembler.pop('r12') == [0x41, 0x5c])
176
177 def testAsmLoads(self):
178 # TODO constant add testcases
179 assert(assembler.mov('rbx', 'r14') == [0x4c, 0x89, 0xf3])
180 assert(assembler.mov('r12', 'r8') == [0x4d, 0x89, 0xc4])
181 assert(assembler.mov('rdi', 'rsp') == [0x48, 0x89, 0xe7])
182
183 def testAsmMemLoads(self):
184 assert(assembler.mov('rax', ['r8','r15',0x11]) == [0x4b,0x8b,0x44,0x38,0x11])
185 assert(assembler.mov('r13', ['rbp','rcx',0x23]) == [0x4c,0x8b,0x6c,0xd,0x23])
186
187 assert(assembler.mov('r9', ['rbp',-0x33]) == [0x4c,0x8b,0x4d,0xcd])
188 #assert(assembler.movreg64('rbx', ['rax']) == [0x48, 0x8b,0x18])
189
190 assert(assembler.mov('rax', [0xb000]) == [0x48,0x8b,0x4,0x25,0x0,0xb0,0x0,0x0])
191 assert(assembler.mov('r11', [0xa0]) == [0x4c,0x8b,0x1c,0x25,0xa0,0x0,0x0,0x0])
192
193 assert(assembler.mov('r11', ['RIP', 0xf]) == [0x4c,0x8b,0x1d,0x0f,0x0,0x0,0x0])
194
195 def testAsmMemStores(self):
196 assert(assembler.mov(['rbp', 0x13],'rbx') == [0x48,0x89,0x5d,0x13])
197 assert(assembler.mov(['r12', 0x12],'r9') == [0x4d,0x89,0x4c,0x24,0x12])
198 assert(assembler.mov(['rcx', 0x11],'r14') == [0x4c,0x89,0x71,0x11])
199
200
201 assert(assembler.mov([0xab], 'rbx') == [0x48,0x89,0x1c,0x25,0xab,0x0,0x0,0x0])
202 assert(assembler.mov([0xcd], 'r13') == [0x4c,0x89,0x2c,0x25,0xcd,0x0,0x0,0x0])
203
204 assert(assembler.mov(['RIP', 0xf], 'r9') == [0x4c,0x89,0x0d,0x0f,0x0,0x0,0x0])
205
206 def testAsmMOV8(self):
207 assert(assembler.mov(['rbp', -8], 'al') == [0x88, 0x45, 0xf8])
208 assert(assembler.mov(['r11', 9], 'cl') == [0x41, 0x88, 0x4b, 0x09])
209
210 assert(assembler.mov(['rbx'], 'al') == [0x88, 0x03])
211 assert(assembler.mov(['r11'], 'dl') == [0x41, 0x88, 0x13])
212
213 def testAsmLea(self):
214 assert(assembler.leareg64('r11', ['RIP', 0xf]) == [0x4c,0x8d,0x1d,0x0f,0x0,0x0,0x0])
215 assert(assembler.leareg64('rsi', ['RIP', 0x7]) == [0x48,0x8d,0x35,0x07,0x0,0x0,0x0])
216
217 assert(assembler.leareg64('rcx', ['rbp', -8]) == [0x48,0x8d,0x4d,0xf8])
218
219 def testAssemblerCMP(self):
220 assert(assembler.cmpreg64('rdi', 'r13') == [0x4c, 0x39, 0xef])
221 assert(assembler.cmpreg64('rbx', 'r14') == [0x4c, 0x39, 0xf3])
222 assert(assembler.cmpreg64('r12', 'r9') == [0x4d, 0x39, 0xcc])
223
224 assert(assembler.cmpreg64('rdi', 1) == [0x48, 0x83, 0xff, 0x01])
225 assert(assembler.cmpreg64('r11', 2) == [0x49, 0x83, 0xfb, 0x02])
226 def testAssemblerADD(self):
227 assert(assembler.addreg64('rbx', 'r13') == [0x4c, 0x01, 0xeb])
228 assert(assembler.addreg64('rax', 'rbx') == [0x48, 0x01, 0xd8])
229 assert(assembler.addreg64('r12', 'r13') == [0x4d, 0x01, 0xec])
230
231 assert(assembler.addreg64('rbx', 0x13) == [0x48, 0x83, 0xc3, 0x13])
232 assert(assembler.addreg64('r11', 0x1234567) == [0x49, 0x81, 0xc3, 0x67, 0x45,0x23,0x1])
233 assert(assembler.addreg64('rsp', 0x33) == [0x48, 0x83, 0xc4, 0x33])
234
235 def testAssemblerSUB(self):
236 assert(assembler.subreg64('rdx', 'r14') == [0x4c, 0x29, 0xf2])
237 assert(assembler.subreg64('r15', 'rbx') == [0x49, 0x29, 0xdf])
238 assert(assembler.subreg64('r8', 'r9') == [0x4d, 0x29, 0xc8])
239
240 assert(assembler.subreg64('rsp', 0x123456) == [0x48, 0x81, 0xec, 0x56,0x34,0x12,0x0])
241 assert(assembler.subreg64('rsp', 0x12) == [0x48, 0x83, 0xec, 0x12])
242
243 def testAssemblerIDIV(self):
244 assert(assembler.idivreg64('r11') == [0x49, 0xf7, 0xfb])
245 assert(assembler.idivreg64('rcx') == [0x48, 0xf7, 0xf9])
246 assert(assembler.idivreg64('rsp') == [0x48, 0xf7, 0xfc])
247
248 def testAssemblerIMUL(self):
249 assert(assembler.imulreg64_rax('rdi') == [0x48, 0xf7, 0xef])
250 assert(assembler.imulreg64_rax('r10') == [0x49, 0xf7, 0xea])
251 assert(assembler.imulreg64_rax('rdx') == [0x48, 0xf7, 0xea])
252
253 assert(assembler.imulreg64('r11', 'rdi') == [0x4c, 0xf, 0xaf, 0xdf])
254 assert(assembler.imulreg64('r12', 'rbx') == [0x4c, 0xf, 0xaf, 0xe3])
255 # nasm generates this machine code: 0x4d, 0x6b, 0xff, 0xee
256 # This also works: 4D0FAFFE (another variant?? )
257 assert(assembler.imulreg64('r15', 'r14') == [0x4d, 0x0f, 0xaf, 0xfe])
258 def testProject(self):
259 p = Project('test.xml', isnew=True)
260 p.name = "Test project"
261 p.files.append('main.mod')
262 p.files.append('test.mod')
263 p.save('test.xml')
264
265 q = Project('test.xml')
266
267 assert(p.name == q.name)
268 assert(p.files == q.files)
269 # TODO: remove test.xml test file
270 os.remove('test.xml')
271
272 if __name__ == '__main__':
273 unittest.main()
274