use crate::mechanics::evolution::policies::DirectionPolicy;
use std::f32::consts::PI;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Oscillating;
impl Oscillating {
pub const PERIOD: f32 = 100.0;
}
impl DirectionPolicy for Oscillating {
fn calculate_direction(_current_value: f32, _min: f32, _max: f32, elapsed_time: f32) -> f32 {
let phase = (2.0 * PI * elapsed_time) / Self::PERIOD;
phase.cos()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_oscillating_at_zero() {
let direction = Oscillating::calculate_direction(50.0, 0.0, 100.0, 0.0);
assert!((direction - 1.0).abs() < f32::EPSILON);
}
#[test]
fn test_oscillating_at_quarter_period() {
let direction = Oscillating::calculate_direction(50.0, 0.0, 100.0, 25.0);
assert!(direction.abs() < 0.01);
}
#[test]
fn test_oscillating_at_half_period() {
let direction = Oscillating::calculate_direction(50.0, 0.0, 100.0, 50.0);
assert!((direction - (-1.0)).abs() < 0.01);
}
#[test]
fn test_oscillating_at_three_quarter_period() {
let direction = Oscillating::calculate_direction(50.0, 0.0, 100.0, 75.0);
assert!(direction.abs() < 0.01);
}
#[test]
fn test_oscillating_full_period() {
let direction = Oscillating::calculate_direction(50.0, 0.0, 100.0, 100.0);
assert!((direction - 1.0).abs() < 0.01);
}
#[test]
fn test_oscillating_independent_of_value() {
let t = 30.0;
let dir1 = Oscillating::calculate_direction(10.0, 0.0, 100.0, t);
let dir2 = Oscillating::calculate_direction(90.0, 0.0, 100.0, t);
assert!((dir1 - dir2).abs() < f32::EPSILON);
}
}