In the process of writing the mutation tester "Off By One" for my favorite mutation testing environment ( NinjaTurtles ), I wrote the following code to enable me to verify the correctness of my implementation:
public int SumTo(int max) { int sum = 0; for (var i = 1; i <= max; i++) { sum += i; } return sum; }
now this seems simple enough, and it didn't seem to me that there would be a problem trying to change all literal integer constants in IL. In the end, only 3 ( 0 , 1 and ++ ).
WRONG!
In the first run, it became apparent that in this particular case it would never work. What for? Since changing the code to
public int SumTo(int max) { int sum = 0; for (var i = 0; i <= max; i++) { sum += i; } return sum; }
only adds 0 (zero) to the sum, and this obviously has no effect. A different story if it was a multiple set, but in this case it is not.
Now there is a fairly simple algorithm for calculating the sum of integers
sum = max * (max + 1) / 2;
which I could easily knock down mutations, since adding or subtracting 1 from any of the constants will lead to an error. (given that max >= 0 )
So, the problem is solved for this particular case. Although he did not do what I wanted for a mutation test, which was to check what would happen when I lose ++ , a virtually infinite loop. But this is another problem.
So - My Question: Are there any trivial or non-trivial cases when a cycle starting with 0 or 1 can lead to a "mutation from one" error that cannot be reorganized (test code or test) in the same way? (please examples)
Note Mutation tests fail when a test package passes after applying a mutation.
Update: an example of something less trivial, but something that could still be refactored for the test to fail, would be as follows
public int SumArray(int[] array) { int sum = 0; for (var i = 0; i < array.Length; i++) { sum += array[i]; } return sum; }
Checking mutations for this code did not work when changing var i=0 to var i=1 if the test input you gave it was new[] {0,1,2,3,4,5,6,7,8,9} . However, change the test input to new[] {9,8,7,6,5,4,3,2,1,0} and mutation testing will fail. Thus, a successful refactor proves testing.