Skip to content

Fix tensor EqualWithTolerance test helper#127868

Open
lilinus wants to merge 2 commits intodotnet:mainfrom
lilinus:fix-tensor-equal-with-tolerance
Open

Fix tensor EqualWithTolerance test helper#127868
lilinus wants to merge 2 commits intodotnet:mainfrom
lilinus:fix-tensor-equal-with-tolerance

Conversation

@lilinus
Copy link
Copy Markdown
Contributor

@lilinus lilinus commented May 6, 2026

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).

Copilot AI review requested due to automatic review settings May 6, 2026 14:50
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 6, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 Helpers tolerance comparison helper from predicate-style (IsEqualWithTolerance) to assertion-style, delegating to AssertExtensions.Equal where applicable.
  • Add AssertExtensions.Equal overloads for NFloat (variance-based and bitwise-equal variants) and fix NFloat tolerance 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Numerics.Tensors community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants