Templatize ApproxEqualFailure and assert_approx_equal#51
Merged
Conversation
Add ApproxEqualFailure(float, float, float) constructor and a virtual
assert_approx_equal(float, ...) overload so float values are never
implicitly promoted to double in the failure-reporting path.
Introduce check_approx_fail<T> as a dispatch template: the primary
form does static_cast<double> (making integral conversions explicit and
unambiguous), while the explicit float specialisation passes values
through as-is. check_approx now delegates to check_approx_fail.
CHECK_EQUAL already handled float correctly via string_from(float).
Add FailureWithCHECK_EQUAL_Float and FloatsEqual{Expected,Actual,
Threshold}IsNaN tests to cover the new float paths.
ApproxEqualFailure becomes a class template ApproxEqualFailure<T>. Its constructor uses string_from(T) for formatting and detects NaN via the IEEE 754 self-comparison trick (v != v), the same approach used in approx_equal — so no type-specific code, no hard-coded double, no string matching. Shell::assert_approx_equal becomes a non-virtual template method that constructs ApproxEqualFailure<T> directly. check_approx<T> passes its T values straight through, with no cast and no dispatch helper. The virtual seam for overriding is add_failure, as with every other assertion.
ApproxEqualFailure is a class template; explicit template argument required in C++11/14 since CTAD is C++17-only. Also wrap long lines to satisfy clang-format.
792e49b to
26dd665
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
CHECK_APPROX(1.0f, 1.5f, 0.1f)was silently promoting all threefloatvalues todoubleon the failure-reporting path (passed toassert_approx_equal(double, double, double)). Similarly,CHECK_APPROX(110, 111, 10)was casting integers todouble. This PR fixes the root cause by making the failure type itself generic.ApproxEqualFailure<T>is now a class template. Its constructor:string_from(T)(type-correct forint,float,double, etc.)v != v), the same approach already used inapprox_equal<T>doubleanywhere in its interfaceShell::assert_approx_equalbecomes a non-virtual template method that constructsApproxEqualFailure<T>directly. The virtual seam for observing failures isadd_failure, as with every other assertion.check_approx<T>now passes itsTvalues straight through — no cast, no dispatch helper, no overload ambiguity.As a secondary change,
CHECK_EQUALwithfloatis explicitly covered by a passing test and a failing-message test (it was already correct, but untested).Related Issues
Fixes # (issue number)
Type of Change
Checklist
docs/for any user-facing changes.mu::tinynamespace,INCLUDED_MUTINY_guards,mutiny_C-prefix)..hand.c.cpp) is required for parity.CONTRIBUTING.mdfile to ensure compliance with architectural guidelines.