blob: ddb169fe58957c22e38593ee69acc8663024838d [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)):
Zackery Spytzceb93f42017-07-29 10:05:55 -0600142 support.run_doctest(test_listcomps, 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)