Repository navigation
fix(range): use the step magnitude in the generic GetRange - #75
Merged
Merged
Conversation
GetRange<T> 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
Code coverage overviewLanguages: C# C# / code-coverage-agent
View coverage of the 1 most impacted file.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the change to normalize negative generic range steps.
No open issues 🎉
What changed in this PR
Fixes #74 by making generic NumericRangeTools.GetRange<T> use the magnitude of negative steps, preserving ascending, inclusive range behavior.
Changes:
- Normalizes generic steps with
T.Absand documents overflow for signed integer minimum values. - Removes an unreachable zero-increment check from the
DateTimeiterator. - Adds eight regression tests covering negative steps and overflow, with capped enumeration to prevent hangs.
| File | Description |
|---|---|
Plugin.BaseTypeExtensions/NumericRangeTools.cs |
Normalizes generic steps, documents overflow, and removes the redundant temporal check. |
Plugin.BaseTypeExtensions.Tests/NumericRangeToolsTests.cs |
Adds negative-step coverage across numeric types and bounds, plus the minimum-value overflow case. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NumericRangeTools.GetRange<T>now uses the magnitude ofstep, like thedouble,floatandDateTimeoverloads. A negative step gives the same sequence as its absolute value instead of never ending. The sequence stays ascending and inclusive, so existing results do not change.The dead
if (increment == 0)check in theDateTimeoverload is removed. A zero step is already rejected before the iterator starts, so the check could never be true.Fixes #74
Why
With a negative step the loop walked upward from
Min(start, end)while the step moved the value away fromMax(start, end), so the loop condition stayed true. Floating-point types never ended, and signed integers kept yielding until the value wrapped around.Change Type
Affected Areas
Behavior And Compatibility
Signatures are unchanged. A negative step used to hang the caller and now returns the ascending sequence. For a step of zero, a positive step and the
DateTimeoverload nothing changes.One new edge: the minimum value of a signed integer type (for example
int.MinValue) has no positive counterpart, soT.AbsthrowsOverflowExceptionwhile enumerating. Before this change that case cycled through the values without ending. It is documented on the method and covered by a test.Tests
8 tests added for the generic overload: negative steps with ints, longs, decimals and doubles, with a large step, with reversed bounds and with negative bounds, plus the
int.MinValuecase. They cap the sequence withTake(100), so a regression fails the assertion instead of hanging the run. All 8 failed before the change and pass now. 498 tests pass locally (net9.0).Documentation
Checklist
type(scope): subject)