89

Is there an easy way to check in a unit test that two arrays are equal (that is, have the same number of elements, and each element is the same?).

In Java, I would use assertArrayEquals (foo, bar);, but there seems to be no equivalent for C#. I tried Assert.AreEqual(new string[]{"a", "b"}, MyFunc("ab"));, but even though the function returns an array with "a", "b" the check still fails

This is using Visual Studio 2008 Team Suite, with the built-in unit test framework.

Anteru
  • 19,042
  • 12
  • 77
  • 121

4 Answers4

163

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

Anteru
  • 19,042
  • 12
  • 77
  • 121
  • Just keep in mind the following, http://stackoverflow.com/questions/5194966/mstest-collectionassert-areequivalent-failed-the-expected-collection-contains the objects may be compared explicitly with `object.Equals` and the `IEqualityComparer` may have to be defined in order to get passed a failed assertion. – atconway Dec 30 '13 at 23:23
  • 2
    I wish this gave more detailed messages when it failed. "Different number of elements" and "Element at index 0 do not match" are slightly useless. *What are they then?!* – Colonel Panic May 15 '15 at 15:45
  • 2
    `CollectionAssert.AreEquivalent` (available from Visual Studio 2010) gives more informative results. For instance, when the number of elements differ, the message states the expected and the actual number of elements – Dennie Jan 09 '18 at 14:48
26

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.)
Erix Xu
  • 277
  • 3
  • 2
3

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

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

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;
}
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90