| 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 | // 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 | 
|---|
| 53 | tafel = World::get()->getPeriode(); | 
|---|
| 54 | tafel->AddElement(hydrogen); | 
|---|
| 55 |  | 
|---|
| 56 | // construct molecule (tetraeder of hydrogens) | 
|---|
| 57 | TestMolecule = World::get()->createMolecule(); | 
|---|
| 58 | Walker = World::get()->createAtom(); | 
|---|
| 59 | Walker->type = hydrogen; | 
|---|
| 60 | Walker->node->Init(1., 0., 1. ); | 
|---|
| 61 | TestMolecule->AddAtom(Walker); | 
|---|
| 62 | Walker = World::get()->createAtom(); | 
|---|
| 63 | Walker->type = hydrogen; | 
|---|
| 64 | Walker->node->Init(0., 1., 1. ); | 
|---|
| 65 | TestMolecule->AddAtom(Walker); | 
|---|
| 66 | Walker = World::get()->createAtom(); | 
|---|
| 67 | Walker->type = hydrogen; | 
|---|
| 68 | Walker->node->Init(1., 1., 0. ); | 
|---|
| 69 | TestMolecule->AddAtom(Walker); | 
|---|
| 70 | Walker = World::get()->createAtom(); | 
|---|
| 71 | Walker->type = hydrogen; | 
|---|
| 72 | Walker->node->Init(0., 0., 0. ); | 
|---|
| 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 | 
|---|
| 84 | World::get()->destroyMolecule(TestMolecule); | 
|---|
| 85 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
| 86 | // are all cleaned when the world is destroyed | 
|---|
| 87 | World::destroy(); | 
|---|
| 88 | MemoryUsageObserver::purgeInstance(); | 
|---|
| 89 | logger::purgeInstance(); | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | /** Tests whether setup worked correctly. | 
|---|
| 93 | * | 
|---|
| 94 | */ | 
|---|
| 95 | void ListOfBondsTest::SetupTest() | 
|---|
| 96 | { | 
|---|
| 97 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->empty() ); | 
|---|
| 98 | CPPUNIT_ASSERT_EQUAL( (size_t)4, TestMolecule->size() ); | 
|---|
| 99 | }; | 
|---|
| 100 |  | 
|---|
| 101 | /** Unit Test of molecule::AddBond() | 
|---|
| 102 | * | 
|---|
| 103 | */ | 
|---|
| 104 | void ListOfBondsTest::AddingBondTest() | 
|---|
| 105 | { | 
|---|
| 106 | bond *Binder = NULL; | 
|---|
| 107 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 108 | atom *atom1 = *iter; | 
|---|
| 109 | iter++; | 
|---|
| 110 | atom *atom2 = *iter; | 
|---|
| 111 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 112 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 113 |  | 
|---|
| 114 | // add bond | 
|---|
| 115 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 116 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 117 | bond *TestBond = TestMolecule->first->next; | 
|---|
| 118 | CPPUNIT_ASSERT_EQUAL ( TestBond, Binder ); | 
|---|
| 119 |  | 
|---|
| 120 | // check that bond contains the two atoms | 
|---|
| 121 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom1) ); | 
|---|
| 122 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom2) ); | 
|---|
| 123 |  | 
|---|
| 124 | // check that bond is present in both atoms | 
|---|
| 125 | bond *TestBond1 = *(atom1->ListOfBonds.begin()); | 
|---|
| 126 | CPPUNIT_ASSERT_EQUAL( TestBond1, Binder ); | 
|---|
| 127 | bond *TestBond2 = *(atom2->ListOfBonds.begin()); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( TestBond2, Binder ); | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | /** Unit Test of molecule::RemoveBond() | 
|---|
| 132 | * | 
|---|
| 133 | */ | 
|---|
| 134 | void ListOfBondsTest::RemovingBondTest() | 
|---|
| 135 | { | 
|---|
| 136 | bond *Binder = NULL; | 
|---|
| 137 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 138 | atom *atom1 = *iter; | 
|---|
| 139 | iter++; | 
|---|
| 140 | atom *atom2 = *iter; | 
|---|
| 141 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 142 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 143 |  | 
|---|
| 144 | // add bond | 
|---|
| 145 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 146 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 147 |  | 
|---|
| 148 | // remove bond | 
|---|
| 149 | TestMolecule->RemoveBond(Binder); | 
|---|
| 150 |  | 
|---|
| 151 | // check if removed from atoms | 
|---|
| 152 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() ); | 
|---|
| 154 |  | 
|---|
| 155 | // check if removed from molecule | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last ); | 
|---|
| 157 | }; | 
|---|
| 158 |  | 
|---|
| 159 | /** Unit Test of molecule::RemoveBonds() | 
|---|
| 160 | * | 
|---|
| 161 | */ | 
|---|
| 162 | void ListOfBondsTest::RemovingBondsTest() | 
|---|
| 163 | { | 
|---|
| 164 | bond *Binder = NULL; | 
|---|
| 165 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 166 | atom *atom1 = *iter; | 
|---|
| 167 | iter++; | 
|---|
| 168 | atom *atom2 = *iter; | 
|---|
| 169 | iter++; | 
|---|
| 170 | atom *atom3 = *iter; | 
|---|
| 171 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 172 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 173 | CPPUNIT_ASSERT( atom3 != NULL ); | 
|---|
| 174 |  | 
|---|
| 175 | // add bond | 
|---|
| 176 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 177 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 178 | Binder = TestMolecule->AddBond(atom1, atom3, 1); | 
|---|
| 179 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 180 | Binder = TestMolecule->AddBond(atom2, atom3, 1); | 
|---|
| 181 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 182 |  | 
|---|
| 183 | // check that all are present | 
|---|
| 184 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom1->ListOfBonds.size() ); | 
|---|
| 185 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom2->ListOfBonds.size() ); | 
|---|
| 186 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom3->ListOfBonds.size() ); | 
|---|
| 187 |  | 
|---|
| 188 | // remove bond | 
|---|
| 189 | TestMolecule->RemoveBonds(atom1); | 
|---|
| 190 |  | 
|---|
| 191 | // check if removed from atoms | 
|---|
| 192 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 193 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() ); | 
|---|
| 194 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom3->ListOfBonds.size() ); | 
|---|
| 195 |  | 
|---|
| 196 | // check if removed from molecule | 
|---|
| 197 | CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, Binder ); | 
|---|
| 198 | CPPUNIT_ASSERT_EQUAL( Binder->next, TestMolecule->last ); | 
|---|
| 199 | }; | 
|---|
| 200 |  | 
|---|
| 201 | /** Unit Test of delete(bond *) | 
|---|
| 202 | * | 
|---|
| 203 | */ | 
|---|
| 204 | void ListOfBondsTest::DeleteBondTest() | 
|---|
| 205 | { | 
|---|
| 206 | bond *Binder = NULL; | 
|---|
| 207 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 208 | atom *atom1 = *iter; | 
|---|
| 209 | iter++; | 
|---|
| 210 | atom *atom2 = *iter; | 
|---|
| 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 bond | 
|---|
| 219 | delete(Binder); | 
|---|
| 220 |  | 
|---|
| 221 | // check if removed from atoms | 
|---|
| 222 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 223 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() ); | 
|---|
| 224 |  | 
|---|
| 225 | // check if removed from molecule | 
|---|
| 226 | CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last ); | 
|---|
| 227 | }; | 
|---|
| 228 |  | 
|---|
| 229 | /** Unit Test of molecule::RemoveAtom() | 
|---|
| 230 | * | 
|---|
| 231 | */ | 
|---|
| 232 | void ListOfBondsTest::RemoveAtomTest() | 
|---|
| 233 | { | 
|---|
| 234 | bond *Binder = NULL; | 
|---|
| 235 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 236 | atom *atom1 = *iter; | 
|---|
| 237 | iter++; | 
|---|
| 238 | atom *atom2 = *iter; | 
|---|
| 239 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 240 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 241 |  | 
|---|
| 242 | // add bond | 
|---|
| 243 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 244 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 245 |  | 
|---|
| 246 | // remove atom2 | 
|---|
| 247 | TestMolecule->RemoveAtom(atom2); | 
|---|
| 248 |  | 
|---|
| 249 | // check bond if removed from other atom | 
|---|
| 250 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 251 |  | 
|---|
| 252 | // check if removed from molecule | 
|---|
| 253 | CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last ); | 
|---|
| 254 | }; | 
|---|
| 255 |  | 
|---|
| 256 | /** Unit Test of delete(atom *) | 
|---|
| 257 | * | 
|---|
| 258 | */ | 
|---|
| 259 | void ListOfBondsTest::DeleteAtomTest() | 
|---|
| 260 | { | 
|---|
| 261 | bond *Binder = NULL; | 
|---|
| 262 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
| 263 | atom *atom1 = *iter; | 
|---|
| 264 | iter++; | 
|---|
| 265 | atom *atom2 = *iter; | 
|---|
| 266 | CPPUNIT_ASSERT( atom1 != NULL ); | 
|---|
| 267 | CPPUNIT_ASSERT( atom2 != NULL ); | 
|---|
| 268 |  | 
|---|
| 269 | // add bond | 
|---|
| 270 | Binder = TestMolecule->AddBond(atom1, atom2, 1); | 
|---|
| 271 | CPPUNIT_ASSERT( Binder != NULL ); | 
|---|
| 272 |  | 
|---|
| 273 | // remove atom2 | 
|---|
| 274 | World::get()->destroyAtom(atom2); | 
|---|
| 275 |  | 
|---|
| 276 | // check bond if removed from other atom | 
|---|
| 277 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() ); | 
|---|
| 278 |  | 
|---|
| 279 | // check if removed from molecule | 
|---|
| 280 | CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last ); | 
|---|
| 281 | }; | 
|---|