Assert.Areequal() fails when everything seems to be right

Asked

Viewed 144 times

1

I can’t find any errors in my Bubble Sort algorithm. But the assert returns error.

I’m passing wrong arguments to the 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");
    }

1 answer

3


I will not evaluate the algorithm, but rather the unit test. It is wrong. The method Assert.AreSame() checks if the two variables point to the same object. And obviously they don’t point. You’re not comparing the things you imagine. And note that you are not using the method indicated in the question.

To solve this would have to use methods that analyze the values of all elements of arrays and check if all hit. Only if all hit is that ok. You can use Enumerable.SequenceEqual() to make such a check within a Assert.IsTrue().

  • Thanks, solved the problem and understood how Assert.Aresame works, before I had used Assert.Areequal() and also had not solved. Thanks for the help and I’m sorry for the English.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.