| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
| [57adc7] | 23 | /*
 | 
|---|
 | 24 |  * MoleculeDescriptorTest.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Mar 4, 2010
 | 
|---|
 | 27 |  *      Author: crueger
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [57adc7] | 35 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 36 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 37 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 38 | #include <iostream>
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | #include <Descriptors/MoleculeDescriptor.hpp>
 | 
|---|
 | 41 | #include <Descriptors/MoleculeIdDescriptor.hpp>
 | 
|---|
| [12c4cb] | 42 | #include <Descriptors/MoleculeNameDescriptor.hpp>
 | 
|---|
 | 43 | #include <Descriptors/MoleculeOrderDescriptor.hpp>
 | 
|---|
| [57adc7] | 44 | 
 | 
|---|
 | 45 | #include "World.hpp"
 | 
|---|
 | 46 | #include "molecule.hpp"
 | 
|---|
 | 47 | 
 | 
|---|
| [6c9adc] | 48 | #include "MoleculeDescriptorUnitTest.hpp"
 | 
|---|
 | 49 | 
 | 
|---|
| [57adc7] | 50 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 51 | #include "UnitTestMain.hpp"
 | 
|---|
 | 52 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | /********************************************** Test classes **************************************/
 | 
|---|
 | 55 | // Registers the fixture into the 'registry'
 | 
|---|
 | 56 | CPPUNIT_TEST_SUITE_REGISTRATION( MoleculeDescriptorTest );
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | // set up and tear down
 | 
|---|
 | 59 | void MoleculeDescriptorTest::setUp(){
 | 
|---|
| [23b547] | 60 |   World::getInstance();
 | 
|---|
| [57adc7] | 61 |   for(int i=0;i<MOLECULE_COUNT;++i){
 | 
|---|
| [23b547] | 62 |     molecules[i]= World::getInstance().createMolecule();
 | 
|---|
| [57adc7] | 63 |     moleculeIds[i]= molecules[i]->getId();
 | 
|---|
 | 64 |   }
 | 
|---|
 | 65 | }
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | void MoleculeDescriptorTest::tearDown(){
 | 
|---|
| [23b547] | 68 |   World::purgeInstance();
 | 
|---|
| [57adc7] | 69 | }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | // some helper functions
 | 
|---|
| [1259df] | 72 | static bool hasAllMolecules(
 | 
|---|
 | 73 |     std::vector<const molecule*> &molecules,
 | 
|---|
 | 74 |     moleculeId_t ids[MOLECULE_COUNT],
 | 
|---|
 | 75 |     std::set<moleculeId_t> excluded = std::set<moleculeId_t>())
 | 
|---|
 | 76 | {
 | 
|---|
| [57adc7] | 77 |   for(int i=0;i<MOLECULE_COUNT;++i){
 | 
|---|
 | 78 |     moleculeId_t id = ids[i];
 | 
|---|
 | 79 |     if(!excluded.count(id)){
 | 
|---|
| [1259df] | 80 |       std::vector<const molecule*>::const_iterator iter;
 | 
|---|
| [57adc7] | 81 |       bool res=false;
 | 
|---|
 | 82 |       for(iter=molecules.begin();iter!=molecules.end();++iter){
 | 
|---|
 | 83 |         res |= (*iter)->getId() == id;
 | 
|---|
 | 84 |       }
 | 
|---|
 | 85 |       if(!res) {
 | 
|---|
 | 86 |         cout << "Molecule " << id << " missing in returned list" << endl;
 | 
|---|
 | 87 |         return false;
 | 
|---|
 | 88 |       }
 | 
|---|
 | 89 |     }
 | 
|---|
 | 90 |   }
 | 
|---|
 | 91 |   return true;
 | 
|---|
 | 92 | }
 | 
|---|
 | 93 | 
 | 
|---|
| [1259df] | 94 | static bool hasNoDuplicateMolecules(std::vector<const molecule*> &molecules){
 | 
|---|
| [57adc7] | 95 |   std::set<moleculeId_t> found;
 | 
|---|
| [1259df] | 96 |   std::vector<const molecule*>::const_iterator iter;
 | 
|---|
| [57adc7] | 97 |   for(iter=molecules.begin();iter!=molecules.end();++iter){
 | 
|---|
 | 98 |     int id = (*iter)->getId();
 | 
|---|
 | 99 |     if(found.count(id))
 | 
|---|
 | 100 |       return false;
 | 
|---|
 | 101 |     found.insert(id);
 | 
|---|
 | 102 |   }
 | 
|---|
 | 103 |   return true;
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | void MoleculeDescriptorTest::MoleculeBaseSetsTest(){
 | 
|---|
| [1259df] | 108 |   std::vector<const molecule*> allMolecules =
 | 
|---|
 | 109 |       const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules());
 | 
|---|
| [57adc7] | 110 |   CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds));
 | 
|---|
 | 111 |   CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules));
 | 
|---|
 | 112 | 
 | 
|---|
| [1259df] | 113 |   std::vector<const molecule*> noMolecules =
 | 
|---|
 | 114 |       const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules());
 | 
|---|
| [57adc7] | 115 |   CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty());
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | void MoleculeDescriptorTest::MoleculeIdTest(){
 | 
|---|
 | 118 |   // test Molecules from boundaries and middle of the set
 | 
|---|
| [63fb7a] | 119 |   const molecule* testMolecule;
 | 
|---|
 | 120 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 121 |       getMolecule(MoleculeById(moleculeIds[0]));
 | 
|---|
| [57adc7] | 122 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
 | 123 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId());
 | 
|---|
| [63fb7a] | 124 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 125 |       getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2]));
 | 
|---|
| [57adc7] | 126 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
 | 127 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId());
 | 
|---|
| [63fb7a] | 128 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 129 |       getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1]));
 | 
|---|
| [57adc7] | 130 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
 | 131 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId());
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |   // find some ID that has not been created
 | 
|---|
 | 134 |   moleculeId_t outsideId=0;
 | 
|---|
 | 135 |   bool res = false;
 | 
|---|
 | 136 |   for(outsideId=0;!res;++outsideId) {
 | 
|---|
 | 137 |     res = true;
 | 
|---|
 | 138 |     for(int i = 0; i < MOLECULE_COUNT; ++i){
 | 
|---|
 | 139 |       res &= moleculeIds[i]!=outsideId;
 | 
|---|
 | 140 |     }
 | 
|---|
 | 141 |   }
 | 
|---|
 | 142 |   // test from outside of set
 | 
|---|
| [63fb7a] | 143 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 144 |       getMolecule(MoleculeById(outsideId));
 | 
|---|
| [57adc7] | 145 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
 | 146 | }
 | 
|---|
 | 147 | void MoleculeDescriptorTest::MoleculeCalcTest(){
 | 
|---|
 | 148 |   // test some elementary set operations
 | 
|---|
 | 149 |   {
 | 
|---|
| [1259df] | 150 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 151 |         const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()||NoMolecules());
 | 
|---|
| [57adc7] | 152 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
 | 153 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 154 |   }
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 |   {
 | 
|---|
| [1259df] | 157 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 158 |         const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||AllMolecules());
 | 
|---|
| [57adc7] | 159 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
 | 160 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 161 |   }
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |   {
 | 
|---|
| [1259df] | 164 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 165 |         const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()&&AllMolecules());
 | 
|---|
| [57adc7] | 166 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
 | 167 |   }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 |   {
 | 
|---|
| [1259df] | 170 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 171 |         const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&NoMolecules());
 | 
|---|
| [57adc7] | 172 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
 | 173 |   }
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 |   {
 | 
|---|
| [1259df] | 176 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 177 |         const_cast<const World &>(World::getInstance()).getAllMolecules(!AllMolecules());
 | 
|---|
| [57adc7] | 178 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
 | 179 |   }
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |   {
 | 
|---|
| [1259df] | 182 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 183 |         const_cast<const World &>(World::getInstance()).getAllMolecules(!NoMolecules());
 | 
|---|
| [57adc7] | 184 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
 | 185 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 186 |   }
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 |   // exclude and include some molecules
 | 
|---|
 | 189 |   {
 | 
|---|
| [1259df] | 190 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 191 |         const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
 | 
|---|
| [57adc7] | 192 |     std::set<moleculeId_t> excluded;
 | 
|---|
 | 193 |     excluded.insert(moleculeIds[MOLECULE_COUNT/2]);
 | 
|---|
 | 194 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded));
 | 
|---|
 | 195 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 196 |     CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size());
 | 
|---|
 | 197 |   }
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 |   {
 | 
|---|
| [1259df] | 200 |     std::vector<const molecule*> testMolecules =
 | 
|---|
 | 201 |         const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
 | 
|---|
| [57adc7] | 202 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size());
 | 
|---|
 | 203 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId());
 | 
|---|
 | 204 |   }
 | 
|---|
 | 205 | }
 | 
|---|
| [12c4cb] | 206 | 
 | 
|---|
 | 207 | void MoleculeDescriptorTest::MoleculeNameTest()
 | 
|---|
 | 208 | {
 | 
|---|
| [63fb7a] | 209 |   const molecule* testMolecule;
 | 
|---|
| [12c4cb] | 210 | 
 | 
|---|
 | 211 |   // name each molecule
 | 
|---|
 | 212 |   for(int i=1;i<=MOLECULE_COUNT;++i)
 | 
|---|
 | 213 |     molecules[i-1]->setName(toString(i));
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |   // retrieve each
 | 
|---|
 | 216 |   for(int i=1;i<=MOLECULE_COUNT;++i) {
 | 
|---|
| [63fb7a] | 217 |     testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 218 |         getMolecule(MoleculeByName(toString(i)));
 | 
|---|
| [12c4cb] | 219 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
 | 
|---|
 | 220 |   }
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 |   // check for non-present name
 | 
|---|
| [63fb7a] | 223 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 224 |       getMolecule(MoleculeByName("not present"));
 | 
|---|
| [12c4cb] | 225 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
 | 226 | }
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 | 
 | 
|---|
 | 229 | void MoleculeDescriptorTest::MoleculeOrderTest()
 | 
|---|
 | 230 | {
 | 
|---|
| [63fb7a] | 231 |   const molecule* testMolecule;
 | 
|---|
| [12c4cb] | 232 | 
 | 
|---|
 | 233 |   // test in normal order: 1, 2, ...
 | 
|---|
 | 234 |   for(int i=1;i<=MOLECULE_COUNT;++i){
 | 
|---|
| [63fb7a] | 235 |     testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 236 |         getMolecule(MoleculeByOrder(i));
 | 
|---|
| [12c4cb] | 237 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
 | 
|---|
 | 238 |   }
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 |   // test in reverse order: -1, -2, ...
 | 
|---|
 | 241 |   for(int i=1; i<= MOLECULE_COUNT;++i){
 | 
|---|
| [63fb7a] | 242 |     testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 243 |         getMolecule(MoleculeByOrder(-i));
 | 
|---|
| [12c4cb] | 244 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId());
 | 
|---|
 | 245 |   }
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 |   // test from outside of set
 | 
|---|
| [63fb7a] | 248 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 249 |       getMolecule(MoleculeByOrder(MOLECULE_COUNT+1));
 | 
|---|
| [12c4cb] | 250 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
| [63fb7a] | 251 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 252 |       getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1));
 | 
|---|
| [12c4cb] | 253 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
 | 254 | }
 | 
|---|