Skip to content

Commit

Permalink
Merge pull request nunit#4754 from manfred-brands/TupleTolerance
Browse files Browse the repository at this point in the history
Tuple and Properties compare now obey DefaultFloatingPointTolerance
  • Loading branch information
mikkelbu authored Jul 11, 2024
2 parents 74f7675 + 5a8f869 commit 6d6a424
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,27 @@ public static EqualMethodResult Equal(object x, object y, ref Tolerance toleranc
string declaringTypeName = xType.Name;

uint redoWithoutTolerance = 0x0;
if (tolerance.HasVariance)
for (int i = 0; i < properties.Length; i++)
{
for (int i = 0; i < properties.Length; i++)
{
PropertyInfo property = properties[i];
object? xPropertyValue = property.GetValue(x, null);
object? yPropertyValue = property.GetValue(y, null);
PropertyInfo property = properties[i];
object? xPropertyValue = property.GetValue(x, null);
object? yPropertyValue = property.GetValue(y, null);

EqualMethodResult result = equalityComparer.AreEqual(xPropertyValue, yPropertyValue, ref tolerance, comparisonState);
if (result == EqualMethodResult.ComparedNotEqual)
{
return PropertyNotEqualResult(equalityComparer, i, declaringTypeName, property.Name, xPropertyValue, yPropertyValue);
}

if (result == EqualMethodResult.ToleranceNotSupported)
{
redoWithoutTolerance |= 1U << i;
}
EqualMethodResult result = equalityComparer.AreEqual(xPropertyValue, yPropertyValue, ref tolerance, comparisonState);
if (result == EqualMethodResult.ComparedNotEqual)
{
return PropertyNotEqualResult(equalityComparer, i, declaringTypeName, property.Name, xPropertyValue, yPropertyValue);
}

if (redoWithoutTolerance == (1U << properties.Length) - 1)
return EqualMethodResult.ToleranceNotSupported;
}
else
{
redoWithoutTolerance = (1U << properties.Length) - 1;
if (result == EqualMethodResult.ToleranceNotSupported)
{
redoWithoutTolerance |= 1U << i;
}
}

if (redoWithoutTolerance == (1U << properties.Length) - 1)
return EqualMethodResult.ToleranceNotSupported;

if (redoWithoutTolerance != 0)
{
Tolerance noTolerance = Tolerance.Exact;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,22 @@ public static EqualMethodResult Equal(object x, object y, ref Tolerance toleranc
ComparisonState comparisonState = state.PushComparison(x, y);

uint redoWithoutTolerance = 0x0;
if (tolerance.HasVariance)
for (int i = 0; i < numberOfGenericArgs; i++)
{
for (int i = 0; i < numberOfGenericArgs; i++)
{
string propertyName = i < 7 ? "Item" + (i + 1) : "Rest";
object? xItem = getValue(xType, propertyName, x);
object? yItem = getValue(yType, propertyName, y);

EqualMethodResult result = equalityComparer.AreEqual(xItem, yItem, ref tolerance, comparisonState);
if (result == EqualMethodResult.ComparedNotEqual)
return result;
if (result == EqualMethodResult.ToleranceNotSupported)
redoWithoutTolerance |= 1U << i;
}
string propertyName = i < 7 ? "Item" + (i + 1) : "Rest";
object? xItem = getValue(xType, propertyName, x);
object? yItem = getValue(yType, propertyName, y);

if (redoWithoutTolerance == (1U << numberOfGenericArgs) - 1)
return EqualMethodResult.ToleranceNotSupported;
}
else
{
redoWithoutTolerance = (1U << numberOfGenericArgs) - 1;
EqualMethodResult result = equalityComparer.AreEqual(xItem, yItem, ref tolerance, comparisonState);
if (result == EqualMethodResult.ComparedNotEqual)
return result;
if (result == EqualMethodResult.ToleranceNotSupported)
redoWithoutTolerance |= 1U << i;
}

if (redoWithoutTolerance == (1U << numberOfGenericArgs) - 1)
return EqualMethodResult.ToleranceNotSupported;

if (redoWithoutTolerance != 0)
{
Tolerance noTolerance = Tolerance.Exact;
Expand Down
10 changes: 10 additions & 0 deletions src/NUnitFramework/tests/Assertions/AssertThatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ public void AssertThatEqualsWithClassWithSomeToleranceAwareMembers()
});
}

[Test]
[DefaultFloatingPointTolerance(0.1)]
public void AssertThatEqualsWithClassWithSomeToleranceAwareMembersUsesDefaultFloatingPointTolerance()
{
var zero = new ClassWithSomeToleranceAwareMembers(0, 0.0, string.Empty, null);
var instance = new ClassWithSomeToleranceAwareMembers(1, 1.1, "1.1", zero);

Assert.That(new ClassWithSomeToleranceAwareMembers(1, 1.2, "1.1", zero), Is.EqualTo(instance).UsingPropertiesComparer());
}

private sealed class ClassWithSomeToleranceAwareMembers
{
public ClassWithSomeToleranceAwareMembers(int valueA, double valueB, string valueC, ClassWithSomeToleranceAwareMembers? chained)
Expand Down
18 changes: 18 additions & 0 deletions src/NUnitFramework/tests/Constraints/ToleranceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@ public void DefaultTolerance_Success()
Assert.That(2.05d, Is.EqualTo(2.0d));
}

[Test, DefaultFloatingPointTolerance(0.1)]
public void DefaultToleranceOnTuple_Success()
{
Assert.That((2.05d, 0.95d), Is.EqualTo((2.0d, 1.0d)));
}

[Test, DefaultFloatingPointTolerance(0.1)]
public void DefaultToleranceOnMixedTuple_Success()
{
Assert.That((1, 2.05d, true), Is.EqualTo((1, 2.0d, true)));
}

[Test, DefaultFloatingPointTolerance(0.01)]
public void DefaultTolerance_Failure()
{
Assert.That(2.05d, Is.Not.EqualTo(2.0d));
}

[Test, DefaultFloatingPointTolerance(0.01)]
public void DefaultToleranceOnTuple_Failure()
{
Assert.That((2.05d, 0.95d), Is.Not.EqualTo((2.0d, 1.0d)));
}

[Test, DefaultFloatingPointTolerance(0.5)]
public void TestToleranceDefault()
{
Expand Down

0 comments on commit 6d6a424

Please sign in to comment.