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