- File:
-
- 1 edited
-
src/unittests/DescriptorUnittest.cpp (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/unittests/DescriptorUnittest.cpp
r9b6b2f r46d958 19 19 #include "atom.hpp" 20 20 21 #ifdef HAVE_TESTRUNNER22 #include "UnitTestMain.hpp"23 #endif /*HAVE_TESTRUNNER*/24 25 /********************************************** Test classes **************************************/26 21 // Registers the fixture into the 'registry' 27 22 CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest ); 28 29 // some stubs30 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 45 23 46 24 // set up and tear down … … 48 26 World::get(); 49 27 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(); 51 30 } 52 31 } 53 32 void DescriptorUnittest::tearDown(){ 54 33 World::destroy(); 55 for(int i=0;i<ATOM_COUNT;++i){56 delete atoms[i];57 }58 34 } 59 35 60 36 // 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)){ 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)){ 64 41 std::vector<atom*>::iterator iter; 65 42 bool res=false; 66 43 for(iter=atoms.begin();iter!=atoms.end();++iter){ 67 res |= (*iter)->getId() == i ;44 res |= (*iter)->getId() == id; 68 45 } 69 46 if(!res) { 70 cout << "Atom " << i << " missing in returned list" << endl;47 cout << "Atom " << id << " missing in returned list" << endl; 71 48 return false; 72 49 } … … 91 68 void DescriptorUnittest::AtomBaseSetsTest(){ 92 69 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)); 94 71 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms)); 95 72 … … 100 77 // test Atoms from boundaries and middle of the set 101 78 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()); 108 88 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 } 109 99 // test from outside of set 110 testAtom = World::get()->getAtom(AtomById( ATOM_COUNT));100 testAtom = World::get()->getAtom(AtomById(outsideId)); 111 101 CPPUNIT_ASSERT(!testAtom); 112 102 } … … 115 105 { 116 106 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)); 118 108 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); 119 109 } … … 121 111 { 122 112 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)); 124 114 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); 125 115 } … … 142 132 { 143 133 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)); 145 135 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); 146 136 } … … 148 138 // exclude and include some atoms 149 139 { 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]))); 151 141 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)); 154 144 CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms)); 155 145 CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size()); … … 157 147 158 148 { 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]))); 160 150 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()); 162 152 } 163 153 } 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.
