use resource_timeline::{ActivityId, Bounds, Error, Impact, Timeline};
const A: ActivityId = ActivityId(0);
fn battery(budget: usize) -> Timeline {
let bounds = Bounds::new(0, 10).expect("valid");
Timeline::depletable(0, 5, bounds, budget).expect("valid")
}
#[test]
fn invalid_bounds() {
assert_eq!(
Bounds::new(2, 1),
Err(Error::InvalidBounds { min: 2, max: 1 })
);
assert_eq!(
Timeline::claimable(0, -1, 4),
Err(Error::InvalidBounds { min: 0, max: -1 })
);
}
#[test]
fn budget_exhausted() {
let mut timeline = battery(1);
timeline
.add(A, Impact::Step { at: 0, delta: 1 })
.expect("fits");
assert_eq!(
timeline.add(A, Impact::Step { at: 1, delta: 1 }),
Err(Error::BudgetExhausted { limit: 1 })
);
assert_eq!(timeline.impacts().len(), 1);
}
#[test]
fn before_start() {
let mut timeline = battery(4);
assert_eq!(
timeline.add(A, Impact::Step { at: -1, delta: 1 }),
Err(Error::BeforeStart { at: -1, start: 0 })
);
assert_eq!(
timeline.add(
A,
Impact::Claim {
start: -1,
end: 3,
amount: 1
}
),
Err(Error::BeforeStart { at: -1, start: 0 })
);
}
#[test]
fn empty_interval() {
let mut timeline = battery(4);
assert_eq!(
timeline.add(
A,
Impact::Claim {
start: 3,
end: 3,
amount: 1
}
),
Err(Error::EmptyInterval { start: 3, end: 3 })
);
assert_eq!(
timeline.add(
A,
Impact::Claim {
start: 3,
end: 2,
amount: 1
}
),
Err(Error::EmptyInterval { start: 3, end: 2 })
);
}
#[test]
fn non_positive_amount() {
let mut timeline = battery(4);
assert_eq!(
timeline.add(
A,
Impact::Claim {
start: 0,
end: 1,
amount: 0
}
),
Err(Error::NonPositiveAmount { amount: 0 })
);
assert_eq!(
timeline.add(
A,
Impact::Claim {
start: 0,
end: 1,
amount: i64::MIN
}
),
Err(Error::NonPositiveAmount { amount: i64::MIN })
);
}
#[test]
fn kind_mismatch() {
let mut instrument = Timeline::claimable(0, 1, 4).expect("valid");
assert_eq!(
instrument.add(A, Impact::Step { at: 0, delta: -1 }),
Err(Error::KindMismatch)
);
}
#[test]
fn overflowing_violation_level() {
let bounds = Bounds::new(0, 10).expect("valid");
let mut timeline = Timeline::depletable(0, 5, bounds, 4).expect("valid");
timeline
.add(
A,
Impact::Step {
at: 1,
delta: i64::MAX,
},
)
.expect("valid");
assert_eq!(timeline.first_violation(), Err(Error::Overflow));
}
#[test]
fn invalid_rates() {
let mut timeline = battery(4);
assert_eq!(
timeline.add(
A,
Impact::Rate {
start: 2,
end: 2,
per_tick: 1
}
),
Err(Error::EmptyInterval { start: 2, end: 2 })
);
let mut instrument = Timeline::claimable(0, 1, 4).expect("valid");
assert_eq!(
instrument.add(
A,
Impact::Rate {
start: 0,
end: 2,
per_tick: -1
}
),
Err(Error::KindMismatch)
);
}
#[test]
fn level_before_start() {
assert_eq!(
battery(1).level_at(-1),
Err(Error::BeforeStart { at: -1, start: 0 })
);
}