views:

244

answers:

1

Short question, why do Assert.AreEqual(1.0, double.NaN, 0.0) pass when Assert.AreEqual(1.0, double.NaN) do not?

Is it an error in MSTest or am I missing something here?

Best regards, Egil.

Update: Should probably add, that the reason behind my question is, that I have a bunch of unit tests that unfortunately passed due to the result of some linear algebraic matrix operation being NaN or (+/-)Infinity. The unit tests are fine, but since Assert.AreEqual on doubles with a delta will pass when actual or/and expected are NaN or Infinity, I was left to believe that the code I was testing was correct.

+6  A: 

Be careful. NaN is weird, somewhat like null in many DBMSs, and you shouldn't be comparing values to it (either directly, or with Assert.AreEqual). From the docs for Double.NaN:

Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN.

double zero = 0;
Console.WriteLine((0 / zero) == Double.NaN);  // prints false
Console.WriteLine(Double.IsNaN(0 / zero));  // prints true

You'd have to peer at the internals of Assert(double, double, double) to see what's going on, but in general, you're depending on undefined behavior relative to NaN.

Michael Petrotta
The story behind my question is a bunch of unit tests that unfortunately passed due to the result of some linear algebraic matrix operation being NaN/(+/-)Infinity. The unit tests are fine, but since Assert.AreEqual on doubles with a delta will pass when actual or/and expected are NaN or Infinity, I was left to believe that my code actually worked...
Egil Hansen
I get that. On the face of it, the behavior is incorrect, and you'd be justified filing an issue on Microsoft Connect. If you like, hook up a symbol server and see how Assert.AreEqual(double, double) differs from Assert.AreEqual(double, double, double).
Michael Petrotta