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

Avoid rand() in the legacy tr1 test suite #4921

Merged

Conversation

StephanTLavavej
Copy link
Member

@StephanTLavavej StephanTLavavej commented Aug 27, 2024

Our bosses and boss-like entities heard that rand() is considered harmful and are starting a push to avoid unnecessary use.

Almost all of our mentions of rand are Standard-mandated (or Standard stable name citations), but I found a few unnecessary uses in our legacy tr1 test suite. We avoid cleaning up its code unless there's a specific reason to mess with it, and this easily qualifies.

  • tr1/tests/algorithm
    • Give rand_gen an mt19937 data member. (Default construction is fine; we weren't attempting to seed rand() before.) This is purely masking bits with & 0xfffff, so there's no need for a uniform_int_distribution.
    • Replace the frand() wrapper with an rng_func lambda. To avoid /clr:pure problems with init-captures, I'm capturing mt by reference (so the lambda doesn't need to be mutable and doesn't need to be passed via ref()). We need to adapt the half-open interface to uniform_int_distribution's fully-closed range.
  • tr1/tests/memory2
    • Construct a local mt19937 mt; within the static void ctors() worker function. (It's important to not use a global engine with multiple threads!) Again, we weren't attempting to seed rand(), so default-constructing mt19937 is fine. And because we're just looking at a bit, we don't need a uniform_int_distribution.

@StephanTLavavej StephanTLavavej added the test Related to test code label Aug 27, 2024
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner August 27, 2024 22:10
@StephanTLavavej StephanTLavavej self-assigned this Aug 27, 2024
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej
Copy link
Member Author

I had to push an additional commit to avoid /clr:pure problems with init-captures.

@StephanTLavavej StephanTLavavej merged commit 91e4255 into microsoft:main Aug 28, 2024
39 checks passed
@StephanTLavavej StephanTLavavej deleted the rand-considered-harmful branch August 28, 2024 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Related to test code
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants