tests: Add more tests for viper, including tests for ViperTypeError's.
This commit is contained in:
parent
5e9810396f
commit
40d43ea88d
|
@ -1,4 +1,14 @@
|
||||||
# using a bool as a conditional
|
# using False as a conditional
|
||||||
|
@micropython.viper
|
||||||
|
def f():
|
||||||
|
x = False
|
||||||
|
if x:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print("not x", x)
|
||||||
|
f()
|
||||||
|
|
||||||
|
# using True as a conditional
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def f():
|
def f():
|
||||||
x = True
|
x = True
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
not x False
|
||||||
x True
|
x True
|
||||||
y 1
|
y 1
|
||||||
|
|
|
@ -1,11 +1,54 @@
|
||||||
# test syntax errors specific to viper code generation
|
# test syntax and type errors specific to viper code generation
|
||||||
|
|
||||||
def test_syntax(code):
|
def test(code):
|
||||||
try:
|
try:
|
||||||
exec(code)
|
exec(code)
|
||||||
except SyntaxError:
|
except (SyntaxError, ViperTypeError) as e:
|
||||||
print("SyntaxError")
|
print(repr(e))
|
||||||
|
|
||||||
# viper: annotations must be identifiers
|
# viper: annotations must be identifiers
|
||||||
test_syntax("@micropython.viper\ndef f(a:1): pass")
|
test("@micropython.viper\ndef f(a:1): pass")
|
||||||
test_syntax("@micropython.viper\ndef f() -> 1: pass")
|
test("@micropython.viper\ndef f() -> 1: pass")
|
||||||
|
|
||||||
|
# local used before type known
|
||||||
|
test("""
|
||||||
|
@micropython.viper
|
||||||
|
def f():
|
||||||
|
print(x)
|
||||||
|
x = 1
|
||||||
|
""")
|
||||||
|
|
||||||
|
# type mismatch storing to local
|
||||||
|
test("""
|
||||||
|
@micropython.viper
|
||||||
|
def f():
|
||||||
|
x = 1
|
||||||
|
y = []
|
||||||
|
x = y
|
||||||
|
""")
|
||||||
|
|
||||||
|
# can't implicitly convert type to bool
|
||||||
|
test("""
|
||||||
|
@micropython.viper
|
||||||
|
def f():
|
||||||
|
x = ptr(0)
|
||||||
|
if x:
|
||||||
|
pass
|
||||||
|
""")
|
||||||
|
|
||||||
|
# incorrect return type
|
||||||
|
test("@micropython.viper\ndef f() -> int: return []")
|
||||||
|
|
||||||
|
# can't do binary op between incompatible types
|
||||||
|
test("@micropython.viper\ndef f(): 1 + []")
|
||||||
|
|
||||||
|
# can't load
|
||||||
|
test("@micropython.viper\ndef f(): 1[0]")
|
||||||
|
test("@micropython.viper\ndef f(): 1[x]")
|
||||||
|
|
||||||
|
# can't store
|
||||||
|
test("@micropython.viper\ndef f(): 1[0] = 1")
|
||||||
|
test("@micropython.viper\ndef f(): 1[x] = 1")
|
||||||
|
|
||||||
|
# must raise an object
|
||||||
|
test("@micropython.viper\ndef f(): raise 1")
|
||||||
|
|
|
@ -1,2 +1,12 @@
|
||||||
SyntaxError
|
SyntaxError('parameter annotation must be an identifier',)
|
||||||
SyntaxError
|
SyntaxError('return annotation must be an identifier',)
|
||||||
|
ViperTypeError("local 'x' used before type known",)
|
||||||
|
ViperTypeError("local 'x' has type 'int' but source is 'object'",)
|
||||||
|
ViperTypeError("can't implicitly convert 'ptr' to 'bool'",)
|
||||||
|
ViperTypeError("return expected 'int' but got 'object'",)
|
||||||
|
ViperTypeError("can't do binary op between 'int' and 'object'",)
|
||||||
|
ViperTypeError("can't load from 'int'",)
|
||||||
|
ViperTypeError("can't load from 'int'",)
|
||||||
|
ViperTypeError("can't store to 'int'",)
|
||||||
|
ViperTypeError("can't store to 'int'",)
|
||||||
|
ViperTypeError('must raise an object',)
|
||||||
|
|
|
@ -12,6 +12,25 @@ def viper_object(x:object, y:object) -> object:
|
||||||
return x + y
|
return x + y
|
||||||
print(viper_object(1, 2))
|
print(viper_object(1, 2))
|
||||||
|
|
||||||
|
# return None as non-object (should return 0)
|
||||||
|
@micropython.viper
|
||||||
|
def viper_ret_none() -> int:
|
||||||
|
return None
|
||||||
|
print(viper_ret_none())
|
||||||
|
|
||||||
|
# 3 args
|
||||||
|
@micropython.viper
|
||||||
|
def viper_3args(a:int, b:int, c:int) -> int:
|
||||||
|
return a + b + c
|
||||||
|
print(viper_3args(1, 2, 3))
|
||||||
|
|
||||||
|
# 4 args
|
||||||
|
@micropython.viper
|
||||||
|
def viper_4args(a:int, b:int, c:int, d:int) -> int:
|
||||||
|
return a + b + c + d
|
||||||
|
# viper call with 4 args not yet supported
|
||||||
|
#print(viper_4args(1, 2, 3, 4))
|
||||||
|
|
||||||
# a local (should have automatic type int)
|
# a local (should have automatic type int)
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def viper_local(x:int) -> int:
|
def viper_local(x:int) -> int:
|
||||||
|
@ -25,6 +44,13 @@ def viper_no_annotation(x, y):
|
||||||
return x * y
|
return x * y
|
||||||
print(viper_no_annotation(4, 5))
|
print(viper_no_annotation(4, 5))
|
||||||
|
|
||||||
|
# unsigned ints
|
||||||
|
@micropython.viper
|
||||||
|
def viper_uint() -> uint:
|
||||||
|
return uint(-1)
|
||||||
|
import sys
|
||||||
|
print(viper_uint() == (sys.maxsize << 1 | 1))
|
||||||
|
|
||||||
# a for loop
|
# a for loop
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def viper_for(a:int, b:int) -> int:
|
def viper_for(a:int, b:int) -> int:
|
||||||
|
@ -48,6 +74,12 @@ def viper_print(x, y:int):
|
||||||
print(x, y + 1)
|
print(x, y + 1)
|
||||||
viper_print(1, 2)
|
viper_print(1, 2)
|
||||||
|
|
||||||
|
# convert constants to objects in tuple
|
||||||
|
@micropython.viper
|
||||||
|
def viper_tuple_consts(x):
|
||||||
|
return (x, 1, False, True)
|
||||||
|
print(viper_tuple_consts(0))
|
||||||
|
|
||||||
# making a tuple from an object and an int
|
# making a tuple from an object and an int
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def viper_tuple(x, y:int):
|
def viper_tuple(x, y:int):
|
||||||
|
@ -75,11 +107,6 @@ try:
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print(repr(e))
|
print(repr(e))
|
||||||
|
|
||||||
# this doesn't work at the moment
|
|
||||||
#@micropython.viper
|
|
||||||
#def g() -> uint:
|
|
||||||
# return -1
|
|
||||||
|
|
||||||
# calling GC after defining the function
|
# calling GC after defining the function
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def viper_gc() -> int:
|
def viper_gc() -> int:
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
6
|
6
|
||||||
3
|
3
|
||||||
|
0
|
||||||
|
6
|
||||||
7
|
7
|
||||||
20
|
20
|
||||||
|
True
|
||||||
49994955
|
49994955
|
||||||
1 1
|
1 1
|
||||||
1 3
|
1 3
|
||||||
|
(0, 1, False, True)
|
||||||
(1, 3)
|
(1, 3)
|
||||||
[1, 3]
|
[1, 3]
|
||||||
[1, 3]
|
[1, 3]
|
||||||
|
|
|
@ -5,6 +5,10 @@
|
||||||
def get(src:ptr16) -> int:
|
def get(src:ptr16) -> int:
|
||||||
return src[0]
|
return src[0]
|
||||||
|
|
||||||
|
@micropython.viper
|
||||||
|
def get1(src:ptr16) -> int:
|
||||||
|
return src[1]
|
||||||
|
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def memadd(src:ptr16, n:int) -> int:
|
def memadd(src:ptr16, n:int) -> int:
|
||||||
sum = 0
|
sum = 0
|
||||||
|
@ -14,5 +18,5 @@ def memadd(src:ptr16, n:int) -> int:
|
||||||
|
|
||||||
b = bytearray(b'1234')
|
b = bytearray(b'1234')
|
||||||
print(b)
|
print(b)
|
||||||
print(get(b))
|
print(get(b), get1(b))
|
||||||
print(memadd(b, 2))
|
print(memadd(b, 2))
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
bytearray(b'1234')
|
bytearray(b'1234')
|
||||||
12849
|
12849 13363
|
||||||
26212
|
26212
|
||||||
|
|
|
@ -4,6 +4,10 @@
|
||||||
def get(src:ptr8) -> int:
|
def get(src:ptr8) -> int:
|
||||||
return src[0]
|
return src[0]
|
||||||
|
|
||||||
|
@micropython.viper
|
||||||
|
def get1(src:ptr8) -> int:
|
||||||
|
return src[1]
|
||||||
|
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def memadd(src:ptr8, n:int) -> int:
|
def memadd(src:ptr8, n:int) -> int:
|
||||||
sum = 0
|
sum = 0
|
||||||
|
@ -22,6 +26,6 @@ def memadd2(src_in) -> int:
|
||||||
|
|
||||||
b = bytearray(b'1234')
|
b = bytearray(b'1234')
|
||||||
print(b)
|
print(b)
|
||||||
print(get(b))
|
print(get(b), get1(b))
|
||||||
print(memadd(b, 4))
|
print(memadd(b, 4))
|
||||||
print(memadd2(b))
|
print(memadd2(b))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
bytearray(b'1234')
|
bytearray(b'1234')
|
||||||
49
|
49 50
|
||||||
202
|
202
|
||||||
202
|
202
|
||||||
|
|
|
@ -4,6 +4,10 @@
|
||||||
def set(dest:ptr8, val:int):
|
def set(dest:ptr8, val:int):
|
||||||
dest[0] = val
|
dest[0] = val
|
||||||
|
|
||||||
|
@micropython.viper
|
||||||
|
def set1(dest:ptr8, val:int):
|
||||||
|
dest[1] = val
|
||||||
|
|
||||||
@micropython.viper
|
@micropython.viper
|
||||||
def memset(dest:ptr8, val:int, n:int):
|
def memset(dest:ptr8, val:int, n:int):
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
|
@ -19,7 +23,10 @@ def memset2(dest_in, val:int):
|
||||||
b = bytearray(4)
|
b = bytearray(4)
|
||||||
print(b)
|
print(b)
|
||||||
|
|
||||||
set(b, 42)
|
set(b, 41)
|
||||||
|
print(b)
|
||||||
|
|
||||||
|
set1(b, 42)
|
||||||
print(b)
|
print(b)
|
||||||
|
|
||||||
memset(b, 43, len(b))
|
memset(b, 43, len(b))
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
bytearray(b'\x00\x00\x00\x00')
|
bytearray(b'\x00\x00\x00\x00')
|
||||||
bytearray(b'*\x00\x00\x00')
|
bytearray(b')\x00\x00\x00')
|
||||||
|
bytearray(b')*\x00\x00')
|
||||||
bytearray(b'++++')
|
bytearray(b'++++')
|
||||||
bytearray(b',,,,')
|
bytearray(b',,,,')
|
||||||
|
|
Loading…
Reference in New Issue