| [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 | // construct element | 
|---|
| [4eb4fe] | 41 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
|  | 42 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen"); | 
|---|
| [266237] | 43 |  | 
|---|
|  | 44 | // construct molecule (tetraeder of hydrogens) | 
|---|
| [23b547] | 45 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| [4eb4fe] | 46 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule"); | 
|---|
| [23b547] | 47 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 48 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [266237] | 49 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 50 | *Walker->node = Vector(1., 0., 1. ); | 
|---|
| [266237] | 51 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 52 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 53 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [266237] | 54 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 55 | *Walker->node = Vector(0., 1., 1. ); | 
|---|
| [266237] | 56 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 57 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 58 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [266237] | 59 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 60 | *Walker->node = Vector(1., 1., 0. ); | 
|---|
| [266237] | 61 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 62 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 63 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [266237] | 64 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 65 | *Walker->node = Vector(0., 0., 0. ); | 
|---|
| [266237] | 66 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 67 |  | 
|---|
|  | 68 | // check that TestMolecule was correctly constructed | 
|---|
| [ea7176] | 69 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| [266237] | 70 | }; | 
|---|
|  | 71 |  | 
|---|
|  | 72 |  | 
|---|
|  | 73 | void ListOfBondsTest::tearDown() | 
|---|
|  | 74 | { | 
|---|
|  | 75 | // remove | 
|---|
| [23b547] | 76 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| [a1510d] | 77 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
|  | 78 | // are all cleaned when the world is destroyed | 
|---|
| [23b547] | 79 | World::purgeInstance(); | 
|---|
| [e6fdbe] | 80 | logger::purgeInstance(); | 
|---|
| [266237] | 81 | }; | 
|---|
|  | 82 |  | 
|---|
| [9879f6] | 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 |  | 
|---|
| [266237] | 92 | /** Unit Test of molecule::AddBond() | 
|---|
|  | 93 | * | 
|---|
|  | 94 | */ | 
|---|
|  | 95 | void ListOfBondsTest::AddingBondTest() | 
|---|
|  | 96 | { | 
|---|
|  | 97 | bond *Binder = NULL; | 
|---|
| [9879f6] | 98 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 99 | atom *atom1 = *iter; | 
|---|
|  | 100 | iter++; | 
|---|
|  | 101 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 ); | 
|---|
| [e08c46] | 108 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 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; | 
|---|
| [9879f6] | 127 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 128 | atom *atom1 = *iter; | 
|---|
|  | 129 | iter++; | 
|---|
|  | 130 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 146 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 147 | }; | 
|---|
|  | 148 |  | 
|---|
|  | 149 | /** Unit Test of molecule::RemoveBonds() | 
|---|
|  | 150 | * | 
|---|
|  | 151 | */ | 
|---|
|  | 152 | void ListOfBondsTest::RemovingBondsTest() | 
|---|
|  | 153 | { | 
|---|
|  | 154 | bond *Binder = NULL; | 
|---|
| [9879f6] | 155 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 156 | atom *atom1 = *iter; | 
|---|
|  | 157 | iter++; | 
|---|
|  | 158 | atom *atom2 = *iter; | 
|---|
|  | 159 | iter++; | 
|---|
|  | 160 | atom *atom3 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 187 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
|  | 188 | CPPUNIT_ASSERT_EQUAL( (unsigned int)1, TestMolecule->CountBonds() ); | 
|---|
| [266237] | 189 | }; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | /** Unit Test of delete(bond *) | 
|---|
|  | 192 | * | 
|---|
|  | 193 | */ | 
|---|
|  | 194 | void ListOfBondsTest::DeleteBondTest() | 
|---|
|  | 195 | { | 
|---|
|  | 196 | bond *Binder = NULL; | 
|---|
| [9879f6] | 197 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 198 | atom *atom1 = *iter; | 
|---|
|  | 199 | iter++; | 
|---|
|  | 200 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 216 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 217 | }; | 
|---|
|  | 218 |  | 
|---|
|  | 219 | /** Unit Test of molecule::RemoveAtom() | 
|---|
|  | 220 | * | 
|---|
|  | 221 | */ | 
|---|
|  | 222 | void ListOfBondsTest::RemoveAtomTest() | 
|---|
|  | 223 | { | 
|---|
|  | 224 | bond *Binder = NULL; | 
|---|
| [9879f6] | 225 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 226 | atom *atom1 = *iter; | 
|---|
|  | 227 | iter++; | 
|---|
|  | 228 | atom *atom2 = *iter; | 
|---|
| [266237] | 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 | 
|---|
| [e08c46] | 243 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 244 | }; | 
|---|
|  | 245 |  | 
|---|
|  | 246 | /** Unit Test of delete(atom *) | 
|---|
|  | 247 | * | 
|---|
|  | 248 | */ | 
|---|
|  | 249 | void ListOfBondsTest::DeleteAtomTest() | 
|---|
|  | 250 | { | 
|---|
| [f8e486] | 251 | atom *atom1 = NULL; | 
|---|
|  | 252 | atom *atom2 = NULL; | 
|---|
| [266237] | 253 | bond *Binder = NULL; | 
|---|
| [f8e486] | 254 | { | 
|---|
|  | 255 | molecule::iterator iter = TestMolecule->begin(); | 
|---|
|  | 256 | atom1 = *iter; | 
|---|
|  | 257 | iter++; | 
|---|
|  | 258 | atom2 = *iter; | 
|---|
|  | 259 | } | 
|---|
| [266237] | 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 |  | 
|---|
| [6cfa36] | 267 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom1->ListOfBonds.size() ); | 
|---|
|  | 268 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() ); | 
|---|
|  | 269 |  | 
|---|
| [a80241] | 270 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() ); | 
|---|
|  | 271 |  | 
|---|
| [266237] | 272 | // remove atom2 | 
|---|
| [23b547] | 273 | World::getInstance().destroyAtom(atom2); | 
|---|
| [266237] | 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 | 
|---|
| [a80241] | 279 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() ); | 
|---|
| [266237] | 280 | }; | 
|---|