Skip to content

fix(range): use the step magnitude in the generic GetRange - #75

Merged
framinosona merged 1 commit into
mainfrom
fix/getrange-negative-step
Nov 23, 2026
Merged

framinosona merged 1 commit into
mainfrom
fix/getrange-negative-step

Conversation

@framinosona

Copy link
Copy Markdown
Member

Summary

NumericRangeTools.GetRange<T> now uses the magnitude of step, like the double, float and DateTime overloads. 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 the DateTime overload 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 from Max(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

  • Feature
  • Bug fix
  • Refactor
  • Docs only
  • CI/build/tooling

Affected Areas

  • String / Numeric / Enum extensions
  • Collection extensions (Dictionary / List / Enumerable / ConcurrentDictionary)
  • Temporal extensions (DateTime / DateOnly / TimeOnly / TimeSpan)
  • Reflection / Assembly extensions
  • Comparison extensions
  • Tests
  • Docs

Behavior And Compatibility

  • Public API changed
  • Exception behavior changed
  • No externally visible behavior change

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 DateTime overload nothing changes.

One new edge: the minimum value of a signed integer type (for example int.MinValue) has no positive counterpart, so T.Abs throws OverflowException while enumerating. Before this change that case cycled through the values without ending. It is documented on the method and covered by a test.

Tests

  • Unit tests added/updated
  • Not applicable (explain)

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.MinValue case. They cap the sequence with Take(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

  • README feature list updated
  • XML docs added/updated
  • Not applicable (explain)

Checklist

  • Commit messages follow Conventional Commits (type(scope): subject)
  • CI passes
  • Change is scoped to one logical unit of work

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
Copilot AI balanced review requested due to automatic review settings November 22, 2026 23:20
@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage-agent

main 17bdfcf fix/getrange-negativ... 0c8c783 +/-
90% 90% 0%
View coverage of the 1 most impacted file.
File main 17bdfcf fix/getrange-negativ... 0c8c783 +/-
NumericRangeTools.cs 98% 100% +2%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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.Abs and documents overflow for signed integer minimum values.
  • Removes an unreachable zero-increment check from the DateTime iterator.
  • 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

@github-actions

Copy link
Copy Markdown

Test Results

498 tests  +8   498 ✅ +8   2s ⏱️ ±0s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 0c8c783. ± Comparison against base commit 17bdfcf.

@framinosona
framinosona merged commit 8ec5d16 into main Nov 23, 2026
14 checks passed
@framinosona
framinosona deleted the fix/getrange-negative-step branch November 23, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] NumericRangeTools.GetRange<T> never ends with a negative step

2 participants