unittesting generic functions

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


On 8/14/14, 3:34 PM, H. S. Teoh via Digitalmars-d wrote:
> How does this relate to writing generic unittests? Since the incoming
> types are generic, you can't assume anything about them beyond what the
> function itself already assumes, so how would the unittest test anything
> beyond what the function already does?

Check the workings of the function via an alternate algorithm for 
example. There's plenty of examples, including unittests inside a 
parameterized struct/class test methods within that class.

> For example, if the function performs x+y on two generic arguments x and
> y, how would a generic unittest check whether the result is correct,
> since you can't assume anything about the concrete values of x and y?
> The only way the unittest can check the result is to see if it equals
> x+y, which defeats the purpose because it's tautological with what the
> function already does, and therefore proves nothing at all.

We could ask the same question about x+y for int in particular: it's a 
primitive so there's not much to test.

This does bring up the interesting point that we need a way to generate 
random values of generic types. 
http://www.haskell.org/haskellwiki/Introduction_to_QuickCheck1 comes to 
mind.


Andrei



More information about the Digitalmars-d mailing list