Re: examples on webpage.
Bart Whiteley <[email protected]> Wed, 12 Feb 2003 16:32:49 -0800
| Newsgroups | gmane.comp.lib.cppunit.devel |
|---|---|
| Message-ID | <[email protected]> |
Actually, Thomas has a good point. The cookbook example has several problems that cause it to not compile. Read the comments within my attachments to see where the cookbook led you astray. Build like this: g++ -c testrunner.cpp g++ -c complexTestSuite.cpp g++ -o testrunner testrunner.o complexTestSuite.o -lcppunit Then just run ./testrunner You may need -L or -I flags to g++ if you installed cppunit someplace non-standard. Hope this helps. -- Bart Whiteley Computer Scientist voice: (925) 423-2249 National Atmospheric Release Advisory Center FAX: (925) 423-8274 Lawrence Livermore National Laboratory email: [email protected] P.O. Box 808, Livermore, CA 94551-0808 MS: L-103
testrunner.cpp
(text/plain, 363 B)
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main( int argc, char **argv)
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( registry.makeTest() );
bool wasSucessful = runner.run( "", false );
return wasSucessful;
}
complexTestSuite.cpp
(text/plain, 1.5 KB)
#include <cppunit/extensions/HelperMacros.h>
class Complex {
friend bool operator ==(const Complex& a, const Complex& b);
double real, imaginary;
public:
Complex( double r, double i = 0 )
: real(r)
, imaginary(i)
{
}
// NOTE: not in cookbook.
Complex operator+(const Complex& rhs)
{
return Complex(real + rhs.real, imaginary + rhs.imaginary);
}
};
bool operator ==( const Complex &a, const Complex &b )
{
// NOTE: different from cookbook.
return ( a.real == b.real ) && ( a.imaginary == b.imaginary );
}
class ComplexNumberTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( ComplexNumberTest );
CPPUNIT_TEST( testEquality );
CPPUNIT_TEST( testAddition );
CPPUNIT_TEST_SUITE_END();
private:
Complex *m_10_1, *m_1_1, *m_11_2;
// NOTE: bookbook has setUp() and tearDown() protected.
// They should be public.
public:
void setUp()
{
m_10_1 = new Complex( 10, 1 );
m_1_1 = new Complex( 1, 1 );
m_11_2 = new Complex( 11, 2 );
}
void tearDown()
{
delete m_10_1;
delete m_1_1;
delete m_11_2;
}
protected:
void testEquality()
{
CPPUNIT_ASSERT( *m_10_1 == *m_10_1 );
CPPUNIT_ASSERT( !(*m_10_1 == *m_11_2) );
}
void testAddition()
{
CPPUNIT_ASSERT( *m_10_1 + *m_1_1 == *m_11_2 );
}
};
// NOTE. this was wrong in the cookbook. should be ComplexNumberTest,
// not ComplexNumber.
// NOTE: cookbook implies this can go at top of file. I actually needs to
// be after the declaration of ComplexNumberText.
CPPUNIT_TEST_SUITE_REGISTRATION( ComplexNumberTest );