rockability
rockability

Reputation: 63

How to kill conditional boundary mutant

I'm starting to dig into Code Analysis and mutants. I'm using PITest as a plugin on my eclipse Project. I ran into this mutant that I can't kill. Suppose I have the following code.

class Mutation{
        public static void main(String [] args){
            int i=0;
            String SPECIAL_CHARS = "?!$@";
            String password = "Something";
            for (int pos = 0; pos < password.length(); pos++) {
                char c = password.charAt(pos);
                if(SPECIAL_CHARS.indexOf(c) < 0) {
                 i++;
                }
            }
    }
}

As far as my understanding goes, If there's a char that doesn't belong to SPECIAL_CHARS (say ( ), variable i will increment. PITest reports two mutants.

enter image description here

With the following information.

enter image description here

enter image description here

I tried to write several Junit test that may kill this mutant without any luck. Can someone please explain me how is it possible to kill it?

I know now, that if my password has ? as the first char of the String password the conditional boundary check may be killed. What about the increment? What does it mean?

Upvotes: 2

Views: 4802

Answers (1)

henry
henry

Reputation: 6096

It is not possible to write a test to kill any of the possible mutants in the code you present as the code doesn't do anything. The method returns void and the code does not perform any side effects.

Pitest could delete all the code from this method and the program would be functionally equivalent.

If the code were modified so that is produced some sort of output (a count of special characters perhaps?) and the input password was not hardcoded, then it would be possible to write tests that distinguished the mutated programs from the un-mutated one.

The conditional boundary mutant produces code equivalent to

if (SPECIAL_CHARS.indexOf(c) <= 0 )

This could be detected by a test case with a special character at the 0 index of the password that checked this was detected in the count.

The increments mutation operator converts the increment to a decrement ie from

i++

to

i--

This is quite an unstable mutation and would be detected by any test case that provided a password containing a special character that asserted on the resulting count.

Upvotes: 2

Related Questions