- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/unittests/atomsCalculationTest.cpp
r229e3c r57adc7 24 24 #include "atom.hpp" 25 25 26 #ifdef HAVE_TESTRUNNER27 #include "UnitTestMain.hpp"28 #endif /*HAVE_TESTRUNNER*/29 30 26 // Registers the fixture into the 'registry' 31 27 CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest ); 32 28 29 // some stubs 30 class AtomStub : public atom { 31 public: 32 AtomStub(atomId_t _id) : 33 atom(), 34 id(_id), 35 manipulated(false) 36 {} 37 38 virtual atomId_t getId(){ 39 return id; 40 } 41 42 virtual void doSomething(){ 43 manipulated = true; 44 } 45 46 bool manipulated; 47 private: 48 atomId_t id; 49 }; 50 33 51 // set up and tear down 34 52 void atomsCalculationTest::setUp(){ 35 World::get Instance();53 World::get(); 36 54 for(int i=0;i<ATOM_COUNT;++i){ 37 atoms[i]= World::getInstance().createAtom();38 atomIds[i]= atoms[i]->getId();55 atoms[i]= new AtomStub(i); 56 World::get()->registerAtom(atoms[i]); 39 57 } 40 58 } 41 59 void atomsCalculationTest::tearDown(){ 42 World:: purgeInstance();43 ActionRegistry::purge Instance();60 World::destroy(); 61 ActionRegistry::purgeRegistry(); 44 62 } 45 63 46 64 // some helper functions 47 static bool hasAllIds(std::vector<atomId_t> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()){ 48 for(int i=0;i<ATOM_COUNT;++i){ 49 atomId_t id = ids[i]; 50 if(!excluded.count(id)){ 51 std::vector<atomId_t>::iterator iter; 65 static bool hasAll(std::vector<int> ids,int min, int max, std::set<int> excluded = std::set<int>()){ 66 for(int i=min;i<max;++i){ 67 if(!excluded.count(i)){ 68 std::vector<int>::iterator iter; 52 69 bool res=false; 53 for(iter= atoms.begin();iter!=atoms.end();++iter){54 res |= (*iter) == i d;70 for(iter=ids.begin();iter!=ids.end();++iter){ 71 res |= (*iter) == i; 55 72 } 56 73 if(!res) { 57 cout << "Atom " << i d<< " missing in returned list" << endl;74 cout << "Atom " << i << " missing in returned list" << endl; 58 75 return false; 59 76 } … … 63 80 } 64 81 65 static bool hasNoDuplicates(std::vector< atomId_t> ids){66 std::set< atomId_t> found;67 std::vector< atomId_t>::iterator iter;82 static bool hasNoDuplicates(std::vector<int> ids){ 83 std::set<int> found; 84 std::vector<int>::iterator iter; 68 85 for(iter=ids.begin();iter!=ids.end();++iter){ 69 86 int id = (*iter); … … 75 92 } 76 93 94 static void operation(atom* _atom){ 95 AtomStub *atom = dynamic_cast<AtomStub*>(_atom); 96 assert(atom); 97 atom->doSomething(); 98 } 99 100 77 101 void atomsCalculationTest::testCalculateSimple(){ 78 AtomsCalculation< atomId_t> *calc = World::getInstance().calcOnAtoms<atomId_t>(boost::bind(&atom::getId,_1),"FOO",AllAtoms());79 std::vector< atomId_t> allIds = (*calc)();80 CPPUNIT_ASSERT(hasAll Ids(allIds,atomIds));102 AtomsCalculation<int> *calc = World::get()->calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms()); 103 std::vector<int> allIds = (*calc)(); 104 CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT)); 81 105 CPPUNIT_ASSERT(hasNoDuplicates(allIds)); 82 106 } 83 107 84 108 void atomsCalculationTest::testCalculateExcluded(){ 85 atomId_t excluded = atomIds[ATOM_COUNT/2];86 AtomsCalculation< atomId_t> *calc = World::getInstance().calcOnAtoms<atomId_t>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded));87 std::vector< atomId_t> allIds = (*calc)();88 std::set< atomId_t> excluded_set;109 int excluded = ATOM_COUNT/2; 110 AtomsCalculation<int> *calc = World::get()->calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded)); 111 std::vector<int> allIds = (*calc)(); 112 std::set<int> excluded_set; 89 113 excluded_set.insert(excluded); 90 CPPUNIT_ASSERT(hasAll Ids(allIds,atomIds,excluded_set));114 CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set)); 91 115 CPPUNIT_ASSERT(hasNoDuplicates(allIds)); 92 116 CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
Note:
See TracChangeset
for help on using the changeset viewer.