Unit tests and verifying pre/post-conditions and invariants

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 16 05:43:08 PDT 2010


On Sat, 14 Aug 2010 23:49:18 -0400, Jonathan M Davis  
<jmdavisprog at gmail.com> wrote:

> Is there a standard and/or acceptable way to make sure that  
> pre-conditions,
> post-conditions, or invariants _fail_ when running unit tests? That is,  
> lets say
> I had a function like this
>
> void func(int x)
> in
> {
>     assert(x < 8);
> }
> body
> {
>   //...
> }
>
>
> and I wanted to test to make sure that func() couldn't be called with  
> any int
> greater or equal to 8, what would I do?

Hm... unit testing your unit tests :)

input contracts and unit tests are supposed to be simple, provable code so  
you don't have to test them.  The above function is obviously a simple  
example, you don't really need to unit test it (right?), so what would a  
complicated in contract look like?

It's also a good idea to avoid complex expressions in unit tests.  If you  
have a complex expression, split it out into several lines to avoid having  
to work through the logic in your head.  Performance/minimal LOC is not a  
goal you need in unit tests/contracts.

-Steve


More information about the Digitalmars-d-learn mailing list