| 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 "listofbondsunittest.hpp"
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "atom.hpp"
 | 
|---|
| 17 | #include "bond.hpp"
 | 
|---|
| 18 | #include "element.hpp"
 | 
|---|
| 19 | #include "molecule.hpp"
 | 
|---|
| 20 | #include "periodentafel.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | /********************************************** Test classes **************************************/
 | 
|---|
| 23 | 
 | 
|---|
| 24 | // Registers the fixture into the 'registry'
 | 
|---|
| 25 | CPPUNIT_TEST_SUITE_REGISTRATION( ListOfBondsTest );
 | 
|---|
| 26 | 
 | 
|---|
| 27 | 
 | 
|---|
| 28 | void ListOfBondsTest::setUp()
 | 
|---|
| 29 | {
 | 
|---|
| 30 |   atom *Walker = NULL;
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   // init private all pointers to zero
 | 
|---|
| 33 |   TestMolecule = NULL;
 | 
|---|
| 34 |   hydrogen = NULL;
 | 
|---|
| 35 |   tafel = NULL;
 | 
|---|
| 36 | 
 | 
|---|
| 37 |   // construct element
 | 
|---|
| 38 |   hydrogen = new element;
 | 
|---|
| 39 |   hydrogen->Z = 1;
 | 
|---|
| 40 |   strcpy(hydrogen->name, "hydrogen");
 | 
|---|
| 41 |   strcpy(hydrogen->symbol, "H");
 | 
|---|
| 42 | 
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   // construct periodentafel
 | 
|---|
| 45 |   tafel = new periodentafel;
 | 
|---|
| 46 |   tafel->AddElement(hydrogen);
 | 
|---|
| 47 | 
 | 
|---|
| 48 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| 49 |   TestMolecule = new molecule(tafel);
 | 
|---|
| 50 |   Walker = new atom();
 | 
|---|
| 51 |   Walker->type = hydrogen;
 | 
|---|
| 52 |   Walker->node->Init(1., 0., 1. );
 | 
|---|
| 53 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 54 |   Walker = new atom();
 | 
|---|
| 55 |   Walker->type = hydrogen;
 | 
|---|
| 56 |   Walker->node->Init(0., 1., 1. );
 | 
|---|
| 57 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 58 |   Walker = new atom();
 | 
|---|
| 59 |   Walker->type = hydrogen;
 | 
|---|
| 60 |   Walker->node->Init(1., 1., 0. );
 | 
|---|
| 61 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 62 |   Walker = new atom();
 | 
|---|
| 63 |   Walker->type = hydrogen;
 | 
|---|
| 64 |   Walker->node->Init(0., 0., 0. );
 | 
|---|
| 65 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 68 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 );
 | 
|---|
| 69 | 
 | 
|---|
| 70 | };
 | 
|---|
| 71 | 
 | 
|---|
| 72 | 
 | 
|---|
| 73 | void ListOfBondsTest::tearDown()
 | 
|---|
| 74 | {
 | 
|---|
| 75 |   // remove
 | 
|---|
| 76 |   delete(TestMolecule);
 | 
|---|
| 77 |   // note that all the atoms are cleaned by TestMolecule
 | 
|---|
| 78 |   delete(tafel);
 | 
|---|
| 79 |   // note that element is cleaned by periodentafel
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /** Unit Test of molecule::AddBond()
 | 
|---|
| 83 |  *
 | 
|---|
| 84 |  */
 | 
|---|
| 85 | void ListOfBondsTest::AddingBondTest()
 | 
|---|
| 86 | {
 | 
|---|
| 87 |   bond *Binder = NULL;
 | 
|---|
| 88 |   atom *atom1 = TestMolecule->start->next;
 | 
|---|
| 89 |   atom *atom2 = atom1->next;
 | 
|---|
| 90 |   CPPUNIT_ASSERT( atom1 != NULL );
 | 
|---|
| 91 |   CPPUNIT_ASSERT( atom2 != NULL );
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   // add bond
 | 
|---|
| 94 |   Binder = TestMolecule->AddBond(atom1, atom2, 1);
 | 
|---|
| 95 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 96 |   bond *TestBond = TestMolecule->first->next;
 | 
|---|
| 97 |   CPPUNIT_ASSERT_EQUAL ( TestBond, Binder );
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   // check that bond contains the two atoms
 | 
|---|
| 100 |   CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom1) );
 | 
|---|
| 101 |   CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom2) );
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // check that bond is present in both atoms
 | 
|---|
| 104 |   bond *TestBond1 = *(atom1->ListOfBonds.begin());
 | 
|---|
| 105 |   CPPUNIT_ASSERT_EQUAL( TestBond1, Binder );
 | 
|---|
| 106 |   bond *TestBond2 = *(atom2->ListOfBonds.begin());
 | 
|---|
| 107 |   CPPUNIT_ASSERT_EQUAL( TestBond2, Binder );
 | 
|---|
| 108 | };
 | 
|---|
| 109 | 
 | 
|---|
| 110 | /** Unit Test of molecule::RemoveBond()
 | 
|---|
| 111 |  *
 | 
|---|
| 112 |  */
 | 
|---|
| 113 | void ListOfBondsTest::RemovingBondTest()
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   bond *Binder = NULL;
 | 
|---|
| 116 |   atom *atom1 = TestMolecule->start->next;
 | 
|---|
| 117 |   atom *atom2 = atom1->next;
 | 
|---|
| 118 |   CPPUNIT_ASSERT( atom1 != NULL );
 | 
|---|
| 119 |   CPPUNIT_ASSERT( atom2 != NULL );
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   // add bond
 | 
|---|
| 122 |   Binder = TestMolecule->AddBond(atom1, atom2, 1);
 | 
|---|
| 123 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   // remove bond
 | 
|---|
| 126 |   TestMolecule->RemoveBond(Binder);
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   // check if removed from atoms
 | 
|---|
| 129 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
 | 
|---|
| 130 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() );
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   // check if removed from molecule
 | 
|---|
| 133 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last );
 | 
|---|
| 134 | };
 | 
|---|
| 135 | 
 | 
|---|
| 136 | /** Unit Test of molecule::RemoveBonds()
 | 
|---|
| 137 |  *
 | 
|---|
| 138 |  */
 | 
|---|
| 139 | void ListOfBondsTest::RemovingBondsTest()
 | 
|---|
| 140 | {
 | 
|---|
| 141 |   bond *Binder = NULL;
 | 
|---|
| 142 |   atom *atom1 = TestMolecule->start->next;
 | 
|---|
| 143 |   atom *atom2 = atom1->next;
 | 
|---|
| 144 |   atom *atom3 = atom2->next;
 | 
|---|
| 145 |   CPPUNIT_ASSERT( atom1 != NULL );
 | 
|---|
| 146 |   CPPUNIT_ASSERT( atom2 != NULL );
 | 
|---|
| 147 |   CPPUNIT_ASSERT( atom3 != NULL );
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   // add bond
 | 
|---|
| 150 |   Binder = TestMolecule->AddBond(atom1, atom2, 1);
 | 
|---|
| 151 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 152 |   Binder = TestMolecule->AddBond(atom1, atom3, 1);
 | 
|---|
| 153 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 154 |   Binder = TestMolecule->AddBond(atom2, atom3, 1);
 | 
|---|
| 155 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // check that all are present
 | 
|---|
| 158 |   CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom1->ListOfBonds.size() );
 | 
|---|
| 159 |   CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom2->ListOfBonds.size() );
 | 
|---|
| 160 |   CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom3->ListOfBonds.size() );
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   // remove bond
 | 
|---|
| 163 |   TestMolecule->RemoveBonds(atom1);
 | 
|---|
| 164 | 
 | 
|---|
| 165 |   // check if removed from atoms
 | 
|---|
| 166 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
 | 
|---|
| 167 |   CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() );
 | 
|---|
| 168 |   CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom3->ListOfBonds.size() );
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   // check if removed from molecule
 | 
|---|
| 171 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, Binder );
 | 
|---|
| 172 |   CPPUNIT_ASSERT_EQUAL( Binder->next, TestMolecule->last );
 | 
|---|
| 173 | };
 | 
|---|
| 174 | 
 | 
|---|
| 175 | /** Unit Test of delete(bond *)
 | 
|---|
| 176 |  *
 | 
|---|
| 177 |  */
 | 
|---|
| 178 | void ListOfBondsTest::DeleteBondTest()
 | 
|---|
| 179 | {
 | 
|---|
| 180 |   bond *Binder = NULL;
 | 
|---|
| 181 |   atom *atom1 = TestMolecule->start->next;
 | 
|---|
| 182 |   atom *atom2 = atom1->next;
 | 
|---|
| 183 |   CPPUNIT_ASSERT( atom1 != NULL );
 | 
|---|
| 184 |   CPPUNIT_ASSERT( atom2 != NULL );
 | 
|---|
| 185 | 
 | 
|---|
| 186 |   // add bond
 | 
|---|
| 187 |   Binder = TestMolecule->AddBond(atom1, atom2, 1);
 | 
|---|
| 188 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 189 | 
 | 
|---|
| 190 |   // remove bond
 | 
|---|
| 191 |   delete(Binder);
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   // check if removed from atoms
 | 
|---|
| 194 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
 | 
|---|
| 195 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() );
 | 
|---|
| 196 | 
 | 
|---|
| 197 |   // check if removed from molecule
 | 
|---|
| 198 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last );
 | 
|---|
| 199 | };
 | 
|---|
| 200 | 
 | 
|---|
| 201 | /** Unit Test of molecule::RemoveAtom()
 | 
|---|
| 202 |  *
 | 
|---|
| 203 |  */
 | 
|---|
| 204 | void ListOfBondsTest::RemoveAtomTest()
 | 
|---|
| 205 | {
 | 
|---|
| 206 |   bond *Binder = NULL;
 | 
|---|
| 207 |   atom *atom1 = TestMolecule->start->next;
 | 
|---|
| 208 |   atom *atom2 = atom1->next;
 | 
|---|
| 209 |   CPPUNIT_ASSERT( atom1 != NULL );
 | 
|---|
| 210 |   CPPUNIT_ASSERT( atom2 != NULL );
 | 
|---|
| 211 | 
 | 
|---|
| 212 |   // add bond
 | 
|---|
| 213 |   Binder = TestMolecule->AddBond(atom1, atom2, 1);
 | 
|---|
| 214 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   // remove atom2
 | 
|---|
| 217 |   TestMolecule->RemoveAtom(atom2);
 | 
|---|
| 218 | 
 | 
|---|
| 219 |   // check bond if removed from other atom
 | 
|---|
| 220 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
 | 
|---|
| 221 | 
 | 
|---|
| 222 |   // check if removed from molecule
 | 
|---|
| 223 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last );
 | 
|---|
| 224 | };
 | 
|---|
| 225 | 
 | 
|---|
| 226 | /** Unit Test of delete(atom *)
 | 
|---|
| 227 |  *
 | 
|---|
| 228 |  */
 | 
|---|
| 229 | void ListOfBondsTest::DeleteAtomTest()
 | 
|---|
| 230 | {
 | 
|---|
| 231 |   bond *Binder = NULL;
 | 
|---|
| 232 |   atom *atom1 = TestMolecule->start->next;
 | 
|---|
| 233 |   atom *atom2 = atom1->next;
 | 
|---|
| 234 |   CPPUNIT_ASSERT( atom1 != NULL );
 | 
|---|
| 235 |   CPPUNIT_ASSERT( atom2 != NULL );
 | 
|---|
| 236 | 
 | 
|---|
| 237 |   // add bond
 | 
|---|
| 238 |   Binder = TestMolecule->AddBond(atom1, atom2, 1);
 | 
|---|
| 239 |   CPPUNIT_ASSERT( Binder != NULL );
 | 
|---|
| 240 | 
 | 
|---|
| 241 |   // remove atom2
 | 
|---|
| 242 |   delete(atom2);
 | 
|---|
| 243 | 
 | 
|---|
| 244 |   // check bond if removed from other atom
 | 
|---|
| 245 |   CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
 | 
|---|
| 246 | 
 | 
|---|
| 247 |   // check if removed from molecule
 | 
|---|
| 248 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->first->next, TestMolecule->last );
 | 
|---|
| 249 | };
 | 
|---|
| 250 | 
 | 
|---|
| 251 | /********************************************** Main routine **************************************/
 | 
|---|
| 252 | 
 | 
|---|
| 253 | int main(int argc, char **argv)
 | 
|---|
| 254 | {
 | 
|---|
| 255 |   // Get the top level suite from the registry
 | 
|---|
| 256 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
| 257 | 
 | 
|---|
| 258 |   // Adds the test to the list of test to run
 | 
|---|
| 259 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
| 260 |   runner.addTest( suite );
 | 
|---|
| 261 | 
 | 
|---|
| 262 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
| 263 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
| 264 |                                                        std::cerr ) );
 | 
|---|
| 265 |   // Run the tests.
 | 
|---|
| 266 |   bool wasSucessful = runner.run();
 | 
|---|
| 267 | 
 | 
|---|
| 268 |   // Return error code 1 if the one of test failed.
 | 
|---|
| 269 |   return wasSucessful ? 0 : 1;
 | 
|---|
| 270 | };
 | 
|---|