Commit a0e8e4ba authored by Kevin Modzelewski's avatar Kevin Modzelewski

Forgot to add the tests for the previous listcomp commit

parent 92117aab
......@@ -27,7 +27,7 @@ make install
### ccache
ccache is a build tool that can help speed up redundant compilations. It's not strictly necessary; you can disable it by adding `USE_CCACHE := 0` to your Makefile.local. It's enabled by default, so to use it you can run
ccache is a build tool that can help speed up redundant compilations. It's not strictly necessary but it's useful enough to be enabled by default; you can disable it by adding `USE_CCACHE := 0` to your Makefile.local. To get it, run:
```
sudo apt-get install ccache
```
......
def f():
def p(i):
print i
return i ** 2
print [p(i) for i in xrange(200000000) if i % 12345 == 0 if i % 301 == 0]
f()
class Union(object):
def __init__(self, subs):
self.subs = subs
def score(self):
t = 0
for s in self.subs:
t += s.score()
t /= len(self.subs) ** 2.0
return t
class Simple(object):
def score(self):
return 1.0
class Poly1(object):
def __init__(self, sub):
self.sub = sub
def score(self):
return self.sub.score()
d = 0.0
def rand():
# Almost cryptographically secure?
global d
d = (d * 1.24591 + .195) % 1
return d
def make_random(x):
if rand() > x:
return Simple()
if rand() < 0.3:
return Union([make_random(0.5 * x - 1), make_random(0.5 * x - 1)])
return Poly1(make_random(x - 1))
print make_random(100000).score()
print [(i, j) for i in range(4) for j in range(4)]
def f():
print [(i, j) for i in range(4) for j in range(4)]
print i, j
f()
# Combine a list comprehension with a bunch of other control-flow expressions:
def f(x, y):
# TODO make sure to use an 'if' in a comprehension where the if contains control flow
print [(i if i%2 else i/2) for i in (xrange(4 if x else 5) if y else xrange(3))]
f(0, 0)
f(0, 1)
f(1, 0)
f(1, 1)
# TODO: test on ifs
def f():
print [(i, j) for (i, j) in {1:2, 3:4, 5:6, 7:8}.items()]
f()
# The expr should not get evaluated if the if-condition fails:
def f():
def p(i):
print i
return i ** 2
print [p(i) for i in xrange(50) if i % 5 == 0 if i % 3 == 0]
f()
def f():
print [(i, j) for i in xrange(4) for j in xrange(i)]
f()
def f():
j = 1
# The 'if' part of this list comprehension references j;
# the first time through it will use the j above, but later times
# it may-or-may-not use the j from the inner part of the listcomp.
print [(i, j) for i in xrange(7) if i%2 != j%2 for j in xrange(i)]
f()
def f():
# Checking the order of evaluation of the if conditions:
def c1(x):
print "c1", x
return x % 2 == 0
def c2(x):
print "c2", x
return x % 3 == 0
print [i for i in xrange(20) if c1(i) if c2(i)]
f()
def control_flow_in_listcomp():
print [(i if i else -1) for i in xrange(10) if (i % 2 == 0 or i % 3 != 0)]
control_flow_in_listcomp()
i = "global"
def f(n):
i = "hello"
# The iterator element will get set, but only
# if the range is non-empty
print [0 for i in xrange(n)]
print n, i * 3
f(0)
f(5)
i = 0
# Though if the ifs empty out the range, the
# name still gets set:
print [0 for i in xrange(5) if 0]
print i
i = "global"
def f3(n):
# The global declaration here will apply to the i in the listcomp:
global i
[0 for i in xrange(n)]
# Calling with an empty range with do nothing:
f3(0)
print i
# Calling with a non-empty range will change the global i
f3(1)
print i
i = "global"
def f2(n, b):
if b:
i = 1
print [0 for i in xrange(n)]
# This i should refer to the local i even if n==0;
# in that case this should be an error
print i
f2(5, 0)
print i
f2(0, 1)
f2(0, 0)
# While perhaps not required in practice, we should have the ability to
# OSR from inside a list comprehension.
# statcheck: stats['OSR exits'] in (1, 2)
def p(i):
print i
for i in xrange(100000):
pass
# [i for i in xrange(100000) if i % 100 == 0]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment