use std::cell::RefCell;
use std::collections::BTreeMap;
use crate::components::Weight;
use crate::dispatch::{DispatchManifest, RiderInfo};
use crate::entity::EntityId;
use slotmap::SlotMap;
thread_local! {
static ID_SOURCE: RefCell<SlotMap<EntityId, ()>> = RefCell::new(SlotMap::with_key());
}
fn fresh_id() -> EntityId {
ID_SOURCE.with(|sm| sm.borrow_mut().insert(()))
}
#[test]
fn waiting_count_at_unvisited_stop_is_zero() {
let manifest = DispatchManifest::default();
let stop = fresh_id();
assert_eq!(manifest.waiting_count_at(stop), 0);
}
#[test]
fn total_weight_at_unvisited_stop_is_zero() {
let manifest = DispatchManifest::default();
let stop = fresh_id();
assert_eq!(manifest.total_weight_at(stop), 0.0);
}
#[test]
fn riding_count_to_unvisited_stop_is_zero() {
let manifest = DispatchManifest::default();
let stop = fresh_id();
assert_eq!(manifest.riding_count_to(stop), 0);
}
#[test]
fn resident_count_at_unvisited_stop_is_zero() {
let manifest = DispatchManifest::default();
let stop = fresh_id();
assert_eq!(manifest.resident_count_at(stop), 0);
}
#[test]
fn arrivals_at_unvisited_stop_is_zero() {
let manifest = DispatchManifest::default();
let stop = fresh_id();
assert_eq!(manifest.arrivals_at(stop), 0);
}
#[test]
fn has_demand_unvisited_stop_is_false() {
let manifest = DispatchManifest::default();
let stop = fresh_id();
assert!(!manifest.has_demand(stop));
}
#[test]
fn empty_waiting_vec_still_returns_zero_count_and_weight() {
let stop = fresh_id();
let mut waiting = BTreeMap::new();
waiting.insert(stop, Vec::<RiderInfo>::new());
let manifest = DispatchManifest {
waiting_at_stop: waiting,
..Default::default()
};
assert_eq!(manifest.waiting_count_at(stop), 0);
assert_eq!(manifest.total_weight_at(stop), 0.0);
}
#[test]
fn total_weight_at_sums_rider_weights() {
let stop = fresh_id();
let rider1 = fresh_id();
let rider2 = fresh_id();
let mut waiting = BTreeMap::new();
waiting.insert(
stop,
vec![
RiderInfo {
id: rider1,
destination: None,
weight: Weight::try_new(70.0).unwrap(),
wait_ticks: 0,
},
RiderInfo {
id: rider2,
destination: None,
weight: Weight::try_new(50.0).unwrap(),
wait_ticks: 0,
},
],
);
let manifest = DispatchManifest {
waiting_at_stop: waiting,
..Default::default()
};
assert_eq!(manifest.total_weight_at(stop), 120.0);
assert_eq!(manifest.waiting_count_at(stop), 2);
}
#[test]
fn resident_count_at_returns_explicit_zero() {
let stop = fresh_id();
let mut residents = BTreeMap::new();
residents.insert(stop, 0_usize);
let manifest = DispatchManifest {
resident_count_at_stop: residents,
..Default::default()
};
assert_eq!(manifest.resident_count_at(stop), 0);
}