| 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 | * ListOfBondsUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: 18 Oct 2009 | 
|---|
| 27 | *      Author: user | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include <cstring> | 
|---|
| 42 |  | 
|---|
| 43 | #include "CodePatterns/Log.hpp" | 
|---|
| 44 | #include "World.hpp" | 
|---|
| 45 | #include "Atom/atom.hpp" | 
|---|
| 46 | #include "Atom/AtomObserver.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | #include "Bond/bond.hpp" | 
|---|
| 49 | #include "Element/element.hpp" | 
|---|
| 50 | #include "molecule.hpp" | 
|---|
| 51 | #include "Element/periodentafel.hpp" | 
|---|
| 52 | #include "World.hpp" | 
|---|
| 53 | #include "WorldTime.hpp" | 
|---|
| 54 |  | 
|---|
| 55 | #include "ListOfBondsUnitTest.hpp" | 
|---|
| 56 |  | 
|---|
| 57 | #ifdef HAVE_TESTRUNNER | 
|---|
| 58 | #include "UnitTestMain.hpp" | 
|---|
| 59 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 60 |  | 
|---|
| 61 | /********************************************** Test classes **************************************/ | 
|---|
| 62 |  | 
|---|
| 63 | // Registers the fixture into the 'registry' | 
|---|
| 64 | CPPUNIT_TEST_SUITE_REGISTRATION( ListOfBondsTest ); | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | void ListOfBondsTest::setUp() | 
|---|
| 68 | { | 
|---|
| 69 | atom *Walker = NULL; | 
|---|
| 70 |  | 
|---|
| 71 | WorldTime::getInstance().setTime(0); | 
|---|
| 72 |  | 
|---|
| 73 | // construct element | 
|---|
| 74 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
| 75 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen"); | 
|---|
| 76 |  | 
|---|
| 77 | // construct molecule (tetraeder of hydrogens) | 
|---|
| 78 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| 79 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule"); | 
|---|
| 80 | Walker = World::getInstance().createAtom(); | 
|---|
| 81 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 82 | Walker->setType(hydrogen); | 
|---|
| 83 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| 84 | TestMolecule->AddAtom(Walker); | 
|---|
| 85 | Walker = World::getInstance().createAtom(); | 
|---|
| 86 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 87 | Walker->setType(hydrogen); | 
|---|
| 88 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| 89 | TestMolecule->AddAtom(Walker); | 
|---|
| 90 | Walker = World::getInstance().createAtom(); | 
|---|
| 91 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 92 | Walker->setType(hydrogen); | 
|---|
| 93 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| 94 | TestMolecule->AddAtom(Walker); | 
|---|
| 95 | Walker = World::getInstance().createAtom(); | 
|---|
| 96 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 97 | Walker->setType(hydrogen); | 
|---|
| 98 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| 99 | TestMolecule->AddAtom(Walker); | 
|---|
| 100 |  | 
|---|
| 101 | // check that TestMolecule was correctly constructed | 
|---|
| 102 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 | void ListOfBondsTest::tearDown() | 
|---|
| 107 | { | 
|---|
| 108 | // remove | 
|---|
| 109 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| 110 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
| 111 | // are all cleaned when the world is destroyed | 
|---|
| 112 | World::purgeInstance(); | 
|---|
| 113 | AtomObserver::purgeInstance(); | 
|---|
| 114 | logger::purgeInstance(); | 
|---|
| 115 | errorLogger::purgeInstance(); | 
|---|
| 116 | WorldTime::purgeInstance(); | 
|---|
| 117 |  | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | /** Tests whether setup worked correctly. | 
|---|
| 121 | * | 
|---|
| 122 | */ | 
|---|
| 123 | void ListOfBondsTest::SetupTest() | 
|---|
| 124 | { | 
|---|
| 125 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->empty() ); | 
|---|
| 126 | CPPUNIT_ASSERT_EQUAL( (size_t)4, TestMolecule->size() ); | 
|---|
| 127 | }; | 
|---|
| 128 |  | 
|---|
| 129 | /** Unit Test of molecule::AddBond() | 
|---|
| 130 | * | 
|---|
| 131 | */ | 
|---|
| 132 | void ListOfBondsTest::AddingBondTest() | 
|---|
| 133 | { | 
|---|
| 134 | bond::ptr Binder; | 
|---|
| 135 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 136 | atom *atom1 = *iter; | 
|---|
| 137 | iter++; | 
|---|
| 138 | atom *atom2 = *iter; | 
|---|
| 139 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 140 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 141 |  | 
|---|
| 142 | // add bond | 
|---|
| 143 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 144 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 145 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() ); | 
|---|
| 146 |  | 
|---|
| 147 | // check that bond contains the two atoms | 
|---|
| 148 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom1) ); | 
|---|
| 149 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom2) ); | 
|---|
| 150 |  | 
|---|
| 151 | // check that bond is present in both atoms | 
|---|
| 152 | const BondList &bondlist1 = atom1->getListOfBonds(); | 
|---|
| 153 | BondList::const_iterator bonditer; | 
|---|
| 154 | bonditer = bondlist1.begin(); | 
|---|
| 155 | bond::ptr TestBond1 = *bonditer; | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL( TestBond1, Binder ); | 
|---|
| 157 | const BondList &bondlist2 = atom2->getListOfBonds(); | 
|---|
| 158 | bonditer = bondlist2.begin(); | 
|---|
| 159 | bond::ptr TestBond2 = *bonditer; | 
|---|
| 160 | CPPUNIT_ASSERT_EQUAL( TestBond2, Binder ); | 
|---|
| 161 | }; | 
|---|
| 162 |  | 
|---|
| 163 | /** Unit Test of delete(bond::ptr ) | 
|---|
| 164 | * | 
|---|
| 165 | */ | 
|---|
| 166 | void ListOfBondsTest::DeleteBondTest() | 
|---|
| 167 | { | 
|---|
| 168 | bond::ptr Binder; | 
|---|
| 169 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 170 | atom *atom1 = *iter; | 
|---|
| 171 | iter++; | 
|---|
| 172 | atom *atom2 = *iter; | 
|---|
| 173 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 174 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 175 |  | 
|---|
| 176 | // add bond | 
|---|
| 177 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 178 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 179 |  | 
|---|
| 180 | // remove bond | 
|---|
| 181 | atom1->removeBond(Binder); | 
|---|
| 182 | // removed for atom2 automatically but check where nothing breaks | 
|---|
| 183 | atom2->removeBond(Binder); | 
|---|
| 184 |  | 
|---|
| 185 | // check if removed from atoms | 
|---|
| 186 | { | 
|---|
| 187 | const BondList& ListOfBonds = atom1->getListOfBonds(); | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() ); | 
|---|
| 189 | } | 
|---|
| 190 | { | 
|---|
| 191 | const BondList& ListOfBonds = atom2->getListOfBonds(); | 
|---|
| 192 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() ); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | // check if removed from molecule | 
|---|
| 196 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 197 | }; | 
|---|
| 198 |  | 
|---|
| 199 | /** Unit Test of delete(atom *) | 
|---|
| 200 | * | 
|---|
| 201 | */ | 
|---|
| 202 | void ListOfBondsTest::DeleteAtomTest() | 
|---|
| 203 | { | 
|---|
| 204 | atom *atom1 = NULL; | 
|---|
| 205 | atom *atom2 = NULL; | 
|---|
| 206 | bond::ptr Binder; | 
|---|
| 207 | { | 
|---|
| 208 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 209 | atom1 = *iter; | 
|---|
| 210 | iter++; | 
|---|
| 211 | atom2 = *iter; | 
|---|
| 212 | } | 
|---|
| 213 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 214 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 215 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->getListOfBonds().size() ); | 
|---|
| 216 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->getListOfBonds().size() ); | 
|---|
| 217 |  | 
|---|
| 218 | // add bond | 
|---|
| 219 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 220 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 221 |  | 
|---|
| 222 | // access test via CurrentTime | 
|---|
| 223 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom1->getListOfBonds().size() ); | 
|---|
| 224 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->getListOfBonds().size() ); | 
|---|
| 225 |  | 
|---|
| 226 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
| 227 |  | 
|---|
| 228 | // remove atom2 | 
|---|
| 229 | World::getInstance().destroyAtom(atom2); | 
|---|
| 230 |  | 
|---|
| 231 | // check bond if removed from other atom for all time steps | 
|---|
| 232 | { | 
|---|
| 233 | const BondList& ListOfBonds = atom1->getListOfBonds(); | 
|---|
| 234 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() ); | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | // check if removed from molecule | 
|---|
| 238 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 239 | }; | 
|---|
| 240 |  | 
|---|
| 241 | /** Unit test on ListOfBonds at multiple time steps. | 
|---|
| 242 | * | 
|---|
| 243 | */ | 
|---|
| 244 | void ListOfBondsTest::MultipleTimeStepTest() | 
|---|
| 245 | { | 
|---|
| 246 | atom *atom1 = NULL; | 
|---|
| 247 | atom *atom2 = NULL; | 
|---|
| 248 | bond::ptr Binder; | 
|---|
| 249 | { | 
|---|
| 250 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 251 | atom1 = *iter; | 
|---|
| 252 | iter++; | 
|---|
| 253 | atom2 = *iter; | 
|---|
| 254 | } | 
|---|
| 255 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 256 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 257 |  | 
|---|
| 258 | // add bond | 
|---|
| 259 | WorldTime::getInstance().setTime(0); | 
|---|
| 260 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 261 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 262 | WorldTime::getInstance().setTime(1); | 
|---|
| 263 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 264 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 265 |  | 
|---|
| 266 | // access test via CurrentTime | 
|---|
| 267 | { // time step 0 | 
|---|
| 268 | WorldTime::getInstance().setTime(0); | 
|---|
| 269 | { | 
|---|
| 270 | const BondList& ListOfBonds = atom1->getListOfBonds(); | 
|---|
| 271 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 272 | } | 
|---|
| 273 | { | 
|---|
| 274 | const BondList& ListOfBonds = atom2->getListOfBonds(); | 
|---|
| 275 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 276 | } | 
|---|
| 277 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
| 278 | } | 
|---|
| 279 | { // time step 1 | 
|---|
| 280 | WorldTime::getInstance().setTime(1); | 
|---|
| 281 | { | 
|---|
| 282 | const BondList& ListOfBonds = atom1->getListOfBonds(); | 
|---|
| 283 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 284 | } | 
|---|
| 285 | { | 
|---|
| 286 | const BondList& ListOfBonds = atom2->getListOfBonds(); | 
|---|
| 287 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 288 | } | 
|---|
| 289 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
| 290 | WorldTime::getInstance().setTime(0); | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | // access time step directly. | 
|---|
| 294 | { // time step 0 | 
|---|
| 295 | { | 
|---|
| 296 | const BondList& ListOfBonds = atom1->getListOfBondsAtStep(0); | 
|---|
| 297 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 298 | } | 
|---|
| 299 | { | 
|---|
| 300 | const BondList& ListOfBonds = atom2->getListOfBondsAtStep(0); | 
|---|
| 301 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 302 | } | 
|---|
| 303 | } | 
|---|
| 304 | { // time step 1 | 
|---|
| 305 | { | 
|---|
| 306 | const BondList& ListOfBonds = atom1->getListOfBondsAtStep(1); | 
|---|
| 307 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 308 | } | 
|---|
| 309 | { | 
|---|
| 310 | const BondList& ListOfBonds = atom1->getListOfBondsAtStep(1); | 
|---|
| 311 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() ); | 
|---|
| 312 | } | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | // remove atom2 | 
|---|
| 316 | World::getInstance().destroyAtom(atom2); | 
|---|
| 317 |  | 
|---|
| 318 | // check bond if removed from other atom for all time steps | 
|---|
| 319 | { | 
|---|
| 320 | WorldTime::getInstance().setTime(0); | 
|---|
| 321 | const BondList& ListOfBonds = atom1->getListOfBonds(); | 
|---|
| 322 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() ); | 
|---|
| 323 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 324 | } | 
|---|
| 325 | { | 
|---|
| 326 | WorldTime::getInstance().setTime(1); | 
|---|
| 327 | const BondList& ListOfBonds = atom1->getListOfBonds(); | 
|---|
| 328 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() ); | 
|---|
| 329 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 330 | WorldTime::getInstance().setTime(0); | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | } | 
|---|