On Thu, Mar 14, 2013 at 07:00:47PM +0100, Dicebot wrote:
> On Thursday, 14 March 2013 at 17:26:18 UTC, Andrei Alexandrescu
> wrote:
> >Walter has measured coverage of Phobos unittests a couple of times,
> >it's very high. But I agree it would be nice to have it as a target.
> >
> >Andrei

Beware the fallacy that 100% line coverage of a template == 100%
coverage of correctness in *every possible instantiation*.

Just because template X has 100% line coverage for !(int,int) does not
necessarily mean it will work for !(int,string), for example. Or
!(int,const(int)). Or !(const(int),int). Or any number of subtle but
sometimes important combinations.


> Sarcasm aside, this brought me to an idea for utilities for template
> fuzzy testing  to check some instantiation combinations. Which is much
> more useful than plain line coverage check in case of templates. I
> have submitted pull request only a few weeks ago that fixed std.traits
> bug where template has failed to instantiate for function types (but
> not other types) despite 100% line coverage by unit test. It could
> have been checked automagically.
> 
> Not sure what usage interface may be though.

I've written code like this before, though it definitely can use some
refinement:

        struct MySet(ElemType) { ... }
        unittest {
                void test(T)(T sampleArgs) {
                        auto set = MySet!T(sampleArgs);
                        assert( ... /* test for MySet behaviour here */);
                }
                void testNumArray(T, U...)() {
                        T[] data = [1,2,3,4,5];
                        test(data);
                        testNumArray!(U);
                }
                void testCharArray(T, U...)() {
                        T[] data = "abc";
                        test(data);
                        testCharArray!(U);
                }

                testNumArray!(byte, ubyte, int, uint, long, ulong)();
                testCharArray!(char, wchar, dchar, const(char),
                        const(wchar), const(dchar), immutable(char),
                        immutable(wchar), immutable(dchar))();

                // Coverage is still not really complete; we'd need to
                // add struct and class, and nested arrays, etc..
        }

The fact that D actually lets you do this, is quite awesome.


T

-- 
He who laughs last thinks slowest.

Reply via email to