This is an example of testing a Queue Look for the possible error and design a test for that error Search for the keyword "error" This gives you experience with CppUnit // main.cpp #include "QueueTemplateTest.h" #include int main(int argc, char** argv) { CppUnit::TextUi::TestRunner runner; // add any suite of tests to run runner.addTest(QueueTemplateTest::suite()); // run tests and display results runner.run(); return 0; } // customer.h -- #ifndef CUSTOMER_H_ #define CUSTOMER_H_ // class of objects that will be contained in the queue class Customer { public: Customer() { arrive = processtime = 0; } void set(long whenArrived); long when() const { return arrive; } int ptime() const { return processtime; } private: long arrive; int processtime; }; // whenArrived is the time at which the customer arrives // the arrival time is set to when and the processing // time set to a random value in the range 1 - 3 void Customer::set(long whenArrived) { processtime = rand() % 3 + 1; arrive = whenArrived; } #endif // #ifndef CUSTOMER_H_ // queuetp.h -- interface for a queue template #ifndef QUEUETP_H_ #define QUEUETP_H_ // This queue will contain any objects template class QueueTp { public: enum {DEFAULT_Q_SIZE = 10}; // create queue with a size limit QueueTp(int size = DEFAULT_Q_SIZE) : MAX_SIZE(size); ~QueueTp(); bool isempty() const; bool isfull() const; int count() const; // add item to end bool enqueue(const Type &item); // remove item from front bool dequeue(Type &item); private: struct Node { Type item; struct Node * next;}; Node * front; Node * rear; int items; const int MAX_SIZE; // preemptive definitions to prevent public copying QueueTp(const QueueTp & q) : MAX_SIZE(0) { } QueueTp & operator=(const QueueTp & q) { return *this;} }; // QueueTp methods template QueueTp::QueueTp(int size) { front = rear = NULL; items = 0; } template QueueTp::~QueueTp() { Node * temp; // while queue is not yet empty while (front != NULL) { temp = front; front = front->next; delete temp; } } template bool QueueTp::isempty() const { return items == 0; } template bool QueueTp::isfull() const { return items == MAX_SIZE; } template int QueueTp::count() const { return items; } // Add item to queue template bool QueueTp::enqueue(const Type & item) { if (isfull()) return false; Node * add = new Node; if (add == NULL) return false; add->item = item; add->next = NULL; items++; // if queue is empty, place item at front if (front == NULL) front = add; // else place at rear have rear point to new node else rear->next = add; // is this an error? Shouldn't it be NULL on an empty queue rear = add; return true; } // Place front item into item variable and remove from queue template bool QueueTp::dequeue(Type & item) { if (front == NULL) return false; item = front->item; items--; Node * temp = front; front = front->next; delete temp; if (items == 0) rear = NULL; return true; } #endif #ifndef QUEUETEMPLATETEST_H_ #define QUEUETEMPLATETEST_H_ #include #include #include // class with all the test methods class QueueTemplateTest : public CppUnit::TestFixture { public: void setUp(); void tearDown(); // Test methods void testQueueCreation(); // test adding items to queue void testEnQueue(); void testQueueCount(); // test removing items from queue void testDeQueue(); void testIsEmpty(); void testIsFull(); // function to build and return the suite containing all tests static CppUnit::Test* suite(); }; #endif //#ifndef QUEUETEMPLATETEST_H_ #include #include #include "QueueTemplateTest.h" #include "queuetp.h" #include "customer.h" void QueueTemplateTest::setUp() { } void QueueTemplateTest::tearDown() { } // Test methods // test queue creation void QueueTemplateTest::testQueueCreation() { // simply create object. // cppunit will report an error if an exception is thrown // during object creation QueueTp myQueue; } // test adding items to queue void QueueTemplateTest::testEnQueue() { QueueTp myQueue; Customer customer; CPPUNIT_ASSERT( myQueue.enqueue(customer) ); } // test number of items in queue void QueueTemplateTest::testQueueCount() { QueueTp myQueue; Customer customer; const int MAX_ITEMS = 5; for ( int count =0; count < MAX_ITEMS; count ++ ) { myQueue.enqueue(customer); } CPPUNIT_ASSERT( myQueue.count() == MAX_ITEMS ); } // test removing items from queue void QueueTemplateTest::testDeQueue() { QueueTp myQueue; Customer customer; // no item in queue yet CPPUNIT_ASSERT( myQueue.dequeue(customer) == false ); // add element myQueue.enqueue(customer); // an item is in the queue and should be removed successfully CPPUNIT_ASSERT( myQueue.dequeue(customer) == true ); } // test isempty() method void QueueTemplateTest::testIsEmpty() { QueueTp myQueue; CPPUNIT_ASSERT( myQueue.isempty() ); } // test isfull() method void QueueTemplateTest::testIsFull() { QueueTp myQueue; Customer customer; const int MAX = QueueTp::DEFAULT_Q_SIZE for ( int count=0; count < MAX; count++ ) { myQueue.enqueue(customer); } CPPUNIT_ASSERT( myQueue.isfull() ); } // function to build and return the suite containing all tests CppUnit::Test* QueueTemplateTest::suite() { // create suite of test object CppUnit::TestSuite* suiteOfTests = new CppUnit::TestSuite("QueueTemplateTest"); // add tests to suite suiteOfTests->addTest( new CppUnit::TestCaller("testQueueCreation", &QueueTemplateTest::testQueueCreation) ); suiteOfTests->addTest( new CppUnit::TestCaller("testEnQueue", &QueueTemplateTest::testEnQueue) ); suiteOfTests->addTest( new CppUnit::TestCaller("testQueueCount", &QueueTemplateTest::testQueueCount) ); suiteOfTests->addTest( new CppUnit::TestCaller("testDeQueue", &QueueTemplateTest::testDeQueue) ); suiteOfTests->addTest( new CppUnit::TestCaller("testIsEmpty", &QueueTemplateTest::testIsEmpty) ); suiteOfTests->addTest( new CppUnit::TestCaller("testIsFull", &QueueTemplateTest::testIsFull) ); return suiteOfTests; }