| [57adc7] | 1 | /* | 
|---|
|  | 2 | * MoleculeDescriptorTest.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Mar 4, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #include "MoleculeDescriptorTest.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/MoleculeDescriptor.hpp> | 
|---|
|  | 16 | #include <Descriptors/MoleculeIdDescriptor.hpp> | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "World.hpp" | 
|---|
|  | 19 | #include "molecule.hpp" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 22 | #include "UnitTestMain.hpp" | 
|---|
|  | 23 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 24 |  | 
|---|
|  | 25 | /********************************************** Test classes **************************************/ | 
|---|
|  | 26 | // Registers the fixture into the 'registry' | 
|---|
|  | 27 | CPPUNIT_TEST_SUITE_REGISTRATION( MoleculeDescriptorTest ); | 
|---|
|  | 28 |  | 
|---|
|  | 29 | // set up and tear down | 
|---|
|  | 30 | void MoleculeDescriptorTest::setUp(){ | 
|---|
| [23b547] | 31 | World::getInstance(); | 
|---|
| [57adc7] | 32 | for(int i=0;i<MOLECULE_COUNT;++i){ | 
|---|
| [23b547] | 33 | molecules[i]= World::getInstance().createMolecule(); | 
|---|
| [57adc7] | 34 | moleculeIds[i]= molecules[i]->getId(); | 
|---|
|  | 35 | } | 
|---|
|  | 36 | } | 
|---|
|  | 37 |  | 
|---|
|  | 38 | void MoleculeDescriptorTest::tearDown(){ | 
|---|
| [23b547] | 39 | World::purgeInstance(); | 
|---|
| [57adc7] | 40 | } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // some helper functions | 
|---|
|  | 43 | static bool hasAllMolecules(std::vector<molecule*> molecules,moleculeId_t ids[MOLECULE_COUNT], std::set<moleculeId_t> excluded = std::set<moleculeId_t>()){ | 
|---|
|  | 44 | for(int i=0;i<MOLECULE_COUNT;++i){ | 
|---|
|  | 45 | moleculeId_t id = ids[i]; | 
|---|
|  | 46 | if(!excluded.count(id)){ | 
|---|
|  | 47 | std::vector<molecule*>::iterator iter; | 
|---|
|  | 48 | bool res=false; | 
|---|
|  | 49 | for(iter=molecules.begin();iter!=molecules.end();++iter){ | 
|---|
|  | 50 | res |= (*iter)->getId() == id; | 
|---|
|  | 51 | } | 
|---|
|  | 52 | if(!res) { | 
|---|
|  | 53 | cout << "Molecule " << id << " missing in returned list" << endl; | 
|---|
|  | 54 | return false; | 
|---|
|  | 55 | } | 
|---|
|  | 56 | } | 
|---|
|  | 57 | } | 
|---|
|  | 58 | return true; | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | static bool hasNoDuplicateMolecules(std::vector<molecule*> molecules){ | 
|---|
|  | 62 | std::set<moleculeId_t> found; | 
|---|
|  | 63 | std::vector<molecule*>::iterator iter; | 
|---|
|  | 64 | for(iter=molecules.begin();iter!=molecules.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 |  | 
|---|
|  | 74 | void MoleculeDescriptorTest::MoleculeBaseSetsTest(){ | 
|---|
| [23b547] | 75 | std::vector<molecule*> allMolecules = World::getInstance().getAllMolecules(AllMolecules()); | 
|---|
| [57adc7] | 76 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds)); | 
|---|
|  | 77 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules)); | 
|---|
|  | 78 |  | 
|---|
| [23b547] | 79 | std::vector<molecule*> noMolecules = World::getInstance().getAllMolecules(NoMolecules()); | 
|---|
| [57adc7] | 80 | CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty()); | 
|---|
|  | 81 | } | 
|---|
|  | 82 | void MoleculeDescriptorTest::MoleculeIdTest(){ | 
|---|
|  | 83 | // test Molecules from boundaries and middle of the set | 
|---|
|  | 84 | molecule* testMolecule; | 
|---|
| [23b547] | 85 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[0])); | 
|---|
| [57adc7] | 86 | CPPUNIT_ASSERT(testMolecule); | 
|---|
|  | 87 | CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId()); | 
|---|
| [23b547] | 88 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2])); | 
|---|
| [57adc7] | 89 | CPPUNIT_ASSERT(testMolecule); | 
|---|
|  | 90 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId()); | 
|---|
| [23b547] | 91 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1])); | 
|---|
| [57adc7] | 92 | CPPUNIT_ASSERT(testMolecule); | 
|---|
|  | 93 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId()); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | // find some ID that has not been created | 
|---|
|  | 96 | moleculeId_t outsideId=0; | 
|---|
|  | 97 | bool res = false; | 
|---|
|  | 98 | for(outsideId=0;!res;++outsideId) { | 
|---|
|  | 99 | res = true; | 
|---|
|  | 100 | for(int i = 0; i < MOLECULE_COUNT; ++i){ | 
|---|
|  | 101 | res &= moleculeIds[i]!=outsideId; | 
|---|
|  | 102 | } | 
|---|
|  | 103 | } | 
|---|
|  | 104 | // test from outside of set | 
|---|
| [23b547] | 105 | testMolecule = World::getInstance().getMolecule(MoleculeById(outsideId)); | 
|---|
| [57adc7] | 106 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
|  | 107 | } | 
|---|
|  | 108 | void MoleculeDescriptorTest::MoleculeCalcTest(){ | 
|---|
|  | 109 | // test some elementary set operations | 
|---|
|  | 110 | { | 
|---|
| [23b547] | 111 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()||NoMolecules()); | 
|---|
| [57adc7] | 112 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
|  | 113 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | { | 
|---|
| [23b547] | 117 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||AllMolecules()); | 
|---|
| [57adc7] | 118 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
|  | 119 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | { | 
|---|
| [23b547] | 123 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()&&AllMolecules()); | 
|---|
| [57adc7] | 124 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
|  | 127 | { | 
|---|
| [23b547] | 128 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&NoMolecules()); | 
|---|
| [57adc7] | 129 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | { | 
|---|
| [23b547] | 133 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!AllMolecules()); | 
|---|
| [57adc7] | 134 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | { | 
|---|
| [23b547] | 138 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!NoMolecules()); | 
|---|
| [57adc7] | 139 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
|  | 140 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
|  | 141 | } | 
|---|
|  | 142 |  | 
|---|
|  | 143 | // exclude and include some molecules | 
|---|
|  | 144 | { | 
|---|
| [23b547] | 145 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2]))); | 
|---|
| [57adc7] | 146 | std::set<moleculeId_t> excluded; | 
|---|
|  | 147 | excluded.insert(moleculeIds[MOLECULE_COUNT/2]); | 
|---|
|  | 148 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded)); | 
|---|
|  | 149 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
|  | 150 | CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size()); | 
|---|
|  | 151 | } | 
|---|
|  | 152 |  | 
|---|
|  | 153 | { | 
|---|
| [23b547] | 154 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2]))); | 
|---|
| [57adc7] | 155 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size()); | 
|---|
|  | 156 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId()); | 
|---|
|  | 157 | } | 
|---|
|  | 158 | } | 
|---|