1
0
dust/dust-lang/tests/comparison.rs

170 lines
4.9 KiB
Rust
Raw Permalink Normal View History

2024-11-01 00:33:46 +00:00
use dust_lang::*;
#[test]
fn equal() {
let source = "1 == 2";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
vec![
(
*Instruction::equal(true, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(2, 4)
),
(Instruction::jump(1, true), Span(2, 4)),
(Instruction::load_boolean(0, true, true), Span(2, 4)),
(Instruction::load_boolean(0, false, false), Span(2, 4)),
(Instruction::r#return(true), Span(6, 6)),
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::integer(1), ValueOwned::integer(2)],
2024-11-01 00:33:46 +00:00
vec![]
)),
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::boolean(false))));
2024-11-01 00:33:46 +00:00
}
#[test]
fn greater() {
let source = "1 > 2";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
vec![
(
*Instruction::less_equal(false, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(2, 3)
),
(Instruction::jump(1, true), Span(2, 3)),
(Instruction::load_boolean(0, true, true), Span(2, 3)),
(Instruction::load_boolean(0, false, false), Span(2, 3)),
(Instruction::r#return(true), Span(5, 5)),
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::integer(1), ValueOwned::integer(2)],
2024-11-01 00:33:46 +00:00
vec![]
)),
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::boolean(false))));
2024-11-01 00:33:46 +00:00
}
#[test]
fn greater_than_or_equal() {
let source = "1 >= 2";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
vec![
(
*Instruction::less(false, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(2, 4)
),
(Instruction::jump(1, true), Span(2, 4)),
(Instruction::load_boolean(0, true, true), Span(2, 4)),
(Instruction::load_boolean(0, false, false), Span(2, 4)),
(Instruction::r#return(true), Span(6, 6)),
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::integer(1), ValueOwned::integer(2)],
2024-11-01 00:33:46 +00:00
vec![]
)),
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::boolean(false))));
2024-11-01 00:33:46 +00:00
}
#[test]
fn less_than() {
let source = "1 < 2";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
vec![
(
*Instruction::less(true, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(2, 3)
),
(Instruction::jump(1, true), Span(2, 3)),
(Instruction::load_boolean(0, true, true), Span(2, 3)),
(Instruction::load_boolean(0, false, false), Span(2, 3)),
(Instruction::r#return(true), Span(5, 5)),
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::integer(1), ValueOwned::integer(2)],
2024-11-01 00:33:46 +00:00
vec![]
)),
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::boolean(true))));
2024-11-01 00:33:46 +00:00
}
#[test]
fn less_than_or_equal() {
let source = "1 <= 2";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
vec![
(
*Instruction::less_equal(true, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(2, 4)
),
(Instruction::jump(1, true), Span(2, 4)),
(Instruction::load_boolean(0, true, true), Span(2, 4)),
(Instruction::load_boolean(0, false, false), Span(2, 4)),
(Instruction::r#return(true), Span(6, 6)),
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::integer(1), ValueOwned::integer(2)],
2024-11-01 00:33:46 +00:00
vec![]
)),
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::boolean(true))));
2024-11-01 00:33:46 +00:00
}
#[test]
fn not_equal() {
let source = "1 != 2";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
vec![
(
*Instruction::equal(false, 0, 1)
.set_b_is_constant()
.set_c_is_constant(),
Span(2, 4)
),
(Instruction::jump(1, true), Span(2, 4)),
(Instruction::load_boolean(0, true, true), Span(2, 4)),
(Instruction::load_boolean(0, false, false), Span(2, 4)),
(Instruction::r#return(true), Span(6, 6)),
],
2024-11-11 00:28:21 +00:00
vec![ValueOwned::integer(1), ValueOwned::integer(2)],
2024-11-01 00:33:46 +00:00
vec![]
)),
);
2024-11-11 00:28:21 +00:00
assert_eq!(run(source), Ok(Some(ValueOwned::boolean(true))));
2024-11-01 00:33:46 +00:00
}