unittesting generic functions

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 14 17:51:58 PDT 2014


On 8/14/14, 3:37 PM, Jonathan M Davis wrote:
> 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.

We need a way to generate correct but random values generically.

> 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.

My thesis here is, if you get to instantiate it you must be able to 
trigger some testing for it.

> 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.

Agreed.

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

I feel this is fertile ground. We should think more about it.


Andrei



More information about the Digitalmars-d mailing list