Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/atomsCalculationTest.cpp

    r229e3c r57adc7  
    2424#include "atom.hpp"
    2525
    26 #ifdef HAVE_TESTRUNNER
    27 #include "UnitTestMain.hpp"
    28 #endif /*HAVE_TESTRUNNER*/
    29 
    3026// Registers the fixture into the 'registry'
    3127CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest );
    3228
     29// some stubs
     30class AtomStub : public atom {
     31public:
     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;
     47private:
     48  atomId_t id;
     49};
     50
    3351// set up and tear down
    3452void atomsCalculationTest::setUp(){
    35   World::getInstance();
     53  World::get();
    3654  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]);
    3957  }
    4058}
    4159void atomsCalculationTest::tearDown(){
    42   World::purgeInstance();
    43   ActionRegistry::purgeInstance();
     60  World::destroy();
     61  ActionRegistry::purgeRegistry();
    4462}
    4563
    4664// 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;
     65static 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;
    5269      bool res=false;
    53       for(iter=atoms.begin();iter!=atoms.end();++iter){
    54         res |= (*iter) == id;
     70      for(iter=ids.begin();iter!=ids.end();++iter){
     71        res |= (*iter) == i;
    5572      }
    5673      if(!res) {
    57         cout << "Atom " << id << " missing in returned list" << endl;
     74        cout << "Atom " << i << " missing in returned list" << endl;
    5875        return false;
    5976      }
     
    6380}
    6481
    65 static bool hasNoDuplicates(std::vector<atomId_t> ids){
    66   std::set<atomId_t> found;
    67   std::vector<atomId_t>::iterator iter;
     82static bool hasNoDuplicates(std::vector<int> ids){
     83  std::set<int> found;
     84  std::vector<int>::iterator iter;
    6885  for(iter=ids.begin();iter!=ids.end();++iter){
    6986    int id = (*iter);
     
    7592}
    7693
     94static void operation(atom* _atom){
     95  AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
     96  assert(atom);
     97  atom->doSomething();
     98}
     99
     100
    77101void 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(hasAllIds(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));
    81105  CPPUNIT_ASSERT(hasNoDuplicates(allIds));
    82106}
    83107
    84108void 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;
    89113  excluded_set.insert(excluded);
    90   CPPUNIT_ASSERT(hasAllIds(allIds,atomIds,excluded_set));
     114  CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set));
    91115  CPPUNIT_ASSERT(hasNoDuplicates(allIds));
    92116  CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
Note: See TracChangeset for help on using the changeset viewer.