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

Python code coverage for Lib/test/test_listcomps.py

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