| [bcf653] | 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 |  | 
|---|
| [266237] | 8 | /* | 
|---|
|  | 9 | * listofbondsunittest.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: 18 Oct 2009 | 
|---|
|  | 12 | *      Author: user | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [266237] | 20 | using namespace std; | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 23 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 24 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 25 |  | 
|---|
| [49e1ae] | 26 | #include <cstring> | 
|---|
|  | 27 |  | 
|---|
| [266237] | 28 | #include "listofbondsunittest.hpp" | 
|---|
|  | 29 |  | 
|---|
| [46d958] | 30 | #include "World.hpp" | 
|---|
| [266237] | 31 | #include "atom.hpp" | 
|---|
|  | 32 | #include "bond.hpp" | 
|---|
|  | 33 | #include "element.hpp" | 
|---|
|  | 34 | #include "molecule.hpp" | 
|---|
|  | 35 | #include "periodentafel.hpp" | 
|---|
| [e6fdbe] | 36 | #include "World.hpp" | 
|---|
| [266237] | 37 |  | 
|---|
| [9b6b2f] | 38 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 39 | #include "UnitTestMain.hpp" | 
|---|
|  | 40 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| [266237] | 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 | 
|---|
| [4eb4fe] | 53 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
|  | 54 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen"); | 
|---|
| [266237] | 55 |  | 
|---|
|  | 56 | // construct molecule (tetraeder of hydrogens) | 
|---|
| [23b547] | 57 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| [4eb4fe] | 58 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule"); | 
|---|
| [23b547] | 59 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 60 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 61 | Walker->setType(hydrogen); | 
|---|
|  | 62 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| [266237] | 63 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 64 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 65 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 66 | Walker->setType(hydrogen); | 
|---|
|  | 67 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| [266237] | 68 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 69 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 70 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 71 | Walker->setType(hydrogen); | 
|---|
|  | 72 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| [266237] | 73 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 74 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 75 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 76 | Walker->setType(hydrogen); | 
|---|
|  | 77 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| [266237] | 78 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 79 |  | 
|---|
|  | 80 | // check that TestMolecule was correctly constructed | 
|---|
| [ea7176] | 81 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| [266237] | 82 | }; | 
|---|
|  | 83 |  | 
|---|
|  | 84 |  | 
|---|
|  | 85 | void ListOfBondsTest::tearDown() | 
|---|
|  | 86 | { | 
|---|
|  | 87 | // remove | 
|---|
| [23b547] | 88 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| [a1510d] | 89 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
|  | 90 | // are all cleaned when the world is destroyed | 
|---|
| [23b547] | 91 | World::purgeInstance(); | 
|---|
| [e6fdbe] | 92 | logger::purgeInstance(); | 
|---|
| [266237] | 93 | }; | 
|---|
|  | 94 |  | 
|---|
| [9879f6] | 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 |  | 
|---|
| [266237] | 104 | /** Unit Test of molecule::AddBond() | 
|---|
|  | 105 | * | 
|---|
|  | 106 | */ | 
|---|
|  | 107 | void ListOfBondsTest::AddingBondTest() | 
|---|
|  | 108 | { | 
|---|
|  | 109 | bond *Binder = NULL; | 
|---|
| [9879f6] | 110 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 111 | atom *atom1 = *iter; | 
|---|
|  | 112 | iter++; | 
|---|
|  | 113 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 ); | 
|---|
| [e08c46] | 120 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 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; | 
|---|
| [9879f6] | 139 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 140 | atom *atom1 = *iter; | 
|---|
|  | 141 | iter++; | 
|---|
|  | 142 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 158 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 159 | }; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | /** Unit Test of molecule::RemoveBonds() | 
|---|
|  | 162 | * | 
|---|
|  | 163 | */ | 
|---|
|  | 164 | void ListOfBondsTest::RemovingBondsTest() | 
|---|
|  | 165 | { | 
|---|
|  | 166 | bond *Binder = NULL; | 
|---|
| [9879f6] | 167 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 168 | atom *atom1 = *iter; | 
|---|
|  | 169 | iter++; | 
|---|
|  | 170 | atom *atom2 = *iter; | 
|---|
|  | 171 | iter++; | 
|---|
|  | 172 | atom *atom3 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 199 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
|  | 200 | CPPUNIT_ASSERT_EQUAL( (unsigned int)1, TestMolecule->CountBonds() ); | 
|---|
| [266237] | 201 | }; | 
|---|
|  | 202 |  | 
|---|
|  | 203 | /** Unit Test of delete(bond *) | 
|---|
|  | 204 | * | 
|---|
|  | 205 | */ | 
|---|
|  | 206 | void ListOfBondsTest::DeleteBondTest() | 
|---|
|  | 207 | { | 
|---|
|  | 208 | bond *Binder = NULL; | 
|---|
| [9879f6] | 209 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 210 | atom *atom1 = *iter; | 
|---|
|  | 211 | iter++; | 
|---|
|  | 212 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 228 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 229 | }; | 
|---|
|  | 230 |  | 
|---|
|  | 231 | /** Unit Test of molecule::RemoveAtom() | 
|---|
|  | 232 | * | 
|---|
|  | 233 | */ | 
|---|
|  | 234 | void ListOfBondsTest::RemoveAtomTest() | 
|---|
|  | 235 | { | 
|---|
|  | 236 | bond *Binder = NULL; | 
|---|
| [9879f6] | 237 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 238 | atom *atom1 = *iter; | 
|---|
|  | 239 | iter++; | 
|---|
|  | 240 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 255 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 256 | }; | 
|---|
|  | 257 |  | 
|---|
|  | 258 | /** Unit Test of delete(atom *) | 
|---|
|  | 259 | * | 
|---|
|  | 260 | */ | 
|---|
|  | 261 | void ListOfBondsTest::DeleteAtomTest() | 
|---|
|  | 262 | { | 
|---|
| [f8e486] | 263 | atom *atom1 = NULL; | 
|---|
|  | 264 | atom *atom2 = NULL; | 
|---|
| [266237] | 265 | bond *Binder = NULL; | 
|---|
| [f8e486] | 266 | { | 
|---|
|  | 267 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 268 | atom1 = *iter; | 
|---|
|  | 269 | iter++; | 
|---|
|  | 270 | atom2 = *iter; | 
|---|
|  | 271 | } | 
|---|
| [266237] | 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 |  | 
|---|
| [6cfa36] | 279 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom1->ListOfBonds.size() ); | 
|---|
|  | 280 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() ); | 
|---|
|  | 281 |  | 
|---|
| [a80241] | 282 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
|  | 283 |  | 
|---|
| [266237] | 284 | // remove atom2 | 
|---|
| [23b547] | 285 | World::getInstance().destroyAtom(atom2); | 
|---|
| [266237] | 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 | 
|---|
| [a80241] | 291 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 292 | }; | 
|---|