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