Skip to content

FIXME: swift/validation-test/stdlib/Set.swift:95 #455

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

Merged
merged 1 commit into from
Dec 22, 2015

Conversation

frootloops
Copy link
Contributor

Use real arc4random_uniform() instead of just random() % max

  1. Is truncatingBitPattern proper way to prevent integer overflow?
  2. Does UInt32 available on every platform?

Off topic question:

Why do you use if true {} in test cases? Is it equivalent for do {}?

@gparker42
Copy link
Contributor

No, UInt32(truncatingBitPattern:) is not the right answer here. If the max value passed to uniformRandom() is too bit to fit in a UInt32 then truncatingBitPattern: will silently shrink it. Then the random numbers you get are in fact not uniform in the requested range.

UInt32 is available on every platform. There is a platform difference with Int: Int is only 32 bits on 32-bit platforms. That will make your Int() conversion fail when arc4random_uniform's result is too big.

This is only a test and it looks like we only call uniformRandom with small values. In that case it should be fine to use checked conversions to UInt32 and back to Int. They will never fail with the tests as written today, and if something does fail someday then we can write a more complicated fix.

@gparker42
Copy link
Contributor

The if true statements probably pre-date the existence of do. IIRC do was added late in the development of Swift 2.0.

@frootloops
Copy link
Contributor Author

@gparker42 thanks for comments. I have updated the PR. Is that ok?

@gribozavr
Copy link
Contributor

Looks good, thanks!

gribozavr added a commit that referenced this pull request Dec 22, 2015
FIXME: swift/validation-test/stdlib/Set.swift:95
@gribozavr gribozavr merged commit 87bd891 into swiftlang:master Dec 22, 2015
@frootloops frootloops deleted the uniform-random branch March 27, 2016 05:46
freak4pc pushed a commit to freak4pc/swift that referenced this pull request Sep 28, 2022
…ocket

XFAIL ReactiveCocoa and BlueSocket targets due to LLVM assertions after rebranch
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.

4 participants