Skip to content

std.random: No equality checks for doc example unittests#5762

Merged
dlang-bot merged 1 commit intodlang:stablefrom
kinke:random
Oct 8, 2017
Merged

std.random: No equality checks for doc example unittests#5762
dlang-bot merged 1 commit intodlang:stablefrom
kinke:random

Conversation

@kinke
Copy link
Contributor

@kinke kinke commented Oct 7, 2017

No description provided.

@kinke kinke requested a review from wilzbach as a code owner October 7, 2017 18:51
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @kinke! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.

Some tips to help speed things up:

  • smaller, focused PRs are easier to review than big ones

  • try not to mix up refactoring or style changes with bug fixes or feature enhancements

  • provide helpful commit messages explaining the rationale behind each change

Bear in mind that large or tricky changes may require multiple rounds of review and revision.

Please see CONTRIBUTING.md for more information.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@PetarKirov
Copy link
Member

PetarKirov commented Oct 7, 2017

Perhaps we should make the assertion conditional on the size of real?

On the other hand I'm not sure why the documentation example assumes that Random is an always an alias for Mt19937 as before that, readers are left with the expectation that the alias is implementation defined and that perhaps one shouldn't rely on it if they're writing portable code:

The standard library provides an alias $(D_PARAM Random) for
whichever generator it considers the most fit for the target
environment.

@WebDrake please advice.

@kinke
Copy link
Contributor Author

kinke commented Oct 7, 2017

Even leaving the alias thing aside, I think checking for exact FP equality here is a bad idea (and no need for reals - just use double for portability; the rhs is a double too btw).

@WebDrake
Copy link
Contributor

WebDrake commented Oct 7, 2017

It does indeed seem dodgy to assume that Random is Mt19937, although right now the assumption holds true for all supported architectures. Note that this affects all the equality checks for variates in this unittest example, not just the real-valued one.

TBH I think the problems of this unittest come out of a confused purpose: it's really a module-level documentation example, so there's no need for it to check these result values at all. Checks on the results of deterministically seeded RNGs can be (and are) made in the more in-depth checks of those RNGs themselves.

I'd suggest just cutting all the equality checks on results in this unittest block. If not I guess approxEqual will suffice: feqrel is much better for comparing floating-point results, but is harder for the reader to understand, which in this context is probably more important.

Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

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

Testing exact equality of floating-point values in the documentation of this module is not a good idea. It can be either non-portable (wrong on some platforms) or correct, but requiring too much effort than necessary for something that ought to be simple in which case distracting the users from the main objective of the example.

assert(r == 79.65429843861011285);
import std.math : approxEqual;
assert(approxEqual(r, 79.65429843861011285));

Copy link
Member

@PetarKirov PetarKirov Oct 7, 2017

Choose a reason for hiding this comment

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

Given @WebDrake advice, I think it's better to simply remove the equality comparisons and instead assert that the numbers are in the expected intervals.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ZombineDev I take it you're talking here only about the floating-point number? The integer variates generated elsewhere in the unittest should be exactly equal, assuming that Random is reliably Mt19937.

Copy link
Member

@PetarKirov PetarKirov Oct 7, 2017

Choose a reason for hiding this comment

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

Yes, I mean the integer cases too, since I don't think knowledge of the Random's implementation should be assumed for the readers of the first example in the documentation. Testing the exact values should be done only when the exact random engine is explicitly specified, in addition to its seed, IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, so you mean in this case you'd check that the resulting number is >= 0 and < 100, and for the uniform(0, 15, rnd) call you'd check that it's >= 0 and < 15?

That sounds reasonable, yes. Note that for the uniform!uint case this won't really work since the range of possible values covers the entire range of possible values for that type; but you don't really have to validate that result at all anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

They depend on the `Random` alias as well as the `real` precision.
@kinke kinke changed the title std.random: Relax unittest assertion for 64-bit reals std.random: No equality checks for doc example unittests Oct 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants