Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/unittests/DescriptorUnittest.cpp

    r9b6b2f r46d958  
    1919#include "atom.hpp"
    2020
    21 #ifdef HAVE_TESTRUNNER
    22 #include "UnitTestMain.hpp"
    23 #endif /*HAVE_TESTRUNNER*/
    24 
    25 /********************************************** Test classes **************************************/
    2621// Registers the fixture into the 'registry'
    2722CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest );
    28 
    29 // some stubs
    30 class AtomStub : public atom {
    31 public:
    32   AtomStub(int _id) :
    33   atom(),
    34   id(_id)
    35   {}
    36 
    37   virtual int getId(){
    38     return id;
    39   }
    40 
    41 private:
    42   int id;
    43 };
    44 
    4523
    4624// set up and tear down
     
    4826  World::get();
    4927  for(int i=0;i<ATOM_COUNT;++i){
    50     atoms[i]= new AtomStub(i);
     28    atoms[i]= World::get()->createAtom();
     29    atomIds[i] = atoms[i]->getId();
    5130  }
    5231}
    5332void DescriptorUnittest::tearDown(){
    5433  World::destroy();
    55   for(int i=0;i<ATOM_COUNT;++i){
    56     delete atoms[i];
    57   }
    5834}
    5935
    6036// some helper functions
    61 bool 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)){
     37bool 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)){
    6441      std::vector<atom*>::iterator iter;
    6542      bool res=false;
    6643      for(iter=atoms.begin();iter!=atoms.end();++iter){
    67         res |= (*iter)->getId() == i;
     44        res |= (*iter)->getId() == id;
    6845      }
    6946      if(!res) {
    70         cout << "Atom " << i << " missing in returned list" << endl;
     47        cout << "Atom " << id << " missing in returned list" << endl;
    7148        return false;
    7249      }
     
    9168void DescriptorUnittest::AtomBaseSetsTest(){
    9269  std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
    93   CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,0,ATOM_COUNT));
     70  CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,atomIds));
    9471  CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
    9572
     
    10077  // test Atoms from boundaries and middle of the set
    10178  atom* testAtom;
    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());
     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());
    10888
     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  }
    10999  // test from outside of set
    110   testAtom = World::get()->getAtom(AtomById(ATOM_COUNT));
     100  testAtom = World::get()->getAtom(AtomById(outsideId));
    111101  CPPUNIT_ASSERT(!testAtom);
    112102}
     
    115105  {
    116106    std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms());
    117     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
     107    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
    118108    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    119109  }
     
    121111  {
    122112    std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms());
    123     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
     113    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
    124114    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    125115  }
     
    142132  {
    143133    std::vector<atom*> testAtoms = World::get()->getAllAtoms(!NoAtoms());
    144     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
     134    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
    145135    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    146136  }
     
    148138  // exclude and include some atoms
    149139  {
    150     std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(ATOM_COUNT/2)));
     140    std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
    151141    std::set<int> excluded;
    152     excluded.insert(ATOM_COUNT/2);
    153     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT,excluded));
     142    excluded.insert(atomIds[ATOM_COUNT/2]);
     143    CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds,excluded));
    154144    CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
    155145    CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
     
    157147
    158148  {
    159     std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(ATOM_COUNT/2)));
     149    std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
    160150    CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
    161     CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtoms[0]->getId());
     151    CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
    162152  }
    163153}
     154
     155/********************************************** Main routine **************************************/
     156
     157int 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.