Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/DescriptorUnittest.cpp

    r46d958 r9b6b2f  
    1919#include "atom.hpp"
    2020
     21#ifdef HAVE_TESTRUNNER
     22#include "UnitTestMain.hpp"
     23#endif /*HAVE_TESTRUNNER*/
     24
     25/********************************************** Test classes **************************************/
    2126// Registers the fixture into the 'registry'
    2227CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest );
     28
     29// some stubs
     30class AtomStub : public atom {
     31public:
     32  AtomStub(int _id) :
     33  atom(),
     34  id(_id)
     35  {}
     36
     37  virtual int getId(){
     38    return id;
     39  }
     40
     41private:
     42  int id;
     43};
     44
    2345
    2446// set up and tear down
     
    2648  World::get();
    2749  for(int i=0;i<ATOM_COUNT;++i){
    28     atoms[i]= World::get()->createAtom();
    29     atomIds[i] = atoms[i]->getId();
     50    atoms[i]= new AtomStub(i);
    3051  }
    3152}
    3253void DescriptorUnittest::tearDown(){
    3354  World::destroy();
     55  for(int i=0;i<ATOM_COUNT;++i){
     56    delete atoms[i];
     57  }
    3458}
    3559
    3660// some helper functions
    37 bool hasAll(std::vector<atom*> atoms,int ids[ATOM_COUNT], std::set<int> excluded = std::set<int>()){
    38   for(int i=0;i<ATOM_COUNT;++i){
    39     int id = ids[i];
    40     if(!excluded.count(id)){
     61bool hasAll(std::vector<atom*> atoms,int min, int max, std::set<int> excluded = std::set<int>()){
     62  for(int i=min;i<max;++i){
     63    if(!excluded.count(i)){
    4164      std::vector<atom*>::iterator iter;
    4265      bool res=false;
    4366      for(iter=atoms.begin();iter!=atoms.end();++iter){
    44         res |= (*iter)->getId() == id;
     67        res |= (*iter)->getId() == i;
    4568      }
    4669      if(!res) {
    47         cout << "Atom " << id << " missing in returned list" << endl;
     70        cout << "Atom " << i << " missing in returned list" << endl;
    4871        return false;
    4972      }
     
    6891void DescriptorUnittest::AtomBaseSetsTest(){
    6992  std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
    70   CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,atomIds));
     93  CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,0,ATOM_COUNT));
    7194  CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
    7295
     
    77100  // test Atoms from boundaries and middle of the set
    78101  atom* testAtom;
    79   testAtom = World::get()->getAtom(AtomById(atomIds[0]));
    80   CPPUNIT_ASSERT(testAtom);
    81   CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
    82   testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT/2]));
    83   CPPUNIT_ASSERT(testAtom);
    84   CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
    85   testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT-1]));
    86   CPPUNIT_ASSERT(testAtom);
    87   CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
     102  testAtom = World::get()->getAtom(AtomById(0));
     103  CPPUNIT_ASSERT_EQUAL( 0, testAtom->getId());
     104  testAtom = World::get()->getAtom(AtomById(ATOM_COUNT/2));
     105  CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtom->getId());
     106  testAtom = World::get()->getAtom(AtomById(ATOM_COUNT-1));
     107  CPPUNIT_ASSERT_EQUAL( ATOM_COUNT-1, testAtom->getId());
    88108
    89   // find some ID that has not been created
    90   int outsideId =-1;
    91   bool res = false;
    92   while(!res) {
    93     ++outsideId;
    94     res = true;
    95     for(int i = 0; i < ATOM_COUNT; ++i){
    96       res &= atomIds[i]!=outsideId;
    97     }
    98   }
    99109  // test from outside of set
    100   testAtom = World::get()->getAtom(AtomById(outsideId));
     110  testAtom = World::get()->getAtom(AtomById(ATOM_COUNT));
    101111  CPPUNIT_ASSERT(!testAtom);
    102112}
     
    105115  {
    106116    std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms());
    107     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
     117    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
    108118    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    109119  }
     
    111121  {
    112122    std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms());
    113     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
     123    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
    114124    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    115125  }
     
    132142  {
    133143    std::vector<atom*> testAtoms = World::get()->getAllAtoms(!NoAtoms());
    134     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
     144    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
    135145    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    136146  }
     
    138148  // exclude and include some atoms
    139149  {
    140     std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
     150    std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(ATOM_COUNT/2)));
    141151    std::set<int> excluded;
    142     excluded.insert(atomIds[ATOM_COUNT/2]);
    143     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds,excluded));
     152    excluded.insert(ATOM_COUNT/2);
     153    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT,excluded));
    144154    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    145155    CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
     
    147157
    148158  {
    149     std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
     159    std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(ATOM_COUNT/2)));
    150160    CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
    151     CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
     161    CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtoms[0]->getId());
    152162  }
    153163}
    154 
    155 /********************************************** Main routine **************************************/
    156 
    157 int main(int argc, char **argv)
    158 {
    159   // Get the top level suite from the registry
    160   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
    161 
    162   // Adds the test to the list of test to run
    163   CppUnit::TextUi::TestRunner runner;
    164   runner.addTest( suite );
    165 
    166   // Change the default outputter to a compiler error format outputter
    167   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
    168                                                        std::cerr ) );
    169   // Run the tests.
    170   bool wasSucessful = runner.run();
    171 
    172   // Return error code 1 if the one of test failed.
    173   return wasSucessful ? 0 : 1;
    174 };
Note: See TracChangeset for help on using the changeset viewer.