std.unittests for (final?) review

Jonathan M Davis jmdavisProg at gmx.com
Mon Jan 3 03:09:05 PST 2011


On Monday 03 January 2011 02:39:59 Jens Mueller wrote:
> Jonathan M Davis wrote:
> > On Monday 03 January 2011 01:38:29 Vladimir Panteleev wrote:
> > > On Mon, 03 Jan 2011 06:44:50 +0200, Jonathan M Davis
> > > <jmdavisProg at gmx.com>
> > > 
> > > wrote:
> > > > So, please have a look at the code.
> > > 
> > > Just one thing: wouldn't these functions also be useful in contract
> > > programming (invariants etc.)? Perhaps they should just be added to
> > > std.exception?
> > 
> > Well, they're written with unit tests in mind rather than contracts. Unit
> > tests and contracts have similar but different purposes. I wouldn't
> > really want to conflate the two. Also, it's std.exception now rather
> > than std.contracts specifically because it doesn't really relate to
> > contracts, so even if you did want to use unit testing functions in
> > contracts, I'm not sure that std.exception is the right place to put
> > such functions anyway.
> > 
> > As it stands, the entire module is in a version(unittest) block, so the
> > functions can't be used outside of unit tests.
> > 
> > If a lot of people really thought that unit testing functions would be
> > useful and reasonable to use inside of contracts, then it would make
> > some sense for the unit testing functions to no longer be in a
> > version(unittest) block. But personally, I'd be a bit worried to see
> > more than simple assertions inside of contracts. Contracts should be
> > efficient, since they're going to be run while the code is running
> > normally, whereas unit tests don't have to worry about efficiency quite
> > as much. I believe that the functions that I have are efficient, but I
> > can easily see functions intended to aid in unit testing being totally
> > inappropriate in a contract due to efficiency concerns. And of course, a
> > number of the unit testing functions just don't make sense in contracts
> > regardless - like assertExThrown.
> 
> What do you mean by "running normally"? I think since they are compiled
> away with -release they are not run normally.

I mean when the program is running. If you're running a debug version of your 
program (which is perfectly normal and common), the contracts are going to run. 
They're _supposed_ to run while the program is running. That's the point. 
Naturally, you compile them out for release, but contracts need to be written in 
way that the program works just fine with them compiled in.

Unit tests, on the other hand, don't run while the program is running. They're 
run once when the program starts up (assuming that -unittest was used), and then 
they're done. You don't want them to be super slow, but they're not getting 
called multiple times or running while the program is, so they don't have to be 
as efficient.

- Jonathan M Davis


More information about the Digitalmars-d mailing list