unittesting generic functions
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Thu Aug 14 15:34:13 PDT 2014
On Thu, Aug 14, 2014 at 02:34:02PM -0700, Andrei Alexandrescu via Digitalmars-d wrote:
[...]
> Clearly unittests that mock up arguments etc. are a useful device for
> unittesting. But the point of generic unittests is a tad different and
> has to do with a fundamental difference between C++'s and D's approach
> to generics.
>
> In C++ it's entirely acceptable to fail to instantiate a template. In
> fact the C++ idiom SFINAE is literally based on that - "Substitution
> Failure Is Not An Error". There's a bunch of detail to it but bottom
> line it's totally fine for a template to fail to instantiate, both
> technically and socially (e.g. you get syntax errors in the template
> code etc). There's a good amount of dissatisfaction in the C++
> community about that, which has led to a lot of work on concepts.
>
> In D, it's not acceptable to fail to instantiate; a template should
> either instantiate and work, get filtered out by a template
> constraint, or fail with information by means of a static assert.
> Random syntax errors inside the template are considered poor style.
>
> 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.
[...]
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?
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.
T
--
"640K ought to be enough" -- Bill G. (allegedly), 1984. "The Internet is not a primary goal for PC usage" -- Bill G., 1995. "Linux has no impact on Microsoft's strategy" -- Bill G., 1999.
More information about the Digitalmars-d
mailing list