use super::types::Environment;
pub trait DirectionPolicy {
fn calculate_direction(current_value: f32, min: f32, max: f32, elapsed_time: f32) -> f32;
}
pub trait EnvironmentalPolicy {
fn calculate_environmental_multiplier(environment: &Environment) -> f32;
}
pub trait RateCalculationPolicy {
fn calculate_rate(
base_rate: f32,
current_value: f32,
min: f32,
max: f32,
direction_multiplier: f32,
environmental_multiplier: f32,
) -> f32;
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
struct TestGrowth;
impl DirectionPolicy for TestGrowth {
fn calculate_direction(_: f32, _: f32, _: f32, _: f32) -> f32 {
1.0
}
}
struct TestDecay;
impl DirectionPolicy for TestDecay {
fn calculate_direction(_: f32, _: f32, _: f32, _: f32) -> f32 {
-1.0
}
}
struct TestNoEnvironment;
impl EnvironmentalPolicy for TestNoEnvironment {
fn calculate_environmental_multiplier(_: &Environment) -> f32 {
1.0
}
}
struct TestLinearRate;
impl RateCalculationPolicy for TestLinearRate {
fn calculate_rate(
base_rate: f32,
_: f32,
_: f32,
_: f32,
direction_multiplier: f32,
environmental_multiplier: f32,
) -> f32 {
base_rate * direction_multiplier * environmental_multiplier
}
}
#[test]
fn test_direction_policy_growth() {
let direction = TestGrowth::calculate_direction(50.0, 0.0, 100.0, 0.0);
assert_eq!(direction, 1.0);
}
#[test]
fn test_direction_policy_decay() {
let direction = TestDecay::calculate_direction(50.0, 0.0, 100.0, 0.0);
assert_eq!(direction, -1.0);
}
#[test]
fn test_environmental_policy_no_effect() {
let env = Environment {
temperature: 25.0,
humidity: 0.5,
pressure: 1.0,
custom: HashMap::new(),
};
let multiplier = TestNoEnvironment::calculate_environmental_multiplier(&env);
assert_eq!(multiplier, 1.0);
}
#[test]
fn test_rate_calculation_linear() {
let rate = TestLinearRate::calculate_rate(
2.0, 50.0, 0.0, 100.0, 1.0, 1.5, );
assert_eq!(rate, 3.0); }
#[test]
fn test_rate_calculation_with_negative_direction() {
let rate = TestLinearRate::calculate_rate(
2.0, 50.0, 0.0, 100.0, -1.0, 1.0, );
assert_eq!(rate, -2.0); }
}