[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[266237] | 8 | /*
|
---|
[f844ef] | 9 | * ListOfBondsUnitTest.cpp
|
---|
[266237] | 10 | *
|
---|
| 11 | * Created on: 18 Oct 2009
|
---|
| 12 | * Author: user
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[266237] | 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | #include <cppunit/CompilerOutputter.h>
|
---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 25 |
|
---|
[49e1ae] | 26 | #include <cstring>
|
---|
| 27 |
|
---|
[266237] | 28 |
|
---|
[46d958] | 29 | #include "World.hpp"
|
---|
[266237] | 30 | #include "atom.hpp"
|
---|
| 31 | #include "bond.hpp"
|
---|
| 32 | #include "element.hpp"
|
---|
| 33 | #include "molecule.hpp"
|
---|
| 34 | #include "periodentafel.hpp"
|
---|
[e6fdbe] | 35 | #include "World.hpp"
|
---|
[266237] | 36 |
|
---|
[f844ef] | 37 | #include "ListOfBondsUnitTest.hpp"
|
---|
| 38 |
|
---|
[9b6b2f] | 39 | #ifdef HAVE_TESTRUNNER
|
---|
| 40 | #include "UnitTestMain.hpp"
|
---|
| 41 | #endif /*HAVE_TESTRUNNER*/
|
---|
[266237] | 42 |
|
---|
| 43 | /********************************************** Test classes **************************************/
|
---|
| 44 |
|
---|
| 45 | // Registers the fixture into the 'registry'
|
---|
| 46 | CPPUNIT_TEST_SUITE_REGISTRATION( ListOfBondsTest );
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | void ListOfBondsTest::setUp()
|
---|
| 50 | {
|
---|
| 51 | atom *Walker = NULL;
|
---|
| 52 |
|
---|
| 53 | // construct element
|
---|
[4eb4fe] | 54 | hydrogen = World::getInstance().getPeriode()->FindElement(1);
|
---|
| 55 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
|
---|
[266237] | 56 |
|
---|
| 57 | // construct molecule (tetraeder of hydrogens)
|
---|
[23b547] | 58 | TestMolecule = World::getInstance().createMolecule();
|
---|
[4eb4fe] | 59 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
|
---|
[23b547] | 60 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 61 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 62 | Walker->setType(hydrogen);
|
---|
| 63 | Walker->setPosition(Vector(1., 0., 1. ));
|
---|
[266237] | 64 | TestMolecule->AddAtom(Walker);
|
---|
[23b547] | 65 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 66 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 67 | Walker->setType(hydrogen);
|
---|
| 68 | Walker->setPosition(Vector(0., 1., 1. ));
|
---|
[266237] | 69 | TestMolecule->AddAtom(Walker);
|
---|
[23b547] | 70 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 71 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 72 | Walker->setType(hydrogen);
|
---|
| 73 | Walker->setPosition(Vector(1., 1., 0. ));
|
---|
[266237] | 74 | TestMolecule->AddAtom(Walker);
|
---|
[23b547] | 75 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 76 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 77 | Walker->setType(hydrogen);
|
---|
| 78 | Walker->setPosition(Vector(0., 0., 0. ));
|
---|
[266237] | 79 | TestMolecule->AddAtom(Walker);
|
---|
| 80 |
|
---|
| 81 | // check that TestMolecule was correctly constructed
|
---|
[ea7176] | 82 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
|
---|
[266237] | 83 | };
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | void ListOfBondsTest::tearDown()
|
---|
| 87 | {
|
---|
| 88 | // remove
|
---|
[23b547] | 89 | World::getInstance().destroyMolecule(TestMolecule);
|
---|
[a1510d] | 90 | // note that all the atoms, molecules, the tafel and the elements
|
---|
| 91 | // are all cleaned when the world is destroyed
|
---|
[23b547] | 92 | World::purgeInstance();
|
---|
[e6fdbe] | 93 | logger::purgeInstance();
|
---|
[266237] | 94 | };
|
---|
| 95 |
|
---|
[9879f6] | 96 | /** Tests whether setup worked correctly.
|
---|
| 97 | *
|
---|
| 98 | */
|
---|
| 99 | void ListOfBondsTest::SetupTest()
|
---|
| 100 | {
|
---|
| 101 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->empty() );
|
---|
| 102 | CPPUNIT_ASSERT_EQUAL( (size_t)4, TestMolecule->size() );
|
---|
| 103 | };
|
---|
| 104 |
|
---|
[266237] | 105 | /** Unit Test of molecule::AddBond()
|
---|
| 106 | *
|
---|
| 107 | */
|
---|
| 108 | void ListOfBondsTest::AddingBondTest()
|
---|
| 109 | {
|
---|
| 110 | bond *Binder = NULL;
|
---|
[9879f6] | 111 | molecule::iterator iter = TestMolecule->begin();
|
---|
| 112 | atom *atom1 = *iter;
|
---|
| 113 | iter++;
|
---|
| 114 | atom *atom2 = *iter;
|
---|
[266237] | 115 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
| 116 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
| 117 |
|
---|
| 118 | // add bond
|
---|
| 119 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
| 120 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
[e08c46] | 121 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() );
|
---|
[266237] | 122 |
|
---|
| 123 | // check that bond contains the two atoms
|
---|
| 124 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom1) );
|
---|
| 125 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom2) );
|
---|
| 126 |
|
---|
| 127 | // check that bond is present in both atoms
|
---|
| 128 | bond *TestBond1 = *(atom1->ListOfBonds.begin());
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL( TestBond1, Binder );
|
---|
| 130 | bond *TestBond2 = *(atom2->ListOfBonds.begin());
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( TestBond2, Binder );
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 | /** Unit Test of molecule::RemoveBond()
|
---|
| 135 | *
|
---|
| 136 | */
|
---|
| 137 | void ListOfBondsTest::RemovingBondTest()
|
---|
| 138 | {
|
---|
| 139 | bond *Binder = NULL;
|
---|
[9879f6] | 140 | molecule::iterator iter = TestMolecule->begin();
|
---|
| 141 | atom *atom1 = *iter;
|
---|
| 142 | iter++;
|
---|
| 143 | atom *atom2 = *iter;
|
---|
[266237] | 144 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
| 145 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
| 146 |
|
---|
| 147 | // add bond
|
---|
| 148 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
| 149 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 150 |
|
---|
| 151 | // remove bond
|
---|
| 152 | TestMolecule->RemoveBond(Binder);
|
---|
| 153 |
|
---|
| 154 | // check if removed from atoms
|
---|
| 155 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
|
---|
| 156 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() );
|
---|
| 157 |
|
---|
| 158 | // check if removed from molecule
|
---|
[e08c46] | 159 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
[266237] | 160 | };
|
---|
| 161 |
|
---|
| 162 | /** Unit Test of molecule::RemoveBonds()
|
---|
| 163 | *
|
---|
| 164 | */
|
---|
| 165 | void ListOfBondsTest::RemovingBondsTest()
|
---|
| 166 | {
|
---|
| 167 | bond *Binder = NULL;
|
---|
[9879f6] | 168 | molecule::iterator iter = TestMolecule->begin();
|
---|
| 169 | atom *atom1 = *iter;
|
---|
| 170 | iter++;
|
---|
| 171 | atom *atom2 = *iter;
|
---|
| 172 | iter++;
|
---|
| 173 | atom *atom3 = *iter;
|
---|
[266237] | 174 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
| 175 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
| 176 | CPPUNIT_ASSERT( atom3 != NULL );
|
---|
| 177 |
|
---|
| 178 | // add bond
|
---|
| 179 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
| 180 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 181 | Binder = TestMolecule->AddBond(atom1, atom3, 1);
|
---|
| 182 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 183 | Binder = TestMolecule->AddBond(atom2, atom3, 1);
|
---|
| 184 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 185 |
|
---|
| 186 | // check that all are present
|
---|
| 187 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom1->ListOfBonds.size() );
|
---|
| 188 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom2->ListOfBonds.size() );
|
---|
| 189 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, atom3->ListOfBonds.size() );
|
---|
| 190 |
|
---|
| 191 | // remove bond
|
---|
| 192 | TestMolecule->RemoveBonds(atom1);
|
---|
| 193 |
|
---|
| 194 | // check if removed from atoms
|
---|
| 195 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
|
---|
| 196 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() );
|
---|
| 197 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom3->ListOfBonds.size() );
|
---|
| 198 |
|
---|
| 199 | // check if removed from molecule
|
---|
[e08c46] | 200 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
| 201 | CPPUNIT_ASSERT_EQUAL( (unsigned int)1, TestMolecule->CountBonds() );
|
---|
[266237] | 202 | };
|
---|
| 203 |
|
---|
| 204 | /** Unit Test of delete(bond *)
|
---|
| 205 | *
|
---|
| 206 | */
|
---|
| 207 | void ListOfBondsTest::DeleteBondTest()
|
---|
| 208 | {
|
---|
| 209 | bond *Binder = NULL;
|
---|
[9879f6] | 210 | molecule::iterator iter = TestMolecule->begin();
|
---|
| 211 | atom *atom1 = *iter;
|
---|
| 212 | iter++;
|
---|
| 213 | atom *atom2 = *iter;
|
---|
[266237] | 214 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
| 215 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
| 216 |
|
---|
| 217 | // add bond
|
---|
| 218 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
| 219 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 220 |
|
---|
| 221 | // remove bond
|
---|
| 222 | delete(Binder);
|
---|
| 223 |
|
---|
| 224 | // check if removed from atoms
|
---|
| 225 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
|
---|
| 226 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom2->ListOfBonds.size() );
|
---|
| 227 |
|
---|
| 228 | // check if removed from molecule
|
---|
[e08c46] | 229 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
[266237] | 230 | };
|
---|
| 231 |
|
---|
| 232 | /** Unit Test of molecule::RemoveAtom()
|
---|
| 233 | *
|
---|
| 234 | */
|
---|
| 235 | void ListOfBondsTest::RemoveAtomTest()
|
---|
| 236 | {
|
---|
| 237 | bond *Binder = NULL;
|
---|
[9879f6] | 238 | molecule::iterator iter = TestMolecule->begin();
|
---|
| 239 | atom *atom1 = *iter;
|
---|
| 240 | iter++;
|
---|
| 241 | atom *atom2 = *iter;
|
---|
[266237] | 242 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
| 243 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
| 244 |
|
---|
| 245 | // add bond
|
---|
| 246 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
| 247 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 248 |
|
---|
| 249 | // remove atom2
|
---|
| 250 | TestMolecule->RemoveAtom(atom2);
|
---|
| 251 |
|
---|
| 252 | // check bond if removed from other atom
|
---|
| 253 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
|
---|
| 254 |
|
---|
| 255 | // check if removed from molecule
|
---|
[e08c46] | 256 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
[266237] | 257 | };
|
---|
| 258 |
|
---|
| 259 | /** Unit Test of delete(atom *)
|
---|
| 260 | *
|
---|
| 261 | */
|
---|
| 262 | void ListOfBondsTest::DeleteAtomTest()
|
---|
| 263 | {
|
---|
[f8e486] | 264 | atom *atom1 = NULL;
|
---|
| 265 | atom *atom2 = NULL;
|
---|
[266237] | 266 | bond *Binder = NULL;
|
---|
[f8e486] | 267 | {
|
---|
| 268 | molecule::iterator iter = TestMolecule->begin();
|
---|
| 269 | atom1 = *iter;
|
---|
| 270 | iter++;
|
---|
| 271 | atom2 = *iter;
|
---|
| 272 | }
|
---|
[266237] | 273 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
| 274 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
| 275 |
|
---|
| 276 | // add bond
|
---|
| 277 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
| 278 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
| 279 |
|
---|
[6cfa36] | 280 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom1->ListOfBonds.size() );
|
---|
| 281 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, atom2->ListOfBonds.size() );
|
---|
| 282 |
|
---|
[a80241] | 283 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
| 284 |
|
---|
[266237] | 285 | // remove atom2
|
---|
[23b547] | 286 | World::getInstance().destroyAtom(atom2);
|
---|
[266237] | 287 |
|
---|
| 288 | // check bond if removed from other atom
|
---|
| 289 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, atom1->ListOfBonds.size() );
|
---|
| 290 |
|
---|
| 291 | // check if removed from molecule
|
---|
[a80241] | 292 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
[266237] | 293 | };
|
---|