use proptest::prelude::*;
use temporal_control::{Error, Stn, Ticks, TimePoint};
pub type ConstraintSpec = (usize, usize, Option<i64>, Option<i64>);
#[derive(Debug, Clone)]
pub struct StnSpec {
pub n: usize,
pub constraints: Vec<ConstraintSpec>,
}
impl StnSpec {
pub fn build(&self) -> Result<Stn, Error> {
let mut stn = Stn::new();
for _ in 0..self.n {
stn.add_time_point()?;
}
for &(from, to, lo, hi) in &self.constraints {
stn.add_constraint(TimePoint(from), TimePoint(to), lo.map(Ticks), hi.map(Ticks))?;
}
Ok(stn)
}
}
pub fn random_stn() -> impl Strategy<Value = StnSpec> {
(1usize..=7).prop_flat_map(|n| {
let bound = proptest::option::weighted(0.8, -20i64..=20);
let constraint = (0..n, 0..n, bound.clone(), bound);
proptest::collection::vec(constraint, 0..14)
.prop_map(move |constraints| StnSpec { n, constraints })
})
}
pub fn consistent_stn() -> impl Strategy<Value = StnSpec> {
(1usize..=7).prop_flat_map(|n| {
let schedule = proptest::collection::vec(-50i64..=50, n);
let slack = proptest::option::weighted(0.8, 0i64..=10);
let constraint = (0..n, 0..n, slack.clone(), slack);
let constraints = proptest::collection::vec(constraint, 0..14);
(schedule, constraints).prop_map(move |(times, raw)| StnSpec {
n,
constraints: raw
.into_iter()
.map(|(i, j, lo_slack, hi_slack)| {
let d = times[j] - times[i];
(i, j, lo_slack.map(|s| d - s), hi_slack.map(|s| d + s))
})
.collect(),
})
})
}
pub fn any_stn() -> impl Strategy<Value = StnSpec> {
prop_oneof![random_stn(), consistent_stn()]
}
pub fn extreme_stn() -> impl Strategy<Value = StnSpec> {
let extreme = prop_oneof![
Just(i64::MIN),
Just(i64::MIN + 1),
Just(i64::MAX),
Just(i64::MAX - 1),
Just(i64::MAX / 2),
Just(i64::MIN / 2),
-2i64..=2,
];
(1usize..=5).prop_flat_map(move |n| {
let bound = proptest::option::of(extreme.clone());
let constraint = (0..n, 0..n, bound.clone(), bound);
proptest::collection::vec(constraint, 0..10)
.prop_map(move |constraints| StnSpec { n, constraints })
})
}