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