2021-03-08 08:55:26 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, INT};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_left_shift() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-10 16:06:20 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("4 << 2")?, 16);
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_right_shift() -> Result<(), Box<EvalAltResult>> {
|
2020-04-07 07:23:06 +02:00
|
|
|
let engine = Engine::new();
|
2020-03-10 16:06:20 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("9 >> 1")?, 4);
|
2020-03-02 15:11:56 +01:00
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
2021-06-02 08:29:18 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
#[test]
|
|
|
|
fn test_bit_fields() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
assert!(!engine.eval::<bool>("let x = 10; x[0]")?);
|
|
|
|
assert!(engine.eval::<bool>("let x = 10; x[1]")?);
|
|
|
|
assert!(!engine.eval::<bool>("let x = 10; x[-1]")?);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = 10; x[0] = true; x[1] = false; x")?,
|
|
|
|
9
|
|
|
|
);
|
|
|
|
assert_eq!(engine.eval::<INT>("let x = 10; get_bits(x, 1, 3)")?, 5);
|
2021-12-15 05:06:17 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let x = 10; x[1..=3]")?, 5);
|
2022-01-13 11:13:27 +01:00
|
|
|
assert!(engine.eval::<INT>("let x = 10; x[1..99]").is_err());
|
|
|
|
assert!(engine.eval::<INT>("let x = 10; x[-1..3]").is_err());
|
2021-06-02 08:29:18 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = 10; set_bits(x, 1, 3, 7); x")?,
|
|
|
|
14
|
|
|
|
);
|
2022-01-13 11:13:27 +01:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2022-01-14 16:19:27 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2022-01-13 11:13:27 +01:00
|
|
|
{
|
|
|
|
assert_eq!(engine.eval::<INT>("let x = 255; get_bits(x, -60, 2)")?, 3);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = 0; set_bits(x, -64, 1, 15); x")?,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let x = 0; set_bits(x, -60, 2, 15); x")?,
|
|
|
|
0b00110000
|
|
|
|
);
|
|
|
|
}
|
2021-12-15 05:06:17 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let x = 10; x[1..4] = 7; x")?, 14);
|
2021-06-02 08:29:18 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = 0b001101101010001;
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
for b in bits(x, 2, 10) {
|
|
|
|
if b { count += 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
count
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
5
|
|
|
|
);
|
2021-12-15 05:06:17 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let x = 0b001101101010001;
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
for b in bits(x, 2..=11) {
|
|
|
|
if b { count += 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
count
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
5
|
|
|
|
);
|
2021-06-02 08:29:18 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|