/* * manipulateAtomsTest.cpp * * Created on: Feb 18, 2010 * Author: crueger */ #include "manipulateAtomsTest.hpp" #include #include #include #include #include #include "Descriptors/AtomDescriptor.hpp" #include "Descriptors/AtomIdDescriptor.hpp" #include "Actions/ManipulateAtomsProcess.hpp" #include "Actions/ActionRegistry.hpp" #include "Actions/ActionHistory.hpp" #include "World.hpp" #include "atom.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest ); // some stubs class AtomStub : public atom { public: AtomStub(int _id) : atom(), manipulated(false), id(_id) {} virtual atomId_t getId(){ return id; } virtual void doSomething(){ manipulated = true; } bool manipulated; private: atomId_t id; }; class countObserver : public Observer{ public: countObserver() : count(0) {} virtual ~countObserver(){} void update(Observable *){ count++; } void subjectKilled(Observable *) {} int count; }; // set up and tear down void manipulateAtomsTest::setUp(){ ActionHistory::init(); World::getInstance(); for(int i=0;i(_atom); assert(atom); atom->doSomething(); } void manipulateAtomsTest::testManipulateSimple(){ ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms()); proc->call(); std::vector allAtoms = World::getInstance().getAllAtoms(AllAtoms()); std::vector::iterator iter; for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){ AtomStub *atom; atom = dynamic_cast(*iter); assert(atom); CPPUNIT_ASSERT(atom->manipulated); } } void manipulateAtomsTest::testManipulateExcluded(){ ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2)); proc->call(); std::vector allAtoms = World::getInstance().getAllAtoms(AllAtoms()); std::vector::iterator iter; for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){ AtomStub *atom; atom = dynamic_cast(*iter); assert(atom); if(atom->getId()!=(int)ATOM_COUNT/2) CPPUNIT_ASSERT(atom->manipulated); else CPPUNIT_ASSERT(!atom->manipulated); } } void manipulateAtomsTest::testObserver(){ countObserver *obs = new countObserver(); World::getInstance().signOn(obs); ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms()); proc->call(); CPPUNIT_ASSERT_EQUAL(1,obs->count); World::getInstance().signOff(obs); delete obs; }