Skip to content

Commit

Permalink
Add bool tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie committed Oct 17, 2020
1 parent d8875cb commit 7b345d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
20 changes: 19 additions & 1 deletion jit/src/lib.rs
Expand Up @@ -216,6 +216,12 @@ impl From<f64> for AbiValue {
}
}

impl From<bool> for AbiValue {
fn from(b: bool) -> Self {
AbiValue::Bool(b)
}
}

impl TryFrom<AbiValue> for i64 {
type Error = ();

Expand All @@ -238,9 +244,21 @@ impl TryFrom<AbiValue> for f64 {
}
}

impl TryFrom<AbiValue> for bool {
type Error = ();

fn try_from(value: AbiValue) -> Result<Self, Self::Error> {
match value {
AbiValue::Bool(b) => Ok(b),
_ => Err(())
}
}
}

fn type_check(ty: &JitType, val: &AbiValue) -> Result<(), JitArgumentError> {
match (ty, val) {
(JitType::Int, AbiValue::Int(_)) | (JitType::Float, AbiValue::Float(_)) => Ok(()),
(JitType::Int, AbiValue::Int(_)) | (JitType::Float, AbiValue::Float(_)) |
(JitType::Bool, AbiValue::Bool(_)) => Ok(()),
_ => Err(JitArgumentError::ArgumentTypeMismatch),
}
}
Expand Down
36 changes: 36 additions & 0 deletions jit/tests/bool_tests.rs
@@ -0,0 +1,36 @@
#[test]
fn test_return() {
let return_ = jit_function! { return_(a: bool) -> bool => r##"
def return_(a: bool):
return a
"## };

assert_eq!(return_(true), Ok(true));
assert_eq!(return_(false), Ok(false));
}

#[test]
fn test_const() {
let const_true = jit_function! { const_true(a: i64) -> bool => r##"
def const_true(a: int):
return True
"## };
assert_eq!(const_true(0), Ok(true));

let const_false = jit_function! { const_false(a: i64) -> bool => r##"
def const_false(a: int):
return False
"## };
assert_eq!(const_false(0), Ok(false));
}

#[test]
fn test_not() {
let not_ = jit_function! { not_(a: bool) -> bool => r##"
def not_(a: bool):
return not a
"## };

assert_eq!(not_(true), Ok(false));
assert_eq!(not_(false), Ok(true));
}
1 change: 1 addition & 0 deletions jit/tests/common.rs
Expand Up @@ -18,6 +18,7 @@ impl Function {
Some(StackValue::String(annotation)) => match annotation.as_str() {
"int" => JitType::Int,
"float" => JitType::Float,
"bool" => JitType::Bool,
_ => panic!("Unrecognised jit type"),
},
_ => panic!("Argument have annotation"),
Expand Down
1 change: 1 addition & 0 deletions jit/tests/lib.rs
@@ -1,5 +1,6 @@
#[macro_use]
mod common;
mod bool_tests;
mod float_tests;
mod int_tests;
mod misc_tests;

0 comments on commit 7b345d0

Please sign in to comment.