hcoles / pitest

State of the art mutation testing system for the JVM
Apache License 2.0
1.69k stars 357 forks source link

Final local variable not mutating with Invert Negatives mutator #292

Open Kwaq opened 8 years ago

Kwaq commented 8 years ago

Is Invert Negatives Mutator supposed to work only on method params? It looks like it works on local variables, but only if they are not final. This works:

  public int containsINeg() {
      int i = 1;
      return -i;
    }

but this is not

  public int containsINeg() {
      final int i = 1;
      return -i;
    }

Test case at my repo 501463d7ba6efe6aaea09ff7f98eac667eefd7ac

hcoles commented 8 years ago

The 2nd version will not generate an INEG instruction for pitest to mutate.

The final keyword enables the compiler to definitively infer the value i will have, so it can replace the code with the equivalent

public int containsINeg() {
      return -1;
}

The mutator could be expanded to also negate the sign of any constants, but I'm not sure this would be a good idea.

I don't usually enable the existing constants mutator myself as it generates a lot of noise - this would suffer the same problem. If we were to negate constants it would be best implemented as a separate mutator that could be optionally enabled.

Kwaq commented 8 years ago

@hcoles Thanks for the explanation.