2020-03-10 12:48:47 +01:00
|
|
|
#![cfg(not(feature = "no_float"))]
|
2020-03-02 15:11:56 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, RegisterFn};
|
2017-11-03 17:58:51 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn test_float() -> Result<(), EvalAltResult> {
|
2017-11-03 17:58:51 +01:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2019-10-09 13:06:32 +02:00
|
|
|
assert_eq!(
|
2020-03-02 15:11:56 +01:00
|
|
|
engine.eval::<bool>("let x = 0.0; let y = 1.0; x < y")?,
|
|
|
|
true
|
2019-10-09 13:06:32 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-03-02 15:11:56 +01:00
|
|
|
engine.eval::<bool>("let x = 0.0; let y = 1.0; x > y")?,
|
|
|
|
false
|
2019-10-09 13:06:32 +02:00
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
assert_eq!(engine.eval::<f64>("let x = 9.9999; x")?, 9.9999);
|
|
|
|
|
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-02 15:11:56 +01:00
|
|
|
fn struct_with_float() -> Result<(), EvalAltResult> {
|
2017-11-03 17:58:51 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestStruct {
|
|
|
|
x: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStruct {
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.x += 5.789_f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_x(&mut self) -> f64 {
|
|
|
|
self.x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_x(&mut self, new_x: f64) {
|
|
|
|
self.x = new_x;
|
|
|
|
}
|
|
|
|
|
2020-03-19 06:52:10 +01:00
|
|
|
fn new() -> Self {
|
2017-11-03 17:58:51 +01:00
|
|
|
TestStruct { x: 1.0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_type::<TestStruct>();
|
|
|
|
|
|
|
|
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
|
|
|
|
engine.register_fn("update", TestStruct::update);
|
|
|
|
engine.register_fn("new_ts", TestStruct::new);
|
|
|
|
|
2019-10-09 13:06:32 +02:00
|
|
|
assert_eq!(
|
2020-03-02 15:11:56 +01:00
|
|
|
engine.eval::<f64>("let ts = new_ts(); ts.update(); ts.x")?,
|
|
|
|
6.789
|
2019-10-09 13:06:32 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-03-02 15:11:56 +01:00
|
|
|
engine.eval::<f64>("let ts = new_ts(); ts.x = 10.1001; ts.x")?,
|
|
|
|
10.1001
|
2019-10-09 13:06:32 +02:00
|
|
|
);
|
2020-03-02 15:11:56 +01:00
|
|
|
|
|
|
|
Ok(())
|
2017-11-03 17:58:51 +01:00
|
|
|
}
|