Skip to content

Commit

Permalink
rune: Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 5, 2024
1 parent 930d24d commit 466b829
Showing 1 changed file with 83 additions and 74 deletions.
157 changes: 83 additions & 74 deletions crates/rune/src/tests/vm_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ prelude!();
use VmErrorKind::*;

macro_rules! op_tests {
($ty:ty, ! $lhs:literal = $out:expr) => {
op_tests!(@unary $ty, !, $lhs, $out);
};

($ty:ty, $lhs:literal $op:tt $rhs:literal = $out:expr) => {
op_tests!(@binary $ty, $lhs, $op, $rhs, $out);
};

(@binary $ty:ty, $lhs:literal, $op:tt, $rhs:literal, $out:expr) => {
let out: $ty = rune!(pub fn main() { let a = $lhs; let b = $rhs; a $op b});
assert_eq!(out, $out);

Expand Down Expand Up @@ -60,7 +68,66 @@ macro_rules! op_tests {
lhs = stringify!($lhs), rhs = stringify!($rhs), op = stringify!($op),
));
assert_eq!(out, $out);
}
};

(@unary $ty:ty, $op:tt, $lhs:literal, $out:expr) => {
let out: $ty = rune!(pub fn main() { let a = $lhs; $op a});
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"pub fn main() {{ let a = {lhs}; {op} a }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"struct Foo {{ padding, field }}; pub fn main() {{ let a = Foo{{ padding: 0, field: {lhs} }}; {op} a.field }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"enum Enum {{ Foo {{ padding, field }} }}; pub fn main() {{ let a = Enum::Foo {{ padding: 0, field: {lhs} }}; {op} a.field }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"pub fn main() {{ let a = #{{ padding: 0, field: {lhs} }}; {op} a.field }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"pub fn main() {{ let a = (0, {lhs}); {op} a.1 }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"struct Foo(padding, a); pub fn main() {{ let a = Foo(0, {lhs}); {op} a.1 }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"enum Enum {{ Foo(padding, a) }}; pub fn main() {{ let a = Enum::Foo(0, {lhs}); {op} a.1 }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"pub fn main() {{ let a = Ok({lhs}); {op} a.0 }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);

let out: $ty = eval(&format!(
r#"pub fn main() {{ let a = Some({lhs}); {op} a.0 }}"#,
lhs = stringify!($lhs), op = stringify!($op),
));
assert_eq!(out, $out);
};
}

macro_rules! error_test {
Expand Down Expand Up @@ -100,115 +167,57 @@ macro_rules! error_test {
}

#[test]
fn test_add() {
fn i64() {
op_tests!(i64, 10 + 2 = 12);
error_test!(9223372036854775807i64 + 2 = Overflow);
}

#[test]
fn test_sub() {
op_tests!(i64, 10 - 2 = 8);
error_test!(-9223372036854775808i64 - 2 = Underflow);
}

#[test]
fn test_mul() {
op_tests!(i64, 10 * 2 = 20);
error_test!(9223372036854775807i64 * 2 = Overflow);
}

#[test]
fn test_div() {
op_tests!(i64, 10 / 2 = 5);
error_test!(10 / 0 = DivideByZero);
}

#[test]
fn test_rem() {
op_tests!(i64, 10 % 3 = 1);
error_test!(10 % 0 = DivideByZero);
}

#[test]
fn test_bit_ops_i64() {
op_tests!(i64, 0b1100 & 0b0110 = 0b1100 & 0b0110);
op_tests!(i64, 0b1100 ^ 0b0110 = 0b1100 ^ 0b0110);
op_tests!(i64, 0b1100 | 0b0110 = 0b1100 | 0b0110);
op_tests!(i64, 0b1100 << 2 = 0b1100 << 2);
op_tests!(i64, 0b1100 >> 2 = 0b1100 >> 2);
op_tests!(i64, !0b10100i64 = !0b10100i64);

error_test!(9223372036854775807i64 + 2 = Overflow);
error_test!(-9223372036854775808i64 - 2 = Underflow);
error_test!(9223372036854775807i64 * 2 = Overflow);
error_test!(10 / 0 = DivideByZero);
error_test!(10 % 0 = DivideByZero);
error_test!(0b1 << 64 = Overflow);
error_test!(0b1 >> 64 = Underflow);
}

#[test]
fn test_bit_ops_u64() {
fn u64() {
op_tests!(u64, 0b1100 & 0b0110 = 0b1100 & 0b0110);
op_tests!(u64, 0b1100 ^ 0b0110 = 0b1100 ^ 0b0110);
op_tests!(u64, 0b1100 | 0b0110 = 0b1100 | 0b0110);
op_tests!(u64, 0b1100 << 2 = 0b1100 << 2);
op_tests!(u64, 0b1100 >> 2 = 0b1100 >> 2);
op_tests!(u64, !0b10100u64 = !0b10100u64);

error_test!(0b1 << 64 = Overflow);
error_test!(0b1 >> 64 = Underflow);
}

#[test]
fn test_bit_ops_u8() {
fn u8() {
op_tests!(u8, 0b1100u8 & 0b0110u8 = 0b1100u8 & 0b0110u8);
op_tests!(u8, 0b1100u8 ^ 0b0110u8 = 0b1100u8 ^ 0b0110u8);
op_tests!(u8, 0b1100u8 | 0b0110u8 = 0b1100u8 | 0b0110u8);
op_tests!(u8, 0b1100u8 << 2 = 0b1100u8 << 2);
op_tests!(u8, 0b1100u8 >> 2 = 0b1100u8 >> 2);
op_tests!(u64, !0b10100u8 = !0b10100u64);
}

#[test]
fn test_bit_ops_i8() {
fn i8() {
op_tests!(i8, 0b1100i8 & 0b0110i8 = 0b1100i8 & 0b0110i8);
op_tests!(i8, 0b1100i8 ^ 0b0110i8 = 0b1100i8 ^ 0b0110i8);
op_tests!(i8, 0b1100i8 | 0b0110i8 = 0b1100i8 | 0b0110i8);
op_tests!(i8, 0b1100i8 << 2 = 0b1100i8 << 2);
op_tests!(i8, 0b1100i8 >> 2 = 0b1100i8 >> 2);
}

#[test]
fn test_bitwise_not_i64() {
let out: i64 = rune!(
pub fn main() {
let a = 0b10100i64;
!a
}
);
assert_eq!(out, !0b10100i64);
}

#[test]
fn test_bitwise_not_u64() {
let out: u64 = rune!(
pub fn main() {
let a = 0b10100u64;
!a
}
);
assert_eq!(out, !0b10100u64);
}

#[test]
fn test_bitwise_not_u8() {
let out: u64 = rune!(
pub fn main() {
let a = 0b10100u8;
!a
}
);
assert_eq!(out, !0b10100u64);
}

#[test]
fn test_bitwise_not_i8() {
let out: i64 = rune!(
pub fn main() {
let a = 0b10100i8;
!a
}
);
assert_eq!(out, !0b10100i64);
op_tests!(i64, !0b10100i8 = !0b10100i64);
}

0 comments on commit 466b829

Please sign in to comment.