Fix tensor EqualWithTolerance test helper#127868
Open
lilinus wants to merge 2 commits intodotnet:mainfrom
Open
Fix tensor EqualWithTolerance test helper#127868lilinus wants to merge 2 commits intodotnet:mainfrom
lilinus wants to merge 2 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics-tensors |
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR refactors tensor test comparisons to centralize tolerance-based equality assertions, and expands test assertion utilities to support NFloat comparisons.
Changes:
- Replace manual tolerance checks and custom exception throwing with
Helpers.AssertEqualWithTolerance(...)across tensor tests. - Rework
Helperstolerance comparison helper from predicate-style (IsEqualWithTolerance) to assertion-style, delegating toAssertExtensions.Equalwhere applicable. - Add
AssertExtensions.Equaloverloads forNFloat(variance-based and bitwise-equal variants) and fixNFloattolerance selection.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.NonGeneric.Single.cs | Switches float tolerance comparison to centralized assertion helper. |
| src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs | Replaces several per-element tolerance checks and detailed failure messages with centralized assertions. |
| src/libraries/System.Numerics.Tensors/tests/Helpers.cs | Introduces AssertEqualWithTolerance with scaled tolerance and routes to AssertExtensions (including NFloat), plus fixes NFloat tolerance selection. |
| src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs | Adds NFloat equality assertions (variance-based and bitwise) with documentation. |
Comment on lines
+36
to
41
| T actualTolerance = tolerance ?? DefaultTolerance<T>.Value; | ||
| if (!T.IsZero(actualTolerance)) | ||
| { | ||
| return false; | ||
| T scaledTolerance = T.Max(T.Abs(expected), T.Abs(actual)) * actualTolerance; | ||
| actualTolerance = T.Max(scaledTolerance, actualTolerance); | ||
| } |
Comment on lines
+43
to
+58
| if (typeof(T) == typeof(double)) | ||
| { | ||
| AssertExtensions.Equal((double)(object)expected, (double)(object)actual, (double)(object)actualTolerance); | ||
| } | ||
| else if (typeof(T) == typeof(float)) | ||
| { | ||
| AssertExtensions.Equal((float)(object)expected, (float)(object)actual, (float)(object)actualTolerance); | ||
| } | ||
| else if (typeof(T) == typeof(Half)) | ||
| { | ||
| AssertExtensions.Equal((Half)(object)expected, (Half)(object)actual, (Half)(object)actualTolerance); | ||
| } | ||
| else if (typeof(T) == typeof(NFloat)) | ||
| { | ||
| AssertExtensions.Equal((NFloat)(object)expected, (NFloat)(object)actual, (NFloat)(object)actualTolerance); | ||
| } |
| { | ||
| throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateTruncating(source.Span[i])}."); | ||
| } | ||
| Helpers.AssertEqualWithTolerance(TTo.CreateTruncating(source.Span[i]), destination.Span[i]); |
| throw EqualException.ForMismatchedValues(ToStringPadded(expected), ToStringPadded(actual)); | ||
| } | ||
|
|
||
| /// <summary>Verifies that two <see cref="NFloat"/> values's binary representations are identical.</summary> |
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.
Addresses the issues special values with IsEqualWithTolerance described in #107934.
If vectorization is enabled again then tests are failing (which is expected, since the vectorized versions give a different answer).