| [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 | 
 | 
|---|
 | 18 | #include "atom.hpp"
 | 
|---|
 | 19 | #include "bond.hpp"
 | 
|---|
 | 20 | #include "element.hpp"
 | 
|---|
 | 21 | #include "molecule.hpp"
 | 
|---|
 | 22 | #include "periodentafel.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | /********************************************** Test classes **************************************/
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | // Registers the fixture into the 'registry'
 | 
|---|
 | 27 | CPPUNIT_TEST_SUITE_REGISTRATION( ListOfBondsTest );
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | void ListOfBondsTest::setUp()
 | 
|---|
 | 31 | {
 | 
|---|
 | 32 |   atom *Walker = NULL;
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 |   // init private all pointers to zero
 | 
|---|
 | 35 |   TestMolecule = NULL;
 | 
|---|
 | 36 |   hydrogen = NULL;
 | 
|---|
 | 37 |   tafel = NULL;
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 |   // construct element
 | 
|---|
 | 40 |   hydrogen = new element;
 | 
|---|
 | 41 |   hydrogen->Z = 1;
 | 
|---|
 | 42 |   strcpy(hydrogen->name, "hydrogen");
 | 
|---|
 | 43 |   strcpy(hydrogen->symbol, "H");
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |   // construct periodentafel
 | 
|---|
 | 47 |   tafel = new periodentafel;
 | 
|---|
 | 48 |   tafel->AddElement(hydrogen);
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
 | 51 |   TestMolecule = new molecule(tafel);
 | 
|---|
 | 52 |   Walker = new atom();
 | 
|---|
 | 53 |   Walker->type = hydrogen;
 | 
|---|
 | 54 |   Walker->node->Init(1., 0., 1. );
 | 
|---|
 | 55 |   TestMolecule->AddAtom(Walker);
 | 
|---|
 | 56 |   Walker = new atom();
 | 
|---|
 | 57 |   Walker->type = hydrogen;
 | 
|---|
 | 58 |   Walker->node->Init(0., 1., 1. );
 | 
|---|
 | 59 |   TestMolecule->AddAtom(Walker);
 | 
|---|
 | 60 |   Walker = new atom();
 | 
|---|
 | 61 |   Walker->type = hydrogen;
 | 
|---|
 | 62 |   Walker->node->Init(1., 1., 0. );
 | 
|---|
 | 63 |   TestMolecule->AddAtom(Walker);
 | 
|---|
 | 64 |   Walker = new atom();
 | 
|---|
 | 65 |   Walker->type = hydrogen;
 | 
|---|
 | 66 |   Walker->node->Init(0., 0., 0. );
 | 
|---|
 | 67 |   TestMolecule->AddAtom(Walker);
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |   // check that TestMolecule was correctly constructed
 | 
|---|
 | 70 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 );
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | };
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 | void ListOfBondsTest::tearDown()
 | 
|---|
 | 76 | {
 | 
|---|
 | 77 |   // remove
 | 
|---|
 | 78 |   delete(TestMolecule);
 | 
|---|
 | 79 |   // note that all the atoms are cleaned by TestMolecule
 | 
|---|
 | 80 |   delete(tafel);
 | 
|---|
 | 81 |   // note that element is cleaned by periodentafel
 | 
|---|
 | 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 |   delete(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 | };
 | 
|---|
 | 252 | 
 | 
|---|
 | 253 | /********************************************** Main routine **************************************/
 | 
|---|
 | 254 | 
 | 
|---|
 | 255 | int main(int argc, char **argv)
 | 
|---|
 | 256 | {
 | 
|---|
 | 257 |   // Get the top level suite from the registry
 | 
|---|
 | 258 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
 | 259 | 
 | 
|---|
 | 260 |   // Adds the test to the list of test to run
 | 
|---|
 | 261 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
 | 262 |   runner.addTest( suite );
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
 | 265 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
 | 266 |                                                        std::cerr ) );
 | 
|---|
 | 267 |   // Run the tests.
 | 
|---|
 | 268 |   bool wasSucessful = runner.run();
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 |   // Return error code 1 if the one of test failed.
 | 
|---|
 | 271 |   return wasSucessful ? 0 : 1;
 | 
|---|
 | 272 | };
 | 
|---|