| [7a1ce5] | 1 | /* | 
|---|
|  | 2 | * DescriptorUnittest.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Feb 9, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [57adc7] | 8 | #include "AtomDescriptorTest.hpp" | 
|---|
| [7a1ce5] | 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 |  | 
|---|
| [9b6b2f] | 21 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 22 | #include "UnitTestMain.hpp" | 
|---|
|  | 23 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 24 |  | 
|---|
|  | 25 | /********************************************** Test classes **************************************/ | 
|---|
| [7a1ce5] | 26 | // Registers the fixture into the 'registry' | 
|---|
| [57adc7] | 27 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest ); | 
|---|
| [7a1ce5] | 28 |  | 
|---|
|  | 29 | // set up and tear down | 
|---|
| [57adc7] | 30 | void AtomDescriptorTest::setUp(){ | 
|---|
| [23b547] | 31 | World::getInstance(); | 
|---|
| [7a1ce5] | 32 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| [23b547] | 33 | atoms[i]= World::getInstance().createAtom(); | 
|---|
| [57adc7] | 34 | atomIds[i]= atoms[i]->getId(); | 
|---|
| [7a1ce5] | 35 | } | 
|---|
|  | 36 | } | 
|---|
| [57adc7] | 37 |  | 
|---|
|  | 38 | void AtomDescriptorTest::tearDown(){ | 
|---|
| [23b547] | 39 | World::purgeInstance(); | 
|---|
| [7a1ce5] | 40 | } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // some helper functions | 
|---|
| [57adc7] | 43 | static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()){ | 
|---|
| [46d958] | 44 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| [57adc7] | 45 | atomId_t id = ids[i]; | 
|---|
| [46d958] | 46 | if(!excluded.count(id)){ | 
|---|
| [7a1ce5] | 47 | std::vector<atom*>::iterator iter; | 
|---|
|  | 48 | bool res=false; | 
|---|
|  | 49 | for(iter=atoms.begin();iter!=atoms.end();++iter){ | 
|---|
| [46d958] | 50 | res |= (*iter)->getId() == id; | 
|---|
| [7a1ce5] | 51 | } | 
|---|
|  | 52 | if(!res) { | 
|---|
| [46d958] | 53 | cout << "Atom " << id << " missing in returned list" << endl; | 
|---|
| [7a1ce5] | 54 | return false; | 
|---|
|  | 55 | } | 
|---|
|  | 56 | } | 
|---|
|  | 57 | } | 
|---|
|  | 58 | return true; | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [57adc7] | 61 | static bool hasNoDuplicateAtoms(std::vector<atom*> atoms){ | 
|---|
|  | 62 | std::set<atomId_t> found; | 
|---|
| [7a1ce5] | 63 | std::vector<atom*>::iterator iter; | 
|---|
|  | 64 | for(iter=atoms.begin();iter!=atoms.end();++iter){ | 
|---|
|  | 65 | int id = (*iter)->getId(); | 
|---|
|  | 66 | if(found.count(id)) | 
|---|
|  | 67 | return false; | 
|---|
|  | 68 | found.insert(id); | 
|---|
|  | 69 | } | 
|---|
|  | 70 | return true; | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
|  | 73 |  | 
|---|
| [57adc7] | 74 | void AtomDescriptorTest::AtomBaseSetsTest(){ | 
|---|
| [23b547] | 75 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms()); | 
|---|
| [57adc7] | 76 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds)); | 
|---|
|  | 77 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms)); | 
|---|
| [7a1ce5] | 78 |  | 
|---|
| [23b547] | 79 | std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms()); | 
|---|
| [7a1ce5] | 80 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty()); | 
|---|
|  | 81 | } | 
|---|
| [57adc7] | 82 | void AtomDescriptorTest::AtomIdTest(){ | 
|---|
| [7a1ce5] | 83 | // test Atoms from boundaries and middle of the set | 
|---|
|  | 84 | atom* testAtom; | 
|---|
| [23b547] | 85 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0])); | 
|---|
| [46d958] | 86 | CPPUNIT_ASSERT(testAtom); | 
|---|
|  | 87 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId()); | 
|---|
| [23b547] | 88 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2])); | 
|---|
| [46d958] | 89 | CPPUNIT_ASSERT(testAtom); | 
|---|
|  | 90 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId()); | 
|---|
| [23b547] | 91 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1])); | 
|---|
| [46d958] | 92 | CPPUNIT_ASSERT(testAtom); | 
|---|
|  | 93 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId()); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | // find some ID that has not been created | 
|---|
| [57adc7] | 96 | atomId_t outsideId=0; | 
|---|
| [46d958] | 97 | bool res = false; | 
|---|
| [57adc7] | 98 | for(outsideId=0;!res;++outsideId) { | 
|---|
| [46d958] | 99 | res = true; | 
|---|
|  | 100 | for(int i = 0; i < ATOM_COUNT; ++i){ | 
|---|
|  | 101 | res &= atomIds[i]!=outsideId; | 
|---|
|  | 102 | } | 
|---|
|  | 103 | } | 
|---|
| [7a1ce5] | 104 | // test from outside of set | 
|---|
| [23b547] | 105 | testAtom = World::getInstance().getAtom(AtomById(outsideId)); | 
|---|
| [7a1ce5] | 106 | CPPUNIT_ASSERT(!testAtom); | 
|---|
|  | 107 | } | 
|---|
| [57adc7] | 108 | void AtomDescriptorTest::AtomCalcTest(){ | 
|---|
| [7a1ce5] | 109 | // test some elementary set operations | 
|---|
|  | 110 | { | 
|---|
| [23b547] | 111 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms()); | 
|---|
| [57adc7] | 112 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
|  | 113 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | { | 
|---|
| [23b547] | 117 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms()); | 
|---|
| [57adc7] | 118 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
|  | 119 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | { | 
|---|
| [23b547] | 123 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms()); | 
|---|
| [7a1ce5] | 124 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
|  | 127 | { | 
|---|
| [23b547] | 128 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms()); | 
|---|
| [7a1ce5] | 129 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | { | 
|---|
| [23b547] | 133 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms()); | 
|---|
| [7a1ce5] | 134 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | { | 
|---|
| [23b547] | 138 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms()); | 
|---|
| [57adc7] | 139 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
|  | 140 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 141 | } | 
|---|
|  | 142 | // exclude and include some atoms | 
|---|
|  | 143 | { | 
|---|
| [23b547] | 144 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2]))); | 
|---|
| [57adc7] | 145 | std::set<atomId_t> excluded; | 
|---|
| [46d958] | 146 | excluded.insert(atomIds[ATOM_COUNT/2]); | 
|---|
| [57adc7] | 147 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded)); | 
|---|
|  | 148 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 149 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size()); | 
|---|
|  | 150 | } | 
|---|
|  | 151 |  | 
|---|
|  | 152 | { | 
|---|
| [23b547] | 153 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2]))); | 
|---|
| [7a1ce5] | 154 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size()); | 
|---|
| [46d958] | 155 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId()); | 
|---|
| [7a1ce5] | 156 | } | 
|---|
|  | 157 | } | 
|---|