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 |
|
---|
37 | #include "ListOfBondsUnitTest.hpp"
|
---|
38 |
|
---|
39 | #ifdef HAVE_TESTRUNNER
|
---|
40 | #include "UnitTestMain.hpp"
|
---|
41 | #endif /*HAVE_TESTRUNNER*/
|
---|
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
|
---|
54 | hydrogen = World::getInstance().getPeriode()->FindElement(1);
|
---|
55 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
|
---|
56 |
|
---|
57 | // construct molecule (tetraeder of hydrogens)
|
---|
58 | TestMolecule = World::getInstance().createMolecule();
|
---|
59 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
|
---|
60 | Walker = World::getInstance().createAtom();
|
---|
61 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
62 | Walker->setType(hydrogen);
|
---|
63 | Walker->setPosition(Vector(1., 0., 1. ));
|
---|
64 | TestMolecule->AddAtom(Walker);
|
---|
65 | Walker = World::getInstance().createAtom();
|
---|
66 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
67 | Walker->setType(hydrogen);
|
---|
68 | Walker->setPosition(Vector(0., 1., 1. ));
|
---|
69 | TestMolecule->AddAtom(Walker);
|
---|
70 | Walker = World::getInstance().createAtom();
|
---|
71 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
72 | Walker->setType(hydrogen);
|
---|
73 | Walker->setPosition(Vector(1., 1., 0. ));
|
---|
74 | TestMolecule->AddAtom(Walker);
|
---|
75 | Walker = World::getInstance().createAtom();
|
---|
76 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
77 | Walker->setType(hydrogen);
|
---|
78 | Walker->setPosition(Vector(0., 0., 0. ));
|
---|
79 | TestMolecule->AddAtom(Walker);
|
---|
80 |
|
---|
81 | // check that TestMolecule was correctly constructed
|
---|
82 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | void ListOfBondsTest::tearDown()
|
---|
87 | {
|
---|
88 | // remove
|
---|
89 | World::getInstance().destroyMolecule(TestMolecule);
|
---|
90 | // note that all the atoms, molecules, the tafel and the elements
|
---|
91 | // are all cleaned when the world is destroyed
|
---|
92 | World::purgeInstance();
|
---|
93 | logger::purgeInstance();
|
---|
94 | };
|
---|
95 |
|
---|
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 |
|
---|
105 | /** Unit Test of molecule::AddBond()
|
---|
106 | *
|
---|
107 | */
|
---|
108 | void ListOfBondsTest::AddingBondTest()
|
---|
109 | {
|
---|
110 | bond *Binder = NULL;
|
---|
111 | molecule::iterator iter = TestMolecule->begin();
|
---|
112 | atom *atom1 = *iter;
|
---|
113 | iter++;
|
---|
114 | atom *atom2 = *iter;
|
---|
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 );
|
---|
121 | CPPUNIT_ASSERT_EQUAL ( true, TestMolecule->hasBondStructure() );
|
---|
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 | BondList::const_iterator bonditer;
|
---|
129 | bonditer = atom1->getListOfBonds().begin();
|
---|
130 | bond *TestBond1 = *bonditer;
|
---|
131 | CPPUNIT_ASSERT_EQUAL( TestBond1, Binder );
|
---|
132 | bonditer = atom2->getListOfBonds().begin();
|
---|
133 | bond *TestBond2 = *bonditer;
|
---|
134 | CPPUNIT_ASSERT_EQUAL( TestBond2, Binder );
|
---|
135 | };
|
---|
136 |
|
---|
137 | /** Unit Test of molecule::RemoveBond()
|
---|
138 | *
|
---|
139 | */
|
---|
140 | void ListOfBondsTest::RemovingBondTest()
|
---|
141 | {
|
---|
142 | bond *Binder = NULL;
|
---|
143 | molecule::iterator iter = TestMolecule->begin();
|
---|
144 | atom *atom1 = *iter;
|
---|
145 | iter++;
|
---|
146 | atom *atom2 = *iter;
|
---|
147 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
148 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
149 |
|
---|
150 | // add bond
|
---|
151 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
152 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
153 |
|
---|
154 | // remove bond
|
---|
155 | TestMolecule->RemoveBond(Binder);
|
---|
156 |
|
---|
157 | // check if removed from atoms
|
---|
158 | {
|
---|
159 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
160 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
161 | }
|
---|
162 | {
|
---|
163 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
164 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
165 | }
|
---|
166 |
|
---|
167 | // check if removed from molecule
|
---|
168 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
169 | };
|
---|
170 |
|
---|
171 | /** Unit Test of molecule::RemoveBonds()
|
---|
172 | *
|
---|
173 | */
|
---|
174 | void ListOfBondsTest::RemovingBondsTest()
|
---|
175 | {
|
---|
176 | bond *Binder = NULL;
|
---|
177 | molecule::iterator iter = TestMolecule->begin();
|
---|
178 | atom *atom1 = *iter;
|
---|
179 | iter++;
|
---|
180 | atom *atom2 = *iter;
|
---|
181 | iter++;
|
---|
182 | atom *atom3 = *iter;
|
---|
183 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
184 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
185 | CPPUNIT_ASSERT( atom3 != NULL );
|
---|
186 |
|
---|
187 | // add bond
|
---|
188 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
189 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
190 | Binder = TestMolecule->AddBond(atom1, atom3, 1);
|
---|
191 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
192 | Binder = TestMolecule->AddBond(atom2, atom3, 1);
|
---|
193 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
194 |
|
---|
195 | // check that all are present
|
---|
196 | {
|
---|
197 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
198 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, ListOfBonds.size() );
|
---|
199 | }
|
---|
200 | {
|
---|
201 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
202 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, ListOfBonds.size() );
|
---|
203 | }
|
---|
204 | {
|
---|
205 | const BondList& ListOfBonds = atom3->getListOfBonds();
|
---|
206 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, ListOfBonds.size() );
|
---|
207 | }
|
---|
208 |
|
---|
209 | // remove bond
|
---|
210 | TestMolecule->RemoveBonds(atom1);
|
---|
211 |
|
---|
212 | // check if removed from atoms
|
---|
213 | {
|
---|
214 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
215 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
216 | }
|
---|
217 | {
|
---|
218 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
219 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
220 | }
|
---|
221 | {
|
---|
222 | const BondList& ListOfBonds = atom3->getListOfBonds();
|
---|
223 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
224 | }
|
---|
225 |
|
---|
226 | // check if removed from molecule
|
---|
227 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
228 | CPPUNIT_ASSERT_EQUAL( (unsigned int)1, TestMolecule->CountBonds() );
|
---|
229 | };
|
---|
230 |
|
---|
231 | /** Unit Test of delete(bond *)
|
---|
232 | *
|
---|
233 | */
|
---|
234 | void ListOfBondsTest::DeleteBondTest()
|
---|
235 | {
|
---|
236 | bond *Binder = NULL;
|
---|
237 | molecule::iterator iter = TestMolecule->begin();
|
---|
238 | atom *atom1 = *iter;
|
---|
239 | iter++;
|
---|
240 | atom *atom2 = *iter;
|
---|
241 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
242 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
243 |
|
---|
244 | // add bond
|
---|
245 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
246 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
247 |
|
---|
248 | // remove bond
|
---|
249 | delete(Binder);
|
---|
250 |
|
---|
251 | // check if removed from atoms
|
---|
252 | {
|
---|
253 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
254 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
255 | }
|
---|
256 | {
|
---|
257 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
258 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
259 | }
|
---|
260 |
|
---|
261 | // check if removed from molecule
|
---|
262 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
263 | };
|
---|
264 |
|
---|
265 | /** Unit Test of molecule::RemoveAtom()
|
---|
266 | *
|
---|
267 | */
|
---|
268 | void ListOfBondsTest::RemoveAtomTest()
|
---|
269 | {
|
---|
270 | bond *Binder = NULL;
|
---|
271 | molecule::iterator iter = TestMolecule->begin();
|
---|
272 | atom *atom1 = *iter;
|
---|
273 | iter++;
|
---|
274 | atom *atom2 = *iter;
|
---|
275 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
276 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
277 |
|
---|
278 | // add bond
|
---|
279 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
280 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
281 |
|
---|
282 | // remove atom2
|
---|
283 | TestMolecule->RemoveAtom(atom2);
|
---|
284 |
|
---|
285 | // check bond if removed from other atom
|
---|
286 | {
|
---|
287 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
288 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
289 | }
|
---|
290 |
|
---|
291 | // check if removed from molecule
|
---|
292 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
293 | };
|
---|
294 |
|
---|
295 | /** Unit Test of delete(atom *)
|
---|
296 | *
|
---|
297 | */
|
---|
298 | void ListOfBondsTest::DeleteAtomTest()
|
---|
299 | {
|
---|
300 | atom *atom1 = NULL;
|
---|
301 | atom *atom2 = NULL;
|
---|
302 | bond *Binder = NULL;
|
---|
303 | {
|
---|
304 | molecule::iterator iter = TestMolecule->begin();
|
---|
305 | atom1 = *iter;
|
---|
306 | iter++;
|
---|
307 | atom2 = *iter;
|
---|
308 | }
|
---|
309 | CPPUNIT_ASSERT( atom1 != NULL );
|
---|
310 | CPPUNIT_ASSERT( atom2 != NULL );
|
---|
311 |
|
---|
312 | // add bond
|
---|
313 | Binder = TestMolecule->AddBond(atom1, atom2, 1);
|
---|
314 | CPPUNIT_ASSERT( Binder != NULL );
|
---|
315 |
|
---|
316 | {
|
---|
317 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
318 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
319 | }
|
---|
320 | {
|
---|
321 | const BondList& ListOfBonds = atom2->getListOfBonds();
|
---|
322 | CPPUNIT_ASSERT_EQUAL( (size_t) 1, ListOfBonds.size() );
|
---|
323 | }
|
---|
324 |
|
---|
325 | CPPUNIT_ASSERT_EQUAL( true, TestMolecule->hasBondStructure() );
|
---|
326 |
|
---|
327 | // remove atom2
|
---|
328 | World::getInstance().destroyAtom(atom2);
|
---|
329 |
|
---|
330 | // check bond if removed from other atom
|
---|
331 | {
|
---|
332 | const BondList& ListOfBonds = atom1->getListOfBonds();
|
---|
333 | CPPUNIT_ASSERT_EQUAL( (size_t) 0, ListOfBonds.size() );
|
---|
334 | }
|
---|
335 |
|
---|
336 | // check if removed from molecule
|
---|
337 | CPPUNIT_ASSERT_EQUAL( false, TestMolecule->hasBondStructure() );
|
---|
338 | };
|
---|