Mercurial > lcfOS
annotate python/testc3.py @ 215:c1ccb1cb4cef
Major changes in c3 frontend
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 13:00:03 +0200 |
parents | 003c8a976fff |
children | 8b2e5f3cd579 |
rev | line source |
---|---|
158 | 1 import c3 |
2 import time, ppci, x86, ir | |
167 | 3 import unittest |
148 | 4 |
162 | 5 testsrc = """package test; |
148 | 6 |
215 | 7 /* |
8 demo of the source that is correct :) | |
9 */ | |
10 var int c, d; | |
163 | 11 var double e; |
12 var int f; | |
13 | |
167 | 14 const int A = 1337; |
149 | 15 |
16 function void test1() | |
148 | 17 { |
215 | 18 var int bdd; |
148 | 19 var int a = 10; |
215 | 20 bdd = 20; |
155 | 21 var int buf; |
148 | 22 var int i; |
23 i = 2; | |
155 | 24 var int zero = i - 2; |
148 | 25 if (i > 1) |
26 { | |
157 | 27 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1; |
148 | 28 } |
155 | 29 else |
30 { | |
31 ;;; | |
157 | 32 } |
155 | 33 |
34 t2(2, 3); | |
148 | 35 } |
36 | |
215 | 37 function int t2(int a, int b) |
148 | 38 { |
157 | 39 if (a > 0) |
40 { | |
215 | 41 a = 2 + t2(a - 1, 10); |
157 | 42 } |
43 | |
148 | 44 return a + b; |
45 } | |
46 | |
215 | 47 var int a, b; |
48 | |
166 | 49 function int t3(int aap, int blah) |
50 { | |
215 | 51 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true) |
166 | 52 { |
215 | 53 a = 2 + t2(a - 1, 0); |
166 | 54 } |
55 | |
56 return a + b; | |
57 } | |
58 | |
155 | 59 var int hahaa = 23 * 2; |
150 | 60 |
149 | 61 |
148 | 62 """ |
63 | |
204 | 64 class testLexer(unittest.TestCase): |
205 | 65 def testUnexpectedCharacter(self): |
66 snippet = """ var s \u6c34 """ | |
67 with self.assertRaises(ppci.CompilerError): | |
68 list(c3.lexer.tokenize(snippet)) | |
69 | |
204 | 70 def testBlockComment(self): |
71 snippet = """ | |
72 /* Demo */ | |
73 var int x = 0; | |
74 """ | |
75 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
76 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
205 | 77 |
204 | 78 def testBlockCommentMultiLine(self): |
79 snippet = """ | |
80 /* Demo | |
81 bla1 | |
82 bla2 | |
83 */ | |
84 var int x = 0; | |
85 """ | |
86 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
87 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
88 | |
89 class testBuilder(unittest.TestCase): | |
215 | 90 def setUp(self): |
91 self.diag = ppci.DiagnosticsManager() | |
92 self.builder = c3.Builder(self.diag) | |
93 self.diag.clear() | |
94 | |
95 def testSrc(self): | |
96 self.expectOK(testsrc) | |
97 | |
98 def expectErrors(self, snippet, rows): | |
99 """ Helper to test for expected errors on rows """ | |
100 ircode = self.builder.build(snippet) | |
101 actualErrors = [err.row for err in self.diag.diags] | |
102 if rows != actualErrors: | |
103 self.diag.printErrors(snippet) | |
104 self.assertSequenceEqual(rows, actualErrors) | |
105 self.assertFalse(ircode) | |
106 | |
107 def expectOK(self, snippet): | |
108 ircode = self.builder.build(snippet) | |
109 if not ircode: | |
110 self.diag.printErrors(snippet) | |
111 self.assertTrue(ircode) | |
112 | |
113 def testFunctArgs(self): | |
167 | 114 snippet = """ |
115 package testargs; | |
116 function void t2(int a, double b) | |
117 { | |
118 t2(2, 2); | |
119 t2(2); | |
120 t2(1, 1.2); | |
121 } | |
122 """ | |
215 | 123 self.expectErrors(snippet, [5, 6]) |
148 | 124 |
215 | 125 def testExpressions(self): |
167 | 126 snippet = """ |
127 package test; | |
128 function void t(int a, double b) | |
129 { | |
130 var int a2; | |
131 var bool c; | |
132 | |
133 a2 = b * a; | |
134 c = a; | |
135 c = b > 1; | |
136 } | |
137 """ | |
215 | 138 self.expectErrors(snippet, [8, 9, 10]) |
205 | 139 |
215 | 140 def testEmpty(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
141 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
142 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
143 """ |
215 | 144 self.expectErrors(snippet, [3]) |
205 | 145 |
215 | 146 def testEmpty2(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
147 snippet = "" |
215 | 148 self.expectErrors(snippet, [1]) |
205 | 149 |
215 | 150 def testRedefine(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
151 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
152 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
153 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
154 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
155 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
156 """ |
215 | 157 self.expectErrors(snippet, [5]) |
205 | 158 |
215 | 159 def testWhile(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
160 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
161 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
162 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
164 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
165 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
166 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
167 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
168 i = i + 3; |
186 | 169 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
170 } |
205 | 171 |
172 while(true) | |
173 { | |
174 } | |
175 | |
176 while(false) | |
177 { | |
178 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
179 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
180 """ |
215 | 181 self.expectOK(snippet) |
182 | |
183 def testIf(self): | |
184 snippet = """ | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
185 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 { |
180 | 188 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
190 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
191 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
193 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
194 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
195 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
197 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
198 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
199 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
200 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
201 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
202 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 } |
215 | 204 """ |
205 self.expectOK(snippet) | |
213 | 206 |
215 | 207 @unittest.skip |
208 def testPointerType(self): | |
213 | 209 snippet = """ |
210 package testpointer; | |
211 var int* pa; | |
212 function void t(int a, double b) | |
213 { | |
214 *pa = 22; | |
215 } | |
216 """ | |
215 | 217 self.expectOK(snippet) |
213 | 218 |
215 | 219 @unittest.skip |
220 def testComplexType(self): | |
213 | 221 snippet = """ |
222 package testpointer; | |
223 type int my_int; | |
224 | |
225 type struct { | |
226 int x, y; | |
227 } point; | |
228 | |
229 type struct { | |
230 int mem1; | |
231 int memb2; | |
232 point P1; | |
233 } my_struct; | |
234 | |
235 type my_struct* my_sptr; | |
236 | |
237 function void t(int a, double b, my_sptr x) | |
238 { | |
239 var my_struct *msp; | |
240 | |
241 msp = x; | |
242 *pa = 22; | |
243 x->memb2 = *pa + a * b; | |
244 | |
245 mxp->P1.x = a * x->P1.y; | |
246 } | |
247 """ | |
215 | 248 self.expectOK(snippet) |
205 | 249 |
215 | 250 def test2(self): |
205 | 251 # testsrc2 is valid code: |
215 | 252 snippet = """ |
205 | 253 package test2; |
254 | |
255 function void tst() | |
256 { | |
257 var int a, b; | |
258 a = 2 * 33 - 12; | |
259 b = a * 2 + 13; | |
260 a = b + a; | |
261 if (a > b and b == 3) | |
262 { | |
263 var int x = a; | |
264 x = b * 2 - a; | |
265 a = x*x; | |
266 } | |
267 else | |
268 { | |
269 a = b + a; | |
270 } | |
271 } | |
272 | |
273 """ | |
215 | 274 self.expectOK(snippet) |
167 | 275 |
276 if __name__ == '__main__': | |
277 unittest.main() | |
278 | |
279 |