blob: 663ccbbe194f0a1764d25432620de343b4b0d7fa [file] [log] [blame]
Nick Coghlan650f0d02007-04-15 12:05:43 +00001doctests = """
2########### Tests borrowed from or inspired by test_genexps.py ############
3
4Test simple loop with conditional
5
6 >>> sum([i*i for i in range(100) if i&1 == 1])
7 166650
8
9Test simple nesting
10
11 >>> [(i,j) for i in range(3) for j in range(4)]
12 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
13
14Test nesting with the inner expression dependent on the outer
15
16 >>> [(i,j) for i in range(4) for j in range(i)]
17 [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
18
19Make sure the induction variable is not exposed
20
21 >>> i = 20
22 >>> sum([i*i for i in range(100)])
23 328350
24
25 >>> i
26 20
27
28Verify that syntax error's are raised for listcomps used as lvalues
29
30 >>> [y for y in (1,2)] = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
31 Traceback (most recent call last):
32 ...
33 SyntaxError: ...
34
35 >>> [y for y in (1,2)] += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
36 Traceback (most recent call last):
37 ...
38 SyntaxError: ...
39
40
41########### Tests borrowed from or inspired by test_generators.py ############
42
43Make a nested list comprehension that acts like range()
44
45 >>> def frange(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000046 ... return [i for i in range(n)]
Nick Coghlan650f0d02007-04-15 12:05:43 +000047 >>> frange(10)
48 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
49
50Same again, only as a lambda expression instead of a function definition
51
Guido van Rossum805365e2007-05-07 22:24:25 +000052 >>> lrange = lambda n: [i for i in range(n)]
Nick Coghlan650f0d02007-04-15 12:05:43 +000053 >>> lrange(10)
54 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
55
56Generators can call other generators:
57
58 >>> def grange(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000059 ... for x in [i for i in range(n)]:
Nick Coghlan650f0d02007-04-15 12:05:43 +000060 ... yield x
61 >>> list(grange(5))
62 [0, 1, 2, 3, 4]
63
64
65Make sure that None is a valid return value
66
Guido van Rossum805365e2007-05-07 22:24:25 +000067 >>> [None for i in range(10)]
Nick Coghlan650f0d02007-04-15 12:05:43 +000068 [None, None, None, None, None, None, None, None, None, None]
69
70########### Tests for various scoping corner cases ############
71
72Return lambdas that use the iteration variable as a default argument
73
74 >>> items = [(lambda i=i: i) for i in range(5)]
75 >>> [x() for x in items]
76 [0, 1, 2, 3, 4]
77
78Same again, only this time as a closure variable
79
80 >>> items = [(lambda: i) for i in range(5)]
81 >>> [x() for x in items]
82 [4, 4, 4, 4, 4]
83
84Another way to test that the iteration variable is local to the list comp
85
86 >>> items = [(lambda: i) for i in range(5)]
87 >>> i = 20
88 >>> [x() for x in items]
89 [4, 4, 4, 4, 4]
90
91And confirm that a closure can jump over the list comp scope
92
93 >>> items = [(lambda: y) for i in range(5)]
94 >>> y = 2
95 >>> [x() for x in items]
96 [2, 2, 2, 2, 2]
97
98We also repeat each of the above scoping tests inside a function
99
100 >>> def test_func():
101 ... items = [(lambda i=i: i) for i in range(5)]
102 ... return [x() for x in items]
103 >>> test_func()
104 [0, 1, 2, 3, 4]
105
106 >>> def test_func():
107 ... items = [(lambda: i) for i in range(5)]
108 ... return [x() for x in items]
109 >>> test_func()
110 [4, 4, 4, 4, 4]
111
112 >>> def test_func():
113 ... items = [(lambda: i) for i in range(5)]
114 ... i = 20
115 ... return [x() for x in items]
116 >>> test_func()
117 [4, 4, 4, 4, 4]
118
119 >>> def test_func():
120 ... items = [(lambda: y) for i in range(5)]
121 ... y = 2
122 ... return [x() for x in items]
123 >>> test_func()
124 [2, 2, 2, 2, 2]
125
126"""
127
128
129__test__ = {'doctests' : doctests}
130
131def test_main(verbose=None):
132 import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000133 from test import support
Nick Coghlan650f0d02007-04-15 12:05:43 +0000134 from test import test_listcomps
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000135 support.run_doctest(test_listcomps, verbose)
Nick Coghlan650f0d02007-04-15 12:05:43 +0000136
137 # verify reference counting
138 if verbose and hasattr(sys, "gettotalrefcount"):
139 import gc
140 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000141 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000142 support.run_doctest(test_genexps, verbose)
Nick Coghlan650f0d02007-04-15 12:05:43 +0000143 gc.collect()
144 counts[i] = sys.gettotalrefcount()
145 print(counts)
146
147if __name__ == "__main__":
148 test_main(verbose=True)
149doctests = """
150########### Tests borrowed from or inspired by test_genexps.py ############
151
152Test simple loop with conditional
153
154 >>> sum([i*i for i in range(100) if i&1 == 1])
155 166650
156
157Test simple nesting
158
159 >>> [(i,j) for i in range(3) for j in range(4)]
160 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
161
162Test nesting with the inner expression dependent on the outer
163
164 >>> [(i,j) for i in range(4) for j in range(i)]
165 [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
166
167Make sure the induction variable is not exposed
168
169 >>> i = 20
170 >>> sum([i*i for i in range(100)])
171 328350
172
173 >>> i
174 20
175
176Verify that syntax error's are raised for listcomps used as lvalues
177
178 >>> [y for y in (1,2)] = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
179 Traceback (most recent call last):
180 ...
181 SyntaxError: ...
182
183 >>> [y for y in (1,2)] += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
184 Traceback (most recent call last):
185 ...
186 SyntaxError: ...
187
188
189########### Tests borrowed from or inspired by test_generators.py ############
190
191Make a nested list comprehension that acts like range()
192
193 >>> def frange(n):
Guido van Rossum805365e2007-05-07 22:24:25 +0000194 ... return [i for i in range(n)]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000195 >>> frange(10)
196 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
197
198Same again, only as a lambda expression instead of a function definition
199
Guido van Rossum805365e2007-05-07 22:24:25 +0000200 >>> lrange = lambda n: [i for i in range(n)]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000201 >>> lrange(10)
202 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
203
204Generators can call other generators:
205
206 >>> def grange(n):
Guido van Rossum805365e2007-05-07 22:24:25 +0000207 ... for x in [i for i in range(n)]:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000208 ... yield x
209 >>> list(grange(5))
210 [0, 1, 2, 3, 4]
211
212
213Make sure that None is a valid return value
214
Guido van Rossum805365e2007-05-07 22:24:25 +0000215 >>> [None for i in range(10)]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000216 [None, None, None, None, None, None, None, None, None, None]
217
218########### Tests for various scoping corner cases ############
219
220Return lambdas that use the iteration variable as a default argument
221
222 >>> items = [(lambda i=i: i) for i in range(5)]
223 >>> [x() for x in items]
224 [0, 1, 2, 3, 4]
225
226Same again, only this time as a closure variable
227
228 >>> items = [(lambda: i) for i in range(5)]
229 >>> [x() for x in items]
230 [4, 4, 4, 4, 4]
231
232Another way to test that the iteration variable is local to the list comp
233
234 >>> items = [(lambda: i) for i in range(5)]
235 >>> i = 20
236 >>> [x() for x in items]
237 [4, 4, 4, 4, 4]
238
239And confirm that a closure can jump over the list comp scope
240
241 >>> items = [(lambda: y) for i in range(5)]
242 >>> y = 2
243 >>> [x() for x in items]
244 [2, 2, 2, 2, 2]
245
246We also repeat each of the above scoping tests inside a function
247
248 >>> def test_func():
249 ... items = [(lambda i=i: i) for i in range(5)]
250 ... return [x() for x in items]
251 >>> test_func()
252 [0, 1, 2, 3, 4]
253
254 >>> def test_func():
255 ... items = [(lambda: i) for i in range(5)]
256 ... return [x() for x in items]
257 >>> test_func()
258 [4, 4, 4, 4, 4]
259
260 >>> def test_func():
261 ... items = [(lambda: i) for i in range(5)]
262 ... i = 20
263 ... return [x() for x in items]
264 >>> test_func()
265 [4, 4, 4, 4, 4]
266
267 >>> def test_func():
268 ... items = [(lambda: y) for i in range(5)]
269 ... y = 2
270 ... return [x() for x in items]
271 >>> test_func()
272 [2, 2, 2, 2, 2]
273
274"""
275
276
277__test__ = {'doctests' : doctests}
278
279def test_main(verbose=None):
280 import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000281 from test import support
Nick Coghlan650f0d02007-04-15 12:05:43 +0000282 from test import test_listcomps
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000283 support.run_doctest(test_listcomps, verbose)
Nick Coghlan650f0d02007-04-15 12:05:43 +0000284
285 # verify reference counting
286 if verbose and hasattr(sys, "gettotalrefcount"):
287 import gc
288 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000289 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000290 support.run_doctest(test_genexps, verbose)
Nick Coghlan650f0d02007-04-15 12:05:43 +0000291 gc.collect()
292 counts[i] = sys.gettotalrefcount()
293 print(counts)
294
295if __name__ == "__main__":
296 test_main(verbose=True)
297doctests = """
298########### Tests borrowed from or inspired by test_genexps.py ############
299
300Test simple loop with conditional
301
302 >>> sum([i*i for i in range(100) if i&1 == 1])
303 166650
304
305Test simple nesting
306
307 >>> [(i,j) for i in range(3) for j in range(4)]
308 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
309
310Test nesting with the inner expression dependent on the outer
311
312 >>> [(i,j) for i in range(4) for j in range(i)]
313 [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
314
315Make sure the induction variable is not exposed
316
317 >>> i = 20
318 >>> sum([i*i for i in range(100)])
319 328350
320
321 >>> i
322 20
323
324Verify that syntax error's are raised for listcomps used as lvalues
325
326 >>> [y for y in (1,2)] = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
327 Traceback (most recent call last):
328 ...
329 SyntaxError: ...
330
331 >>> [y for y in (1,2)] += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
332 Traceback (most recent call last):
333 ...
334 SyntaxError: ...
335
336
337########### Tests borrowed from or inspired by test_generators.py ############
338
339Make a nested list comprehension that acts like range()
340
341 >>> def frange(n):
Guido van Rossum805365e2007-05-07 22:24:25 +0000342 ... return [i for i in range(n)]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000343 >>> frange(10)
344 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
345
346Same again, only as a lambda expression instead of a function definition
347
Guido van Rossum805365e2007-05-07 22:24:25 +0000348 >>> lrange = lambda n: [i for i in range(n)]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000349 >>> lrange(10)
350 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
351
352Generators can call other generators:
353
354 >>> def grange(n):
Guido van Rossum805365e2007-05-07 22:24:25 +0000355 ... for x in [i for i in range(n)]:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000356 ... yield x
357 >>> list(grange(5))
358 [0, 1, 2, 3, 4]
359
360
361Make sure that None is a valid return value
362
Guido van Rossum805365e2007-05-07 22:24:25 +0000363 >>> [None for i in range(10)]
Nick Coghlan650f0d02007-04-15 12:05:43 +0000364 [None, None, None, None, None, None, None, None, None, None]
365
366########### Tests for various scoping corner cases ############
367
368Return lambdas that use the iteration variable as a default argument
369
370 >>> items = [(lambda i=i: i) for i in range(5)]
371 >>> [x() for x in items]
372 [0, 1, 2, 3, 4]
373
374Same again, only this time as a closure variable
375
376 >>> items = [(lambda: i) for i in range(5)]
377 >>> [x() for x in items]
378 [4, 4, 4, 4, 4]
379
380Another way to test that the iteration variable is local to the list comp
381
382 >>> items = [(lambda: i) for i in range(5)]
383 >>> i = 20
384 >>> [x() for x in items]
385 [4, 4, 4, 4, 4]
386
387And confirm that a closure can jump over the list comp scope
388
389 >>> items = [(lambda: y) for i in range(5)]
390 >>> y = 2
391 >>> [x() for x in items]
392 [2, 2, 2, 2, 2]
393
394We also repeat each of the above scoping tests inside a function
395
396 >>> def test_func():
397 ... items = [(lambda i=i: i) for i in range(5)]
398 ... return [x() for x in items]
399 >>> test_func()
400 [0, 1, 2, 3, 4]
401
402 >>> def test_func():
403 ... items = [(lambda: i) for i in range(5)]
404 ... return [x() for x in items]
405 >>> test_func()
406 [4, 4, 4, 4, 4]
407
408 >>> def test_func():
409 ... items = [(lambda: i) for i in range(5)]
410 ... i = 20
411 ... return [x() for x in items]
412 >>> test_func()
413 [4, 4, 4, 4, 4]
414
415 >>> def test_func():
416 ... items = [(lambda: y) for i in range(5)]
417 ... y = 2
418 ... return [x() for x in items]
419 >>> test_func()
420 [2, 2, 2, 2, 2]
421
422"""
423
424
425__test__ = {'doctests' : doctests}
426
427def test_main(verbose=None):
428 import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000429 from test import support
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 from test import test_listcomps
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000431 support.run_doctest(test_listcomps, verbose)
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432
433 # verify reference counting
434 if verbose and hasattr(sys, "gettotalrefcount"):
435 import gc
436 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000437 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000438 support.run_doctest(test_listcomps, verbose)
Nick Coghlan650f0d02007-04-15 12:05:43 +0000439 gc.collect()
440 counts[i] = sys.gettotalrefcount()
441 print(counts)
442
443if __name__ == "__main__":
444 test_main(verbose=True)