From 0c8c783d19e20467c9b17ac25090d7298d98102f Mon Sep 17 00:00:00 2001 From: Francois Raminosona Date: Mon, 23 Nov 2026 00:20:37 +0100 Subject: [PATCH] fix(range): use the step magnitude in the generic GetRange GetRange looped without end on a negative step, because the loop always walks upward from the smaller bound while the step moved the value away from the larger one. Use the absolute value of the step, as the double, float and DateTime overloads already do. The sequence stays ascending and inclusive. Also remove the dead increment == 0 check in the DateTime overload, since a zero step is rejected before the iterator starts. Fixes #74 --- .../NumericRangeToolsTests.cs | 53 +++++++++++++++++++ .../NumericRangeTools.cs | 11 ++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Plugin.BaseTypeExtensions.Tests/NumericRangeToolsTests.cs b/Plugin.BaseTypeExtensions.Tests/NumericRangeToolsTests.cs index 9ab860d..5005f29 100644 --- a/Plugin.BaseTypeExtensions.Tests/NumericRangeToolsTests.cs +++ b/Plugin.BaseTypeExtensions.Tests/NumericRangeToolsTests.cs @@ -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(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(); + } + #endregion #region Double GetRange Tests diff --git a/Plugin.BaseTypeExtensions/NumericRangeTools.cs b/Plugin.BaseTypeExtensions/NumericRangeTools.cs index b87a999..373c2e8 100644 --- a/Plugin.BaseTypeExtensions/NumericRangeTools.cs +++ b/Plugin.BaseTypeExtensions/NumericRangeTools.cs @@ -26,8 +26,11 @@ public static class NumericRangeTools /// /// 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. /// + /// Thrown while enumerating when `step` is the minimum value of a signed integer type, whose magnitude cannot be represented. public static IEnumerable GetRange(T start, T end, T step) where T : INumber { if (step == T.Zero) @@ -35,6 +38,7 @@ public static IEnumerable GetRange(T start, T end, T step) where T : INumb yield break; } + var increment = T.Abs(step); var minimum = T.Min(start, end); var maximum = T.Max(start, end); var current = minimum; @@ -42,7 +46,7 @@ public static IEnumerable GetRange(T start, T end, T step) where T : INumb while (current <= maximum) { yield return current; - current += step; + current += increment; } } @@ -150,11 +154,6 @@ IEnumerable Iterate() var tickStep = step.Ticks; var increment = Math.Abs(tickStep); - if (increment == 0) - { - yield break; - } - if (!isAscending) { increment = -increment;