2020-04-15 16:21:23 +02:00
|
|
|
#![cfg(not(feature = "no_std"))]
|
2020-06-17 03:54:17 +02:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2020-04-11 10:06:57 +02:00
|
|
|
|
2020-07-27 12:10:45 +02:00
|
|
|
use rhai::{Engine, EvalAltResult};
|
2020-04-11 10:06:57 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
use rhai::FLOAT;
|
|
|
|
|
2020-07-27 12:10:45 +02:00
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
use rhai::INT;
|
|
|
|
|
2020-04-11 10:06:57 +02:00
|
|
|
#[test]
|
2020-04-21 17:25:12 +02:00
|
|
|
fn test_timestamp() -> Result<(), Box<EvalAltResult>> {
|
2020-04-11 10:06:57 +02:00
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<String>("type_of(timestamp())")?, "timestamp");
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
assert!(
|
|
|
|
engine.eval::<FLOAT>(
|
|
|
|
r#"
|
|
|
|
let time = timestamp();
|
|
|
|
let x = 10_000;
|
|
|
|
while x > 0 { x -= 1; }
|
|
|
|
elapsed(time)
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-04-11 10:06:57 +02:00
|
|
|
)? < 10.0
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
assert!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let time = timestamp();
|
|
|
|
let x = 10_000;
|
|
|
|
while x > 0 { x -= 1; }
|
|
|
|
elapsed(time)
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-04-11 10:06:57 +02:00
|
|
|
)? < 10
|
|
|
|
);
|
|
|
|
|
2020-08-16 17:41:59 +02:00
|
|
|
assert!(engine.eval::<bool>(
|
|
|
|
r"
|
|
|
|
let time1 = timestamp();
|
|
|
|
for x in range(0, 10000) {}
|
|
|
|
let time2 = timestamp();
|
|
|
|
time1 <= time2
|
|
|
|
"
|
|
|
|
)?);
|
|
|
|
|
2020-04-11 10:06:57 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-26 13:45:33 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_timestamp_op() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
assert!(
|
|
|
|
(engine.eval::<FLOAT>(
|
|
|
|
r#"
|
|
|
|
let time1 = timestamp();
|
|
|
|
let time2 = time1 + 123.45;
|
|
|
|
time2 - time1
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-09-26 13:45:33 +02:00
|
|
|
)? - 123.45)
|
|
|
|
.abs()
|
|
|
|
< 0.001
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
assert!(
|
|
|
|
(engine.eval::<FLOAT>(
|
|
|
|
r#"
|
|
|
|
let time1 = timestamp();
|
|
|
|
let time2 = time1 - 123.45;
|
|
|
|
time1 - time2
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-09-26 13:45:33 +02:00
|
|
|
)? - 123.45)
|
|
|
|
.abs()
|
|
|
|
< 0.001
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let time1 = timestamp();
|
|
|
|
let time2 = time1 + 42;
|
|
|
|
time2 - time1
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-09-26 13:45:33 +02:00
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let time1 = timestamp();
|
|
|
|
let time2 = time1 - 42;
|
|
|
|
time1 - time2
|
2021-03-27 11:08:34 +01:00
|
|
|
"#
|
2020-09-26 13:45:33 +02:00
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|