summaryrefslogtreecommitdiff
path: root/Lib/test/test_grammar.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-05-19 08:20:33 +0000
committerRaymond Hettinger <python@rcn.com>2004-05-19 08:20:33 +0000
commit7499e562f0c628d4e608f4069ad20a0086c15534 (patch)
tree78ad1b9c52166983ecb01bf8fd7fc8df3ffd3618 /Lib/test/test_grammar.py
parentae0d7cddf3c960fcc302e2296447f5f236af10e5 (diff)
downloadcpython-7499e562f0c628d4e608f4069ad20a0086c15534.tar.gz
SF patch #872326: Generator expression implementation
(Code contributed by Jiwon Seo.) The documentation portion of the patch is being re-worked and will be checked-in soon. Likewise, PEP 289 will be updated to reflect Guido's rationale for the design decisions on binding behavior (as described in in his patch comments and in discussions on python-dev). The test file, test_genexps.py, is written in doctest format and is meant to exercise all aspects of the the patch. Further additions are welcome from everyone. Please stress test this new feature as much as possible before the alpha release.
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r--Lib/test/test_grammar.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 0eed4bbc7b..e0d5b745d6 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -739,3 +739,46 @@ print [
for (sp_sno, sp_pno) in suppart
if sno == sp_sno and pno == sp_pno
]
+
+# generator expression tests
+g = ([x for x in range(10)] for x in range(1))
+verify(g.next() == [x for x in range(10)])
+try:
+ g.next()
+ raise TestFailed, 'should produce StopIteration exception'
+except StopIteration:
+ pass
+
+a = 1
+try:
+ g = (a for d in a)
+ g.next()
+ raise TestFailed, 'should produce TypeError'
+except TypeError:
+ pass
+
+verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd'])
+verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy'])
+
+a = [x for x in range(10)]
+b = (x for x in (y for y in a))
+verify(sum(b) == sum([x for x in range(10)]))
+
+verify(sum(x**2 for x in range(10)) == sum([x**2 for x in range(10)]))
+verify(sum(x*x for x in range(10) if x%2) == sum([x*x for x in range(10) if x%2]))
+verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)]))
+verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)]))
+verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)]))
+verify(sum(x for x in (y for y in (z for z in range(10) if True)) if True) == sum([x for x in range(10)]))
+verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0)
+check_syntax("foo(x for x in range(10), 100)")
+check_syntax("foo(100, x for x in range(10))")
+
+# test for outmost iterable precomputation
+x = 10; g = (i for i in range(x)); x = 5
+verify(len(list(g)) == 10)
+
+# This should hold, since we're only precomputing outmost iterable.
+x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
+x = 5; t = True;
+verify([(i,j) for i in range(10) for j in range(5)] == list(g))