| 1 | /*
 | 
|---|
| 2 |  * DescriptorUnittest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Feb 9, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "DescriptorUnittest.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 13 | #include <iostream>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <Descriptors/AtomDescriptor.hpp>
 | 
|---|
| 16 | #include <Descriptors/AtomIdDescriptor.hpp>
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "World.hpp"
 | 
|---|
| 19 | #include "atom.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | // Registers the fixture into the 'registry'
 | 
|---|
| 22 | CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest );
 | 
|---|
| 23 | 
 | 
|---|
| 24 | // set up and tear down
 | 
|---|
| 25 | void DescriptorUnittest::setUp(){
 | 
|---|
| 26 |   World::get();
 | 
|---|
| 27 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 28 |     atoms[i]= World::get()->createAtom();
 | 
|---|
| 29 |     atomIds[i] = atoms[i]->getId();
 | 
|---|
| 30 |   }
 | 
|---|
| 31 | }
 | 
|---|
| 32 | void DescriptorUnittest::tearDown(){
 | 
|---|
| 33 |   World::destroy();
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | // 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)){
 | 
|---|
| 41 |       std::vector<atom*>::iterator iter;
 | 
|---|
| 42 |       bool res=false;
 | 
|---|
| 43 |       for(iter=atoms.begin();iter!=atoms.end();++iter){
 | 
|---|
| 44 |         res |= (*iter)->getId() == id;
 | 
|---|
| 45 |       }
 | 
|---|
| 46 |       if(!res) {
 | 
|---|
| 47 |         cout << "Atom " << id << " missing in returned list" << endl;
 | 
|---|
| 48 |         return false;
 | 
|---|
| 49 |       }
 | 
|---|
| 50 |     }
 | 
|---|
| 51 |   }
 | 
|---|
| 52 |   return true;
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | bool hasNoDuplicates(std::vector<atom*> atoms){
 | 
|---|
| 56 |   std::set<int> found;
 | 
|---|
| 57 |   std::vector<atom*>::iterator iter;
 | 
|---|
| 58 |   for(iter=atoms.begin();iter!=atoms.end();++iter){
 | 
|---|
| 59 |     int id = (*iter)->getId();
 | 
|---|
| 60 |     if(found.count(id))
 | 
|---|
| 61 |       return false;
 | 
|---|
| 62 |     found.insert(id);
 | 
|---|
| 63 |   }
 | 
|---|
| 64 |   return true;
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | 
 | 
|---|
| 68 | void DescriptorUnittest::AtomBaseSetsTest(){
 | 
|---|
| 69 |   std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
 | 
|---|
| 70 |   CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,atomIds));
 | 
|---|
| 71 |   CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   std::vector<atom*> noAtoms = World::get()->getAllAtoms(NoAtoms());
 | 
|---|
| 74 |   CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
 | 
|---|
| 75 | }
 | 
|---|
| 76 | void DescriptorUnittest::AtomIdTest(){
 | 
|---|
| 77 |   // test Atoms from boundaries and middle of the set
 | 
|---|
| 78 |   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());
 | 
|---|
| 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 |   }
 | 
|---|
| 99 |   // test from outside of set
 | 
|---|
| 100 |   testAtom = World::get()->getAtom(AtomById(outsideId));
 | 
|---|
| 101 |   CPPUNIT_ASSERT(!testAtom);
 | 
|---|
| 102 | }
 | 
|---|
| 103 | void DescriptorUnittest::AtomCalcTest(){
 | 
|---|
| 104 |   // test some elementary set operations
 | 
|---|
| 105 |   {
 | 
|---|
| 106 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms());
 | 
|---|
| 107 |     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
 | 
|---|
| 108 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
 | 
|---|
| 109 |   }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   {
 | 
|---|
| 112 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms());
 | 
|---|
| 113 |     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
 | 
|---|
| 114 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
 | 
|---|
| 115 |   }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   {
 | 
|---|
| 118 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()&&AllAtoms());
 | 
|---|
| 119 |     CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
 | 
|---|
| 120 |   }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   {
 | 
|---|
| 123 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&NoAtoms());
 | 
|---|
| 124 |     CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
 | 
|---|
| 125 |   }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   {
 | 
|---|
| 128 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(!AllAtoms());
 | 
|---|
| 129 |     CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
 | 
|---|
| 130 |   }
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   {
 | 
|---|
| 133 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(!NoAtoms());
 | 
|---|
| 134 |     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds));
 | 
|---|
| 135 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
 | 
|---|
| 136 |   }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // exclude and include some atoms
 | 
|---|
| 139 |   {
 | 
|---|
| 140 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
 | 
|---|
| 141 |     std::set<int> excluded;
 | 
|---|
| 142 |     excluded.insert(atomIds[ATOM_COUNT/2]);
 | 
|---|
| 143 |     CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds,excluded));
 | 
|---|
| 144 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
 | 
|---|
| 145 |     CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
 | 
|---|
| 146 |   }
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   {
 | 
|---|
| 149 |     std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
 | 
|---|
| 150 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
 | 
|---|
| 151 |     CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
 | 
|---|
| 152 |   }
 | 
|---|
| 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 | };
 | 
|---|