Function has limits same from both boundaries so changed conditional boundary fails but should not

49 Views Asked by At

I have function defined as:

  • For x < 0 function returns 0
  • For x >= 0 function returns x

I implemented it as

if (x < 0) return 0;
return x;

I have test testing that for x equals to 0 does return 0.

But mutant

if (x <= 0) return 0;
return x;

is surviving.

Any tips how to cover this mutant?

1

There are 1 best solutions below

0
On BEST ANSWER

On the face of it this is a classic equivalent mutant. However, your if statement can be replaced with

return Math.max(0,x);

Which is an improvement as it expresses the intent of the logic at a glance, and does not create the mutation.

The Math.max implementation will be a variation of your original code. In that location there is no improvement that can be made, and the mutation there must be treated as equivalent (i.e. ignored).