Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Plugin.BaseTypeExtensions.Tests/NumericRangeToolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,59 @@ public void GetRange_GenericMixedSignNumbers()
NumericRangeTools.GetRange(-2, 2, 1).Should().Equal(-2, -1, 0, 1, 2);
}

// The negative-step tests cap the sequence with Take so that a regression fails the assertion instead of hanging the run.

[Fact]
public void GetRange_GenericNegativeStepUsesMagnitude()
{
NumericRangeTools.GetRange(1, 5, -1).Take(100).Should().Equal(1, 2, 3, 4, 5);
}

[Fact]
public void GetRange_GenericNegativeStepWithLargeStep()
{
NumericRangeTools.GetRange(1, 10, -5).Take(100).Should().Equal(1, 6);
}

[Fact]
public void GetRange_GenericNegativeStepWithReversedBounds()
{
NumericRangeTools.GetRange(5, 1, -1).Take(100).Should().Equal(1, 2, 3, 4, 5);
}

[Fact]
public void GetRange_GenericNegativeStepWithNegativeNumbers()
{
NumericRangeTools.GetRange(-5, -1, -2).Take(100).Should().Equal(-5, -3, -1);
}

[Fact]
public void GetRange_GenericNegativeStepWithLongs()
{
NumericRangeTools.GetRange(1L, 3L, -1L).Take(100).Should().Equal(1L, 2L, 3L);
}

[Fact]
public void GetRange_GenericNegativeStepWithDecimals()
{
NumericRangeTools.GetRange(1.0m, 2.0m, -0.5m).Take(100).Should().Equal(1.0m, 1.5m, 2.0m);
}

[Fact]
public void GetRange_GenericNegativeStepWithDoubles()
{
// The explicit type argument selects the generic overload instead of the double one.
NumericRangeTools.GetRange<double>(1.0, 2.0, -0.5).Take(100).Should().Equal(1.0, 1.5, 2.0);
}

[Fact]
public void GetRange_GenericMinValueStepThrowsInsteadOfLooping()
{
var act = () => NumericRangeTools.GetRange(0, 10, int.MinValue).Take(100).ToList();

act.Should().Throw<OverflowException>();
}

#endregion

#region Double GetRange Tests
Expand Down
11 changes: 5 additions & 6 deletions Plugin.BaseTypeExtensions/NumericRangeTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,27 @@ public static class NumericRangeTools
/// <remarks>
/// This method generates a sequence of numbers starting from `start` to `end`, inclusive, with the specified `step`.
/// If `step` is zero, an empty sequence is returned.
/// The sequence is always ascending from the smaller to the larger of `start` and `end`, and only the magnitude of `step` is used,
/// so a negative `step` gives the same sequence as its absolute value.
/// The sequence will include both `start` and `end` if they are reachable with the given step.
/// </remarks>
/// <exception cref="OverflowException">Thrown while enumerating when `step` is the minimum value of a signed integer type, whose magnitude cannot be represented.</exception>
public static IEnumerable<T> GetRange<T>(T start, T end, T step) where T : INumber<T>
{
if (step == T.Zero)
{
yield break;
}

var increment = T.Abs(step);
var minimum = T.Min(start, end);
var maximum = T.Max(start, end);
var current = minimum;

while (current <= maximum)
{
yield return current;
current += step;
current += increment;
}
}

Expand Down Expand Up @@ -150,11 +154,6 @@ IEnumerable<DateTime> Iterate()
var tickStep = step.Ticks;
var increment = Math.Abs(tickStep);

if (increment == 0)
{
yield break;
}

if (!isAscending)
{
increment = -increment;
Expand Down
Loading