Skip to content

float tests: deduplicate min, max, and rounding tests #142243

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jun 9, 2025

Part of #141726

Best reviewed commit-by-commit.

  • Use assert_biteq! in the mod.rs tests. This requires some trickery to make shadowing macros with imports work.
  • The min, max, minimum, maximum tests in tests/floats/f*.rs are entirely subsumed by what we already have in tests/float/mod.rs, so I just removed them.
  • The rounding tests (floor etc) in f*.rs had more test points, so I copied them over. They didn't have 0.5 and -0.5 though which seem like interesting points in particular regarding the sign of the resulting zero if that's what it sounds to, and they didn't max min/max/inf/nan tests, so this was really a merger of both tests.

r? @tgross35

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 9, 2025
assert_biteq!((-0.0 as Float).midpoint(-0.0), -0.0);
assert_biteq!((-5.0 as Float).midpoint(5.0), 0.0);
assert_biteq!(Float::MAX.midpoint(Float::MIN), 0.0);
assert_biteq!(Float::MIN.midpoint(Float::MAX), 0.0);
Copy link
Member Author

@RalfJung RalfJung Jun 9, 2025

Choose a reason for hiding this comment

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

The old test seemed to assume that the result of MIN.midpoint(MAX) should be -0.0, but actually we return 0.0.

Copy link
Member

Choose a reason for hiding this comment

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

The midpoint function in C++ with clang and GCC also seems to return +0.0, so this should be fine. We don't guarantee the sign of zero anyway for midpoint.

@RalfJung RalfJung force-pushed the float-test-dedup branch from dc526c1 to 7171984 Compare June 9, 2025 12:18
@RalfJung RalfJung force-pushed the float-test-dedup branch from 7171984 to 1b6bb45 Compare June 9, 2025 12:24
assert_eq!(Float::MAX.ceil(), Float::MAX);
assert_eq!(Float::MIN.ceil(), Float::MIN);
assert_eq!(Float::MIN_POSITIVE.ceil(), 1.0);
assert_eq!((-Float::MIN_POSITIVE).ceil(), 0.0);
Copy link
Member Author

Choose a reason for hiding this comment

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

A bunch of the -Float::MIN_POSITIVE rounding tests were missing the - as well.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the cleanup. One question then r=me.

@@ -14,10 +37,11 @@ macro_rules! assert_approx_eq {
);
}};
}
pub(crate) use assert_approx_eq_ as assert_approx_eq;
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason for the trailing _ rather than just pub(crate) use assert_approx_eq;? I assume there may be some name conflicts.

Copy link
Member Author

Choose a reason for hiding this comment

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

I got an error about name clashes:

error[E0659]: `assert_approx_eq` is ambiguous
   --> library/coretests/tests/floats/mod.rs:681:9
    |
681 |         assert_approx_eq!((-1.7 as Float).fract(), -0.7);
    |         ^^^^^^^^^^^^^^^^ ambiguous name
    |
    = note: ambiguous because of a conflict between a `macro_rules` name and a non-`macro_rules` name from another module

I think I'll pick actually proper names for both runtime and const macros and use imports for name management, that's probably clearer.

Copy link
Member Author

Choose a reason for hiding this comment

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

I restructured the macros a bit to make it more clear (I think), please take a look. Putting the const ones in a module doesn't work out that well since (a) macros have their own special global namespace, and (b) we can't glob-import them anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that looks good to me. Maybe just add a breadcrumb comment at the use {assert_*_const as assert_*} remapping? Also if you don't mind grabbing it while you're here, I notice the preexisting doc example for the float_test macro is missing its closing /// ``` .

I still can't wrap my head around the way macro namespaces work sometimes...

Copy link
Member Author

Choose a reason for hiding this comment

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

There's already a comment there. What would you like to have added to it?

I still can't wrap my head around the way macro namespaces work sometimes...

Yeah.^^ I wonder if we can get rid of it in a future edition...

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a comment at the assert_approx_eq_rt as assert_approx_eq version mentioning that the rt version is used by default so const can shadow it, I was just thinking to mirror this at the place we shadow it (lines 193-194). But it's noncritical, r=me with or without it.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Oops forgot the ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants