Additional assert macros
Brought to you by:
blep
Add a CPPUNIT_ASSERT_NOT_NULL macro:
In addition to the other assert macros a simple one to
test that a pointer is not null would be nice. Could
add a corresponding assert that a pointer is a null.
Add a CPPUNIT_ASSERT_STRING_EQUALS macro
Also a test for string equality would be helpful.
Logged In: YES
user_id=651057
Try this diff for ASSERT_NOT_NULL ASSERT_IS_NULL
--- ./examples/cppunittest/TestAssertTest.cpp.orig 2002-
11-26 11:35:21.000000000 +1100
+++ ./examples/cppunittest/TestAssertTest.cpp 2002-
11-26 11:44:48.000000000 +1100
@@ -52,20 +52,43 @@
}
static int foo() { return 1; }
+static void* bar() { return &foo; }
+static void* nullbar() { return (void*)0; }
void
TestAssertTest::testAssertEqual()
{
CPPUNIT_ASSERT_EQUAL( 1, 1 );
CPPUNIT_ASSERT_EQUAL( 1, foo() );
}
void
+TestAssertTest::testAssertNotNull()
+{
+ int dummy;
+ void* ptr = &dummy;
+ CPPUNIT_ASSERT_NOT_NULL( this );
+ CPPUNIT_ASSERT_NOT_NULL( &dummy );
+ CPPUNIT_ASSERT_NOT_NULL( ptr );
+ CPPUNIT_ASSERT_NOT_NULL( bar() );
+}
+
+
+void
+TestAssertTest::testAssertIsNull()
+{
+ void* ptr = 0;
+ CPPUNIT_ASSERT_IS_NULL( ptr );
+ CPPUNIT_ASSERT_IS_NULL( nullbar() );
+}
+
+
+void
TestAssertTest::testAssertMessageTrue()
{
CPPUNIT_ASSERT_MESSAGE( "This test should not
failed", true );
}
--- ./examples/cppunittest/TestAssertTest.h.orig 2002-
11-26 11:17:50.000000000 +1100
+++ ./examples/cppunittest/TestAssertTest.h 2002-
11-26 11:19:29.000000000 +1100
@@ -8,10 +8,12 @@
{
CPPUNIT_TEST_SUITE( TestAssertTest );
CPPUNIT_TEST( testAssertTrue );
CPPUNIT_TEST_FAIL( testAssertFalse );
CPPUNIT_TEST( testAssertEqual );
+ CPPUNIT_TEST( testAssertNotNull );
+ CPPUNIT_TEST( testAssertIsNull );
CPPUNIT_TEST( testAssertMessageTrue );
CPPUNIT_TEST( testAssertMessageFalse );
CPPUNIT_TEST( testAssertDoubleEquals );
CPPUNIT_TEST_FAIL( testAssertDoubleNotEquals1 );
CPPUNIT_TEST_FAIL( testAssertDoubleNotEquals2 );
@@ -31,10 +33,13 @@
void testAssertTrue();
void testAssertFalse();
void testAssertEqual();
+ void testAssertNotNull();
+ void testAssertIsNull();
+
void testAssertMessageTrue();
void testAssertMessageFalse();
void testAssertDoubleEquals();
void testAssertDoubleNotEquals1();
--- ./include/cppunit/TestAssert.h.orig 2002-11-26
10:28:23.000000000 +1100
+++ ./include/cppunit/TestAssert.h 2002-11-26
11:52:42.000000000 +1100
@@ -127,10 +127,47 @@
( ::CppUnit::Asserter::failIf( !(condition), \ "", \ CPPUNIT_SOURCELINE() ) )
#endif
+
+/** Assertions that a condition results in a result which is \c
not null.
+ * \ingroup Assertions
+ */
+#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
+#ifndef CPPUNIT_CONTACT
+#define CPPUNIT_CONCAT1(A,B) A##B
+#define CPPUNIT_CONCAT(A,B) CPPUNIT_CONCAT1(A, B)
+#endif
+#define CPPUNIT_ASSERT_NOT_NULL
(condition) \ + ( ::CppUnit::Asserter::failIf( (condition) == (void*)0, \ + CPPUNIT_CONCAT(#condition," !=
(void*)0)"), \ + CPPUNIT_SOURCELINE() ) )
+#else
+#define CPPUNIT_ASSERT_NOT_NULL
(condition) \ + ( ::CppUnit::Asserter::failIf( (condition) == (void*)0, \ + "", \ + CPPUNIT_SOURCELINE() ) )
+#endif
+
+
+/** Assertions that a condition results in a result which is \c
null.
+ * \ingroup Assertions
+ */
+#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
+#define CPPUNIT_ASSERT_IS_NULL(condition) \ + ( ::CppUnit::Asserter::failIf( (condition) != (void*)0, \ + CPPUNIT_CONCAT(#condition," ==
(void*)0)"), \ + CPPUNIT_SOURCELINE() ) )
+#else
+#define CPPUNIT_ASSERT_IS_NULL(condition) \ + ( ::CppUnit::Asserter::failIf( (condition) != (void*)0, \ + "", \ + CPPUNIT_SOURCELINE() ) )
+#endif
+
+
/** Assertion with a user specified message.
* \ingroup Assertions
* \param message Message reported in diagnostic if \a
condition evaluates
* to \c false.
* \param condition If this condition evaluates to \c false then
the