| 1 | /*
 | 
|---|
| 2 |  * CacheableTest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Feb 2, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "CacheableTest.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include <boost/bind.hpp>
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Patterns/Cacheable.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | // Registers the fixture into the 'registry'
 | 
|---|
| 19 | CPPUNIT_TEST_SUITE_REGISTRATION( CacheableTest );
 | 
|---|
| 20 | 
 | 
|---|
| 21 | class threeNumbers : public Observable {
 | 
|---|
| 22 | public:
 | 
|---|
| 23 |   bool hasRecalced;
 | 
|---|
| 24 |   int x;
 | 
|---|
| 25 |   int y;
 | 
|---|
| 26 |   int z;
 | 
|---|
| 27 | 
 | 
|---|
| 28 |   void setX(int _x){
 | 
|---|
| 29 |     OBSERVE;
 | 
|---|
| 30 |     x = _x;
 | 
|---|
| 31 |   }
 | 
|---|
| 32 | 
 | 
|---|
| 33 |   void setY
 | 
|---|
| 34 |   (int _y){
 | 
|---|
| 35 |     OBSERVE;
 | 
|---|
| 36 |     y = _y;
 | 
|---|
| 37 |   }
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   void setZ(int _z){
 | 
|---|
| 40 |     OBSERVE;
 | 
|---|
| 41 |     z = _z;
 | 
|---|
| 42 |   }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   int calcSum(){
 | 
|---|
| 45 |     hasRecalced = true;
 | 
|---|
| 46 |     return x+y+z;
 | 
|---|
| 47 |   }
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   threeNumbers(int _x,int _y, int _z) :
 | 
|---|
| 50 |     sum(this,boost::bind(&threeNumbers::calcSum,this)),
 | 
|---|
| 51 |     x(_x),y(_y),z(_z),
 | 
|---|
| 52 |     hasRecalced(false)
 | 
|---|
| 53 |   {}
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   Cacheable<int> sum;
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | 
 | 
|---|
| 59 | 
 | 
|---|
| 60 | 
 | 
|---|
| 61 | void CacheableTest::setUp(){
 | 
|---|
| 62 |   numbers = new threeNumbers(1,2,3);
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | void CacheableTest::tearDown(){
 | 
|---|
| 66 |   delete numbers;
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | void CacheableTest::doesRecalcTest()
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   CPPUNIT_ASSERT_EQUAL( 6, *(numbers->sum));
 | 
|---|
| 72 |   CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
 | 
|---|
| 73 |   numbers->hasRecalced=false;
 | 
|---|
| 74 |   numbers->setX(4);
 | 
|---|
| 75 |   CPPUNIT_ASSERT_EQUAL( false, numbers->hasRecalced);
 | 
|---|
| 76 |   CPPUNIT_ASSERT_EQUAL( 9, *(numbers->sum));
 | 
|---|
| 77 |   CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced);
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | 
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /********************************************** Main routine **************************************/
 | 
|---|
| 83 | 
 | 
|---|
| 84 | int main(int argc, char **argv)
 | 
|---|
| 85 | {
 | 
|---|
| 86 |   // Get the top level suite from the registry
 | 
|---|
| 87 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   // Adds the test to the list of test to run
 | 
|---|
| 90 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
| 91 |   runner.addTest( suite );
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
| 94 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
| 95 |                                                        std::cerr ) );
 | 
|---|
| 96 |   // Run the tests.
 | 
|---|
| 97 |   bool wasSucessful = runner.run();
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   // Return error code 1 if the one of test failed.
 | 
|---|
| 100 |   return wasSucessful ? 0 : 1;
 | 
|---|
| 101 | };
 | 
|---|
| 102 | 
 | 
|---|