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