Skip to content

Commit 7b345d0

Browse files
committed
Add bool tests
1 parent d8875cb commit 7b345d0

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

jit/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ impl From<f64> for AbiValue {
216216
}
217217
}
218218

219+
impl From<bool> for AbiValue {
220+
fn from(b: bool) -> Self {
221+
AbiValue::Bool(b)
222+
}
223+
}
224+
219225
impl TryFrom<AbiValue> for i64 {
220226
type Error = ();
221227

@@ -238,9 +244,21 @@ impl TryFrom<AbiValue> for f64 {
238244
}
239245
}
240246

247+
impl TryFrom<AbiValue> for bool {
248+
type Error = ();
249+
250+
fn try_from(value: AbiValue) -> Result<Self, Self::Error> {
251+
match value {
252+
AbiValue::Bool(b) => Ok(b),
253+
_ => Err(())
254+
}
255+
}
256+
}
257+
241258
fn type_check(ty: &JitType, val: &AbiValue) -> Result<(), JitArgumentError> {
242259
match (ty, val) {
243-
(JitType::Int, AbiValue::Int(_)) | (JitType::Float, AbiValue::Float(_)) => Ok(()),
260+
(JitType::Int, AbiValue::Int(_)) | (JitType::Float, AbiValue::Float(_)) |
261+
(JitType::Bool, AbiValue::Bool(_)) => Ok(()),
244262
_ => Err(JitArgumentError::ArgumentTypeMismatch),
245263
}
246264
}

jit/tests/bool_tests.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[test]
2+
fn test_return() {
3+
let return_ = jit_function! { return_(a: bool) -> bool => r##"
4+
def return_(a: bool):
5+
return a
6+
"## };
7+
8+
assert_eq!(return_(true), Ok(true));
9+
assert_eq!(return_(false), Ok(false));
10+
}
11+
12+
#[test]
13+
fn test_const() {
14+
let const_true = jit_function! { const_true(a: i64) -> bool => r##"
15+
def const_true(a: int):
16+
return True
17+
"## };
18+
assert_eq!(const_true(0), Ok(true));
19+
20+
let const_false = jit_function! { const_false(a: i64) -> bool => r##"
21+
def const_false(a: int):
22+
return False
23+
"## };
24+
assert_eq!(const_false(0), Ok(false));
25+
}
26+
27+
#[test]
28+
fn test_not() {
29+
let not_ = jit_function! { not_(a: bool) -> bool => r##"
30+
def not_(a: bool):
31+
return not a
32+
"## };
33+
34+
assert_eq!(not_(true), Ok(false));
35+
assert_eq!(not_(false), Ok(true));
36+
}

jit/tests/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl Function {
1818
Some(StackValue::String(annotation)) => match annotation.as_str() {
1919
"int" => JitType::Int,
2020
"float" => JitType::Float,
21+
"bool" => JitType::Bool,
2122
_ => panic!("Unrecognised jit type"),
2223
},
2324
_ => panic!("Argument have annotation"),

jit/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[macro_use]
22
mod common;
3+
mod bool_tests;
34
mod float_tests;
45
mod int_tests;
56
mod misc_tests;

0 commit comments

Comments
 (0)