rhai/tests/number_literals.rs

74 lines
1.9 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, INT};
2017-11-03 17:58:51 +01:00
#[test]
fn test_number_literal() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
assert_eq!(engine.eval::<INT>("42")?, 42);
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval::<String>("42.type_of()")?,
if cfg!(feature = "only_i32") {
"i32"
} else {
"i64"
}
);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
#[test]
fn test_hex_literal() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
assert_eq!(engine.eval::<INT>("let x = 0xf; x")?, 15);
assert_eq!(engine.eval::<INT>("let x = 0Xf; x")?, 15);
assert_eq!(engine.eval::<INT>("let x = 0xff; x")?, 255);
2020-03-02 15:11:56 +01:00
#[cfg(not(feature = "only_i32"))]
assert_eq!(engine.eval::<INT>("let x = 0xffffffffffffffff; x")?, -1);
#[cfg(feature = "only_i32")]
assert_eq!(engine.eval::<INT>("let x = 0xffffffff; x")?, -1);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
#[test]
fn test_octal_literal() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
assert_eq!(engine.eval::<INT>("let x = 0o77; x")?, 63);
assert_eq!(engine.eval::<INT>("let x = 0O77; x")?, 63);
assert_eq!(engine.eval::<INT>("let x = 0o1234; x")?, 668);
2020-03-02 15:11:56 +01:00
Ok(())
2017-11-03 17:58:51 +01:00
}
#[test]
fn test_binary_literal() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 17:58:51 +01:00
assert_eq!(engine.eval::<INT>("let x = 0b1111; x")?, 15);
assert_eq!(engine.eval::<INT>("let x = 0B1111; x")?, 15);
assert_eq!(
engine.eval::<INT>("let x = 0b0011_1100_1010_0101; x")?,
2020-03-02 15:11:56 +01:00
15525
);
#[cfg(not(feature = "only_i32"))]
assert_eq!(
engine.eval::<INT>(
"let x = 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111; x"
)?,
-1
);
#[cfg(feature = "only_i32")]
assert_eq!(
engine.eval::<INT>("let x = 0b11111111_11111111_11111111_11111111; x")?,
-1
);
2020-03-02 15:11:56 +01:00
Ok(())
}