September 19, 2016 Re: Ah, simple solution to unittests inside templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 9/18/16 8:14 AM, Andrei Alexandrescu wrote: > On 9/18/16 6:00 AM, Jonathan M Davis via Digitalmars-d wrote: >> Yes. That's DIP 82: >> >> http://wiki.dlang.org/DIP82 >> >> I need to go over it again and then introduce it into the new DIP >> process. >> But I really think that that's where we should go to fix this problem. > > Just a thought: things that we can't do have high priority. Things that > we can do with a modest cost are much less attractive. Consider: > > struct Awesome(A, B, C) > { > private enum ut = is(A == int) && is(B == int) && is(C == int); > > static if (ut) unittest > { > ... > } > } > > You're looking at an overhead with a small fixed cost plus a few > characters ("if (ut)") per unittest. This is exactly how I did RedBlackTree unit tests: https://github.com/dlang/phobos/blob/master/std/container/rbtree.d#L748 https://github.com/dlang/phobos/blob/master/std/container/rbtree.d#L815 This is still less than ideal, and has caused some real problems over the years. e.g.: https://issues.dlang.org/show_bug.cgi?id=12246 https://issues.dlang.org/show_bug.cgi?id=14082 The best solution IMO is not to run templated unit tests unless specifically requested. Perhaps only run them when instantiated inside a unittest block? All that being said, running unit tests on EVERY integral type caught about 3 bugs in the compiler when I was creating dcollections, and found several corner-case bugs in my code as well. It was well worth the "overhead" IMO. -Steve |
September 19, 2016 Re: Ah, simple solution to unittests inside templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sat, Sep 17, 2016 at 01:22:52PM -0400, Andrei Alexandrescu via Digitalmars-d wrote: > Recall the discussion a few days ago about unittests inside templates being instantiated with the template. Often that's desirable, but sometimes not - for example when you want to generate nice ddoc unittests and avoid bloating. For those cases, here's a simple solution that I don't think has been mentioned: I've already mentioned this exact idea recently in the d-learn forum. Nobody responded then. > /** Awesome struct */ > struct Awesome(T) > { > /** Awesome function. */ > void awesome() {} > > /// > static if (is(T == int)) unittest > { > Awesome awesome; > awesome.awesome; > } > } > > The unittest documentation is nicely generated. The unittest code itself is only generated for one instantiation. [...] And you also have to make sure Awesome!int is actually instantiated, otherwise the unittest won't actually run! T -- In theory, there is no difference between theory and practice. |
September 19, 2016 Re: Ah, simple solution to unittests inside templates | ||||
---|---|---|---|---|
| ||||
On Mon, Sep 19, 2016 at 09:02:05AM -0700, H. S. Teoh via Digitalmars-d wrote: > On Sat, Sep 17, 2016 at 01:22:52PM -0400, Andrei Alexandrescu via Digitalmars-d wrote: > > Recall the discussion a few days ago about unittests inside templates being instantiated with the template. Often that's desirable, but sometimes not - for example when you want to generate nice ddoc unittests and avoid bloating. For those cases, here's a simple solution that I don't think has been mentioned: > > I've already mentioned this exact idea recently in the d-learn forum. Nobody responded then. [...] Correction: it was on this very same forum: http://forum.dlang.org/post/mailman.96.1472754654.2965.digitalmars-d@puremagic.com T -- May you live all the days of your life. -- Jonathan Swift |
September 22, 2016 Re: Ah, simple solution to unittests inside templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 18 September 2016 at 12:16:54 UTC, Andrei Alexandrescu wrote: > I don't see that as much of a hurdle seeing as any template written has a few "obvious" types it'll work with. To encapsulate that if needed: > > struct Awesome(A, B, C) > { > private enum ut = is(A == int) && is(B == int) && is(C == int); > unittest { alias Awe = Awesome!(int, int, int); } > > static if (ut) unittest > { > ... > } > } I like how the non-static unittest ensures that any instantiation of Awesome triggers the static unittest. I've encapsulated this pattern into a template: https://github.com/ntrel/stuff/blob/master/testinstanceflag.d#L34 Awesome would then just need one line instead of two before the 'static if' line: private enum ut = testInstanceFlag!(Awesome, int, int, int); (testInstanceFlag currently only works with one argument though). The nice thing is that people reading the source can look up the docs for testInstanceFlag to learn about it. |
Copyright © 1999-2021 by the D Language Foundation