blob: b09fc952bf561ec6243b8072434a363df753fc54 [file] [log] [blame]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001doctests = """
2
3Test simple loop with conditional
4
5 >>> sum(i*i for i in range(100) if i&1 == 1)
6 166650
7
8Test simple nesting
9
10 >>> list((i,j) for i in range(3) for j in range(4) )
11 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
12
13Test nesting with the inner expression dependent on the outer
14
15 >>> list((i,j) for i in range(4) for j in range(i) )
16 [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
17
18Make sure the induction variable is not exposed
19
20 >>> i = 20
21 >>> sum(i*i for i in range(100))
22 328350
23 >>> i
24 20
25
26Test first class
27
28 >>> g = (i*i for i in range(4))
29 >>> type(g)
30 <type 'generator'>
31 >>> list(g)
32 [0, 1, 4, 9]
33
34Test direct calls to next()
35
36 >>> g = (i*i for i in range(3))
37 >>> g.next()
38 0
39 >>> g.next()
40 1
41 >>> g.next()
42 4
43 >>> g.next()
44 Traceback (most recent call last):
45 File "<pyshell#21>", line 1, in -toplevel-
46 g.next()
47 StopIteration
48
49Does it stay stopped?
50
51 >>> g.next()
52 Traceback (most recent call last):
53 File "<pyshell#21>", line 1, in -toplevel-
54 g.next()
55 StopIteration
56 >>> list(g)
57 []
58
59Test running gen when defining function is out of scope
60
61 >>> def f(n):
62 ... return (i*i for i in xrange(n))
63 ...
64 >>> list(f(10))
65 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
66
67 >>> def f(n):
68 ... return ((i,j) for i in xrange(3) for j in xrange(n))
69 ...
70 >>> list(f(4))
71 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
72 >>> def f(n):
73 ... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n))
74 ...
75 >>> list(f(4))
76 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
77 >>> list(f(2))
78 [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
79
80#Verify that parenthesis are required in a statement
81#>>> def f(n):
82#... return i*i for i in xrange(n)
83#...
84#SyntaxError: invalid syntax
85
86Verify early binding for the outermost for-expression
87
88 >>> x=10
89 >>> g = (i*i for i in range(x))
90 >>> x = 5
91 >>> list(g)
92 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
93
94Verify late binding for the outermost if-expression
95
96 >>> include = (2,4,6,8)
97 >>> g = (i*i for i in range(10) if i in include)
98 >>> include = (1,3,5,7,9)
99 >>> list(g)
100 [1, 9, 25, 49, 81]
101
102Verify late binding for the innermost for-expression
103
104 >>> g = ((i,j) for i in range(3) for j in range(x))
105 >>> x = 4
106 >>> list(g)
107 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
108
109Verify re-use of tuples (a side benefit of using genexps over listcomps)
110
111 >>> tupleids = map(id, ((i,i) for i in xrange(10)))
112 >>> max(tupleids) - min(tupleids)
113 0
114
115
116
117########### Tests borrowed from or inspired by test_generators.py ############
118
119Make a generator that acts like range()
120
121 >>> yrange = lambda n: (i for i in xrange(n))
122 >>> list(yrange(10))
123 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
124
125Generators always return to the most recent caller:
126
127 >>> def creator():
128 ... r = yrange(5)
129 ... print "creator", r.next()
130 ... return r
131 ...
132 >>> def caller():
133 ... r = creator()
134 ... for i in r:
135 ... print "caller", i
136 ...
137 >>> caller()
138 creator 0
139 caller 1
140 caller 2
141 caller 3
142 caller 4
143
144Generators can call other generators:
145
146 >>> def zrange(n):
147 ... for i in yrange(n):
148 ... yield i
149 ...
150 >>> list(zrange(5))
151 [0, 1, 2, 3, 4]
152
153
154Verify that a gen exp cannot be resumed while it is actively running:
155
156 >>> g = (me.next() for i in xrange(10))
157 >>> me = g
158 >>> me.next()
159 Traceback (most recent call last):
160 File "<pyshell#30>", line 1, in -toplevel-
161 me.next()
162 File "<pyshell#28>", line 1, in <generator expression>
163 g = (me.next() for i in xrange(10))
164 ValueError: generator already executing
165
166Verify exception propagation
167
168 >>> g = (10 // i for i in (5, 0, 2))
169 >>> g.next()
170 2
171 >>> g.next()
172 Traceback (most recent call last):
173 File "<pyshell#37>", line 1, in -toplevel-
174 g.next()
175 File "<pyshell#35>", line 1, in <generator expression>
176 g = (10 // i for i in (5, 0, 2))
177 ZeroDivisionError: integer division or modulo by zero
178 >>> g.next()
179 Traceback (most recent call last):
180 File "<pyshell#38>", line 1, in -toplevel-
181 g.next()
182 StopIteration
183
184Make sure that None is a valid return value
185
186 >>> list(None for i in xrange(10))
187 [None, None, None, None, None, None, None, None, None, None]
188
189Check that generator attributes are present
190
191 >>> g = (i*i for i in range(3))
192 >>> expected = set(['gi_frame', 'gi_running', 'next'])
193 >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
194 True
195
196 >>> print g.next.__doc__
197 x.next() -> the next value, or raise StopIteration
198 >>> import types
199 >>> isinstance(g, types.GeneratorType)
200 True
201
202Check the __iter__ slot is defined to return self
203
204 >>> iter(g) is g
205 True
206
207Verify that the running flag is set properly
208
209 >>> g = (me.gi_running for i in (0,1))
210 >>> me = g
211 >>> me.gi_running
212 0
213 >>> me.next()
214 1
215 >>> me.gi_running
216 0
217
218Verify that genexps are weakly referencable
219
220 >>> import weakref
221 >>> g = (i*i for i in range(4))
222 >>> wr = weakref.ref(g)
223 >>> wr() is g
224 True
225 >>> p = weakref.proxy(g)
226 >>> list(p)
227 [0, 1, 4, 9]
228
229
230"""
231
232
233__test__ = {'doctests' : doctests}
234
235def test_main(verbose=None):
236 import sys
237 from test import test_support
238 from test import test_genexps
239 test_support.run_doctest(test_genexps, verbose)
240
241 # verify reference counting
242 if verbose and hasattr(sys, "gettotalrefcount"):
243 import gc
244 counts = [None] * 5
245 for i in xrange(len(counts)):
246 test_support.run_doctest(test_genexps, verbose)
247 gc.collect()
248 counts[i] = sys.gettotalrefcount()
249 print counts
250
251if __name__ == "__main__":
252 test_main(verbose=True)
253
254
255
256
257
258