Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve UInt32 and UInt64 value generation (avoid overflows) #173

Merged
merged 1 commit into from
Jul 27, 2018

Conversation

jwChung
Copy link
Contributor

@jwChung jwChung commented Jul 27, 2018

This commit addresses that generating uint32 and uint64 returns
out-of-range values. This problem occurs in numeric conversions.

Copy link
Member

@moodmosaic moodmosaic left a comment

Choose a reason for hiding this comment

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

Before:

Gen.uint64
<| Range.constant 1UL System.UInt64.MaxValue
|> Gen.sample 0 100
|> printfn "%A";;

[0UL; 1UL; 1UL; 1UL; 1UL; 0UL; 0UL; 18446744073709551615UL;
 18446744073709551615UL; 0UL; 1UL; 18446744073709551615UL;
 18446744073709551615UL; 1UL; 18446744073709551615UL; 18446744073709551615UL;
 1UL; 18446744073709551615UL; 1UL; 18446744073709551615UL; 0UL; 0UL; 0UL;
 18446744073709551615UL; 1UL; 18446744073709551615UL; 0UL; 1UL; 0UL;
 18446744073709551615UL; 18446744073709551615UL; 0UL; 18446744073709551615UL;
 1UL; 1UL; 1UL; 0UL; 0UL; 18446744073709551615UL; 18446744073709551615UL;
 18446744073709551615UL; 1UL; 0UL; 0UL; 0UL; 0UL; 0UL; 0UL; 1UL;
 18446744073709551615UL; 1UL; 1UL; 0UL; 18446744073709551615UL;
 18446744073709551615UL; 1UL; 0UL; 0UL; 18446744073709551615UL; 0UL; 1UL;
 18446744073709551615UL; 1UL; 18446744073709551615UL; 1UL; 1UL; 1UL; 1UL; 0UL;
 1UL; 1UL; 0UL; 1UL; 1UL; 0UL; 1UL; 0UL; 1UL; 0UL; 18446744073709551615UL; 1UL;
 1UL; 18446744073709551615UL; 1UL; 1UL; 18446744073709551615UL;
 18446744073709551615UL; 18446744073709551615UL; 1UL; 18446744073709551615UL;
 18446744073709551615UL; 1UL; 0UL; 0UL; 0UL; 18446744073709551615UL; 1UL;
 18446744073709551615UL; 0UL; 0UL]

Gen.uint32
<| Range.constant 1ul System.UInt32.MaxValue
|> Gen.sample 0 100
|> printfn "%A";;

[1u; 0u; 1u; 1u; 0u; 4294967295u; 4294967295u; 4294967295u; 4294967295u; 0u; 0u;
 0u; 4294967295u; 1u; 1u; 4294967295u; 1u; 4294967295u; 1u; 0u; 0u; 4294967295u;
 1u; 0u; 0u; 0u; 4294967295u; 4294967295u; 4294967295u; 4294967295u; 1u;
 4294967295u; 1u; 1u; 4294967295u; 4294967295u; 0u; 4294967295u; 1u; 4294967295u;
 4294967295u; 0u; 1u; 0u; 0u; 0u; 0u; 0u; 1u; 0u; 0u; 0u; 4294967295u; 0u; 1u;
 4294967295u; 4294967295u; 0u; 4294967295u; 1u; 4294967295u; 0u; 1u; 4294967295u;
 4294967295u; 1u; 4294967295u; 4294967295u; 1u; 1u; 0u; 1u; 4294967295u; 0u; 0u;
 1u; 0u; 4294967295u; 0u; 0u; 1u; 1u; 1u; 4294967295u; 4294967295u; 0u;
 4294967295u; 0u; 4294967295u; 1u; 1u; 1u; 1u; 4294967295u; 0u; 0u; 1u; 1u; 0u;
 4294967295u]

After:

Gen.uint64
<| Range.constant 1UL System.UInt64.MaxValue
|> Gen.sample 0 100
|> printfn "%A";;

[2498403739076676224UL; 14686488830716721533UL; 15060649087678658021UL;
 14687915671906609877UL; 2473731566202280459UL; 7932287968553153516UL;
 17997876096413333861UL; 1954396047969092075UL; 11467673716131606464UL;
 16502524655665555371UL; 6476649784954775512UL; 11015835334263304921UL;
 4565341449158837349UL; 4992266745420203237UL; 14796061238259238252UL;
 9224451719846922181UL; 12140194053311208723UL; 7152701471785094332UL;
 14260847524336288176UL; 1327169825871526133UL; 3236033480317536578UL;
 9711340010905513418UL; 17362191733660649768UL; 15028773197631658022UL;
 4401063588021638190UL; 2173926872429101826UL; 10102456011173435133UL;
 10541440701220256699UL; 12102226498968826492UL; 865149196378201934UL;
 7051113424890842342UL; 15479184010170608853UL; 3693498501286720050UL;
 8953249896568744605UL; 2214212945174753746UL; 874335212248618315UL;
 17413183895861082926UL; 18289783691587402611UL; 7217530153722507993UL;
 505366596473522233UL; 7466784497136263628UL; 8170433925812084725UL;
 17712862127694188136UL; 17965429884390848025UL; 2750118359672990563UL;
 5229423810457802562UL; 18186010166743620521UL; 35861201397219814UL;
 3686467274711386996UL; 2525166504417088431UL; 16000009089759337890UL;
 10315548929024782602UL; 11841886359500280793UL; 12773895087246209687UL;
 12673289704560946321UL; 202494343947928373UL; 1630518297649618022UL;
 303857859674010863UL; 3608065142938058988UL; 8730030979913854932UL;
 11793548316429436211UL; 17078552293862203534UL; 7955936792931358313UL;
 9926880546488300612UL; 10253604107537741757UL; 13384821904469339468UL;
 1247502302416799664UL; 7185582133517139245UL; 2160358955331001586UL;
 11571458362145491739UL; 13913989931997910106UL; 17767948837352327224UL;
 6082889150188646854UL; 16455716784489130775UL; 6128618319676098311UL;
 8346277648319490570UL; 17309209515123449733UL; 1587743984226686375UL;
 5503133900148485560UL; 14812645168301394466UL; 8347150219438448069UL;
 10224805311249131514UL; 2629382821209763597UL; 9269955454364854074UL;
 8500421173199738008UL; 3293439567089071956UL; 14142596526489348973UL;
 8851450358367469331UL; 11279291938308384654UL; 738982759180403740UL;
 11829346004600141973UL; 7705612232733301371UL; 568143724085394529UL;
 1388006797057718134UL; 810716714295787341UL; 14221443581659863651UL;
 1542244782511889224UL; 12622728484474818250UL; 16064458438841498650UL;
 16504020426620369688UL]

Gen.uint32
<| Range.constant 1ul System.UInt32.MaxValue
|> Gen.sample 0 100
|> printfn "%A";;

[3266210104u; 3997547383u; 3091657861u; 874610718u; 1426404512u; 1441511489u;
 2199743998u; 18303082u; 798083377u; 2480925132u; 674664625u; 1887214159u;
 4172234907u; 1418205737u; 3030964377u; 28148652u; 1510603349u; 1418511825u;
 3575672614u; 1610466171u; 2445070493u; 3865826712u; 3933136706u; 1333087401u;
 3076010716u; 3180807542u; 2614027567u; 2218775822u; 3920165467u; 799626282u;
 3830663958u; 3234623204u; 1324770618u; 267832797u; 2432860318u; 2707493875u;
 2335745456u; 3947413352u; 3785012252u; 1634236436u; 2742442274u; 373353660u;
 1715055018u; 419143215u; 3733278606u; 756585652u; 573484670u; 2985216391u;
 1472500887u; 1597263907u; 2604862089u; 3892785542u; 71992595u; 587473453u;
 364618516u; 1807891597u; 789627545u; 2169422976u; 3154210946u; 1034391753u;
 2687599194u; 4134446416u; 2671288547u; 2854594738u; 2045054041u; 3942628912u;
 2461747586u; 560896652u; 4287073553u; 1773332563u; 4082332739u; 3535582393u;
 1054913933u; 2993218980u; 3414056071u; 1078240603u; 1496799741u; 2776424284u;
 2409255375u; 3388279757u; 3820229754u; 2502940596u; 485146309u; 606048597u;
 495834175u; 3215145207u; 3843201793u; 2763630263u; 3576694349u; 1920815691u;
 3735436135u; 1786356749u; 1612627940u; 2841355786u; 1460012399u; 3764197693u;
 3480454098u; 2783822261u; 3254307900u; 446620966u]

@moodmosaic
Copy link
Member

moodmosaic commented Jul 27, 2018

@jwChung Good catch! See also this recent version of the original Numeric.fs. They do the same there and they seem to keep the old method with the cast for backwards compatibility with .NET 3.x.

💯

let gen = Gen.uint64 <| Range.constant 1UL UInt64.MaxValue
let actual = Gen.sample 0 100 gen
Assertions.test <@ actual |> List.contains 0UL |> not @>
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to qualify the module Assertions? test <@ actual … should work fine.

let gen = Gen.uint32 <| Range.constant 1ul UInt32.MaxValue
let actual = Gen.sample 0 100 gen
Assertions.test <@ actual |> List.contains 0ul |> not @>
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to qualify the module Assertions? test <@ actual … should work fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha, I didn't know that it was automatically opened by AutoOpenAttribute.

@moodmosaic
Copy link
Member

I left two μ-nitpicks, but should be OK to be merged even if you don't want to address those 👍

This commit addresses that generating uint32 and uint64 returns
out-of-range values. This problem occurs in numeric conversions.
@jwChung jwChung force-pushed the address-out-of-range-value branch from a8423bd to 9a95b50 Compare July 27, 2018 14:27
@jwChung
Copy link
Contributor Author

jwChung commented Jul 27, 2018

According to the original Numeric.fs, it seems that there are the same problems with other types - sbyte, int16, byte and uint16 especially float32 and decimal. If I encounter the problem, I'll open an issue or PRs.

@moodmosaic
Copy link
Member

Perhaps we can also update Numeric.fs with the latest one.

@moodmosaic moodmosaic merged commit 252e23a into hedgehogqa:master Jul 27, 2018
@moodmosaic
Copy link
Member

/cc @jystic

@moodmosaic
Copy link
Member

Thank you, @jwChung 💯

@jwChung jwChung deleted the address-out-of-range-value branch July 28, 2018 03:38
@moodmosaic moodmosaic changed the title fix for generating out-of-range values, Improve UInt32 and UInt64 value generation (avoid overflows) Aug 6, 2018
@ghost ghost added this to the 0.8.2 milestone Sep 22, 2021
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.

2 participants