[TestMethod] // ReSharper disable once InconsistentNaming public void ToString_HasCorrectFormat() { Period length = RandomPeriod(MinPeriod + MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate, MaxLocalDate - length); LocalDate end = start + length; Period step = RandomPeriod(MinPeriod, length - MinPeriod, true, false); LocalDateRange localRange = new LocalDateRange(start, end, step); Assert.AreEqual($"{start} - {end}", localRange.ToString()); }
[TestMethod] // ReSharper disable once InconsistentNaming public void GetEnumerator_UsingLargestPossibleParameters_IteratesSuccessfully() { // Step chosen to avoid an unfeasible number of iterations LocalDateRange localRange = new LocalDateRange( MinLocalDate, MaxLocalDate, PeriodDivideApprox(MaxPeriod, 16, false)); bool iterated = localRange.Any(); Assert.AreEqual(true, iterated, "When iterating across full range, at least one value should be returned"); }
[TestMethod] // ReSharper disable once InconsistentNaming public void ToString_IsNotBlank() { Period length = RandomPeriod(MinPeriod + MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate, MaxLocalDate - length); LocalDate end = start + length; Period step = RandomPeriod(MinPeriod, length - MinPeriod, true, false); LocalDateRange localRange = new LocalDateRange(start, end, step); Assert.AreNotEqual( "", localRange.ToString(), "String representation of range must not be an empty string"); }
[TestMethod] // ReSharper disable once InconsistentNaming public void GetEnumerator_ValuesStayWithinRange() { Period length = RandomPeriod(MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate, MaxLocalDate - length); LocalDate end = start + length; // note that the number of steps is limited to 100 or fewer Period step = PeriodDivideApprox(length, Random.Next(4, 100), false); LocalDateRange localRange = new LocalDateRange(start, end, step); foreach (LocalDate d in localRange) { Assert.IsTrue(d >= start, "Value from iterator must by equal or above start parameter"); Assert.IsTrue(d <= end, "Value from iterator must be equal or below end parameter"); } }
public UniformDateSequence(LocalDateRange range, DatePeriod period, bool exact = true) { Ensure.Bool.IsTrue(exact == true ? Intervals.DateInterval.StartOfInterval(range.Start, period) == range.Start : true); Ensure.Bool.IsTrue(exact == true ? Intervals.DateInterval.StartOfInterval(range.End, period) == range.End : true); var startOfEnd = Intervals.DateInterval.StartOfInterval(range.End, period); if (exact == false && startOfEnd < range.End) { startOfEnd = new Intervals.DateInterval(startOfEnd, period).NextInterval().Date; } _range = new LocalDateRange( Intervals.DateInterval.StartOfInterval(range.Start, period), startOfEnd); _period = period; }
[TestMethod] // ReSharper disable once InconsistentNaming public void GetEnumerator_LengthDivisibleByStep_IterationCountMatchesCalculated() { Period length = RandomPeriod(MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate, MaxLocalDate - length); LocalDate end = start + length; // note that the number of steps is limited to 1000 or fewer Period step = PeriodDivideApprox(length, Random.Next(4, 1000), false); //ensure that step size is a factor of the length of the range start += Period.FromTicks(length.TicksFrom(start) % step.TicksFrom(start)).Normalize(); LocalDateRange localRange = new LocalDateRange(start, end, step); long ticksA = start.TicksTo(end); long ticksB = step.TicksFrom(start); // Range endpoint is inclusive, so must take longo(?) account this extra iteration Assert.AreEqual( (ticksA / ticksB) + 1, localRange.Count(), "Iteration count should be (end-start)/step + 1 where endpoint is included"); }
[TestMethod] // ReSharper disable once InconsistentNaming public void GetEnumerator_LengthNotDivisibleByStep_IterationCountMatchesCalculated() { Period length = RandomPeriod(MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate, MaxLocalDate - length); LocalDate end = start + length; // note that the number of steps is limited to 1000 or fewer Period step = PeriodDivideApprox(length, Random.Next(4, 1000)); //ensure that step size is not a factor of the length of the range if (length.TicksFrom(start) % step.TicksFrom(start) == 0) { start += RandomPeriod(MinPeriod, step - MinPeriod, true, false); } LocalDateRange localRange = new LocalDateRange(start, end, step); long ticksA = start.TicksTo(end); long ticksB = step.TicksFrom(start); Assert.AreEqual( (ticksA / ticksB) + 1, localRange.Count(), "Iteration count should be (start-end)/step +1"); }
[TestMethod] // ReSharper disable once InconsistentNaming public void GetEnumerator_Iterating_DifferenceBetweenIterationsMatchesStepSize() { Period length = RandomPeriod(MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate, MaxLocalDate - length); LocalDate end = start + length; // note that the number of steps is limited to 100 or fewer Period step = PeriodDivideApprox(length, Random.Next(4, 100), false); LocalDateRange localRange = new LocalDateRange(start, end, step); LocalDate? previous = null; foreach (LocalDate d in localRange) { if (previous.HasValue) { IComparer<Period> comparer = Period.CreateComparer(previous.Value.At(new LocalTime())); Assert.AreEqual( 0, comparer.Compare(Period.Between(previous.Value, d), step), "Difference between iteration values should match the step value supplied"); } previous = d; } }
[ExpectedException(typeof(ArgumentOutOfRangeException))] // ReSharper disable once InconsistentNaming public void Constructor_EndBeforeStart_ThrowsArgumentOutOfRangeException() { Period length = RandomPeriod(MinPeriod, MaxPeriod, true, false); LocalDate start = RandomLocalDate(MinLocalDate + length, MaxLocalDate); LocalDate end = start - length; // ReSharper disable once UnusedVariable LocalDateRange localRange = new LocalDateRange(start, end); }