use crate::EntityId;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeficitSegment {
pub depth_mw: Option<f64>,
pub cost_per_mwh: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Bus {
pub id: EntityId,
pub name: String,
pub deficit_segments: Vec<DeficitSegment>,
pub excess_cost: f64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bus_construction() {
let bus = Bus {
id: EntityId::from(1),
name: "Bus A".to_string(),
deficit_segments: vec![
DeficitSegment {
depth_mw: Some(100.0),
cost_per_mwh: 500.0,
},
DeficitSegment {
depth_mw: Some(200.0),
cost_per_mwh: 1000.0,
},
DeficitSegment {
depth_mw: None,
cost_per_mwh: 5000.0,
},
],
excess_cost: 1.0,
};
assert_eq!(bus.id, EntityId::from(1));
assert_eq!(bus.name, "Bus A");
assert_eq!(bus.deficit_segments.len(), 3);
assert_eq!(bus.deficit_segments[2].depth_mw, None);
assert_eq!(bus.excess_cost, 1.0);
}
#[test]
fn test_deficit_segment_unbounded() {
let segment = DeficitSegment {
depth_mw: None,
cost_per_mwh: 9999.0,
};
assert_eq!(segment.depth_mw, None);
assert_eq!(segment.cost_per_mwh, 9999.0);
}
#[test]
fn test_bus_equality() {
let bus_a = Bus {
id: EntityId::from(1),
name: "Bus A".to_string(),
deficit_segments: vec![DeficitSegment {
depth_mw: None,
cost_per_mwh: 500.0,
}],
excess_cost: 1.0,
};
let bus_b = bus_a.clone();
assert_eq!(bus_a, bus_b);
let bus_c = Bus {
id: EntityId::from(2),
..bus_a.clone()
};
assert_ne!(bus_a, bus_c);
}
#[cfg(feature = "serde")]
#[test]
fn test_bus_serde_roundtrip() {
let bus = Bus {
id: EntityId::from(1),
name: "Main Bus".to_string(),
deficit_segments: vec![
DeficitSegment {
depth_mw: Some(100.0),
cost_per_mwh: 500.0,
},
DeficitSegment {
depth_mw: Some(200.0),
cost_per_mwh: 1000.0,
},
DeficitSegment {
depth_mw: None,
cost_per_mwh: 5000.0,
},
],
excess_cost: 1.5,
};
let json = serde_json::to_string(&bus).unwrap();
let deserialized: Bus = serde_json::from_str(&json).unwrap();
assert_eq!(bus, deserialized);
}
}