-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_functions.py
331 lines (286 loc) · 8.98 KB
/
test_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from __future__ import print_function
import vmtest
# FAILED (failures=1) wraps
class TestFunctions(vmtest.VmTestCase):
def test_functions(self):
self.assert_ok("""\
def fn(a, b=17, c="Hello", d=[]):
d.append(99)
print(a, b, c, d)
fn(1)
fn(2, 3)
fn(3, c="Bye")
fn(4, d=["What?"])
fn(5, "b", "c")
""")
def test_recursion(self):
self.assert_ok("""\
def fact(n):
if n <= 1:
return 1
else:
return n * fact(n-1)
f6 = fact(6)
print(f6)
assert f6 == 720
""")
def test_nested_names(self):
self.assert_ok("""\
def one():
x = 1
def two():
x = 2
print(x)
two()
print(x)
one()
""")
def test_calling_functions_with_args_kwargs(self):
self.assert_ok("""\
def fn(a, b=17, c="Hello", d=[]):
d.append(99)
print(a, b, c, d)
fn(6, *[77, 88])
fn(**{'c': 23, 'a': 7})
fn(6, *[77], **{'c': 23, 'd': [123]})
""")
def test_defining_functions_with_args_kwargs(self):
self.assert_ok("""\
def fn(*args):
print("args is %r" % (args,))
fn(1, 2)
""")
self.assert_ok("""\
def fn(**kwargs):
print("kwargs is %r" % (kwargs,))
fn(red=True, blue=False)
""")
self.assert_ok("""\
def fn(*args, **kwargs):
print("args is %r" % (args,))
print("kwargs is %r" % (kwargs,))
fn(1, 2, red=True, blue=False)
""")
self.assert_ok("""\
def fn(x, y, *args, **kwargs):
print("x is %r, y is %r" % (x, y))
print("args is %r" % (args,))
print("kwargs is %r" % (kwargs,))
fn('a', 'b', 1, 2, red=True, blue=False)
""")
def test_defining_functions_with_empty_args_kwargs(self):
self.assert_ok("""\
def fn(*args):
print("args is %r" % (args,))
fn()
""")
self.assert_ok("""\
def fn(**kwargs):
print("kwargs is %r" % (kwargs,))
fn()
""")
self.assert_ok("""\
def fn(*args, **kwargs):
print("args is %r, kwargs is %r" % (args, kwargs))
fn()
""")
def test_partial(self):
self.assert_ok("""\
from _functools import partial
def f(a,b):
return a-b
f7 = partial(f, 7)
four = f7(3)
assert four == 4
""")
def test_partial_with_kwargs(self):
self.assert_ok("""\
from _functools import partial
def f(a,b,c=0,d=0):
return (a,b,c,d)
f7 = partial(f, b=7, c=1)
them = f7(10)
assert them == (10,7,1,0)
""")
# ok
class TestClosures(vmtest.VmTestCase):
def test_closures(self):
self.assert_ok("""\
def make_adder(x):
def add(y):
return x+y
return add
a = make_adder(10)
print(a(7))
assert a(7) == 17
""")
def test_closures_store_deref(self):
self.assert_ok("""\
def make_adder(x):
z = x+1
def add(y):
return x+y+z
return add
a = make_adder(10)
print(a(7))
assert a(7) == 28
""")
def test_closures_in_loop(self):
self.assert_ok("""\
def make_fns(x):
fns = []
for i in range(x):
fns.append(lambda i=i: i)
return fns
fns = make_fns(3)
for f in fns:
print(f())
assert (fns[0](), fns[1](), fns[2]()) == (0, 1, 2)
""")
def test_closures_with_defaults(self):
self.assert_ok("""\
def make_adder(x, y=13, z=43):
def add(q, r=11):
return x+y+z+q+r
return add
a = make_adder(10, 17)
print(a(7))
assert a(7) == 88
""")
def test_deep_closures(self):
self.assert_ok("""\
def f1(a):
b = 2*a
def f2(c):
d = 2*c
def f3(e):
f = 2*e
def f4(g):
h = 2*g
return a+b+c+d+e+f+g+h
return f4
return f3
return f2
answer = f1(3)(4)(5)(6)
print(answer)
assert answer == 54
""")
# FAILED (failures=2, errors=4)
class TestGenerators(vmtest.VmTestCase):
def test_first(self):
self.assert_ok("""\
def two():
yield 1
yield 2
for i in two():
print(i)
""")
def test_partial_generator(self):
self.assert_ok("""\
from _functools import partial
def f(a,b):
num = a+b
while num:
yield num
num -= 1
f2 = partial(f, 2)
three = f2(1)
assert list(three) == [3,2,1]
""")
def test_yield_multiple_values(self):
self.assert_ok("""\
def triples():
yield 1, 2, 3
yield 4, 5, 6
for a, b, c in triples():
print(a, b, c)
""")
def test_simple_generator(self):
self.assert_ok("""\
g = (x for x in [0,1,2])
print(list(g))
""")
def test_generator_from_generator(self):
self.assert_ok("""\
g = (x*x for x in range(5))
h = (y+1 for y in g)
print(list(h))
""")
def test_generator_from_generator2(self):
self.assert_ok("""\
class Thing(object):
RESOURCES = ('abc', 'def')
def get_abc(self):
return "ABC"
def get_def(self):
return "DEF"
def resource_info(self):
for name in self.RESOURCES:
get_name = 'get_' + name
yield name, getattr(self, get_name)
def boom(self):
#d = list((name, get()) for name, get in self.resource_info())
d = [(name, get()) for name, get in self.resource_info()]
return d
print(Thing().boom())
""")
def test_distinguish_iterators_and_generators(self):
self.assert_ok("""\
class Foo(object):
def __iter__(self):
return FooIter()
class FooIter(object):
def __init__(self):
self.state = 0
def __next__(self):
if self.state >= 10:
raise StopIteration
self.state += 1
return self.state
def send(self, n):
print("sending")
def outer():
yield from Foo()
for x in outer():
print(x)
""")
def test_nested_yield_from(self):
self.assert_ok("""\
def main():
x = outer()
next(x)
y = x.send("Hello, World")
print(y)
def outer():
yield from middle()
def middle():
yield from inner()
def inner():
y = yield
yield y
main()
""")
def test_return_from_generator(self):
self.assert_ok("""\
def gen():
yield 1
return 2
x = gen()
while True:
try:
print(next(x))
except StopIteration as e:
print(e.value)
break
""")
def test_return_from_generator_with_yield_from(self):
self.assert_ok("""\
def returner():
if False:
yield
return 1
def main():
y = yield from returner()
print(y)
list(main())
""")