-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_basic.py
599 lines (536 loc) · 15.3 KB
/
test_basic.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
from __future__ import print_function
import vmtest
# FAILED (failures=3) set, dict comprehension
class TestIt(vmtest.VmTestCase):
def test_constant(self):
self.assert_ok("17")
def test_for_loop(self):
self.assert_ok("""\
out = ""
for i in range(5):
out = out + str(i)
print(out)
""")
def test_inplace_operators(self):
self.assert_ok("""\
x, y = 2, 3
x **= y
assert x == 8 and y == 3
x *= y
assert x == 24 and y == 3
x //= y
assert x == 8 and y == 3
x %= y
assert x == 2 and y == 3
x += y
assert x == 5 and y == 3
x -= y
assert x == 2 and y == 3
x <<= y
assert x == 16 and y == 3
x >>= y
assert x == 2 and y == 3
x = 0x8F
x &= 0xA5
assert x == 0x85
x |= 0x10
assert x == 0x95
x ^= 0x33
assert x == 0xA6
""")
def test_inplace_division(self):
self.assert_ok("""\
x, y = 24, 3
x /= y
assert x == 8 and y == 3
assert isinstance(x, int)
x /= y
assert x == 2 and y == 3
assert isinstance(x, int)
""")
def test_slice(self):
self.assert_ok("""\
print("hello, world"[3:8])
""")
self.assert_ok("""\
print("hello, world"[:8])
""")
self.assert_ok("""\
print("hello, world"[3:])
""")
self.assert_ok("""\
print("hello, world"[:])
""")
self.assert_ok("""\
print("hello, world"[::-1])
""")
self.assert_ok("""\
print("hello, world"[3:8:2])
""")
def test_slice_assignment(self):
self.assert_ok("""\
l = list(range(10))
l[3:8] = ["x"]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
l[:8] = ["x"]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
l[3:] = ["x"]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
l[:] = ["x"]
print(l)
""")
def test_slice_deletion(self):
self.assert_ok("""\
l = list(range(10))
del l[3:8]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
del l[:8]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
del l[3:]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
del l[:]
print(l)
""")
self.assert_ok("""\
l = list(range(10))
del l[::2]
print(l)
""")
def test_building_stuff(self):
self.assert_ok("""\
print((1+1, 2+2, 3+3))
""")
self.assert_ok("""\
print([1+1, 2+2, 3+3])
""")
self.assert_ok("""\
print({1:1+1, 2:2+2, 3:3+3})
""")
def test_subscripting(self):
self.assert_ok("""\
l = list(range(10))
print("%s %s %s" % (l[0], l[3], l[9]))
""")
self.assert_ok("""\
l = list(range(10))
l[5] = 17
print(l)
""")
self.assert_ok("""\
l = list(range(10))
del l[5]
print(l)
""")
def test_generator_expression(self):
self.assert_ok("""\
x = "-".join(str(z) for z in range(5))
assert x == "0-1-2-3-4"
""")
self.assert_ok("""\
from textwrap import fill
x = set(['test_str'])
width = 70
indent = 4
blanks = ' ' * indent
res = fill(' '.join(str(elt) for elt in sorted(x)), width,
initial_indent=blanks, subsequent_indent=blanks)
print(res)
""")
def test_list_comprehension(self):
self.assert_ok("""\
x = [z*z for z in range(5)]
assert x == [0, 1, 4, 9, 16]
""")
def test_dict_comprehension(self):
self.assert_ok("""\
x = {z:z*z for z in range(5)}
assert x == {0:0, 1:1, 2:4, 3:9, 4:16}
""")
def test_set_comprehension(self):
self.assert_ok("""\
x = {z*z for z in range(5)}
assert x == {0, 1, 4, 9, 16}
""")
def test_strange_sequence_ops(self):
self.assert_ok("""\
x = [1,2]
x += [3,4]
x *= 2
assert x == [1, 2, 3, 4, 1, 2, 3, 4]
x = [1, 2, 3]
y = x
x[1:2] *= 2
y[1:2] += [1]
assert x == [1, 2, 1, 2, 3]
assert x is y
""")
def test_unary_operators(self):
self.assert_ok("""\
x = 8
print(-x, ~x, not x)
""")
def test_attributes(self):
self.assert_ok("""\
l = lambda: 1
l.foo = 17
print(hasattr(l, "foo"), l.foo)
del l.foo
print(hasattr(l, "foo"))
""")
def test_attribute_inplace_ops(self):
self.assert_ok("""\
l = lambda: 1
l.foo = 17
l.foo -= 3
print(l.foo)
""")
def test_deleting_names(self):
self.assert_ok("""\
g = 17
assert g == 17
del g
g
""", raises=NameError)
def test_deleting_local_names(self):
self.assert_ok("""\
def f():
l = 23
assert l == 23
del l
l
f()
""", raises=NameError)
def test_import(self):
self.assert_ok("""\
import math
print(math.pi, math.e)
from math import sqrt
print(sqrt(2))
from math import *
print(sin(2))
""")
def test_classes(self):
self.assert_ok("""\
class Thing(object):
def __init__(self, x):
self.x = x
def meth(self, y):
return self.x * y
thing1 = Thing(2)
thing2 = Thing(3)
print(thing1.x, thing2.x)
print(thing1.meth(4), thing2.meth(5))
""")
def test_calling_methods_wrong(self):
self.assert_ok("""\
class Thing(object):
def __init__(self, x):
self.x = x
def meth(self, y):
return self.x * y
thing1 = Thing(2)
print(Thing.meth(14))
""", raises=TypeError)
def test_calling_subclass_methods(self):
self.assert_ok("""\
class Thing(object):
def foo(self):
return 17
class SubThing(Thing):
pass
st = SubThing()
print(st.foo())
""")
def test_subclass_attribute(self):
self.assert_ok("""\
class Thing(object):
def __init__(self):
self.foo = 17
class SubThing(Thing):
pass
st = SubThing()
print(st.foo)
""")
def test_subclass_attributes_not_shared(self):
self.assert_ok("""\
class Thing(object):
foo = 17
class SubThing(Thing):
foo = 25
st = SubThing()
t = Thing()
assert st.foo == 25
assert t.foo == 17
""")
def test_object_attrs_not_shared_with_class(self):
self.assert_ok("""\
class Thing(object):
pass
t = Thing()
t.foo = 1
Thing.foo""", raises=AttributeError)
def test_data_descriptors_precede_instance_attributes(self):
self.assert_ok("""\
class Foo(object):
pass
f = Foo()
f.des = 3
class Descr(object):
def __get__(self, obj, cls=None):
return 2
def __set__(self, obj, val):
raise NotImplementedError
Foo.des = Descr()
assert f.des == 2
""")
def test_instance_attrs_precede_non_data_descriptors(self):
self.assert_ok("""\
class Foo(object):
pass
f = Foo()
f.des = 3
class Descr(object):
def __get__(self, obj, cls=None):
return 2
Foo.des = Descr()
assert f.des == 3
""")
def test_subclass_attributes_dynamic(self):
self.assert_ok("""\
class Foo(object):
pass
class Bar(Foo):
pass
b = Bar()
Foo.baz = 3
assert b.baz == 3
""")
def test_attribute_access(self):
self.assert_ok("""\
class Thing(object):
z = 17
def __init__(self):
self.x = 23
t = Thing()
print(Thing.z)
print(t.z)
print(t.x)
""")
self.assert_ok("""\
class Thing(object):
z = 17
def __init__(self):
self.x = 23
t = Thing()
print(t.xyzzy)
""", raises=AttributeError)
def test_staticmethods(self):
self.assert_ok("""\
class Thing(object):
@staticmethod
def smeth(x):
print(x)
@classmethod
def cmeth(cls, x):
print(x)
Thing.smeth(1492)
Thing.cmeth(1776)
""")
def test_unbound_methods(self):
self.assert_ok("""\
class Thing(object):
def meth(self, x):
print(x)
m = Thing.meth
m(Thing(), 1815)
""")
def test_bound_methods(self):
self.assert_ok("""\
class Thing(object):
def meth(self, x):
print(x)
t = Thing()
m = t.meth
m(1815)
""")
def test_callback(self):
self.assert_ok("""\
def lcase(s):
return s.lower()
l = ["xyz", "ABC"]
l.sort(key=lcase)
print(l)
assert l == ["ABC", "xyz"]
""")
def test_unpacking(self):
self.assert_ok("""\
a, b, c = (1, 2, 3)
assert a == 1
assert b == 2
assert c == 3
""")
def test_exec_statement(self):
self.assert_ok("""\
g = {}
exec "a = 11" in g, g
assert g['a'] == 11
""")
def test_jump_if_true_or_pop(self):
self.assert_ok("""\
def f(a, b):
return a or b
assert f(17, 0) == 17
assert f(0, 23) == 23
assert f(0, "") == ""
""")
def test_jump_if_false_or_pop(self):
self.assert_ok("""\
def f(a, b):
return not(a and b)
assert f(17, 0) is True
assert f(0, 23) is True
assert f(0, "") is True
assert f(17, 23) is False
""")
def test_pop_jump_if_true(self):
self.assert_ok("""\
def f(a):
if not a:
return 'foo'
else:
return 'bar'
assert f(0) == 'foo'
assert f(1) == 'bar'
""")
def test_decorator(self):
self.assert_ok("""\
def verbose(func):
def _wrapper(*args, **kwargs):
return func(*args, **kwargs)
return _wrapper
@verbose
def add(x, y):
return x+y
add(7, 3)
""")
def test_multiple_classes(self):
self.assert_ok("""\
class A(object):
def __init__(self, a, b, c):
self.sum = a + b + c
class B(object):
def __init__(self, x):
self.x = x
a = A(1, 2, 3)
b = B(7)
print(a.sum)
print(b.x)
""")
# FAILED (errors=1)
class TestPrinting(vmtest.VmTestCase):
def test_printing(self):
self.assert_ok("print 'hello'")
self.assert_ok("a = 3; print a+4")
self.assert_ok("""
print 'hi', 17, u'bye', 23,
print "", "\t", "the end"
""")
def test_printing_in_a_function(self):
self.assert_ok("""\
def fn():
print "hello"
fn()
print "bye"
""")
def test_printing_to_a_file(self):
self.assert_ok("""\
import sys
print >>sys.stdout, 'hello', 'there'
""")
# FAILED (errors=3)
class TestLoops(vmtest.VmTestCase):
def test_for(self):
self.assert_ok("""\
for i in range(10):
print(i)
print("done")
""")
def test_break(self):
self.assert_ok("""\
for i in range(10):
print(i)
if i == 7:
break
print("done")
""")
def test_continue(self):
self.assert_ok("""\
for i in range(10):
if i % 3 == 0:
continue
print(i)
print("done")
""")
def test_continue_in_try_except(self):
self.assert_ok("""\
for i in range(1,5):
try:
if i % 3 == 0:
continue
print(i)
except ValueError:
pass
print("done")
""")
def test_continue_in_try_finally(self):
self.assert_ok("""\
for i in range(1,5):
try:
if i % 3 == 0:
continue
print(i)
finally:
print(".")
print("done")
""")
# passed
class TestComparisons(vmtest.VmTestCase):
def test_in(self):
self.assert_ok("""\
assert "x" in "xyz"
assert "x" not in "abc"
assert "x" in ("x", "y", "z")
assert "x" not in ("a", "b", "c")
""")
def test_less(self):
self.assert_ok("""\
assert 1 < 3
assert 1 <= 2 and 1 <= 1
assert "a" < "b"
assert "a" <= "b" and "a" <= "a"
""")
def test_greater(self):
self.assert_ok("""\
assert 3 > 1
assert 3 >= 1 and 3 >= 3
assert "z" > "a"
assert "z" >= "a" and "z" >= "z"
""")