Online Book Reader

Home Category

Professional C__ - Marc Gregoire [448]

By Root 1277 0
\

-ldl -std=C++0x *.cpp

See Bonus Chapter 2 on the website (www.wrox.com) for more information on the -std=c++0x flag.

This command is for a default cppunit installation on Linux using the GCC compiler. If you are working on another platform or with another compiler, please consult the cppunit website for instructions. After the code is compiled, linked, and run, you should see output similar to the following:

OK (1 tests)

If you modify the code to assert that 1 < 0, the test will fail; cppunit will report the failure as follows:

!!!FAILURES!!!

Test Results:

Run: 1 Failures: 1 Errors: 0

1) test: testSimple (F) line: 21 ObjectPoolTest.cpp

assertion failed

- Expression: 1 < 0

Note that by using the CPPUNIT_ASSERT macro, the framework was able to pinpoint the exact line on which the test failed — a useful piece of information for debugging.

Adding the Real Tests

Now that the framework is all set up and a simple test is working, it’s time to turn your attention to the ObjectPool class and write some code that actually tests it. All of the following tests will be added to ObjectPoolTest.h and ObjectPoolTest.cpp, just like the earlier simple test.

Before you can write the tests, you’ll need a helper object to use with the ObjectPool class. The ObjectPool creates chunks of objects of a certain type and hands them out to the caller as requested. Some of the tests will need to check if a retrieved object is the same as a previously retrieved object. One way to do this is to create a pool of serial objects — objects that have a monotonically increasing serial number. The following code defines such a class:

#include // For size_t

class Serial

{

public:

Serial();

size_t getSerialNumber() const;

protected:

static size_t sNextSerial;

size_t mSerialNumber;

};

Code snippet from ObjectPoolTest\Serial.h

#include "Serial.h"

Serial::Serial()

{

mSerialNumber = sNextSerial++; // A new object gets the next serial number.

}

size_t Serial::getSerialNumber() const

{

return mSerialNumber;

}

size_t Serial::sNextSerial = 0; // The first serial number is 0.

Code snippet from ObjectPoolTest\Serial.cpp

On to the tests! As an initial sanity check, you might want a test that creates an object pool. If any exceptions are thrown during creation, cppunit will report an error:

void ObjectPoolTest::testCreation()

{

ObjectPool myPool;

}

Code snippet from ObjectPoolTest\ObjectPoolTest.cpp

The next test is a negative test because it is doing something that should fail. In this case, the test tries to create an object pool with an invalid chunk size of 0. The object pool constructor should throw an exception. Normally, cppunit would catch the exception and report an error. However, since that is the desired behavior, the test catches the exception explicitly and sets a flag. The final step of the test is to assert that the flag was set. Thus, if the constructor does not throw an exception, the test will fail:

void ObjectPoolTest::testInvalidChunkSize()

{

bool caughtException = false;

try {

ObjectPool myPool(0);

} catch (const invalid_argument& ex) {

caughtException = true; // OK. We were expecting an exception.

}

CPPUNIT_ASSERT(caughtException);

}

Code snippet from ObjectPoolTest\ObjectPoolTest.cpp

testAcquire() tests a specific piece of public functionality — the ability of the ObjectPool to give out an object. In this case, there is not much to assert. To prove validity of the resulting Serial reference, the test asserts that its serial number is greater than or equal to zero:

void ObjectPoolTest::testAcquire()

{

ObjectPool myPool;

shared_ptr serial = myPool.acquireObject();

CPPUNIT_ASSERT(serial->getSerialNumber() >= 0);

}

Code snippet from ObjectPoolTest\ObjectPoolTest.cpp

The next test is a bit more interesting. The ObjectPool should not give out the same Serial object twice (unless it is explicitly released). This test checks the exclusivity property of the ObjectPool by creating a pool with a fixed chunk size and retrieving exactly that many

Return Main Page Previous Page Next Page

®Online Book Reader