rhai/tests/time.rs

110 lines
2.3 KiB
Rust
Raw Normal View History

#![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
use rhai::{Engine, EvalAltResult};
2020-04-11 10:06:57 +02:00
#[cfg(not(feature = "no_float"))]
use rhai::FLOAT;
#[cfg(feature = "no_float")]
use rhai::INT;
2020-04-11 10:06:57 +02:00
#[test]
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
);
assert!(engine.eval::<bool>(
2021-04-20 06:01:35 +02:00
"
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(())
}