CppUnit: Implementation of CPPUNIT_ASSERT: bug or feature?

Stephan Bergmann sbergman at redhat.com
Thu Apr 27 07:42:11 UTC 2017


In include/cppunit/TestAssert.h, CPPUNIT_ASSERT is effectively defined as

> #define CPPUNIT_ASSERT(condition) CppUnit::Asserter::failIf(!(condition), ...)

So if the expression 'condition' is such that an overloaded operator ! 
applies, then that will be used instead of the expected(?) built-in !.

Should this be considered a bug or a feature?  I am somewhat undecided.

On the one hand, CPPUNIT_ASSERT is used to assert that something is 
"true".  That it uses double negation (failIf, !) for that should 
probably be considered an implementation detail.  And "arbitrary" 
overloaded operator ! shouldn't get into the way of that.

On the other hand, changing this like

> #define CPPUNIT_ASSERT(condition) CppUnit::Asserter::failIf(!(bool(condition)), ...)

could cause existing code to fail.  Either fail to compile (if the 
relevant type cannot be converted to bool) or, worse, cause the 
semantics of the test to change (if the original code relied on a 
specific overloaded operator ! being called).

(I came across this in LO, where cppcanvas/qa/unit/test.cxx contains

> CPPUNIT_ASSERT( aBitmapEx.GetAlpha() );

and the condition is of type AlphaMask that has an overloaded operator ! 
-- but no conversion to bool, which is probably a design bug of its own.)


More information about the LibreOffice mailing list