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