| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * MoleculeDescriptorTest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 4, 2010
 | 
|---|
| 27 |  *      Author: crueger
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 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>
 | 
|---|
| 42 | #include <Descriptors/MoleculeNameDescriptor.hpp>
 | 
|---|
| 43 | #include <Descriptors/MoleculeOrderDescriptor.hpp>
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "World.hpp"
 | 
|---|
| 46 | #include "molecule.hpp"
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "MoleculeDescriptorUnitTest.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 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(){
 | 
|---|
| 60 |   World::getInstance();
 | 
|---|
| 61 |   for(int i=0;i<MOLECULE_COUNT;++i){
 | 
|---|
| 62 |     molecules[i]= World::getInstance().createMolecule();
 | 
|---|
| 63 |     moleculeIds[i]= molecules[i]->getId();
 | 
|---|
| 64 |   }
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | void MoleculeDescriptorTest::tearDown(){
 | 
|---|
| 68 |   World::purgeInstance();
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | // some helper functions
 | 
|---|
| 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 | {
 | 
|---|
| 77 |   for(int i=0;i<MOLECULE_COUNT;++i){
 | 
|---|
| 78 |     moleculeId_t id = ids[i];
 | 
|---|
| 79 |     if(!excluded.count(id)){
 | 
|---|
| 80 |       std::vector<const molecule*>::const_iterator iter;
 | 
|---|
| 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 | 
 | 
|---|
| 94 | static bool hasNoDuplicateMolecules(std::vector<const molecule*> &molecules){
 | 
|---|
| 95 |   std::set<moleculeId_t> found;
 | 
|---|
| 96 |   std::vector<const molecule*>::const_iterator iter;
 | 
|---|
| 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(){
 | 
|---|
| 108 |   std::vector<const molecule*> allMolecules =
 | 
|---|
| 109 |       const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules());
 | 
|---|
| 110 |   CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds));
 | 
|---|
| 111 |   CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules));
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   std::vector<const molecule*> noMolecules =
 | 
|---|
| 114 |       const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules());
 | 
|---|
| 115 |   CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty());
 | 
|---|
| 116 | }
 | 
|---|
| 117 | void MoleculeDescriptorTest::MoleculeIdTest(){
 | 
|---|
| 118 |   // test Molecules from boundaries and middle of the set
 | 
|---|
| 119 |   const molecule* testMolecule;
 | 
|---|
| 120 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 121 |       getMolecule(MoleculeById(moleculeIds[0]));
 | 
|---|
| 122 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
| 123 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId());
 | 
|---|
| 124 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 125 |       getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2]));
 | 
|---|
| 126 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
| 127 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId());
 | 
|---|
| 128 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 129 |       getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1]));
 | 
|---|
| 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
 | 
|---|
| 143 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 144 |       getMolecule(MoleculeById(outsideId));
 | 
|---|
| 145 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
| 146 | }
 | 
|---|
| 147 | void MoleculeDescriptorTest::MoleculeCalcTest(){
 | 
|---|
| 148 |   // test some elementary set operations
 | 
|---|
| 149 |   {
 | 
|---|
| 150 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 151 |         const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()||NoMolecules());
 | 
|---|
| 152 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
| 153 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
| 154 |   }
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   {
 | 
|---|
| 157 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 158 |         const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||AllMolecules());
 | 
|---|
| 159 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
| 160 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
| 161 |   }
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   {
 | 
|---|
| 164 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 165 |         const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()&&AllMolecules());
 | 
|---|
| 166 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
| 167 |   }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   {
 | 
|---|
| 170 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 171 |         const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&NoMolecules());
 | 
|---|
| 172 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
| 173 |   }
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   {
 | 
|---|
| 176 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 177 |         const_cast<const World &>(World::getInstance()).getAllMolecules(!AllMolecules());
 | 
|---|
| 178 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
| 179 |   }
 | 
|---|
| 180 | 
 | 
|---|
| 181 |   {
 | 
|---|
| 182 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 183 |         const_cast<const World &>(World::getInstance()).getAllMolecules(!NoMolecules());
 | 
|---|
| 184 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
| 185 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
| 186 |   }
 | 
|---|
| 187 | 
 | 
|---|
| 188 |   // exclude and include some molecules
 | 
|---|
| 189 |   {
 | 
|---|
| 190 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 191 |         const_cast<const World &>(World::getInstance()).getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
 | 
|---|
| 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 |   {
 | 
|---|
| 200 |     std::vector<const molecule*> testMolecules =
 | 
|---|
| 201 |         const_cast<const World &>(World::getInstance()).getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
 | 
|---|
| 202 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size());
 | 
|---|
| 203 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId());
 | 
|---|
| 204 |   }
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | void MoleculeDescriptorTest::MoleculeNameTest()
 | 
|---|
| 208 | {
 | 
|---|
| 209 |   const molecule* testMolecule;
 | 
|---|
| 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) {
 | 
|---|
| 217 |     testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 218 |         getMolecule(MoleculeByName(toString(i)));
 | 
|---|
| 219 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId());
 | 
|---|
| 220 |   }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |   // check for non-present name
 | 
|---|
| 223 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 224 |       getMolecule(MoleculeByName("not present"));
 | 
|---|
| 225 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
| 226 | }
 | 
|---|
| 227 | 
 | 
|---|
| 228 | 
 | 
|---|
| 229 | void MoleculeDescriptorTest::MoleculeOrderTest()
 | 
|---|
| 230 | {
 | 
|---|
| 231 |   const molecule* testMolecule;
 | 
|---|
| 232 | 
 | 
|---|
| 233 |   // test in normal order: 1, 2, ...
 | 
|---|
| 234 |   for(int i=1;i<=MOLECULE_COUNT;++i){
 | 
|---|
| 235 |     testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 236 |         getMolecule(MoleculeByOrder(i));
 | 
|---|
| 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){
 | 
|---|
| 242 |     testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 243 |         getMolecule(MoleculeByOrder(-i));
 | 
|---|
| 244 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId());
 | 
|---|
| 245 |   }
 | 
|---|
| 246 | 
 | 
|---|
| 247 |   // test from outside of set
 | 
|---|
| 248 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 249 |       getMolecule(MoleculeByOrder(MOLECULE_COUNT+1));
 | 
|---|
| 250 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
| 251 |   testMolecule = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 252 |       getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1));
 | 
|---|
| 253 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
| 254 | }
 | 
|---|