Mercurial > lcfOS
annotate python/testc3.py @ 217:8b2e5f3cd579
Removed some stale python source files
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 14:13:59 +0200 |
parents | c1ccb1cb4cef |
children | 3f6c30a5d234 |
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) | |
217 | 112 return ircode |
215 | 113 |
114 def testFunctArgs(self): | |
167 | 115 snippet = """ |
116 package testargs; | |
117 function void t2(int a, double b) | |
118 { | |
119 t2(2, 2); | |
120 t2(2); | |
121 t2(1, 1.2); | |
122 } | |
123 """ | |
215 | 124 self.expectErrors(snippet, [5, 6]) |
148 | 125 |
215 | 126 def testExpressions(self): |
167 | 127 snippet = """ |
128 package test; | |
129 function void t(int a, double b) | |
130 { | |
131 var int a2; | |
132 var bool c; | |
133 | |
134 a2 = b * a; | |
135 c = a; | |
136 c = b > 1; | |
137 } | |
138 """ | |
215 | 139 self.expectErrors(snippet, [8, 9, 10]) |
205 | 140 |
215 | 141 def testEmpty(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
142 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
143 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
144 """ |
215 | 145 self.expectErrors(snippet, [3]) |
205 | 146 |
215 | 147 def testEmpty2(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
148 snippet = "" |
215 | 149 self.expectErrors(snippet, [1]) |
205 | 150 |
215 | 151 def testRedefine(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
152 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
153 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
154 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
155 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
156 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
157 """ |
215 | 158 self.expectErrors(snippet, [5]) |
205 | 159 |
215 | 160 def testWhile(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
161 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
162 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
164 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
165 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
166 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
167 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
168 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
169 i = i + 3; |
186 | 170 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
171 } |
205 | 172 |
173 while(true) | |
174 { | |
175 } | |
176 | |
177 while(false) | |
178 { | |
179 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
180 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
181 """ |
215 | 182 self.expectOK(snippet) |
183 | |
184 def testIf(self): | |
185 snippet = """ | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 { |
180 | 189 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
190 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
191 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
193 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
194 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
195 b = 2; |
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 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
198 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
199 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
200 b = 1; |
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 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
204 } |
215 | 205 """ |
206 self.expectOK(snippet) | |
213 | 207 |
215 | 208 @unittest.skip |
209 def testPointerType(self): | |
213 | 210 snippet = """ |
211 package testpointer; | |
212 var int* pa; | |
213 function void t(int a, double b) | |
214 { | |
215 *pa = 22; | |
216 } | |
217 """ | |
215 | 218 self.expectOK(snippet) |
213 | 219 |
215 | 220 @unittest.skip |
221 def testComplexType(self): | |
213 | 222 snippet = """ |
223 package testpointer; | |
224 type int my_int; | |
225 | |
226 type struct { | |
227 int x, y; | |
228 } point; | |
229 | |
230 type struct { | |
231 int mem1; | |
232 int memb2; | |
233 point P1; | |
234 } my_struct; | |
235 | |
236 type my_struct* my_sptr; | |
237 | |
238 function void t(int a, double b, my_sptr x) | |
239 { | |
240 var my_struct *msp; | |
241 | |
242 msp = x; | |
243 *pa = 22; | |
244 x->memb2 = *pa + a * b; | |
245 | |
246 mxp->P1.x = a * x->P1.y; | |
247 } | |
248 """ | |
215 | 249 self.expectOK(snippet) |
205 | 250 |
215 | 251 def test2(self): |
205 | 252 # testsrc2 is valid code: |
215 | 253 snippet = """ |
205 | 254 package test2; |
255 | |
256 function void tst() | |
257 { | |
258 var int a, b; | |
259 a = 2 * 33 - 12; | |
260 b = a * 2 + 13; | |
261 a = b + a; | |
262 if (a > b and b == 3) | |
263 { | |
264 var int x = a; | |
265 x = b * 2 - a; | |
266 a = x*x; | |
267 } | |
268 else | |
269 { | |
270 a = b + a; | |
271 } | |
272 } | |
273 | |
274 """ | |
217 | 275 ircode = self.expectOK(snippet) |
276 self.assertEqual(1, len(ircode.Functions)) | |
167 | 277 |
278 if __name__ == '__main__': | |
279 unittest.main() | |
280 | |
281 |