2 ########### Tests mostly copied from test_listcomps.py ############
4 Test simple loop with conditional
6 >>> sum({i*i for i in range(100) if i&1 == 1})
11 >>> {2*y + x + 1 for x in (0,) for y in (1,)}
16 >>> list(sorted({(i,j) for i in range(3) for j in range(4)}))
17 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
19 Test nesting with the inner expression dependent on the outer
21 >>> list(sorted({(i,j) for i in range(4) for j in range(i)}))
22 [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
24 Make sure the induction variable is not exposed
27 >>> sum({i*i for i in range(100)})
33 Verify that syntax error's are raised for setcomps used as lvalues
35 >>> {y for y in (1,2)} = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
36 Traceback (most recent call last):
40 >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
41 Traceback (most recent call last):
46 Make a nested set comprehension that acts like set(range())
49 ... return {i for i in range(n)}
50 >>> list(sorted(srange(10)))
51 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
53 Same again, only as a lambda expression instead of a function definition
55 >>> lrange = lambda n: {i for i in range(n)}
56 >>> list(sorted(lrange(10)))
57 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
59 Generators can call other generators:
62 ... for x in {i for i in range(n)}:
64 >>> list(sorted(grange(5)))
68 Make sure that None is a valid return value
70 >>> {None for i in range(10)}
73 ########### Tests for various scoping corner cases ############
75 Return lambdas that use the iteration variable as a default argument
77 >>> items = {(lambda i=i: i) for i in range(5)}
78 >>> {x() for x in items} == set(range(5))
81 Same again, only this time as a closure variable
83 >>> items = {(lambda: i) for i in range(5)}
84 >>> {x() for x in items}
87 Another way to test that the iteration variable is local to the list comp
89 >>> items = {(lambda: i) for i in range(5)}
91 >>> {x() for x in items}
94 And confirm that a closure can jump over the list comp scope
96 >>> items = {(lambda: y) for i in range(5)}
98 >>> {x() for x in items}
101 We also repeat each of the above scoping tests inside a function
104 ... items = {(lambda i=i: i) for i in range(5)}
105 ... return {x() for x in items}
106 >>> test_func() == set(range(5))
110 ... items = {(lambda: i) for i in range(5)}
111 ... return {x() for x in items}
116 ... items = {(lambda: i) for i in range(5)}
118 ... return {x() for x in items}
123 ... items = {(lambda: y) for i in range(5)}
125 ... return {x() for x in items}
132 __test__
= {'doctests' : doctests
}
134 def test_main(verbose
=None):
136 from test
import test_support
137 from test
import test_setcomps
138 test_support
.run_doctest(test_setcomps
, verbose
)
140 # verify reference counting
141 if verbose
and hasattr(sys
, "gettotalrefcount"):
144 for i
in range(len(counts
)):
145 test_support
.run_doctest(test_setcomps
, verbose
)
147 counts
[i
] = sys
.gettotalrefcount()
150 if __name__
== "__main__":
151 test_main(verbose
=True)