public void NoPeriodsTest() { DateTime test = new DateTime(2011, 4, 12); DateAdd dateAdd = new DateAdd(); Assert.Equal(dateAdd.Add(test, TimeSpan.Zero), test); Assert.Equal(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)), test.Add(new TimeSpan(1, 0, 0, 0))); Assert.Equal(dateAdd.Add(test, new TimeSpan(-1, 0, 0, 0)), test.Add(new TimeSpan(-1, 0, 0, 0))); Assert.Equal(dateAdd.Subtract(test, new TimeSpan(1, 0, 0, 0)), test.Subtract(new TimeSpan(1, 0, 0, 0))); Assert.Equal(dateAdd.Subtract(test, new TimeSpan(-1, 0, 0, 0)), test.Subtract(new TimeSpan(-1, 0, 0, 0))); } // NoPeriodsTest
public void PeriodLimitsSubtractTest() { DateTime test = new DateTime(2011, 4, 30); TimeRange timeRange1 = new TimeRange(new DateTime(2011, 4, 20), new DateTime(2011, 4, 25)); TimeRange timeRange2 = new TimeRange(DateTime.MinValue, new DateTime(2011, 4, 10)); DateAdd dateAdd = new DateAdd(); dateAdd.ExcludePeriods.Add(timeRange1); dateAdd.ExcludePeriods.Add(timeRange2); Assert.Equal(dateAdd.Subtract(test, new TimeSpan(5, 0, 0, 0)), timeRange1.Start); Assert.Null(dateAdd.Subtract(test, new TimeSpan(20, 0, 0, 0))); } // PeriodLimitsSubtractTest
public void IncludeOutsideMinTest() { DateTime test = new DateTime(2011, 4, 12); TimeRange timeRange = new TimeRange(DateTime.MinValue, new DateTime(2011, 4, 10)); DateAdd dateAdd = new DateAdd(); dateAdd.IncludePeriods.Add(timeRange); Assert.Null(dateAdd.Add(test, TimeSpan.Zero)); Assert.Null(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0))); Assert.Equal(dateAdd.Subtract(test, TimeSpan.Zero), new DateTime(2011, 4, 10)); Assert.Equal(dateAdd.Subtract(test, new TimeSpan(1, 0, 0, 0)), new DateTime(2011, 4, 9)); } // IncludeOutsideMinTest
public void IncludeOutsideMaxTest() { DateTime test = new DateTime(2011, 4, 12); TimeRange timeRange = new TimeRange(new DateTime(2011, 4, 20), DateTime.MaxValue); DateAdd dateAdd = new DateAdd(); dateAdd.IncludePeriods.Add(timeRange); Assert.AreEqual(dateAdd.Add(test, TimeSpan.Zero), new DateTime(2011, 4, 20)); Assert.AreEqual(dateAdd.Add(test, new TimeSpan(1, 0, 0, 0)), new DateTime(2011, 4, 21)); Assert.IsNull(dateAdd.Subtract(test, TimeSpan.Zero)); Assert.IsNull(dateAdd.Subtract(test, new TimeSpan(1, 0, 0, 0))); } // IncludeOutsideMaxTest
public void IncludeEqualsExcludeTest() { DateAdd dateAdd = new DateAdd(); dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 10))); dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 10))); DateTime test = new DateTime(2011, 3, 5); Assert.Null(dateAdd.Add(test, TimeSpan.Zero)); Assert.Null(dateAdd.Add(test, new TimeSpan(1))); Assert.Null(dateAdd.Add(test, new TimeSpan(-1))); Assert.Null(dateAdd.Subtract(test, TimeSpan.Zero)); Assert.Null(dateAdd.Subtract(test, new TimeSpan(1))); Assert.Null(dateAdd.Subtract(test, new TimeSpan(-1))); } // IncludeEqualsExcludeTest
public void IncludeExclude2Test() { DateAdd dateAdd = new DateAdd(); dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 1), new DateTime(2011, 3, 5))); dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 5), new DateTime(2011, 3, 10))); dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 10), new DateTime(2011, 3, 15))); dateAdd.ExcludePeriods.Add(new TimeRange(new DateTime(2011, 3, 15), new DateTime(2011, 3, 20))); dateAdd.IncludePeriods.Add(new TimeRange(new DateTime(2011, 3, 20), new DateTime(2011, 3, 25))); DateTime periodStart = new DateTime(2011, 3, 1); DateTime periodEnd = new DateTime(2011, 3, 25); // add from start Assert.Equal(dateAdd.Add(periodStart, Duration.Days(1)), new DateTime(2011, 3, 2)); Assert.Equal(dateAdd.Add(periodStart, Duration.Days(4)), new DateTime(2011, 3, 10)); Assert.Equal(dateAdd.Add(periodStart, Duration.Days(5)), new DateTime(2011, 3, 11)); Assert.Equal(dateAdd.Add(periodStart, Duration.Days(9)), new DateTime(2011, 3, 20)); Assert.Equal(dateAdd.Add(periodStart, Duration.Days(10)), new DateTime(2011, 3, 21)); Assert.Null(dateAdd.Add(periodStart, Duration.Days(15))); // add from end Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-1)), new DateTime(2011, 3, 24)); Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-5)), new DateTime(2011, 3, 15)); Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-6)), new DateTime(2011, 3, 14)); Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-10)), new DateTime(2011, 3, 5)); Assert.Equal(dateAdd.Add(periodEnd, Duration.Days(-11)), new DateTime(2011, 3, 4)); Assert.Null(dateAdd.Add(periodEnd, Duration.Days(-15))); // subtract form end Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(1)), new DateTime(2011, 3, 24)); Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(5)), new DateTime(2011, 3, 15)); Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(6)), new DateTime(2011, 3, 14)); Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(10)), new DateTime(2011, 3, 5)); Assert.Equal(dateAdd.Subtract(periodEnd, Duration.Days(11)), new DateTime(2011, 3, 4)); Assert.Null(dateAdd.Subtract(periodStart, Duration.Days(15))); // subtract form start Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-1)), new DateTime(2011, 3, 2)); Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-4)), new DateTime(2011, 3, 10)); Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-5)), new DateTime(2011, 3, 11)); Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-9)), new DateTime(2011, 3, 20)); Assert.Equal(dateAdd.Subtract(periodStart, Duration.Days(-10)), new DateTime(2011, 3, 21)); Assert.Null(dateAdd.Subtract(periodStart, Duration.Days(-15))); } // IncludeExclude2Test