views:

590

answers:

1

I am converting my unit tests from NUnit to the VisualStudio 2010 unit test framework. This test fails with the message "Assert.AreEqual failed. Expected:<System.UInt16[]>. Actual:<System.UInt16[]>." what's actually very strange. This test was running successfully with NUnit. Any ideas why the types are supposed to be different?

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    Assert.AreEqual(ByteToUShort(array), new ushort[1]);
}

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array)
{
    return ByteToUShort(array, 0, array.Length, EndianType.LittleEndian);
}

public enum EndianType
{
    LittleEndian,
    BigEndian
}

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array, int offset, int length, EndianType endianType)
{
    // argument validation
    if ((length + offset) > array.Length)
        throw new ArgumentException("The length and offset provided extend past the end of the array.");
    if ((length % 2) != 0)
        throw new ArgumentException("The number of bytes to convert must be a multiple of 2.", "length");

    var temp = new ushort[length / 2];

    for (int i = 0, j = offset; i < temp.Length; i++)
    {
        if (endianType == EndianType.LittleEndian)
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) | (((uint)array[j++] & 0xFF) << 8));
        }
        else
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) << 8 | ((uint)array[j++] & 0xFF));
        }
    }

    return temp;
}
+3  A: 

It's not the types that are different, it's the instances. You're comparing two different arrays.

John Saunders
Okay i see. "Assert.That(UShortToByte(array), Is.EqualTo(new ushort[1]));" in NUnit (which works) and "Assert.AreEqual(ByteToUShort(array), new ushort[1]);" in VS are different. Then the only way to test this is "Assert.IsInstanceOfType(UConvert.ByteToUShort(array), typeof(ushort[]));". Thanks!
Martin Buberl
For single and multi-dimensional arrays, as well as for any ICollection, VS provides the CollectionAssert class.CollectionAssert.AreEqual(ByteToUShort(array), new ushort[1]);
Martin Buberl