/* * atomsCalculationTest.cpp * * Created on: Feb 19, 2010 * Author: crueger */ #include "atomsCalculationTest.hpp" #include #include #include #include #include #include "Descriptors/AtomDescriptor.hpp" #include "Descriptors/AtomIdDescriptor.hpp" #include "Actions/AtomsCalculation.hpp" #include "Actions/AtomsCalculation_impl.hpp" #include "Actions/ActionRegistry.hpp" #include "World.hpp" #include "World_calculations.hpp" #include "atom.hpp" // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest ); // some stubs class AtomStub : public atom { public: AtomStub(int _id) : atom(), id(_id), manipulated(false) {} virtual int getId(){ return id; } virtual void doSomething(){ manipulated = true; } bool manipulated; private: int id; }; // set up and tear down void atomsCalculationTest::setUp(){ World::get(); for(int i=0;iregisterAtom(atoms[i]); } } void atomsCalculationTest::tearDown(){ World::destroy(); ActionRegistry::purgeRegistry(); } // some helper functions static bool hasAll(std::vector ids,int min, int max, std::set excluded = std::set()){ for(int i=min;i::iterator iter; bool res=false; for(iter=ids.begin();iter!=ids.end();++iter){ res |= (*iter) == i; } if(!res) { cout << "Atom " << i << " missing in returned list" << endl; return false; } } } return true; } static bool hasNoDuplicates(std::vector ids){ std::set found; std::vector::iterator iter; for(iter=ids.begin();iter!=ids.end();++iter){ int id = (*iter); if(found.count(id)) return false; found.insert(id); } return true; } static void operation(atom* _atom){ AtomStub *atom = dynamic_cast(_atom); assert(atom); atom->doSomething(); } void atomsCalculationTest::testCalculateSimple(){ AtomsCalculation *calc = World::get()->calcOnAtoms(boost::bind(&atom::getId,_1),"FOO",AllAtoms()); std::vector allIds = (*calc)(); CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT)); CPPUNIT_ASSERT(hasNoDuplicates(allIds)); } void atomsCalculationTest::testCalculateExcluded(){ int excluded = ATOM_COUNT/2; AtomsCalculation *calc = World::get()->calcOnAtoms(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded)); std::vector allIds = (*calc)(); std::set excluded_set; excluded_set.insert(excluded); CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set)); CPPUNIT_ASSERT(hasNoDuplicates(allIds)); CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size()); }