Skip to content

Commit

Permalink
fix bitwise.and and add tests (#19)
Browse files Browse the repository at this point in the history
Signed-off-by: eric-therond <[email protected]>
  • Loading branch information
eric-therond authored Jun 22, 2023
1 parent 910ef32 commit e08d13d
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/builtins/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anyhow::Result;

pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
m.insert("bits.and", and);
m.insert("bits.and", lsh);
m.insert("bits.lsh", lsh);
m.insert("bits.negate", negate);
m.insert("bits.or", or);
m.insert("bits.rsh", rsh);
Expand Down
60 changes: 60 additions & 0 deletions tests/interpreter/cases/builtins/bitwise/and.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: bits.and.error.wrongtype1
data: {}
modules:
- |
package test
x = bits.and(1, "str")
query: data.test
error: "`bits.and` expects numeric argument. Got `\"str\"` instead"

- note: bits.and.error.wrongtype2
data: {}
modules:
- |
package test
x = bits.and("str", 1)
query: data.test
error: "`bits.and` expects numeric argument. Got `\"str\"` instead"

- note: bits.and.error.morearg
data: {}
modules:
- |
package test
x = bits.and(1, 1, 1)
query: data.test
error: "`bits.and` expects 2 arguments"

- note: bits.and.error.lessarg
data: {}
modules:
- |
package test
x = bits.and(1)
query: data.test
error: "`bits.and` expects 2 arguments"

- note: bits.and
data: {}
modules:
- |
package test
x1 = bits.and(1, 0)
x2 = bits.and(1, 1)
x3 = bits.and(0, 0)
x4 = bits.and(0, 1)
query: data.test
want_result:
x1 : 0
x2 : 1
x3 : 0
x4 : 0
54 changes: 54 additions & 0 deletions tests/interpreter/cases/builtins/bitwise/lsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: bits.lsh.error.wrongtype1
data: {}
modules:
- |
package test
x = bits.lsh(1, "str")
query: data.test
error: "`bits.lsh` expects numeric argument. Got `\"str\"` instead"

- note: bits.lsh.error.wrongtype2
data: {}
modules:
- |
package test
x = bits.lsh("str", 1)
query: data.test
error: "`bits.lsh` expects numeric argument. Got `\"str\"` instead"

- note: bits.lsh.error.morearg
data: {}
modules:
- |
package test
x = bits.lsh(1, 1, 1)
query: data.test
error: "`bits.lsh` expects 2 arguments"

- note: bits.lsh.error.lessarg
data: {}
modules:
- |
package test
x = bits.lsh(1)
query: data.test
error: "`bits.lsh` expects 2 arguments"

- note: bits.lsh
data: {}
modules:
- |
package test
x = bits.lsh(9, 2)
query: data.test
want_result:
x : 36
40 changes: 40 additions & 0 deletions tests/interpreter/cases/builtins/bitwise/negate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: bits.negate.error.wrongtype
data: {}
modules:
- |
package test
x = bits.negate("str")
query: data.test
error: "`bits.negate` expects numeric argument. Got `\"str\"` instead"

- note: bits.negate.error.morearg
data: {}
modules:
- |
package test
x = bits.negate(1, 1)
query: data.test
error: "`bits.negate` expects 1 argument"

- note: bits.negate
data: {}
modules:
- |
package test
x1 = bits.negate(1)
x2 = bits.negate(0)
x3 = bits.negate(9)
x4 = bits.negate(-50)
query: data.test
want_result:
x1 : -2
x2 : -1
x3 : -10
x4 : 49
60 changes: 60 additions & 0 deletions tests/interpreter/cases/builtins/bitwise/or.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: bits.or.error.wrongtype1
data: {}
modules:
- |
package test
x = bits.or(1, "str")
query: data.test
error: "`bits.or` expects numeric argument. Got `\"str\"` instead"

- note: bits.or.error.wrongtype2
data: {}
modules:
- |
package test
x = bits.or("str", 1)
query: data.test
error: "`bits.or` expects numeric argument. Got `\"str\"` instead"

- note: bits.or.error.morearg
data: {}
modules:
- |
package test
x = bits.or(1, 1, 1)
query: data.test
error: "`bits.or` expects 2 arguments"

- note: bits.or.error.lessarg
data: {}
modules:
- |
package test
x = bits.or(1)
query: data.test
error: "`bits.or` expects 2 arguments"

- note: bits.or
data: {}
modules:
- |
package test
x1 = bits.or(1, 0)
x2 = bits.or(1, 1)
x3 = bits.or(0, 0)
x4 = bits.or(0, 1)
query: data.test
want_result:
x1 : 1
x2 : 1
x3 : 0
x4 : 1
54 changes: 54 additions & 0 deletions tests/interpreter/cases/builtins/bitwise/rsh.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: bits.rsh.error.wrongtype1
data: {}
modules:
- |
package test
x = bits.rsh(1, "str")
query: data.test
error: "`bits.rsh` expects numeric argument. Got `\"str\"` instead"

- note: bits.rsh.error.wrongtype2
data: {}
modules:
- |
package test
x = bits.rsh("str", 1)
query: data.test
error: "`bits.rsh` expects numeric argument. Got `\"str\"` instead"

- note: bits.rsh.error.morearg
data: {}
modules:
- |
package test
x = bits.rsh(1, 1, 1)
query: data.test
error: "`bits.rsh` expects 2 arguments"

- note: bits.rsh.error.lessarg
data: {}
modules:
- |
package test
x = bits.rsh(1)
query: data.test
error: "`bits.rsh` expects 2 arguments"

- note: bits.rsh
data: {}
modules:
- |
package test
x = bits.rsh(9, 2)
query: data.test
want_result:
x : 2
60 changes: 60 additions & 0 deletions tests/interpreter/cases/builtins/bitwise/xor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: bits.xor.error.wrongtype1
data: {}
modules:
- |
package test
x = bits.xor(1, "str")
query: data.test
error: "`bits.xor` expects numeric argument. Got `\"str\"` instead"

- note: bits.xor.error.wrongtype2
data: {}
modules:
- |
package test
x = bits.xor("str", 1)
query: data.test
error: "`bits.xor` expects numeric argument. Got `\"str\"` instead"

- note: bits.xor.error.morearg
data: {}
modules:
- |
package test
x = bits.xor(1, 1, 1)
query: data.test
error: "`bits.xor` expects 2 arguments"

- note: bits.xor.error.lessarg
data: {}
modules:
- |
package test
x = bits.xor(1)
query: data.test
error: "`bits.xor` expects 2 arguments"

- note: bits.xor
data: {}
modules:
- |
package test
x1 = bits.xor(1, 0)
x2 = bits.xor(1, 1)
x3 = bits.xor(0, 0)
x4 = bits.xor(0, 1)
query: data.test
want_result:
x1 : 1
x2 : 0
x3 : 0
x4 : 1

0 comments on commit e08d13d

Please sign in to comment.