Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You cannot fully avoid that, but you can keep things organized. Do it Eiffel-ish.

At the start of your function, check all the prerequisites, e.g:

   if(x<0) throw "x should be non-negative. Got $x"
   if(x>=n)throw "x should be smaller than n. Got $x and $n"
Add tests for edge conditions that do not throw, but, say, increase a global counting edge conditions hit:

  if(x=0) edgeConditionsHit += 1
  if(x=n) edgeConditionsHit += 1
Then, write tests so that you hit all paths in the condition tests.

If that doesn't hit 100% in the rest of the function, the function has code it doesn't need, or your precondition checks aren't complete.

Think about other edge conditions. For example, does your code special-case x=n/2? Add a check on top. And yes, that is implementation-specific, but there is nothing you can do about that.

Of course, you don't need the edge condition and implementation-specific checks in release builds.

With these in hand, you can also split tests into implementation-specific ones and contract-based ones.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: