Cizel
Cizel

Reputation: 69

Assert Statements not functioning properly?

I am having trouble understanding the nature of the assert statement

I thought that my program was not working properly, but when I made my main print out the return value that my program gave back to me, it was the exact value that my assert statement was supposed to accept. I'm not sure what is going on. I copy paste the function into another file, and copy paste my assert statements, this time different assert cases work(some of the cases that didn't work, now work, and some of the cases that worked, now don't). When I worked through the debugger, it seemed that the assert statement is causing a certain logic statement

to not work at times, when the logic is supposed to. I don't understand why... Can someone please explain this to me?

Upvotes: 0

Views: 89

Answers (1)

user7860670
user7860670

Reputation: 37598

The problem is that you modify passed array inside of removeDuplicatedValues so first assert((removeDuplicatedValues(duplicates1, 5)) == 1); assertion succeeds but when you call next assertion using the same array assert((removeDuplicatedValues(duplicates1, 4)) == 1); it will fail because duplicates were already removed from that array. So you should rewrite your tests to use array only once (or even switch to dedicated unit test framework):

{
   string duplicates[7] = { "kek" , "hello" , "kek" , "daisy" , "bear" , "bear" , "bear" };
   assert((removeDuplicatedValues(duplicates, 5)) == 1); // ok
}
{
   string duplicates[7] = { "kek" , "hello" , "kek" , "daisy" , "bear" , "bear" , "bear" };
   assert((removeDuplicatedValues(duplicates, 4)) == 1); // ok
}

Upvotes: 2

Related Questions