From 4ba2a3aa26043d23452eba335bdaa7570a87983c Mon Sep 17 00:00:00 2001 From: Kinesin Data Technologies Incorporated <93931750+kinesintech@users.noreply.github.com> Date: Thu, 29 Sep 2022 13:35:42 -0300 Subject: [PATCH] Adding additional checks for tasks and interval generation --- src/resource_interval.rs | 11 +++++++++-- src/task.rs | 10 +++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/resource_interval.rs b/src/resource_interval.rs index 9c1d4ff..2c0d049 100644 --- a/src/resource_interval.rs +++ b/src/resource_interval.rs @@ -13,7 +13,14 @@ impl ResourceInterval { ResourceInterval(HashMap::new()) } - pub fn insert(&mut self, other: ResourceInterval) { + pub fn insert(&mut self, resource: &Resource, intervals: &IntervalSet) { + self.0 + .entry(resource.clone()) + .or_insert(IntervalSet::new()) + .merge(intervals); + } + + pub fn union(&mut self, other: ResourceInterval) { for (res, is) in other.iter() { self.0 .entry(res.clone()) @@ -22,7 +29,7 @@ impl ResourceInterval { } } - pub fn remove(&mut self, other: ResourceInterval) { + pub fn subtract(&mut self, other: ResourceInterval) { for (res, is) in other.iter() { let avail = self.0.entry(res.clone()).or_insert(IntervalSet::new()); *avail = avail.difference(is); diff --git a/src/task.rs b/src/task.rs index 009d43c..33fbefa 100644 --- a/src/task.rs +++ b/src/task.rs @@ -68,7 +68,7 @@ impl TaskDefinition { requires: self.requires.clone(), schedule: schedule, - valid_over: IntervalSet::from(vec![Interval::new(start, actual_end)]), + valid_over: IntervalSet::from(Interval::new(start, actual_end)), timezone: self.timezone, } } @@ -293,5 +293,13 @@ mod tests { panic!("{:?}", e); } }; + + // Ensure that the intervals generated over the valid period + // exactly cover the valid period + let mut theoretical = ResourceInterval::new(); + theoretical.insert(&"resource_a".to_owned(), &task.valid_over); + theoretical.insert(&"resource_b".to_owned(), &task.valid_over); + let generated = IntervalSet::from(task.generate_intervals(&theoretical).unwrap()); + assert_eq!(task.valid_over, generated); } }