Test simple loop with conditional
>>> sum(i*i for i in range(100) if i&1 == 1)
>>> list((i,j) for i in range(3) for j in range(4) )
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
Test nesting with the inner expression dependent on the outer
>>> list((i,j) for i in range(4) for j in range(i) )
[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
Make sure the induction variable is not exposed
>>> sum(i*i for i in range(100))
>>> g = (i*i for i in range(4))
Test direct calls to next()
>>> g = (i*i for i in range(3))
Traceback (most recent call last):
File "<pyshell#21>", line 1, in -toplevel-
Traceback (most recent call last):
File "<pyshell#21>", line 1, in -toplevel-
Test running gen when defining function is out of scope
... return (i*i for i in xrange(n))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
... return ((i,j) for i in xrange(3) for j in xrange(n))
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n))
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
Verify that parenthesis are required in a statement
... return i*i for i in xrange(n)
Traceback (most recent call last):
SyntaxError: invalid syntax
Verify early binding for the outermost for-expression
>>> g = (i*i for i in range(x))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Verify that the outermost for-expression makes an immediate check
Traceback (most recent call last):
File "<pyshell#4>", line 1, in -toplevel-
TypeError: iteration over non-sequence
Verify late binding for the outermost if-expression
>>> g = (i*i for i in range(10) if i in include)
>>> include = (1,3,5,7,9)
Verify late binding for the innermost for-expression
>>> g = ((i,j) for i in range(3) for j in range(x))
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
Verify re-use of tuples (a side benefit of using genexps over listcomps)
>>> tupleids = map(id, ((i,i) for i in xrange(10)))
>>> max(tupleids) - min(tupleids)
Verify that syntax error's are raised for genexps used as lvalues
>>> (y for y in (1,2)) = 10
Traceback (most recent call last):
SyntaxError: assign to generator expression not possible
>>> (y for y in (1,2)) += 10
Traceback (most recent call last):
SyntaxError: augmented assign to tuple literal or generator expression not possible
########### Tests borrowed from or inspired by test_generators.py ############
Make a generator that acts like range()
>>> yrange = lambda n: (i for i in xrange(n))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Generators always return to the most recent caller:
... print "creator", r.next()
Generators can call other generators:
Verify that a gen exp cannot be resumed while it is actively running:
>>> g = (me.next() for i in xrange(10))
Traceback (most recent call last):
File "<pyshell#30>", line 1, in -toplevel-
File "<pyshell#28>", line 1, in <generator expression>
g = (me.next() for i in xrange(10))
ValueError: generator already executing
Verify exception propagation
>>> g = (10 // i for i in (5, 0, 2))
Traceback (most recent call last):
File "<pyshell#37>", line 1, in -toplevel-
File "<pyshell#35>", line 1, in <generator expression>
g = (10 // i for i in (5, 0, 2))
ZeroDivisionError: integer division or modulo by zero
Traceback (most recent call last):
File "<pyshell#38>", line 1, in -toplevel-
Make sure that None is a valid return value
>>> list(None for i in xrange(10))
[None, None, None, None, None, None, None, None, None, None]
Check that generator attributes are present
>>> g = (i*i for i in range(3))
>>> expected = set(['gi_frame', 'gi_running', 'next'])
>>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
x.next() -> the next value, or raise StopIteration
>>> isinstance(g, types.GeneratorType)
Check the __iter__ slot is defined to return self
Verify that the running flag is set properly
>>> g = (me.gi_running for i in (0,1))
Verify that genexps are weakly referencable
>>> g = (i*i for i in range(4))
__test__
= {'doctests' : doctests
}
def test_main(verbose
=None):
from test
import test_support
from test
import test_genexps
test_support
.run_doctest(test_genexps
, verbose
)
# verify reference counting
if verbose
and hasattr(sys
, "gettotalrefcount"):
for i
in xrange(len(counts
)):
test_support
.run_doctest(test_genexps
, verbose
)
counts
[i
] = sys
.gettotalrefcount()
if __name__
== "__main__":