| 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(std::vector<molecule*> molecules,moleculeId_t ids[MOLECULE_COUNT], std::set<moleculeId_t> excluded = std::set<moleculeId_t>()){ | 
|---|
| 73 | for(int i=0;i<MOLECULE_COUNT;++i){ | 
|---|
| 74 | moleculeId_t id = ids[i]; | 
|---|
| 75 | if(!excluded.count(id)){ | 
|---|
| 76 | std::vector<molecule*>::iterator iter; | 
|---|
| 77 | bool res=false; | 
|---|
| 78 | for(iter=molecules.begin();iter!=molecules.end();++iter){ | 
|---|
| 79 | res |= (*iter)->getId() == id; | 
|---|
| 80 | } | 
|---|
| 81 | if(!res) { | 
|---|
| 82 | cout << "Molecule " << id << " missing in returned list" << endl; | 
|---|
| 83 | return false; | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 | return true; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | static bool hasNoDuplicateMolecules(std::vector<molecule*> molecules){ | 
|---|
| 91 | std::set<moleculeId_t> found; | 
|---|
| 92 | std::vector<molecule*>::iterator iter; | 
|---|
| 93 | for(iter=molecules.begin();iter!=molecules.end();++iter){ | 
|---|
| 94 | int id = (*iter)->getId(); | 
|---|
| 95 | if(found.count(id)) | 
|---|
| 96 | return false; | 
|---|
| 97 | found.insert(id); | 
|---|
| 98 | } | 
|---|
| 99 | return true; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | void MoleculeDescriptorTest::MoleculeBaseSetsTest(){ | 
|---|
| 104 | std::vector<molecule*> allMolecules = World::getInstance().getAllMolecules(AllMolecules()); | 
|---|
| 105 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds)); | 
|---|
| 106 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules)); | 
|---|
| 107 |  | 
|---|
| 108 | std::vector<molecule*> noMolecules = World::getInstance().getAllMolecules(NoMolecules()); | 
|---|
| 109 | CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty()); | 
|---|
| 110 | } | 
|---|
| 111 | void MoleculeDescriptorTest::MoleculeIdTest(){ | 
|---|
| 112 | // test Molecules from boundaries and middle of the set | 
|---|
| 113 | molecule* testMolecule; | 
|---|
| 114 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[0])); | 
|---|
| 115 | CPPUNIT_ASSERT(testMolecule); | 
|---|
| 116 | CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId()); | 
|---|
| 117 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2])); | 
|---|
| 118 | CPPUNIT_ASSERT(testMolecule); | 
|---|
| 119 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId()); | 
|---|
| 120 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1])); | 
|---|
| 121 | CPPUNIT_ASSERT(testMolecule); | 
|---|
| 122 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId()); | 
|---|
| 123 |  | 
|---|
| 124 | // find some ID that has not been created | 
|---|
| 125 | moleculeId_t outsideId=0; | 
|---|
| 126 | bool res = false; | 
|---|
| 127 | for(outsideId=0;!res;++outsideId) { | 
|---|
| 128 | res = true; | 
|---|
| 129 | for(int i = 0; i < MOLECULE_COUNT; ++i){ | 
|---|
| 130 | res &= moleculeIds[i]!=outsideId; | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 | // test from outside of set | 
|---|
| 134 | testMolecule = World::getInstance().getMolecule(MoleculeById(outsideId)); | 
|---|
| 135 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 136 | } | 
|---|
| 137 | void MoleculeDescriptorTest::MoleculeCalcTest(){ | 
|---|
| 138 | // test some elementary set operations | 
|---|
| 139 | { | 
|---|
| 140 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()||NoMolecules()); | 
|---|
| 141 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | { | 
|---|
| 146 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||AllMolecules()); | 
|---|
| 147 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
| 148 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | { | 
|---|
| 152 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()&&AllMolecules()); | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | { | 
|---|
| 157 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&NoMolecules()); | 
|---|
| 158 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | { | 
|---|
| 162 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!AllMolecules()); | 
|---|
| 163 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | { | 
|---|
| 167 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!NoMolecules()); | 
|---|
| 168 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
| 169 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | // exclude and include some molecules | 
|---|
| 173 | { | 
|---|
| 174 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2]))); | 
|---|
| 175 | std::set<moleculeId_t> excluded; | 
|---|
| 176 | excluded.insert(moleculeIds[MOLECULE_COUNT/2]); | 
|---|
| 177 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded)); | 
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 179 | CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size()); | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | { | 
|---|
| 183 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2]))); | 
|---|
| 184 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size()); | 
|---|
| 185 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId()); | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | void MoleculeDescriptorTest::MoleculeNameTest() | 
|---|
| 190 | { | 
|---|
| 191 | molecule* testMolecule; | 
|---|
| 192 |  | 
|---|
| 193 | // name each molecule | 
|---|
| 194 | for(int i=1;i<=MOLECULE_COUNT;++i) | 
|---|
| 195 | molecules[i-1]->setName(toString(i)); | 
|---|
| 196 |  | 
|---|
| 197 | // retrieve each | 
|---|
| 198 | for(int i=1;i<=MOLECULE_COUNT;++i) { | 
|---|
| 199 | testMolecule = World::getInstance().getMolecule(MoleculeByName(toString(i))); | 
|---|
| 200 | CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId()); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | // check for non-present name | 
|---|
| 204 | testMolecule = World::getInstance().getMolecule(MoleculeByName("not present")); | 
|---|
| 205 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 |  | 
|---|
| 209 | void MoleculeDescriptorTest::MoleculeOrderTest() | 
|---|
| 210 | { | 
|---|
| 211 | molecule* testMolecule; | 
|---|
| 212 |  | 
|---|
| 213 | // test in normal order: 1, 2, ... | 
|---|
| 214 | for(int i=1;i<=MOLECULE_COUNT;++i){ | 
|---|
| 215 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(i)); | 
|---|
| 216 | CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId()); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | // test in reverse order: -1, -2, ... | 
|---|
| 220 | for(int i=1; i<= MOLECULE_COUNT;++i){ | 
|---|
| 221 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(-i)); | 
|---|
| 222 | CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId()); | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | // test from outside of set | 
|---|
| 226 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(MOLECULE_COUNT+1)); | 
|---|
| 227 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 228 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1)); | 
|---|
| 229 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 230 | } | 
|---|