Note that there are some explanatory texts on larger screens.

plurals
  1. POOff By One errors and Mutation Testing
    primarykey
    data
    text
    <p>In the process of writing an "Off By One" mutation tester for my favourite mutation testing framework (<a href="http://ninjaturtles.codeplex.com" rel="nofollow noreferrer">NinjaTurtles</a>), I wrote the following code to provide an opportunity to check the correctness of my implementation:</p> <pre><code>public int SumTo(int max) { int sum = 0; for (var i = 1; i &lt;= max; i++) { sum += i; } return sum; } </code></pre> <p>now this seems simple enough, and it didn't strike me that there would be a problem trying to mutate all the literal integer constants in the IL. After all, there are only 3 (the <code>0</code>, the <code>1</code>, and the <code>++</code>). </p> <p><strong>WRONG!</strong></p> <p>It became very obvious on the first run that it was never going to work in this particular instance. Why? Because changing the code to </p> <pre><code>public int SumTo(int max) { int sum = 0; for (var i = 0; i &lt;= max; i++) { sum += i; } return sum; } </code></pre> <p>only adds 0 (zero) to the sum, and this obviously has no effect. Different story if it was the multiple set, but in this instance it was not.</p> <p>Now there's a fairly easy algorithm for working out the sum of integers</p> <pre><code>sum = max * (max + 1) / 2; </code></pre> <p>which I could have fail the mutations easily, since adding or subtracting 1 from either of the constants there will result in an error. (given that <code>max &gt;= 0</code>)</p> <p>So, problem solved for this particular case. Although it did not do what I wanted for the test of the mutation, which was to check what would happen when I lost the <code>++</code> - effectively an infinite loop. But that's another problem.</p> <p><strong>So - My Question:</strong> Are there any trivial or non-trivial cases where a loop starting from 0 or 1 may result in a "mutation off by one" test failure that cannot be refactored (code under test or test) in a similar way? (examples please)</p> <p><strong><em>Note</strong>: Mutation tests fail when the test suite passes after a mutation has been applied.</em></p> <p><strong>Update:</strong> an example of something less trivial, but something that could still have the test refactored so that it failed would be the following</p> <pre><code>public int SumArray(int[] array) { int sum = 0; for (var i = 0; i &lt; array.Length; i++) { sum += array[i]; } return sum; } </code></pre> <p>Mutation testing against this code would fail when changing the <code>var i=0</code> to <code>var i=1</code> if the test input you gave it was <code>new[] {0,1,2,3,4,5,6,7,8,9}</code>. However change the test input to <code>new[] {9,8,7,6,5,4,3,2,1,0}</code>, and the mutation testing will fail. So a successful refactor proves the testing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload