public void FlattenIntoASingleRangeTest() { //[(1, 5), (2, 4), (3, 6)] ---> [(1,6)] var ranges = new List <DateRange>() { QuickRange(1, 5), QuickRange(2, 4), QuickRange(3, 6), }; var flattened = DateRangeHelper.Flatten(ranges).ToList(); Assert.IsTrue(flattened.Count == 1, "flattened.Count == 1"); Assert.AreEqual(QuickRange(1, 6), flattened[0]); }
private static int GeWorkDaysOff(DateRange analyzedPeriod, IEnumerable <DateRange> timeOffs) { IEnumerable <DateRange> flattened = DateRangeHelper.Flatten(timeOffs); return((from interval in flattened where analyzedPeriod.Overlaps(interval) let minDate = interval.StartDate <analyzedPeriod.StartDate ?analyzedPeriod.StartDate : interval.StartDate let maxDate = interval.EndDate> analyzedPeriod.EndDate ? analyzedPeriod.EndDate : interval.EndDate select new DateRange(minDate, maxDate) into trimmed select GetWorkingDays(trimmed)).Sum()); }
public void FlattenIntoMultipleRanges() { //[(0, 3), (1, 2), (2, 4), (5, 9), (6, 8)] --> [(0, 4), (5, 9)] var ranges = new List <DateRange>() { QuickRange(1, 2), QuickRange(5, 9), QuickRange(2, 4), QuickRange(6, 8), QuickRange(0, 3), }; List <DateRange> flattened = DateRangeHelper.Flatten(ranges).ToList(); Assert.IsTrue(flattened.Count == 2, "flattened.Count == 2"); Assert.AreEqual(QuickRange(0, 4), flattened[0]); Assert.AreEqual(QuickRange(5, 9), flattened[1]); }