use crate::models::traits::{FairnessModel, Sorter};
#[derive(Debug)]
pub struct ComparisonFairness {
budget_per_step: usize,
}
impl ComparisonFairness {
pub fn new(budget: usize) -> Self {
Self {
budget_per_step: budget.max(1),
}
}
pub fn get_budget(&self) -> usize {
self.budget_per_step
}
pub fn set_budget(&mut self, budget: usize) {
self.budget_per_step = budget.max(1);
}
}
impl Default for ComparisonFairness {
fn default() -> Self {
Self::new(10)
}
}
impl FairnessModel for ComparisonFairness {
fn allocate_budget(&self, algorithms: &[Box<dyn Sorter>]) -> Vec<usize> {
algorithms
.iter()
.map(|algorithm| {
if algorithm.is_complete() {
0 } else {
self.budget_per_step
}
})
.collect()
}
fn name(&self) -> &str {
"Comparison Budget"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::services::sorters::bubble::BubbleSort;
#[test]
fn test_comparison_fairness_equal_allocation() {
let fairness = ComparisonFairness::new(5);
let algorithms: Vec<Box<dyn Sorter>> = vec![
Box::new(BubbleSort::new()),
Box::new(BubbleSort::new()),
Box::new(BubbleSort::new()),
];
let budgets = fairness.allocate_budget(&algorithms);
assert_eq!(budgets, vec![5, 5, 5]);
}
#[test]
fn test_comparison_fairness_completed_algorithm() {
let fairness = ComparisonFairness::new(10);
let mut bubble1 = BubbleSort::new();
let mut bubble2 = BubbleSort::new();
bubble1.reset(vec![]); bubble2.reset(vec![3, 1, 2]);
let algorithms: Vec<Box<dyn Sorter>> = vec![
Box::new(bubble1),
Box::new(bubble2),
];
let budgets = fairness.allocate_budget(&algorithms);
assert_eq!(budgets[0], 0); assert_eq!(budgets[1], 10); }
#[test]
fn test_comparison_fairness_minimum_budget() {
let fairness = ComparisonFairness::new(0); assert_eq!(fairness.get_budget(), 1);
}
#[test]
fn test_comparison_fairness_name() {
let fairness = ComparisonFairness::new(5);
assert_eq!(fairness.name(), "Comparison Budget");
}
}