Skip to content

Commit 40d43ea

Browse files
committed
tests: Add more tests for viper, including tests for ViperTypeError's.
1 parent 5e98103 commit 40d43ea

12 files changed

+131
-20
lines changed

tests/micropython/viper_cond.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
# using a bool as a conditional
1+
# using False as a conditional
2+
@micropython.viper
3+
def f():
4+
x = False
5+
if x:
6+
pass
7+
else:
8+
print("not x", x)
9+
f()
10+
11+
# using True as a conditional
212
@micropython.viper
313
def f():
414
x = True
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
not x False
12
x True
23
y 1

tests/micropython/viper_error.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
1-
# test syntax errors specific to viper code generation
1+
# test syntax and type errors specific to viper code generation
22

3-
def test_syntax(code):
3+
def test(code):
44
try:
55
exec(code)
6-
except SyntaxError:
7-
print("SyntaxError")
6+
except (SyntaxError, ViperTypeError) as e:
7+
print(repr(e))
88

99
# viper: annotations must be identifiers
10-
test_syntax("@micropython.viper\ndef f(a:1): pass")
11-
test_syntax("@micropython.viper\ndef f() -> 1: pass")
10+
test("@micropython.viper\ndef f(a:1): pass")
11+
test("@micropython.viper\ndef f() -> 1: pass")
12+
13+
# local used before type known
14+
test("""
15+
@micropython.viper
16+
def f():
17+
print(x)
18+
x = 1
19+
""")
20+
21+
# type mismatch storing to local
22+
test("""
23+
@micropython.viper
24+
def f():
25+
x = 1
26+
y = []
27+
x = y
28+
""")
29+
30+
# can't implicitly convert type to bool
31+
test("""
32+
@micropython.viper
33+
def f():
34+
x = ptr(0)
35+
if x:
36+
pass
37+
""")
38+
39+
# incorrect return type
40+
test("@micropython.viper\ndef f() -> int: return []")
41+
42+
# can't do binary op between incompatible types
43+
test("@micropython.viper\ndef f(): 1 + []")
44+
45+
# can't load
46+
test("@micropython.viper\ndef f(): 1[0]")
47+
test("@micropython.viper\ndef f(): 1[x]")
48+
49+
# can't store
50+
test("@micropython.viper\ndef f(): 1[0] = 1")
51+
test("@micropython.viper\ndef f(): 1[x] = 1")
52+
53+
# must raise an object
54+
test("@micropython.viper\ndef f(): raise 1")
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
SyntaxError
2-
SyntaxError
1+
SyntaxError('parameter annotation must be an identifier',)
2+
SyntaxError('return annotation must be an identifier',)
3+
ViperTypeError("local 'x' used before type known",)
4+
ViperTypeError("local 'x' has type 'int' but source is 'object'",)
5+
ViperTypeError("can't implicitly convert 'ptr' to 'bool'",)
6+
ViperTypeError("return expected 'int' but got 'object'",)
7+
ViperTypeError("can't do binary op between 'int' and 'object'",)
8+
ViperTypeError("can't load from 'int'",)
9+
ViperTypeError("can't load from 'int'",)
10+
ViperTypeError("can't store to 'int'",)
11+
ViperTypeError("can't store to 'int'",)
12+
ViperTypeError('must raise an object',)

tests/micropython/viper_misc.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ def viper_object(x:object, y:object) -> object:
1212
return x + y
1313
print(viper_object(1, 2))
1414

15+
# return None as non-object (should return 0)
16+
@micropython.viper
17+
def viper_ret_none() -> int:
18+
return None
19+
print(viper_ret_none())
20+
21+
# 3 args
22+
@micropython.viper
23+
def viper_3args(a:int, b:int, c:int) -> int:
24+
return a + b + c
25+
print(viper_3args(1, 2, 3))
26+
27+
# 4 args
28+
@micropython.viper
29+
def viper_4args(a:int, b:int, c:int, d:int) -> int:
30+
return a + b + c + d
31+
# viper call with 4 args not yet supported
32+
#print(viper_4args(1, 2, 3, 4))
33+
1534
# a local (should have automatic type int)
1635
@micropython.viper
1736
def viper_local(x:int) -> int:
@@ -25,6 +44,13 @@ def viper_no_annotation(x, y):
2544
return x * y
2645
print(viper_no_annotation(4, 5))
2746

47+
# unsigned ints
48+
@micropython.viper
49+
def viper_uint() -> uint:
50+
return uint(-1)
51+
import sys
52+
print(viper_uint() == (sys.maxsize << 1 | 1))
53+
2854
# a for loop
2955
@micropython.viper
3056
def viper_for(a:int, b:int) -> int:
@@ -48,6 +74,12 @@ def viper_print(x, y:int):
4874
print(x, y + 1)
4975
viper_print(1, 2)
5076

77+
# convert constants to objects in tuple
78+
@micropython.viper
79+
def viper_tuple_consts(x):
80+
return (x, 1, False, True)
81+
print(viper_tuple_consts(0))
82+
5183
# making a tuple from an object and an int
5284
@micropython.viper
5385
def viper_tuple(x, y:int):
@@ -75,11 +107,6 @@ def viper_raise(x:int):
75107
except OSError as e:
76108
print(repr(e))
77109

78-
# this doesn't work at the moment
79-
#@micropython.viper
80-
#def g() -> uint:
81-
# return -1
82-
83110
# calling GC after defining the function
84111
@micropython.viper
85112
def viper_gc() -> int:

tests/micropython/viper_misc.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
6
22
3
3+
0
4+
6
35
7
46
20
7+
True
58
49994955
69
1 1
710
1 3
11+
(0, 1, False, True)
812
(1, 3)
913
[1, 3]
1014
[1, 3]

tests/micropython/viper_ptr16_load.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
def get(src:ptr16) -> int:
66
return src[0]
77

8+
@micropython.viper
9+
def get1(src:ptr16) -> int:
10+
return src[1]
11+
812
@micropython.viper
913
def memadd(src:ptr16, n:int) -> int:
1014
sum = 0
@@ -14,5 +18,5 @@ def memadd(src:ptr16, n:int) -> int:
1418

1519
b = bytearray(b'1234')
1620
print(b)
17-
print(get(b))
21+
print(get(b), get1(b))
1822
print(memadd(b, 2))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
bytearray(b'1234')
2-
12849
2+
12849 13363
33
26212

tests/micropython/viper_ptr8_load.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
def get(src:ptr8) -> int:
55
return src[0]
66

7+
@micropython.viper
8+
def get1(src:ptr8) -> int:
9+
return src[1]
10+
711
@micropython.viper
812
def memadd(src:ptr8, n:int) -> int:
913
sum = 0
@@ -22,6 +26,6 @@ def memadd2(src_in) -> int:
2226

2327
b = bytearray(b'1234')
2428
print(b)
25-
print(get(b))
29+
print(get(b), get1(b))
2630
print(memadd(b, 4))
2731
print(memadd2(b))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
bytearray(b'1234')
2-
49
2+
49 50
33
202
44
202

0 commit comments

Comments
 (0)