// SPDX-License-Identifier: GPL-3.0-or-later
//! Min/max brightness range clamping — pure functions, 100% test coverage target.
//!
//! BDD Specs (P4):
//! Given display 1 with min=20%, max=80%
//! When I set brightness to 100%
//! Then brightness is clamped to 80%
//!
//! Given display 1 with min=20%, max=80%
//! When I set brightness to 10%
//! Then brightness is clamped to 20%

/// Clamp a brightness value to [min, max] range.
pub fn clamp_brightness(value: f64, min: f64, max: f64) -> f64 {
    value.clamp(min, max)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clamp_to_max() {
        assert!((clamp_brightness(100.0, 20.0, 80.0) - 80.0).abs() < f64::EPSILON);
    }

    #[test]
    fn clamp_to_min() {
        assert!((clamp_brightness(10.0, 20.0, 80.0) - 20.0).abs() < f64::EPSILON);
    }

    #[test]
    fn within_range_unchanged() {
        assert!((clamp_brightness(50.0, 20.0, 80.0) - 50.0).abs() < f64::EPSILON);
    }

    #[test]
    fn at_min_unchanged() {
        assert!((clamp_brightness(20.0, 20.0, 80.0) - 20.0).abs() < f64::EPSILON);
    }

    #[test]
    fn at_max_unchanged() {
        assert!((clamp_brightness(80.0, 20.0, 80.0) - 80.0).abs() < f64::EPSILON);
    }

    #[test]
    fn no_caps_full_range() {
        assert!((clamp_brightness(50.0, 0.0, 100.0) - 50.0).abs() < f64::EPSILON);
    }

    #[test]
    fn zero_clamp() {
        assert!((clamp_brightness(-5.0, 0.0, 100.0)).abs() < f64::EPSILON);
    }

    #[test]
    fn hundred_clamp() {
        assert!((clamp_brightness(150.0, 0.0, 100.0) - 100.0).abs() < f64::EPSILON);
    }
}
