summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-03-18 12:29:29 +1100
committerDamien George <damien@micropython.org>2024-03-19 10:31:36 +1100
commit3c445f66369a49ba2dfa7e008d0a93c71fb1b6d3 (patch)
tree76fd0e612740ea7099f790801b24078813bf5d60 /tests
parentb50efbd0e319764dab3f49b64519cfea5f2c2bab (diff)
py/emitnative: Implement viper unary ops positive, negative and invert.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/micropython/viper_error.py7
-rw-r--r--tests/micropython/viper_error.py.exp5
-rw-r--r--tests/micropython/viper_unop.py31
-rw-r--r--tests/micropython/viper_unop.py.exp9
4 files changed, 46 insertions, 6 deletions
diff --git a/tests/micropython/viper_error.py b/tests/micropython/viper_error.py
index 80617af0c..6c5c3ba20 100644
--- a/tests/micropython/viper_error.py
+++ b/tests/micropython/viper_error.py
@@ -50,6 +50,9 @@ def f():
# incorrect return type
test("@micropython.viper\ndef f() -> int: return []")
+# can't do unary op of incompatible type
+test("@micropython.viper\ndef f(x:ptr): -x")
+
# can't do binary op between incompatible types
test("@micropython.viper\ndef f(): 1 + []")
test("@micropython.viper\ndef f(x:int, y:uint): x < y")
@@ -69,9 +72,7 @@ test("@micropython.viper\ndef f(x:ptr32): x[x] = None")
test("@micropython.viper\ndef f(): raise 1")
# unary ops not implemented
-test("@micropython.viper\ndef f(x:int): +x")
-test("@micropython.viper\ndef f(x:int): -x")
-test("@micropython.viper\ndef f(x:int): ~x")
+test("@micropython.viper\ndef f(x:int): not x")
# binary op not implemented
test("@micropython.viper\ndef f(x:uint, y:uint): res = x // y")
diff --git a/tests/micropython/viper_error.py.exp b/tests/micropython/viper_error.py.exp
index 31c85b1d8..51cbd6c70 100644
--- a/tests/micropython/viper_error.py.exp
+++ b/tests/micropython/viper_error.py.exp
@@ -5,6 +5,7 @@ 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 unary op of 'ptr'",)
ViperTypeError("can't do binary op between 'int' and 'object'",)
ViperTypeError('comparison of int and uint',)
ViperTypeError("can't load from 'int'",)
@@ -15,9 +16,7 @@ ViperTypeError("can't store to 'int'",)
ViperTypeError("can't store 'None'",)
ViperTypeError("can't store 'None'",)
ViperTypeError('must raise an object',)
-ViperTypeError('unary op __pos__ not implemented',)
-ViperTypeError('unary op __neg__ not implemented',)
-ViperTypeError('unary op __invert__ not implemented',)
+ViperTypeError("'not' not implemented",)
ViperTypeError('div/mod not implemented for uint',)
ViperTypeError('div/mod not implemented for uint',)
ViperTypeError('binary op not implemented',)
diff --git a/tests/micropython/viper_unop.py b/tests/micropython/viper_unop.py
new file mode 100644
index 000000000..61cbd5125
--- /dev/null
+++ b/tests/micropython/viper_unop.py
@@ -0,0 +1,31 @@
+# test unary operators
+
+
+@micropython.viper
+def pos(x: int) -> int:
+ return +x
+
+
+print(pos(0))
+print(pos(1))
+print(pos(-2))
+
+
+@micropython.viper
+def neg(x: int) -> int:
+ return -x
+
+
+print(neg(0))
+print(neg(1))
+print(neg(-2))
+
+
+@micropython.viper
+def inv(x: int) -> int:
+ return ~x
+
+
+print(inv(0))
+print(inv(1))
+print(inv(-2))
diff --git a/tests/micropython/viper_unop.py.exp b/tests/micropython/viper_unop.py.exp
new file mode 100644
index 000000000..6d93312ca
--- /dev/null
+++ b/tests/micropython/viper_unop.py.exp
@@ -0,0 +1,9 @@
+0
+1
+-2
+0
+-1
+2
+-1
+-2
+1