Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/atomsCalculationTest.cpp

    r57adc7 r229e3c  
    2424#include "atom.hpp"
    2525
     26#ifdef HAVE_TESTRUNNER
     27#include "UnitTestMain.hpp"
     28#endif /*HAVE_TESTRUNNER*/
     29
    2630// Registers the fixture into the 'registry'
    2731CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest );
    2832
    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 
    5133// set up and tear down
    5234void atomsCalculationTest::setUp(){
    53   World::get();
     35  World::getInstance();
    5436  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();
    5739  }
    5840}
    5941void atomsCalculationTest::tearDown(){
    60   World::destroy();
    61   ActionRegistry::purgeRegistry();
     42  World::purgeInstance();
     43  ActionRegistry::purgeInstance();
    6244}
    6345
    6446// 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;
     47static 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;
    6952      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;
    7255      }
    7356      if(!res) {
    74         cout << "Atom " << i << " missing in returned list" << endl;
     57        cout << "Atom " << id << " missing in returned list" << endl;
    7558        return false;
    7659      }
     
    8063}
    8164
    82 static bool hasNoDuplicates(std::vector<int> ids){
    83   std::set<int> found;
    84   std::vector<int>::iterator iter;
     65static bool hasNoDuplicates(std::vector<atomId_t> ids){
     66  std::set<atomId_t> found;
     67  std::vector<atomId_t>::iterator iter;
    8568  for(iter=ids.begin();iter!=ids.end();++iter){
    8669    int id = (*iter);
     
    9275}
    9376
    94 static void operation(atom* _atom){
    95   AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
    96   assert(atom);
    97   atom->doSomething();
    98 }
    99 
    100 
    10177void 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));
    10581  CPPUNIT_ASSERT(hasNoDuplicates(allIds));
    10682}
    10783
    10884void 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;
    11389  excluded_set.insert(excluded);
    114   CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set));
     90  CPPUNIT_ASSERT(hasAllIds(allIds,atomIds,excluded_set));
    11591  CPPUNIT_ASSERT(hasNoDuplicates(allIds));
    11692  CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
Note: See TracChangeset for help on using the changeset viewer.