| [8c5327] | 1 | /*
 | 
|---|
 | 2 |  * stackclassunittest.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Oct 27, 2009
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | using namespace std;
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 12 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 13 | 
 | 
|---|
 | 14 | #include "stackclassunittest.hpp"
 | 
|---|
| [e138de] | 15 | #include "log.hpp"
 | 
|---|
| [8c5327] | 16 | 
 | 
|---|
 | 17 | enum { testdimension=3 };
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | /********************************************** Test classes **************************************/
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | // Registers the fixture into the 'registry'
 | 
|---|
 | 22 | CPPUNIT_TEST_SUITE_REGISTRATION( StackClassTest );
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | void StackClassTest::setUp()
 | 
|---|
 | 26 | {
 | 
|---|
 | 27 |   Stack = new StackClass<int *>(testdimension);
 | 
|---|
 | 28 | };
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | void StackClassTest::tearDown()
 | 
|---|
 | 32 | {
 | 
|---|
 | 33 |   Stack->ClearStack();
 | 
|---|
 | 34 |   delete(Stack);
 | 
|---|
 | 35 | };
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | /** UnitTest for StackClass<T> implementation
 | 
|---|
 | 38 |  *
 | 
|---|
 | 39 |  */
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | void StackClassTest::TestImplementation()
 | 
|---|
 | 42 | {
 | 
|---|
 | 43 |   int testfield[testdimension] = {0,1,2};
 | 
|---|
| [e138de] | 44 |   //Log() << Verbose(1) << "Testing the snake stack..." << endl;
 | 
|---|
| [8c5327] | 45 |   for (int i=0;i<testdimension;i++) {
 | 
|---|
| [e138de] | 46 |     //Log() << Verbose(2) << "Filling " << i << "th element of stack." << endl;
 | 
|---|
| [8c5327] | 47 |     Stack->Push(&testfield[i]);
 | 
|---|
 | 48 |   }
 | 
|---|
| [e138de] | 49 |   //Log() << Verbose(0) << endl;
 | 
|---|
| [8c5327] | 50 |   //Output(out);
 | 
|---|
 | 51 |   CPPUNIT_ASSERT_EQUAL(true, Stack->IsFull());
 | 
|---|
 | 52 |   CPPUNIT_ASSERT_EQUAL(false, Stack->IsEmpty());
 | 
|---|
 | 53 |   CPPUNIT_ASSERT_EQUAL((testdimension) % (int)testdimension, Stack->ItemCount());
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |   CPPUNIT_ASSERT_EQUAL( true, Stack->RemoveItem(&testfield[1]) );
 | 
|---|
 | 56 |   CPPUNIT_ASSERT_EQUAL((testdimension-1) % (int)testdimension, Stack->ItemCount());
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 |   CPPUNIT_ASSERT_EQUAL( true, Stack->RemoveItem(&testfield[2]) );
 | 
|---|
 | 59 |   CPPUNIT_ASSERT_EQUAL((testdimension-2) % (int)testdimension, Stack->ItemCount());
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |   CPPUNIT_ASSERT_EQUAL( true, Stack->RemoveItem(&testfield[0]) );
 | 
|---|
 | 62 |   CPPUNIT_ASSERT_EQUAL((testdimension-3) % (int)testdimension, Stack->ItemCount());
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |   Stack->ClearStack();
 | 
|---|
 | 65 |   CPPUNIT_ASSERT_EQUAL(false, Stack->IsFull());
 | 
|---|
 | 66 |   CPPUNIT_ASSERT_EQUAL(true, Stack->IsEmpty());
 | 
|---|
 | 67 |   CPPUNIT_ASSERT_EQUAL(0, Stack->ItemCount());
 | 
|---|
 | 68 | };
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | /********************************************** Main routine **************************************/
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | int main(int argc, char **argv)
 | 
|---|
 | 74 | {
 | 
|---|
 | 75 |   // Get the top level suite from the registry
 | 
|---|
 | 76 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 |   // Adds the test to the list of test to run
 | 
|---|
 | 79 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
 | 80 |   runner.addTest( suite );
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
 | 83 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
 | 84 |                                                        std::cerr ) );
 | 
|---|
 | 85 |   // Run the tests.
 | 
|---|
 | 86 |   bool wasSucessful = runner.run();
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |   // Return error code 1 if the one of test failed.
 | 
|---|
 | 89 |   return wasSucessful ? 0 : 1;
 | 
|---|
 | 90 | };
 | 
|---|