Assert.AreEqual () fails when it seems to be alright

1

I can not seem to find any errors in my Lucky Bubble algorithm. But the assert returns error.

I'm passing erroneous arguments to Assert.AreEqual() ?

//The method for testing
public static int[] BubbleSort(int[] vect)
    {
        for (int i = vect.Length-1; i >=1 ; i--)
        {
            for (int j = 0;  j < i;  j++)
            {
                if (vect[i]<vect[j])
                {
                    var aux = vect[i];
                    vect[i] = vect[j];
                    vect[j] = aux;
                }
            }
        }
        return vect;
    }


    [TestMethod]
    public void TestBubbleSort()
    {
        int[] vetor = new int[10];
        vetor[0] = 9;
        vetor[1] = 8;
        vetor[2] = 7;
        vetor[3] = 5;
        vetor[4] = 6;
        vetor[5] = 2;
        vetor[6] = 3;
        vetor[7] = 1;
        vetor[8] = 4;
        vetor[9] = 0;

        int[] sortVector = new int[10];
        sortVector[0] = 0;
        sortVector[1] = 1;
        sortVector[2] = 2;
        sortVector[3] = 3;
        sortVector[4] = 4;
        sortVector[5] = 5;
        sortVector[6] = 6;
        sortVector[7] = 7;
        sortVector[8] = 8;
        sortVector[9] = 9;

        Assert.AreSame(sortVector, Study.BubbleSort(vetor), "Error");
    }
    
asked by anonymous 13.11.2015 / 14:02

1 answer

3

I will not evaluate the algorithm, but the unit test. He's wrong. The Assert.AreSame() method checks whether the two variables point to the same object. And obviously they do not point. You are not comparing the things you imagine. And note that you are not using the method given in the question.

To solve this, you would have to use methods that analyze the values of all the arrays elements and check if they all match. Only if everyone knocks is that it's ok. You can use Enumerable.SequenceEqual() to do this verification, within an Assert.IsTrue() .

    
13.11.2015 / 14:13