/* * CacheableTest.cpp * * Created on: Feb 2, 2010 * Author: crueger */ #include "CacheableTest.hpp" #include #include #include #include #include "Patterns/Cacheable.hpp" // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( CacheableTest ); class threeNumbers : public Observable { public: bool hasRecalced; int x; int y; int z; void setX(int _x){ START_OBSERVER;; x = _x; FINISH_OBSERVER; } void setY (int _y){ START_OBSERVER;; y = _y; FINISH_OBSERVER; } void setZ(int _z){ START_OBSERVER;; z = _z; FINISH_OBSERVER; } int calcSum(){ hasRecalced = true; return x+y+z; } threeNumbers(int _x,int _y, int _z) : sum(this,boost::bind(&threeNumbers::calcSum,this)), x(_x),y(_y),z(_z), hasRecalced(false) {} Cacheable sum; }; void CacheableTest::setUp(){ numbers = new threeNumbers(1,2,3); } void CacheableTest::tearDown(){ delete numbers; } void CacheableTest::doesRecalcTest() { CPPUNIT_ASSERT_EQUAL( 6, *(numbers->sum)); CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced); numbers->hasRecalced=false; numbers->setX(4); CPPUNIT_ASSERT_EQUAL( false, numbers->hasRecalced); CPPUNIT_ASSERT_EQUAL( 9, *(numbers->sum)); CPPUNIT_ASSERT_EQUAL( true, numbers->hasRecalced); } /********************************************** Main routine **************************************/ int main(int argc, char **argv) { // Get the top level suite from the registry CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); // Adds the test to the list of test to run CppUnit::TextUi::TestRunner runner; runner.addTest( suite ); // Change the default outputter to a compiler error format outputter runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); // Run the tests. bool wasSucessful = runner.run(); // Return error code 1 if the one of test failed. return wasSucessful ? 0 : 1; };