What version of assert should I use, and CPPUNIT

Started by
7 comments, last by Sneftel 17 years, 7 months ago
AFAIK there is no std::assert. Should I just use MSDN's version that came with my compiler? Or is there a better verseion out there? Also, has anyone here ever used cppunit?. I think I am good programmer in most areas, except that of bug testing! I am trying to become better, thus I am researching many methods.
Advertisement
There is no std::assert because assert is implemented as a macro, not a function. It's one of the uglinesses of C++ that things like that can't be done well without reverting to the features of C which are supposedly deprecated. Don't worry, though; the assert() that ships with MSVC is totally standard.
Thanks for the Info Sneftel.
You could make an assert method. This has worked for me :)
Unit testing is a very important part of the development process. I've used CPPUnit. I like it.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by Sneftel
It's one of the uglinesses of C++ that things like that can't be done well without reverting to the features of C which are supposedly deprecated.


Which feature does the assert macro use that is "supposedly deprecated"? I don't think assert or its equivalent can be done in any compiled language without using a macro facility. You need something to convert the expression to a string, and something to provide the file and line numbers.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by Adam Hamilton
You could make an assert method. This has worked for me :)


One of the features of the macro assert() is that it is completely removed in release mode, guaranteeing zero overhead. An assert function will always pay the cost of evaluating the expression (unless the compiler is very smart, or the expression is very simple).

Quote:Original post by JohnBolton
I don't think assert or its equivalent can be done in any compiled language without using a macro facility.


C#'s Debug.Assert() gets away with it without using macros, but some other sort of attribute magic :)

Quote:Or is there a better verseion out there?

For 'normal' applications I've found the default MSVC assert() perfectly fine, but it can cause lockups if you're doing anything tricky. I often define my own as something like

#define assert(X) if (X) {} else __asm int 3

so that it acts like a simple breakpoint, but sometimes something more complicated is required if you're trying to debug a full screen 3D game.
Quote:Original post by hh10k

#define assert(X) if (X) {} else __asm int 3

so that it acts like a simple breakpoint, but sometimes something more complicated is required if you're trying to debug a full screen 3D game.


Beautiful...
Quote:Original post by JohnBolton
Which feature does the assert macro use that is "supposedly deprecated"? I don't think assert or its equivalent can be done in any compiled language without using a macro facility. You need something to convert the expression to a string, and something to provide the file and line numbers.

Of course. But it flies in the face of the oft-repeated assertion that you shouldn't use #define in C++, doncha think?

This topic is closed to new replies.

Advertisement