Skip to content

Commit 5a56e32

Browse files
committed
vm: tests for loops
1 parent ceed469 commit 5a56e32

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

vm/eval.go

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ func debugf(format string, a ...interface{}) {
5454

5555
// Stack operations
5656
func (vm *Vm) STACK_LEVEL() int { return len(vm.frame.Stack) }
57-
func (vm *Vm) EMPTY() bool { return len(vm.frame.Stack) == 0 }
5857
func (vm *Vm) TOP() py.Object { return vm.frame.Stack[len(vm.frame.Stack)-1] }
5958
func (vm *Vm) SECOND() py.Object { return vm.frame.Stack[len(vm.frame.Stack)-2] }
6059
func (vm *Vm) THIRD() py.Object { return vm.frame.Stack[len(vm.frame.Stack)-3] }

vm/tests/loops.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3.4
2+
3+
# While
4+
a = 1
5+
while a < 10:
6+
a += 1
7+
assert a == 10
8+
9+
# While else
10+
a = 1
11+
ok = False
12+
while a < 10:
13+
a += 1
14+
else:
15+
ok = True
16+
assert a == 10
17+
assert ok
18+
19+
# While break
20+
a = 1
21+
ok = True
22+
while True:
23+
if a >= 10:
24+
break
25+
a += 1
26+
else:
27+
ok = False
28+
assert a == 10
29+
assert ok
30+
31+
# While continue
32+
a = 1
33+
while a < 10:
34+
if a == 5:
35+
a += 1000
36+
continue
37+
a += 1
38+
assert a == 1005
39+
40+
# For
41+
a = 0
42+
for i in (1,2,3,4,5):
43+
a += i
44+
assert a == 15
45+
46+
# For else
47+
a = 0
48+
ok = False
49+
for i in (1,2,3,4,5):
50+
a += i
51+
else:
52+
ok = True
53+
assert a == 15
54+
assert ok
55+
56+
# For break
57+
a = 0
58+
ok = True
59+
for i in (1,2,3,4,5):
60+
if i >= 3:
61+
break
62+
a += i
63+
else:
64+
ok = False
65+
assert a == 3
66+
assert ok
67+
68+
# For continue
69+
a = 0
70+
for i in (1,2,3,4,5):
71+
if i == 3:
72+
continue
73+
a += i
74+
assert a == 12
75+
76+
# For continue in try/finally
77+
# FIXME doesn't work yet!
78+
# ok = False
79+
# a = 0
80+
# for i in (1,2,3,4,5):
81+
# if i == 3:
82+
# try:
83+
# continue
84+
# finally:
85+
# ok = True
86+
# a += i
87+
# assert a == 12
88+
# assert ok
89+
90+
# End with this
91+
finished = True

vm/tests/ops.py

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@
9393
assert True is not False
9494
# FIXME EXC_MATCH
9595

96+
# Multiple comparison
97+
98+
assert _2 < _10 < _11 < _100
99+
assert not (_10 < _2 < _11 < _100)
100+
assert _100 > _11 > _10 > _2
101+
96102
# logical
97103
t = True
98104
f = False

0 commit comments

Comments
 (0)