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