Consider the function:
public static ulong[] productFib(ulong prod)
{
ulong fib1 = 0;
ulong fib2 = 1;
while ((fib1 * fib2) < prod)
{
ulong temp = fib1;
fib1 = fib2;
fib2 = temp + fib2;
}
return new ulong[] { fib1, fib2, 1 };
}
I am working through a kata whose test looks like the following:
ulong[] r = new ulong[] { 55, 89, 1 };
Assert.AreEqual(r, Kata.productFib(4895));
In the immediate window, I can see that the return array is not only of type ulong
but has the 3 elements I expect, yet MSTest fails with:
Message: Assert.AreEqual failed. Expected:<System.UInt64[]>. Actual:<System.UInt64[]>.
Is there something wrong with the test assertion, or my function, that would cause this test to fail?
Thank you
Assert.AreEqual()
uses the Equals()
method of the passed arguments. And arrays simply compare their references, not their content. So two different array instances will never pass this test.
You may use linq SequenceEqual
like that:
Assert.IsTrue(r.SequenceEqual(Kata.productFib(4895)));