| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * listofbondsunittest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: 18 Oct 2009 | 
|---|
| 12 | *      Author: user | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | using namespace std; | 
|---|
| 21 |  | 
|---|
| 22 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 24 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include <cstring> | 
|---|
| 27 |  | 
|---|
| 28 | #include "listofbondsunittest.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | #include "World.hpp" | 
|---|
| 31 | #include "atom.hpp" | 
|---|
| 32 | #include "bond.hpp" | 
|---|
| 33 | #include "element.hpp" | 
|---|
| 34 | #include "molecule.hpp" | 
|---|
| 35 | #include "periodentafel.hpp" | 
|---|
| 36 | #include "World.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #ifdef HAVE_TESTRUNNER | 
|---|
| 39 | #include "UnitTestMain.hpp" | 
|---|
| 40 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 41 |  | 
|---|
| 42 | /********************************************** Test classes **************************************/ | 
|---|
| 43 |  | 
|---|
| 44 | // Registers the fixture into the 'registry' | 
|---|
| 45 | CPPUNIT_TEST_SUITE_REGISTRATION( ListOfBondsTest ); | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | void ListOfBondsTest::setUp() | 
|---|
| 49 | { | 
|---|
| 50 | atom *Walker = NULL; | 
|---|
| 51 |  | 
|---|
| 52 | // construct element | 
|---|
| 53 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
| 54 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen"); | 
|---|
| 55 |  | 
|---|
| 56 | // construct molecule (tetraeder of hydrogens) | 
|---|
| 57 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| 58 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule"); | 
|---|
| 59 | Walker = World::getInstance().createAtom(); | 
|---|
| 60 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 61 | Walker->setType(hydrogen); | 
|---|
| 62 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| 63 | TestMolecule->AddAtom(Walker); | 
|---|
| 64 | Walker = World::getInstance().createAtom(); | 
|---|
| 65 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 66 | Walker->setType(hydrogen); | 
|---|
| 67 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| 68 | TestMolecule->AddAtom(Walker); | 
|---|
| 69 | Walker = World::getInstance().createAtom(); | 
|---|
| 70 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 71 | Walker->setType(hydrogen); | 
|---|
| 72 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| 73 | TestMolecule->AddAtom(Walker); | 
|---|
| 74 | Walker = World::getInstance().createAtom(); | 
|---|
| 75 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| 76 | Walker->setType(hydrogen); | 
|---|
| 77 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| 78 | TestMolecule->AddAtom(Walker); | 
|---|
| 79 |  | 
|---|
| 80 | // check that TestMolecule was correctly constructed | 
|---|
| 81 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | void ListOfBondsTest::tearDown() | 
|---|
| 86 | { | 
|---|
| 87 | // remove | 
|---|
| 88 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| 89 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
| 90 | // are all cleaned when the world is destroyed | 
|---|
| 91 | World::purgeInstance(); | 
|---|
| 92 | logger::purgeInstance(); | 
|---|
| 93 | }; | 
|---|
| 94 |  | 
|---|
| 95 | /** Tests whether setup worked correctly. | 
|---|
| 96 | * | 
|---|
| 97 | */ | 
|---|
| 98 | void ListOfBondsTest::SetupTest() | 
|---|
| 99 | { | 
|---|
| 100 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->empty() ); | 
|---|
| 101 | CPPUNIT_ASSERT_EQUAL( (size_t)4, TestMolecule->size() ); | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | /** Unit Test of molecule::AddBond() | 
|---|
| 105 | * | 
|---|
| 106 | */ | 
|---|
| 107 | void ListOfBondsTest::AddingBondTest() | 
|---|
| 108 | { | 
|---|
| 109 | bond *Binder = NULL; | 
|---|
| 110 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 111 | atom *atom1 = *iter; | 
|---|
| 112 | iter++; | 
|---|
| 113 | atom *atom2 = *iter; | 
|---|
| 114 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 115 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 116 |  | 
|---|
| 117 | // add bond | 
|---|
| 118 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 119 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 120 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() ); | 
|---|
| 121 |  | 
|---|
| 122 | // check that bond contains the two atoms | 
|---|
| 123 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom1) ); | 
|---|
| 124 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom2) ); | 
|---|
| 125 |  | 
|---|
| 126 | // check that bond is present in both atoms | 
|---|
| 127 | bond *TestBond1 = *(atom1->ListOfBonds.begin()); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( TestBond1, Binder ); | 
|---|
| 129 | bond *TestBond2 = *(atom2->ListOfBonds.begin()); | 
|---|
| 130 | CPPUNIT_ASSERT_EQUAL( TestBond2, Binder ); | 
|---|
| 131 | }; | 
|---|
| 132 |  | 
|---|
| 133 | /** Unit Test of molecule::RemoveBond() | 
|---|
| 134 | * | 
|---|
| 135 | */ | 
|---|
| 136 | void ListOfBondsTest::RemovingBondTest() | 
|---|
| 137 | { | 
|---|
| 138 | bond *Binder = NULL; | 
|---|
| 139 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 140 | atom *atom1 = *iter; | 
|---|
| 141 | iter++; | 
|---|
| 142 | atom *atom2 = *iter; | 
|---|
| 143 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 144 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 145 |  | 
|---|
| 146 | // add bond | 
|---|
| 147 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 148 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 149 |  | 
|---|
| 150 | // remove bond | 
|---|
| 151 | TestMolecule->RemoveBond(Binder); | 
|---|
| 152 |  | 
|---|
| 153 | // check if removed from atoms | 
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 155 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() ); | 
|---|
| 156 |  | 
|---|
| 157 | // check if removed from molecule | 
|---|
| 158 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 159 | }; | 
|---|
| 160 |  | 
|---|
| 161 | /** Unit Test of molecule::RemoveBonds() | 
|---|
| 162 | * | 
|---|
| 163 | */ | 
|---|
| 164 | void ListOfBondsTest::RemovingBondsTest() | 
|---|
| 165 | { | 
|---|
| 166 | bond *Binder = NULL; | 
|---|
| 167 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 168 | atom *atom1 = *iter; | 
|---|
| 169 | iter++; | 
|---|
| 170 | atom *atom2 = *iter; | 
|---|
| 171 | iter++; | 
|---|
| 172 | atom *atom3 = *iter; | 
|---|
| 173 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 174 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 175 | CPPUNIT_ASSERT( atom3 != NULL ); | 
|---|
| 176 |  | 
|---|
| 177 | // add bond | 
|---|
| 178 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 179 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 180 | Binder = TestMolecule->AddBond(atom1, atom3, 1); | 
|---|
| 181 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 182 | Binder = TestMolecule->AddBond(atom2, atom3, 1); | 
|---|
| 183 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 184 |  | 
|---|
| 185 | // check that all are present | 
|---|
| 186 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom1->ListOfBonds.size() ); | 
|---|
| 187 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom2->ListOfBonds.size() ); | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom3->ListOfBonds.size() ); | 
|---|
| 189 |  | 
|---|
| 190 | // remove bond | 
|---|
| 191 | TestMolecule->RemoveBonds(atom1); | 
|---|
| 192 |  | 
|---|
| 193 | // check if removed from atoms | 
|---|
| 194 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 195 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() ); | 
|---|
| 196 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom3->ListOfBonds.size() ); | 
|---|
| 197 |  | 
|---|
| 198 | // check if removed from molecule | 
|---|
| 199 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
| 200 | CPPUNIT_ASSERT_EQUAL( (unsigned int)1, TestMolecule->CountBonds() ); | 
|---|
| 201 | }; | 
|---|
| 202 |  | 
|---|
| 203 | /** Unit Test of delete(bond *) | 
|---|
| 204 | * | 
|---|
| 205 | */ | 
|---|
| 206 | void ListOfBondsTest::DeleteBondTest() | 
|---|
| 207 | { | 
|---|
| 208 | bond *Binder = NULL; | 
|---|
| 209 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 210 | atom *atom1 = *iter; | 
|---|
| 211 | iter++; | 
|---|
| 212 | atom *atom2 = *iter; | 
|---|
| 213 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 214 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 215 |  | 
|---|
| 216 | // add bond | 
|---|
| 217 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 218 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 219 |  | 
|---|
| 220 | // remove bond | 
|---|
| 221 | delete(Binder); | 
|---|
| 222 |  | 
|---|
| 223 | // check if removed from atoms | 
|---|
| 224 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 225 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() ); | 
|---|
| 226 |  | 
|---|
| 227 | // check if removed from molecule | 
|---|
| 228 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 229 | }; | 
|---|
| 230 |  | 
|---|
| 231 | /** Unit Test of molecule::RemoveAtom() | 
|---|
| 232 | * | 
|---|
| 233 | */ | 
|---|
| 234 | void ListOfBondsTest::RemoveAtomTest() | 
|---|
| 235 | { | 
|---|
| 236 | bond *Binder = NULL; | 
|---|
| 237 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 238 | atom *atom1 = *iter; | 
|---|
| 239 | iter++; | 
|---|
| 240 | atom *atom2 = *iter; | 
|---|
| 241 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 242 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 243 |  | 
|---|
| 244 | // add bond | 
|---|
| 245 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 246 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 247 |  | 
|---|
| 248 | // remove atom2 | 
|---|
| 249 | TestMolecule->RemoveAtom(atom2); | 
|---|
| 250 |  | 
|---|
| 251 | // check bond if removed from other atom | 
|---|
| 252 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 253 |  | 
|---|
| 254 | // check if removed from molecule | 
|---|
| 255 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 256 | }; | 
|---|
| 257 |  | 
|---|
| 258 | /** Unit Test of delete(atom *) | 
|---|
| 259 | * | 
|---|
| 260 | */ | 
|---|
| 261 | void ListOfBondsTest::DeleteAtomTest() | 
|---|
| 262 | { | 
|---|
| 263 | atom *atom1 = NULL; | 
|---|
| 264 | atom *atom2 = NULL; | 
|---|
| 265 | bond *Binder = NULL; | 
|---|
| 266 | { | 
|---|
| 267 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 268 | atom1 = *iter; | 
|---|
| 269 | iter++; | 
|---|
| 270 | atom2 = *iter; | 
|---|
| 271 | } | 
|---|
| 272 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 273 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 274 |  | 
|---|
| 275 | // add bond | 
|---|
| 276 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 277 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 278 |  | 
|---|
| 279 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom1->ListOfBonds.size() ); | 
|---|
| 280 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() ); | 
|---|
| 281 |  | 
|---|
| 282 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
| 283 |  | 
|---|
| 284 | // remove atom2 | 
|---|
| 285 | World::getInstance().destroyAtom(atom2); | 
|---|
| 286 |  | 
|---|
| 287 | // check bond if removed from other atom | 
|---|
| 288 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 289 |  | 
|---|
| 290 | // check if removed from molecule | 
|---|
| 291 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| 292 | }; | 
|---|