1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * ListOfBondsUnitTest.cpp
|
---|
25 | *
|
---|
26 | * Created on: 18 Oct 2009
|
---|
27 | * Author: user
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 |
|
---|
37 | #include <cppunit/CompilerOutputter.h>
|
---|
38 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
39 | #include <cppunit/ui/text/TestRunner.h>
|
---|
40 |
|
---|
41 | #include <cstring>
|
---|
42 |
|
---|
43 | #include "CodePatterns/Log.hpp"
|
---|
44 | #include "World.hpp"
|
---|
45 | #include "Atom/atom.hpp"
|
---|
46 | #include "Bond/bond.hpp"
|
---|
47 | #include "Element/element.hpp"
|
---|
48 | #include "molecule.hpp"
|
---|
49 | #include "Element/periodentafel.hpp"
|
---|
50 | #include "World.hpp"
|
---|
51 | #include "WorldTime.hpp"
|
---|
52 |
|
---|
53 | #include "ListOfBondsUnitTest.hpp"
|
---|
54 |
|
---|
55 | #ifdef HAVE_TESTRUNNER
|
---|
56 | #include "UnitTestMain.hpp"
|
---|
57 | #endif /*HAVE_TESTRUNNER*/
|
---|
58 |
|
---|
59 | /********************************************** Test classes **************************************/
|
---|
60 |
|
---|
61 | // Registers the fixture into the 'registry'
|
---|
62 | CPPUNIT_TEST_SUITE_REGISTRATION( ListOfBondsTest );
|
---|
63 |
|
---|
64 |
|
---|
65 | void ListOfBondsTest::setUp()
|
---|
66 | {
|
---|
67 | atom *Walker = NULL;
|
---|
68 |
|
---|
69 | WorldTime::getInstance().setTime(0);
|
---|
70 |
|
---|
71 | // construct element
|
---|
72 | hydrogen = World::getInstance().getPeriode()->FindElement(1);
|
---|
73 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
|
---|
74 |
|
---|
75 | // construct molecule (tetraeder of hydrogens)
|
---|
76 | TestMolecule = World::getInstance().createMolecule();
|
---|
77 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
|
---|
78 | Walker = World::getInstance().createAtom();
|
---|
79 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
80 | Walker->setType(hydrogen);
|
---|
81 | Walker->setPosition(Vector(1., 0., 1. ));
|
---|
82 | TestMolecule->AddAtom(Walker);
|
---|
83 | Walker = World::getInstance().createAtom();
|
---|
84 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
85 | Walker->setType(hydrogen);
|
---|
86 | Walker->setPosition(Vector(0., 1., 1. ));
|
---|
87 | TestMolecule->AddAtom(Walker);
|
---|
88 | Walker = World::getInstance().createAtom();
|
---|
89 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
90 | Walker->setType(hydrogen);
|
---|
91 | Walker->setPosition(Vector(1., 1., 0. ));
|
---|
92 | TestMolecule->AddAtom(Walker);
|
---|
93 | Walker = World::getInstance().createAtom();
|
---|
94 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
95 | Walker->setType(hydrogen);
|
---|
96 | Walker->setPosition(Vector(0., 0., 0. ));
|
---|
97 | TestMolecule->AddAtom(Walker);
|
---|
98 |
|
---|
99 | // check that TestMolecule was correctly constructed
|
---|
100 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
|
---|
101 | };
|
---|
102 |
|
---|
103 |
|
---|
104 | void ListOfBondsTest::tearDown()
|
---|
105 | {
|
---|
106 | // remove
|
---|
107 | World::getInstance().destroyMolecule(TestMolecule);
|
---|
108 | // note that all the atoms, molecules, the tafel and the elements
|
---|
109 | // are all cleaned when the world is destroyed
|
---|
110 | World::purgeInstance();
|
---|
111 | logger::purgeInstance();
|
---|
112 | };
|
---|
113 |
|
---|
114 | /** Tests whether setup worked correctly.
|
---|
115 | *
|
---|
116 | */
|
---|
117 | void ListOfBondsTest::SetupTest()
|
---|
118 | {
|
---|
119 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->empty() );
|
---|
120 | CPPUNIT_ASSERT_EQUAL( (size_t)4, TestMolecule->size() );
|
---|
121 | };
|
---|
122 |
|
---|
123 | /** Unit Test of molecule::AddBond()
|
---|
124 | *
|
---|
125 | */
|
---|
126 | void ListOfBondsTest::AddingBondTest()
|
---|
127 | {
|
---|
128 | bond *Binder = NULL;
|
---|
129 | molecule::iterator iter = TestMolecule->begin();
|
---|
130 | atom *atom1 = *iter;
|
---|
131 | iter++;
|
---|
132 | atom *atom2 = *iter;
|
---|
133 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
134 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
135 |
|
---|
136 | // add bond
|
---|
137 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
138 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
139 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() );
|
---|
140 |
|
---|
141 | // check that bond contains the two atoms
|
---|
142 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom1) );
|
---|
143 | CPPUNIT_ASSERT_EQUAL( true, Binder->Contains(atom2) );
|
---|
144 |
|
---|
145 | // check that bond is present in both atoms
|
---|
146 | const BondList &bondlist1 = atom1->getListOfBonds();
|
---|
147 | BondList::const_iterator bonditer;
|
---|
148 | bonditer = bondlist1.begin();
|
---|
149 | bond *TestBond1 = *bonditer;
|
---|
150 | CPPUNIT_ASSERT_EQUAL( TestBond1, Binder );
|
---|
151 | const BondList &bondlist2 = atom2->getListOfBonds();
|
---|
152 | bonditer = bondlist2.begin();
|
---|
153 | bond *TestBond2 = *bonditer;
|
---|
154 | CPPUNIT_ASSERT_EQUAL( TestBond2, Binder );
|
---|
155 | };
|
---|
156 |
|
---|
157 | /** Unit Test of molecule::RemoveBond()
|
---|
158 | *
|
---|
159 | */
|
---|
160 | void ListOfBondsTest::RemovingBondTest()
|
---|
161 | {
|
---|
162 | bond *Binder = NULL;
|
---|
163 | molecule::iterator iter = TestMolecule->begin();
|
---|
164 | atom *atom1 = *iter;
|
---|
165 | iter++;
|
---|
166 | atom *atom2 = *iter;
|
---|
167 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
168 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
169 |
|
---|
170 | // add bond
|
---|
171 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
172 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
173 |
|
---|
174 | // remove bond
|
---|
175 | TestMolecule->RemoveBond(Binder);
|
---|
176 |
|
---|
177 | // check if removed from atoms
|
---|
178 | {
|
---|
179 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
180 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
181 | }
|
---|
182 | {
|
---|
183 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
184 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
185 | }
|
---|
186 |
|
---|
187 | // check if removed from molecule
|
---|
188 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
189 | };
|
---|
190 |
|
---|
191 | /** Unit Test of molecule::RemoveBonds()
|
---|
192 | *
|
---|
193 | */
|
---|
194 | void ListOfBondsTest::RemovingBondsTest()
|
---|
195 | {
|
---|
196 | bond *Binder = NULL;
|
---|
197 | molecule::iterator iter = TestMolecule->begin();
|
---|
198 | atom *atom1 = *iter;
|
---|
199 | iter++;
|
---|
200 | atom *atom2 = *iter;
|
---|
201 | iter++;
|
---|
202 | atom *atom3 = *iter;
|
---|
203 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
204 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
205 | CPPUNIT_ASSERT( atom3 != NULL );
|
---|
206 |
|
---|
207 | // add bond
|
---|
208 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
209 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
210 | Binder = TestMolecule->AddBond(atom1, atom3, 1);
|
---|
211 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
212 | Binder = TestMolecule->AddBond(atom2, atom3, 1);
|
---|
213 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
214 |
|
---|
215 | // check that all are present
|
---|
216 | {
|
---|
217 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
218 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, ListOfBonds.size() );
|
---|
219 | }
|
---|
220 | {
|
---|
221 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
222 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, ListOfBonds.size() );
|
---|
223 | }
|
---|
224 | {
|
---|
225 | const BondList& ListOfBonds = atom3->getListOfBonds();
|
---|
226 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, ListOfBonds.size() );
|
---|
227 | }
|
---|
228 |
|
---|
229 | // remove bond
|
---|
230 | TestMolecule->RemoveBonds(atom1);
|
---|
231 |
|
---|
232 | // check if removed from atoms
|
---|
233 | {
|
---|
234 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
235 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
236 | }
|
---|
237 | {
|
---|
238 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
239 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
240 | }
|
---|
241 | {
|
---|
242 | const BondList& ListOfBonds = atom3->getListOfBonds();
|
---|
243 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
244 | }
|
---|
245 |
|
---|
246 | // check if removed from molecule
|
---|
247 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
248 | CPPUNIT_ASSERT_EQUAL( (int)1, TestMolecule->getBondCount() );
|
---|
249 | };
|
---|
250 |
|
---|
251 | /** Unit Test of delete(bond *)
|
---|
252 | *
|
---|
253 | */
|
---|
254 | void ListOfBondsTest::DeleteBondTest()
|
---|
255 | {
|
---|
256 | bond *Binder = NULL;
|
---|
257 | molecule::iterator iter = TestMolecule->begin();
|
---|
258 | atom *atom1 = *iter;
|
---|
259 | iter++;
|
---|
260 | atom *atom2 = *iter;
|
---|
261 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
262 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
263 |
|
---|
264 | // add bond
|
---|
265 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
266 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
267 |
|
---|
268 | // remove bond
|
---|
269 | delete(Binder);
|
---|
270 |
|
---|
271 | // check if removed from atoms
|
---|
272 | {
|
---|
273 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
274 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
275 | }
|
---|
276 | {
|
---|
277 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
278 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
279 | }
|
---|
280 |
|
---|
281 | // check if removed from molecule
|
---|
282 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
283 | };
|
---|
284 |
|
---|
285 | /** Unit Test of molecule::RemoveAtom()
|
---|
286 | *
|
---|
287 | */
|
---|
288 | void ListOfBondsTest::RemoveAtomTest()
|
---|
289 | {
|
---|
290 | bond *Binder = NULL;
|
---|
291 | molecule::iterator iter = TestMolecule->begin();
|
---|
292 | atom *atom1 = *iter;
|
---|
293 | iter++;
|
---|
294 | atom *atom2 = *iter;
|
---|
295 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
296 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
297 |
|
---|
298 | // add bond
|
---|
299 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
300 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
301 |
|
---|
302 | // remove atom2
|
---|
303 | TestMolecule->RemoveAtom(atom2);
|
---|
304 |
|
---|
305 | // check bond if removed from other atom
|
---|
306 | {
|
---|
307 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
308 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
309 | }
|
---|
310 |
|
---|
311 | // check if removed from molecule
|
---|
312 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
313 | };
|
---|
314 |
|
---|
315 | /** Unit Test of delete(atom *)
|
---|
316 | *
|
---|
317 | */
|
---|
318 | void ListOfBondsTest::DeleteAtomTest()
|
---|
319 | {
|
---|
320 | atom *atom1 = NULL;
|
---|
321 | atom *atom2 = NULL;
|
---|
322 | bond *Binder = NULL;
|
---|
323 | {
|
---|
324 | molecule::iterator iter = TestMolecule->begin();
|
---|
325 | atom1 = *iter;
|
---|
326 | iter++;
|
---|
327 | atom2 = *iter;
|
---|
328 | }
|
---|
329 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
330 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
331 |
|
---|
332 | // add bond
|
---|
333 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
334 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
335 |
|
---|
336 | // access test via CurrentTime
|
---|
337 | {
|
---|
338 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
339 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
340 | }
|
---|
341 | {
|
---|
342 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
343 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
344 | }
|
---|
345 |
|
---|
346 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
347 |
|
---|
348 | // remove atom2
|
---|
349 | World::getInstance().destroyAtom(atom2);
|
---|
350 |
|
---|
351 | // check bond if removed from other atom for all time steps
|
---|
352 | {
|
---|
353 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
354 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
355 | }
|
---|
356 |
|
---|
357 | // check if removed from molecule
|
---|
358 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
359 | };
|
---|
360 |
|
---|
361 | /** Unit test on ListOfBonds at multiple time steps.
|
---|
362 | *
|
---|
363 | */
|
---|
364 | void ListOfBondsTest::MultipleTimeStepTest()
|
---|
365 | {
|
---|
366 | atom *atom1 = NULL;
|
---|
367 | atom *atom2 = NULL;
|
---|
368 | bond *Binder = NULL;
|
---|
369 | {
|
---|
370 | molecule::iterator iter = TestMolecule->begin();
|
---|
371 | atom1 = *iter;
|
---|
372 | iter++;
|
---|
373 | atom2 = *iter;
|
---|
374 | }
|
---|
375 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
376 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
377 |
|
---|
378 | // add bond
|
---|
379 | WorldTime::getInstance().setTime(0);
|
---|
380 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
381 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
382 | WorldTime::getInstance().setTime(1);
|
---|
383 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
384 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
385 |
|
---|
386 | // access test via CurrentTime
|
---|
387 | { // time step 0
|
---|
388 | WorldTime::getInstance().setTime(0);
|
---|
389 | {
|
---|
390 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
391 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
392 | }
|
---|
393 | {
|
---|
394 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
395 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
396 | }
|
---|
397 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
398 | }
|
---|
399 | { // time step 1
|
---|
400 | WorldTime::getInstance().setTime(1);
|
---|
401 | {
|
---|
402 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
403 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
404 | }
|
---|
405 | {
|
---|
406 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
407 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
408 | }
|
---|
409 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
410 | WorldTime::getInstance().setTime(0);
|
---|
411 | }
|
---|
412 |
|
---|
413 | // access time step directly.
|
---|
414 | { // time step 0
|
---|
415 | {
|
---|
416 | const BondList& ListOfBonds = atom1->getListOfBondsAtStep(0);
|
---|
417 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
418 | }
|
---|
419 | {
|
---|
420 | const BondList& ListOfBonds = atom2->getListOfBondsAtStep(0);
|
---|
421 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
422 | }
|
---|
423 | }
|
---|
424 | { // time step 1
|
---|
425 | {
|
---|
426 | const BondList& ListOfBonds = atom1->getListOfBondsAtStep(1);
|
---|
427 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
428 | }
|
---|
429 | {
|
---|
430 | const BondList& ListOfBonds = atom1->getListOfBondsAtStep(1);
|
---|
431 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | // remove atom2
|
---|
436 | World::getInstance().destroyAtom(atom2);
|
---|
437 |
|
---|
438 | // check bond if removed from other atom for all time steps
|
---|
439 | {
|
---|
440 | WorldTime::getInstance().setTime(0);
|
---|
441 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
442 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
443 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
444 | }
|
---|
445 | {
|
---|
446 | WorldTime::getInstance().setTime(1);
|
---|
447 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
448 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
449 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
450 | WorldTime::getInstance().setTime(0);
|
---|
451 | }
|
---|
452 |
|
---|
453 | }
|
---|