Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your algorithms probably have a random component. Bring it under control. </p> <p>You can either</p> <ol> <li>Allow the caller to choose the seed for the random number generator. Then use a hardcoded seed in the tests.</li> <li>Have the caller provide a random number generator. Then use a fake random number generator in the tests.</li> </ol> <p>The second option is probably the best, since that will make it easier for you to reason what the correct result of the algorithm is.</p> <p>When unit-testing algorithms, what you want to verify is that you have correctly implemented the algorithm. Not whether the algorithm does what it is supposed to do. Unit-tests should not treat the code-under-test as a black box.</p> <p>You may want to have a separate "performance"-test to compare how different algorithms perform (and whether they actually work), but your unit-tests are really for testing your <em>implementation</em> of the algorithm.</p> <p>For example, when implementing the Foo-Bar-Baz Optimization Algorithm (TM) you might have accidentally written x:=x/2 instead of x:=x/3. This might mean that the algorithm works slower, but still finds the same algorithm. You will need white-box-testing to find such an error.</p> <p><strong>Edit:</strong></p> <blockquote> <p>Unfortunately I cannot choose a fixed seed for the random number generator, because I do not want to test if the algorithm produces the exact same result as before, but I want to test something like "With 90% certainty I get a result with 0.1 or better".</p> </blockquote> <p>I can not see any way to make a test that is both automatic-verifiable and stochastic. Especially not if you want to have any chance of distinguishing real errors from statistic noise.</p> <p>If you want to test "With 90% certainty I get a result with 0.1 or better", I would suggest something like:</p> <pre><code>double expectedResult = ...; double resultMargin = 0.1; int successes = 0; for(int i=0;i&lt;100;i++){ int randomSeed = i; double result = optimizer.Optimize(randomSeed); if(Math.Abs(result, expectedResult)&lt;resultMargin) successes++; } Assert.GreaterThan(90, successes); </code></pre> <p>(Note that this test is deterministic).</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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