use rhai::Engine;
pub fn register_functions(engine: &mut Engine) {
engine.register_fn("mod", |a: i64, b: i64| -> i64 {
if b == 0 {
0 } else {
a % b
}
});
engine.register_fn("%", |a: i64, b: i64| -> i64 {
if b == 0 {
0 } else {
a % b
}
});
engine.register_fn("clamp", clamp_i64);
engine.register_fn("clamp", clamp_f64);
}
fn clamp_i64(value: i64, min: i64, max: i64) -> i64 {
value.clamp(min, max)
}
fn clamp_f64(value: f64, min: f64, max: f64) -> f64 {
value.clamp(min, max)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_clamp_i64_within_range() {
assert_eq!(clamp_i64(5, 0, 10), 5);
assert_eq!(clamp_i64(50, 0, 100), 50);
assert_eq!(clamp_i64(0, 0, 10), 0);
assert_eq!(clamp_i64(10, 0, 10), 10);
}
#[test]
fn test_clamp_i64_below_min() {
assert_eq!(clamp_i64(-5, 0, 10), 0);
assert_eq!(clamp_i64(-100, 0, 100), 0);
assert_eq!(clamp_i64(-1, 0, 10), 0);
}
#[test]
fn test_clamp_i64_above_max() {
assert_eq!(clamp_i64(15, 0, 10), 10);
assert_eq!(clamp_i64(200, 0, 100), 100);
assert_eq!(clamp_i64(11, 0, 10), 10);
}
#[test]
fn test_clamp_i64_negative_range() {
assert_eq!(clamp_i64(-5, -10, -1), -5);
assert_eq!(clamp_i64(-15, -10, -1), -10);
assert_eq!(clamp_i64(0, -10, -1), -1);
}
#[test]
#[should_panic(expected = "assertion failed: min <= max")]
fn test_clamp_i64_inverted_range() {
clamp_i64(5, 10, 0);
}
#[test]
fn test_clamp_f64_within_range() {
assert_eq!(clamp_f64(3.5, 0.0, 5.0), 3.5);
assert_eq!(clamp_f64(2.5, 0.0, 10.0), 2.5);
assert_eq!(clamp_f64(0.0, 0.0, 5.0), 0.0);
assert_eq!(clamp_f64(5.0, 0.0, 5.0), 5.0);
}
#[test]
fn test_clamp_f64_below_min() {
assert_eq!(clamp_f64(-1.5, 0.0, 5.0), 0.0);
assert_eq!(clamp_f64(-100.0, 0.0, 100.0), 0.0);
assert_eq!(clamp_f64(-0.1, 0.0, 10.0), 0.0);
}
#[test]
fn test_clamp_f64_above_max() {
assert_eq!(clamp_f64(7.8, 0.0, 5.0), 5.0);
assert_eq!(clamp_f64(200.5, 0.0, 100.0), 100.0);
assert_eq!(clamp_f64(10.1, 0.0, 10.0), 10.0);
}
#[test]
fn test_clamp_f64_negative_range() {
assert_eq!(clamp_f64(-5.5, -10.0, -1.0), -5.5);
assert_eq!(clamp_f64(-15.0, -10.0, -1.0), -10.0);
assert_eq!(clamp_f64(0.0, -10.0, -1.0), -1.0);
}
#[test]
#[should_panic(expected = "min > max")]
fn test_clamp_f64_inverted_range() {
clamp_f64(5.0, 10.0, 0.0);
}
#[test]
fn test_clamp_f64_fractional() {
assert_eq!(clamp_f64(0.5, 0.0, 1.0), 0.5);
assert_eq!(clamp_f64(1.5, 0.0, 1.0), 1.0);
assert_eq!(clamp_f64(-0.5, 0.0, 1.0), 0.0);
}
}