Mercurial > lcfOS
comparison test/testc3.py @ 287:1c7c1e619be8
File movage
author | Windel Bouwman |
---|---|
date | Thu, 21 Nov 2013 11:57:27 +0100 |
parents | 05184b95fa16 |
children | a747a45dcd78 |
comparison
equal
deleted
inserted
replaced
286:d9df72971cbf | 287:1c7c1e619be8 |
---|---|
1 import c3 | 1 import c3 |
2 import time, ppci, x86, ir | 2 import time |
3 import ppci | |
4 import x86 | |
5 import ir | |
3 import unittest | 6 import unittest |
4 import glob | 7 import glob |
8 import io | |
5 | 9 |
6 testsrc = """module test; | 10 testsrc = """module test; |
7 | 11 |
8 /* | 12 /* |
9 demo of the source that is correct :) | 13 demo of the source that is correct :) |
62 | 66 |
63 """ | 67 """ |
64 | 68 |
65 class testLexer(unittest.TestCase): | 69 class testLexer(unittest.TestCase): |
66 def testUnexpectedCharacter(self): | 70 def testUnexpectedCharacter(self): |
67 snippet = """ var s \u6c34 """ | 71 snippet = io.StringIO(""" var s \u6c34 """) |
68 with self.assertRaises(ppci.CompilerError): | 72 with self.assertRaises(ppci.CompilerError): |
69 list(c3.lexer.tokenize(snippet)) | 73 list(c3.lexer.tokenize(snippet)) |
70 | 74 |
71 def testBlockComment(self): | 75 def testBlockComment(self): |
72 snippet = """ | 76 snippet = io.StringIO(""" |
73 /* Demo */ | 77 /* Demo */ |
74 var int x = 0; | 78 var int x = 0; |
75 """ | 79 """) |
76 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | 80 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] |
77 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | 81 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) |
78 | 82 |
79 def testBlockCommentMultiLine(self): | 83 def testBlockCommentMultiLine(self): |
80 snippet = """ | 84 snippet = io.StringIO(""" |
81 /* Demo | 85 /* Demo |
82 bla1 | 86 bla1 |
83 bla2 | 87 bla2 |
84 */ | 88 */ |
85 var int x = 0; | 89 var int x = 0; |
86 """ | 90 """) |
87 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | 91 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] |
88 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | 92 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) |
93 | |
89 | 94 |
90 class testBuilder(unittest.TestCase): | 95 class testBuilder(unittest.TestCase): |
91 def setUp(self): | 96 def setUp(self): |
92 self.diag = ppci.DiagnosticsManager() | 97 self.diag = ppci.DiagnosticsManager() |
93 self.builder = c3.Builder(self.diag) | 98 self.builder = c3.Builder(self.diag) |
96 def testSrc(self): | 101 def testSrc(self): |
97 self.expectOK(testsrc) | 102 self.expectOK(testsrc) |
98 | 103 |
99 def expectErrors(self, snippet, rows): | 104 def expectErrors(self, snippet, rows): |
100 """ Helper to test for expected errors on rows """ | 105 """ Helper to test for expected errors on rows """ |
101 ircode = self.builder.build(snippet) | 106 ircode = list(self.builder.build([io.StringIO(snippet)])) |
102 actualErrors = [err.row for err in self.diag.diags] | 107 actualErrors = [err.row for err in self.diag.diags] |
103 if rows != actualErrors: | 108 if rows != actualErrors: |
104 self.diag.printErrors(snippet) | 109 self.diag.printErrors(snippet) |
105 self.assertSequenceEqual(rows, actualErrors) | 110 self.assertSequenceEqual(rows, actualErrors) |
106 self.assertFalse(ircode) | 111 # self.assertFalse(all(ircode)) |
107 | 112 |
108 def expectOK(self, snippet, pack_dir=None): | 113 def expectOK(self, snippet): |
109 ircode = self.builder.build(snippet, pack_dir=pack_dir) | 114 if type(snippet) is list: |
115 ircode = self.builder.build(snippet) | |
116 else: | |
117 ircode = self.builder.build([io.StringIO(snippet)]) | |
110 if not ircode: | 118 if not ircode: |
111 self.diag.printErrors(snippet) | 119 self.diag.printErrors(snippet) |
112 self.assertTrue(ircode) | 120 self.assertTrue(all(ircode)) |
113 return ircode | 121 return ircode |
114 | 122 |
115 def testPackage(self): | 123 def testPackage(self): |
116 p1 = """module p1; | 124 p1 = """module p1; |
117 type int A; | 125 type int A; |
118 """ | 126 """ |
119 self.assertTrue(self.builder.build(p1)) | |
120 p2 = """module p2; | 127 p2 = """module p2; |
121 import p1; | 128 import p1; |
122 var A b; | 129 var A b; |
123 """ | 130 """ |
124 self.expectOK(p2) | 131 self.expectOK([io.StringIO(s) for s in (p1, p2)]) |
125 | 132 |
126 def testFunctArgs(self): | 133 def testFunctArgs(self): |
127 snippet = """ | 134 snippet = """ |
128 module testargs; | 135 module testargs; |
129 function void t2(int a, double b) | 136 function void t2(int a, double b) |
130 { | 137 { |
131 t2(2, 2); | 138 t2(2, 2); |
132 t2(2); | 139 t2(2); |
133 t2(1, 1.2); | 140 t2(1, 1.2); |
134 } | 141 } |
135 """ | 142 """ |
136 self.expectErrors(snippet, [5, 6]) | 143 self.expectErrors(snippet, [5, 6]) |
137 | 144 |
138 def testExpressions(self): | 145 def testExpressions(self): |
139 snippet = """ | 146 snippet = """ |
140 module test; | 147 module test; |
141 function void t(int a, double b) | 148 function void t(int a, double b) |
142 { | 149 { |
143 var int a2; | 150 var int a2; |
144 var bool c; | 151 var bool c; |
145 | 152 |
146 a2 = b * a; | 153 a2 = b * a; |
147 c = a; | 154 c = a; |
148 c = b > 1; | 155 c = b > 1; |
149 } | 156 } |
150 """ | 157 """ |
151 self.expectErrors(snippet, [8, 9, 10]) | 158 self.expectErrors(snippet, [8, 9, 10]) |
152 | 159 |
153 def testExpression1(self): | 160 def testExpression1(self): |
154 snippet = """ | 161 snippet = """ |
155 module testexpr1; | 162 module testexpr1; |
156 function void t() | 163 function void t() |
157 { | 164 { |
158 var int a, b, c; | 165 var int a, b, c; |
159 a = 1; | 166 a = 1; |
160 b = a * 2 + a * a; | 167 b = a * 2 + a * a; |
161 c = b * a - 3; | 168 c = b * a - 3; |
162 } | 169 } |
163 """ | 170 """ |
164 self.expectOK(snippet) | 171 self.expectOK(snippet) |
165 | 172 |
166 def testEmpty(self): | 173 def testEmpty(self): |
167 snippet = """ | 174 snippet = """ |
168 module A | 175 module A |
169 """ | 176 """ |
170 self.expectErrors(snippet, [3]) | 177 self.expectErrors(snippet, [3]) |
171 | 178 |
172 def testEmpty2(self): | 179 def testEmpty2(self): |
173 snippet = "" | 180 snippet = "" |
174 self.expectErrors(snippet, [1]) | 181 self.expectErrors(snippet, [1]) |
175 | 182 |
176 def testRedefine(self): | 183 def testRedefine(self): |
177 snippet = """ | 184 snippet = """ |
178 module test; | 185 module test; |
179 var int a; | 186 var int a; |
180 var int b; | 187 var int b; |
181 var int a; | 188 var int a; |
182 """ | 189 """ |
183 self.expectErrors(snippet, [5]) | 190 self.expectErrors(snippet, [5]) |
184 | 191 |
185 def testWhile(self): | 192 def testWhile(self): |
186 snippet = """ | 193 snippet = """ |
187 module tstwhile; | 194 module tstwhile; |
188 var int a; | 195 var int a; |
189 function void t() | 196 function void t() |
190 { | 197 { |
191 var int i = 0; | 198 var int i = 0; |
192 while (i < 1054) | 199 while (i < 1054) |
193 { | 200 { |
194 i = i + 3; | 201 i = i + 3; |
195 a = a + i; | 202 a = a + i; |
200 } | 207 } |
201 | 208 |
202 while(false) | 209 while(false) |
203 { | 210 { |
204 } | 211 } |
205 } | 212 } |
206 """ | 213 """ |
207 self.expectOK(snippet) | 214 self.expectOK(snippet) |
208 | 215 |
209 def testIf(self): | 216 def testIf(self): |
210 snippet = """ | 217 snippet = """ |
211 module tstIFF; | 218 module tstIFF; |
212 function void t(int b) | 219 function void t(int b) |
213 { | 220 { |
214 var int a; | 221 var int a; |
215 a = 2; | 222 a = 2; |
216 if (a > b) | 223 if (a > b) |
217 { | 224 { |
218 if (a > 1337) | 225 if (a > 1337) |
224 { | 231 { |
225 b = 1; | 232 b = 1; |
226 } | 233 } |
227 | 234 |
228 return b; | 235 return b; |
229 } | 236 } |
230 """ | 237 """ |
231 self.expectOK(snippet) | 238 self.expectOK(snippet) |
232 | 239 |
233 def testTypeDef(self): | 240 def testTypeDef(self): |
234 snippet = """ | 241 snippet = """ |
424 a = b + a; | 431 a = b + a; |
425 } | 432 } |
426 } | 433 } |
427 | 434 |
428 """ | 435 """ |
429 ircode = self.expectOK(snippet) | 436 self.expectOK(snippet) |
430 self.assertEqual(1, len(ircode.Functions)) | |
431 | 437 |
432 if __name__ == '__main__': | 438 if __name__ == '__main__': |
433 unittest.main() | 439 unittest.main() |
434 | 440 |
435 | 441 |