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