Share via


Assert if two 2D arrays are equal

Question

Saturday, July 20, 2013 11:34 PM

How can I Assert if two 2D arrays are equal in a unit test?

var a = new double[,] { {1, 2}, {3, 4}};
var b = new double[,] { {1, 2}, {3, 4}};If these were 1D arrays I could do Assert.IsTrue(a.SequenceEqual(b))  

All replies (7)

Sunday, July 21, 2013 6:20 PM âś…Answered

Just noticed, 2 Dimensional array. Using the non-jagged syntax too. Well, I guess you need two nested for-loops then. I personally always use jagged arrays if I need more then 1 Dimension, so I am not certain how to figure out the lenght equivalent for the 1st and 2nd Index.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Sunday, July 21, 2013 12:11 AM

 

Since you cannot use the Hashcode which uses the reference rather than the content of an array, the way to go would be to iterate all values in a nested loop, compare value by value (as in stackoverflow.com/questions/2893297/iterate-multi-dimensional-array-with-nested-foreach-statement), use a boolean which you set to false at the first mismatch, and assert that this boolean is true in your unit test.


Sunday, July 21, 2013 12:16 AM

 * This method test two "2 dimensional" arrays to see if they are the same
     * size and if the items inside are the same.
     * @param expected The expected 2 dimensional array.
     * @param actual The actual 2 dimensional array.
     */
    static public void assertArrayEquals(double[][] expected, double[][] actual) {
        assertArrayEquals(null, expected, actual);
    }   
}


Sunday, July 21, 2013 5:03 AM | 1 vote

Multidimensional arrays do implement IEnumerable so you could still use SequenceEqual if you "cast" them to IEnumerable<double>:

var a = new double[,] { { 1, 2 }, { 3, 4 } };
var b = new double[,] { { 1, 2 }, { 3, 4 } };

Console.WriteLine(a.Cast<double>().SequenceEqual(b.Cast<double>()));

@hassansayedissa: "Since you cannot use the Hashcode which uses the reference rather than the content of an array"

Reference or not you can never use the hashcode to compare objects for equality.


Sunday, July 21, 2013 3:06 PM

try the following, this might be what you are looking for :

var equality =
    //check the number of dimensions
    a.Rank == b.Rank &&
    //check if on every dimension you have the same size 
    Enumerable.Range(0,a.Rank).All(dimension =>
        a.GetLength(dimension) == b.GetLength(dimension))) &&
    //use Cast to turn them into an ienumerable (containing every items of the Array) su you can use SequenceEqual 
    a.Cast<double>().SequenceEqual(b.Cast<double>())

NIMONG Lionel, beginner on sharepoint


Sunday, July 21, 2013 6:15 PM

What you ask for looks like a extension method. Afaik you cannot (and should not) try to inherit array. There are several very different forms of equality/identity (and at least 5 ways to test for either):

http://www.codeproject.com/Articles/18714/Comparing-Values-for-Equality-in-NET-Identity-and

I asume you mean value equality, so something like this:

bool sequencesEqual(double[] a, double[] b){
  //Check if they are the same references
  if(object.referenceEquals(a, b))
    return true;

  //Check if they are equall lenght
  if(a.lenght != b.lenght)
    return false;

  //force check if all the values are the same, return on first dissimilarity
  for(int i = 0; i < a.lenght; i++){
    if(a[i] != b[i])
      return false;
  }

  //They survived the previous check, so they must be equal
  return true;
}

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Friday, July 26, 2013 2:28 AM

Thanks for the suggestions.  It was not the answer I was expecting, but I can do it this way.