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