assert(condition[, message]) patch

Brad Roberts braddr at puremagic.com
Wed May 31 10:42:10 PDT 2006


On Wed, 31 May 2006, Walter Bright wrote:

> braddr at puremagic.com wrote:
> > I have been toying with D to bring myself up to speed and I found myself
> > writing
> > a lot of unit tests, always a good thing.  In those unit tests I found
> > myself
> > writing a lot of blocks like:
> > 
> > if (cond)
> > {
> > writefln("some debugging output to make my life easier");
> > assert(false);
> > }
> > 
> > I know many don't like unit tests to have output, but I do.
> 
> Why not:
> 
> 	assert(!cond); // some debugging output to make my life easier
> 
> ? You'll need to go look at the source anyway when the assert trips, so what
> advantage is there to print the comment?

Often it saves one cycle through the debugger by the expediency of having 
actual information about what caused the assert, more than just 'something 
broke here' that a plain assertion violation causes today.

Take a look at some code I wrote where the unit tests use this feature:

  http://www.puremagic.com/~braddr/d/interned_string.d

A comment can't convey state, just intent.

I've reapplied the referenced patch to dmd/gdc and it works when used 
properly.  However, exactly like it was before, it's got a number of bugs 
when used improperly.  I need to learn more about dmd and gdc both to fix 
the thing to be robust enough to actually merge in.

Later,
Brad



More information about the Digitalmars-d mailing list