ยปCore Development>Code coverage>Lib/test/test_setcomps.py

Python code coverage for Lib/test/test_setcomps.py

#countcontent
1n/adoctests = """
2n/a########### Tests mostly copied from test_listcomps.py ############
3n/a
4n/aTest simple loop with conditional
5n/a
6n/a >>> sum({i*i for i in range(100) if i&1 == 1})
7n/a 166650
8n/a
9n/aTest simple case
10n/a
11n/a >>> {2*y + x + 1 for x in (0,) for y in (1,)}
12n/a {3}
13n/a
14n/aTest simple nesting
15n/a
16n/a >>> list(sorted({(i,j) for i in range(3) for j in range(4)}))
17n/a [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
18n/a
19n/aTest nesting with the inner expression dependent on the outer
20n/a
21n/a >>> list(sorted({(i,j) for i in range(4) for j in range(i)}))
22n/a [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
23n/a
24n/aMake sure the induction variable is not exposed
25n/a
26n/a >>> i = 20
27n/a >>> sum({i*i for i in range(100)})
28n/a 328350
29n/a
30n/a >>> i
31n/a 20
32n/a
33n/aVerify that syntax error's are raised for setcomps used as lvalues
34n/a
35n/a >>> {y for y in (1,2)} = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
36n/a Traceback (most recent call last):
37n/a ...
38n/a SyntaxError: ...
39n/a
40n/a >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
41n/a Traceback (most recent call last):
42n/a ...
43n/a SyntaxError: ...
44n/a
45n/a
46n/aMake a nested set comprehension that acts like set(range())
47n/a
48n/a >>> def srange(n):
49n/a ... return {i for i in range(n)}
50n/a >>> list(sorted(srange(10)))
51n/a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
52n/a
53n/aSame again, only as a lambda expression instead of a function definition
54n/a
55n/a >>> lrange = lambda n: {i for i in range(n)}
56n/a >>> list(sorted(lrange(10)))
57n/a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
58n/a
59n/aGenerators can call other generators:
60n/a
61n/a >>> def grange(n):
62n/a ... for x in {i for i in range(n)}:
63n/a ... yield x
64n/a >>> list(sorted(grange(5)))
65n/a [0, 1, 2, 3, 4]
66n/a
67n/a
68n/aMake sure that None is a valid return value
69n/a
70n/a >>> {None for i in range(10)}
71n/a {None}
72n/a
73n/a########### Tests for various scoping corner cases ############
74n/a
75n/aReturn lambdas that use the iteration variable as a default argument
76n/a
77n/a >>> items = {(lambda i=i: i) for i in range(5)}
78n/a >>> {x() for x in items} == set(range(5))
79n/a True
80n/a
81n/aSame again, only this time as a closure variable
82n/a
83n/a >>> items = {(lambda: i) for i in range(5)}
84n/a >>> {x() for x in items}
85n/a {4}
86n/a
87n/aAnother way to test that the iteration variable is local to the list comp
88n/a
89n/a >>> items = {(lambda: i) for i in range(5)}
90n/a >>> i = 20
91n/a >>> {x() for x in items}
92n/a {4}
93n/a
94n/aAnd confirm that a closure can jump over the list comp scope
95n/a
96n/a >>> items = {(lambda: y) for i in range(5)}
97n/a >>> y = 2
98n/a >>> {x() for x in items}
99n/a {2}
100n/a
101n/aWe also repeat each of the above scoping tests inside a function
102n/a
103n/a >>> def test_func():
104n/a ... items = {(lambda i=i: i) for i in range(5)}
105n/a ... return {x() for x in items}
106n/a >>> test_func() == set(range(5))
107n/a True
108n/a
109n/a >>> def test_func():
110n/a ... items = {(lambda: i) for i in range(5)}
111n/a ... return {x() for x in items}
112n/a >>> test_func()
113n/a {4}
114n/a
115n/a >>> def test_func():
116n/a ... items = {(lambda: i) for i in range(5)}
117n/a ... i = 20
118n/a ... return {x() for x in items}
119n/a >>> test_func()
120n/a {4}
121n/a
122n/a >>> def test_func():
123n/a ... items = {(lambda: y) for i in range(5)}
124n/a ... y = 2
125n/a ... return {x() for x in items}
126n/a >>> test_func()
127n/a {2}
128n/a
129n/a"""
130n/a
131n/a
132n/a__test__ = {'doctests' : doctests}
133n/a
134n/adef test_main(verbose=None):
135n/a import sys
136n/a from test import support
137n/a from test import test_setcomps
138n/a support.run_doctest(test_setcomps, verbose)
139n/a
140n/a # verify reference counting
141n/a if verbose and hasattr(sys, "gettotalrefcount"):
142n/a import gc
143n/a counts = [None] * 5
144n/a for i in range(len(counts)):
145n/a support.run_doctest(test_setcomps, verbose)
146n/a gc.collect()
147n/a counts[i] = sys.gettotalrefcount()
148n/a print(counts)
149n/a
150n/aif __name__ == "__main__":
151n/a test_main(verbose=True)