Ah, simple solution to unittests inside templates

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 19 06:34:04 PDT 2016


On 9/18/16 8:14 AM, Andrei Alexandrescu wrote:
> On 9/18/16 6:00 AM, Jonathan M Davis via Digitalmars-d wrote:
>> Yes. That's DIP 82:
>>
>> http://wiki.dlang.org/DIP82
>>
>> I need to go over it again and then introduce it into the new DIP
>> process.
>> But I really think that that's where we should go to fix this problem.
>
> Just a thought: things that we can't do have high priority. Things that
> we can do with a modest cost are much less attractive. Consider:
>
> struct Awesome(A, B, C)
> {
>     private enum ut = is(A == int) && is(B == int) && is(C == int);
>
>     static if (ut) unittest
>     {
>         ...
>     }
> }
>
> You're looking at an overhead with a small fixed cost plus a few
> characters ("if (ut)") per unittest.

This is exactly how I did RedBlackTree unit tests:

https://github.com/dlang/phobos/blob/master/std/container/rbtree.d#L748
https://github.com/dlang/phobos/blob/master/std/container/rbtree.d#L815

This is still less than ideal, and has caused some real problems over 
the years. e.g.:

https://issues.dlang.org/show_bug.cgi?id=12246
https://issues.dlang.org/show_bug.cgi?id=14082

The best solution IMO is not to run templated unit tests unless 
specifically requested. Perhaps only run them when instantiated inside a 
unittest block?

All that being said, running unit tests on EVERY integral type caught 
about 3 bugs in the compiler when I was creating dcollections, and found 
several corner-case bugs in my code as well. It was well worth the 
"overhead" IMO.

-Steve


More information about the Digitalmars-d mailing list