Assert that arrays are equal in Visual Studio 2008 test framework

It's CollectionAssert.AreEqual, see also the documentation for CollectionAssert.


Class1.cs:


namespace ClassLibrary1
{
    public class Class1
    {
        Array arr1 = new[] { 1, 2, 3, 4, 5 };
        public Array getArray()
        {
            return arr1;
        }
    }
}

ArrayEqualTest.cs:


        [TestMethod()]
        public void getArrayTest()
        {
            Class1 target = new Class1(); 
            Array expected = new []{1,2,3,4,5}; 
            Array actual;
            actual = target.getArray();
            CollectionAssert.AreEqual(expected, actual);
            //Assert.IsTrue(expected.S actual, "is the test results");
        }

Test Success,found the error:


   CollectionAssert.AreEqual failed. (Element at index 3 do not match.)

In .NET 3.5, perhaps consider Assert.IsTrue(foo.SequenceEqual(bar)); - it won't tell you at what index it differs, though.


Ok here is a slightly longer way of doing it...

static void Main(string[] args)
{
    var arr1 = new[] { 1, 2, 3, 4, 5 };
    var arr2 = new[] { 1, 2, 4, 4, 5 };

    Console.WriteLine("Arrays are equal: {0}", equals(arr1, arr2));
}

private static bool equals(IEnumerable arr1, IEnumerable arr2)
{

    var enumerable1 = arr1.OfType<object>();
    var enumerable2 = arr2.OfType<object>();

    if (enumerable1.Count() != enumerable2.Count())
        return false;

    var iter1 = enumerable1.GetEnumerator();
    var iter2 = enumerable2.GetEnumerator();

    while (iter1.MoveNext() && iter2.MoveNext())
    {
        if (!iter1.Current.Equals(iter2.Current))
            return false;
    }

    return true;
}