Is it a bug in unittest ?

Lodovico Giaretta via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 1 11:22:30 PDT 2016


On Thursday, 1 September 2016 at 18:11:55 UTC, angel wrote:
> If one creates a unittest block in a templated class (or 
> struct), the unittest block will be multiplied per class 
> specialization, which might turn out to be quite large number.
>
> E.g.
>  struct A(T) {
>      ...
>      unittest {
>          ...
>      }
>  }
>
>  ...
>
>  auto a = A!int;
>  auto b = A!int;
>  auto c = A!double;
>
> The unittest block will be instantiated twice (per A!int and 
> A!double).
> But in some (many ?) cases unittest doesn't even exercise the 
> generics, merely using some particular type.
>
> What is it, a bug ?

I think this is inteded behaviour. If the unittest is not 
dependent on the current instantiation, you can move it outside 
the template. By the way, it is common practice to put unittests 
after, and not inside, the tested struct/class.

That is:

===========

struct A(T)
{
     unittest
     {
         A a; // uses current instantiation; repeated for every 
instantiation
     }
}
unittest
{
     A!int b;        // tests specific instantiations
     A!double c;     // not repeated
}
===========


More information about the Digitalmars-d mailing list