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