morelinq / MoreLINQ

Extensions to LINQ to Objects
https://morelinq.github.io/
Apache License 2.0
3.63k stars 409 forks source link

Fix `Permutations` for when source length is >12 #979

Closed atifaziz closed 1 year ago

atifaziz commented 1 year ago

This PR fixes #978, by using ulong internally instead of int and adding checked arithmetic. This limits to 18,446,744,073,709,551,615 permutations and will overflow thereafter. A document comment has also been added to call out this limitation.

Due to the computationally expensive nature of enumerating permutations of a source with more than 12 elements, no unit test has been added to exercise the fix. However, a LINQPad query was used as an eyeball test: MoreLinqPermutations.zip. At the time of opening this PR, it will use the latest NuGet package version (3.4.0) and exercise the bug. If the package reference is changed to an assembly reference that's the output of this PR, then it will exercise the fix.

codecov[bot] commented 1 year ago

Codecov Report

Merging #979 (ab9c85b) into master (b99a6a8) will not change coverage. The diff coverage is 100.00%.

:exclamation: Current head ab9c85b differs from pull request most recent head 20d6798. Consider uploading reports for the commit 20d6798 to get more accurate results

@@           Coverage Diff           @@
##           master     #979   +/-   ##
=======================================
  Coverage   92.60%   92.60%           
=======================================
  Files         113      113           
  Lines        3434     3434           
  Branches     1052     1054    +2     
=======================================
  Hits         3180     3180           
  Misses        191      191           
  Partials       63       63           
Impacted Files Coverage Δ
MoreLinq/NestedLoops.cs 100.00% <100.00%> (ø)
MoreLinq/Permutations.cs 96.42% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more