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