Skip to content
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

Unexpected GreaterThan(NaN) behavior #4711

Closed
shimat opened this issue May 14, 2024 · 2 comments
Closed

Unexpected GreaterThan(NaN) behavior #4711

shimat opened this issue May 14, 2024 · 2 comments

Comments

@shimat
Copy link

shimat commented May 14, 2024

Hi,

even though 0.1 > double.NaN returns False, ClassicAssert.GreaterThan(0.1, double.NaN) passes. Of course, the behavior is similar for any value other than 0.1. Is this the intended behavior?

NUnit 4.1.0 / .NET 8

public class Tests
{
    // Fail
    [Test]
    public void DirectGreaterThan() => ClassicAssert.IsTrue(0.1 > double.NaN);

    // Fail
    [Test]
    public void DirectLessThan() => ClassicAssert.IsTrue(0.1 < double.NaN);

    // Pass (???)
    [Test]
    public void NUnitGreaterThan()
    {
        ClassicAssert.Greater(0.1, double.NaN);
        Assert.That(0.1, Is.GreaterThan(double.NaN));
    }

    // Fail
    [Test]
    public void NUnitLessThanTest() => ClassicAssert.LessOrEqual(0.1, double.NaN);
}
@manfred-brands
Copy link
Member

manfred-brands commented May 15, 2024

@shimat I can confirm this issue, but what is correct is not that straight forward:

NUnit will use Double.CompareTo

This contains the following table:
image

[Test]
public void CompareWithNaN()
{
    const double actual = 0.1;

    Assert.Multiple(() =>
    {
        Assert.That(actual.CompareTo(double.NaN), Is.Positive);
        Assert.That(double.NaN.CompareTo(actual), Is.Negative);
        Assert.That(double.NaN.CompareTo(double.NaN), Is.Zero);

        Assert.That(actual, Is.GreaterThan(double.NaN));
        Assert.That(double.NaN, Is.LessThan(actual));
        Assert.That(double.NaN, Is.EqualTo(double.NaN));

        Assert.That(actual > double.NaN, Is.True);
        Assert.That(double.NaN < actual, Is.True);
        Assert.That(double.NaN == double.NaN, Is.True);
    });
}

Which matches the current NUnit behaviour, but not the C# expression as only the last 3 tests fail.

@shimat
Copy link
Author

shimat commented May 22, 2024

Thank you, I understand.
I still think GreaterThan when NaN is in the argument is misleading behavior, but I understand the reason and this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants