unittesting generic functions

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 14 15:37:22 PDT 2014


On Thursday, 14 August 2014 at 21:34:00 UTC, Andrei Alexandrescu 
wrote:
> It follows that once a D template gets instantiated, it's 
> supposed to work as expected for the entire range of types it 
> was meant to.

Yes, but in my experience, it's rarely the case that you can 
actually test a template generically, because construction is not 
generic, so the values to use are hard to generate generically, 
and then creating the values to test the results against are 
frequently just as hard. Where you can test something 
generically, it's great, but it's rarely practical from what I've 
seen. We should support it regardless (and we do currently by 
separating the template declaration from the function 
declaration), and adding the shorter syntax for it that you're 
suggesting may be a good idea, but I really don't see these types 
of tests as being effective in most cases even though it's 
fantastic when you can write such tests.

Also, you still have to specifically instantiate functions like 
this outside of the unittest block that you're proposing - 
particularly if this is in a library, because in that case, the 
function may not actually be used outside of its tests, and the 
unit tests aren't going to run when someone links in the library. 
And if that's the case, you arguably might as well just write 
tests for each of the individual types.

So, ultimately, while these sort of tests can be good, I really 
don't think that they're effective very often.

- Jonathan M Davis


More information about the Digitalmars-d mailing list