1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * AtomDescriptorUnitTest.cpp
|
---|
26 | *
|
---|
27 | * Created on: Feb 9, 2010
|
---|
28 | * Author: crueger
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "AtomDescriptorUnitTest.hpp"
|
---|
37 |
|
---|
38 | #include <cppunit/CompilerOutputter.h>
|
---|
39 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
40 | #include <cppunit/ui/text/TestRunner.h>
|
---|
41 | #include <iostream>
|
---|
42 |
|
---|
43 | #include <Descriptors/AtomDescriptor.hpp>
|
---|
44 | #include <Descriptors/AtomIdDescriptor.hpp>
|
---|
45 | #include <Descriptors/AtomOfMoleculeDescriptor.hpp>
|
---|
46 | #include <Descriptors/AtomOrderDescriptor.hpp>
|
---|
47 | #include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp>
|
---|
48 |
|
---|
49 | #include "World.hpp"
|
---|
50 | #include "Atom/atom.hpp"
|
---|
51 | #include "molecule.hpp"
|
---|
52 | #include "LinearAlgebra/Vector.hpp"
|
---|
53 |
|
---|
54 | #ifdef HAVE_TESTRUNNER
|
---|
55 | #include "UnitTestMain.hpp"
|
---|
56 | #endif /*HAVE_TESTRUNNER*/
|
---|
57 |
|
---|
58 | /********************************************** Test classes **************************************/
|
---|
59 | // Registers the fixture into the 'registry'
|
---|
60 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest );
|
---|
61 |
|
---|
62 | // set up and tear down
|
---|
63 | void AtomDescriptorTest::setUp()
|
---|
64 | {
|
---|
65 | World::getInstance();
|
---|
66 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
67 | atoms[i]= World::getInstance().createAtom();
|
---|
68 | atomIds[i]= atoms[i]->getId();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | void AtomDescriptorTest::tearDown()
|
---|
73 | {
|
---|
74 | World::purgeInstance();
|
---|
75 | }
|
---|
76 |
|
---|
77 | // some helper functions
|
---|
78 | static bool hasAllAtoms(
|
---|
79 | std::vector<const atom*> atoms,
|
---|
80 | atomId_t ids[ATOM_COUNT],
|
---|
81 | std::set<atomId_t> excluded = std::set<atomId_t>())
|
---|
82 | {
|
---|
83 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
84 | atomId_t id = ids[i];
|
---|
85 | if(!excluded.count(id)){
|
---|
86 | std::vector<const atom*>::const_iterator iter;
|
---|
87 | bool res=false;
|
---|
88 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
89 | res |= (*iter)->getId() == id;
|
---|
90 | }
|
---|
91 | if(!res) {
|
---|
92 | cout << "Atom " << id << " missing in returned list" << endl;
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static bool hasNoDuplicateAtoms(std::vector<const atom*> atoms)
|
---|
101 | {
|
---|
102 | std::set<atomId_t> found;
|
---|
103 | std::vector<const atom*>::const_iterator iter;
|
---|
104 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
105 | int id = (*iter)->getId();
|
---|
106 | if(found.count(id))
|
---|
107 | return false;
|
---|
108 | found.insert(id);
|
---|
109 | }
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | void AtomDescriptorTest::AtomBaseSetsTest()
|
---|
115 | {
|
---|
116 | std::vector<const atom*> allAtoms = const_cast<const World &>(World::getInstance()).
|
---|
117 | getAllAtoms(AllAtoms());
|
---|
118 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds));
|
---|
119 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms));
|
---|
120 |
|
---|
121 | std::vector<const atom*> noAtoms = const_cast<const World &>(World::getInstance()).
|
---|
122 | getAllAtoms(NoAtoms());
|
---|
123 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
---|
124 | }
|
---|
125 |
|
---|
126 | void AtomDescriptorTest::AtomIdTest()
|
---|
127 | {
|
---|
128 | // test Atoms from boundaries and middle of the set
|
---|
129 | const atom* testAtom;
|
---|
130 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
131 | getAtom(AtomById(atomIds[0]));
|
---|
132 | CPPUNIT_ASSERT(testAtom);
|
---|
133 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
134 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
135 | getAtom(AtomById(atomIds[ATOM_COUNT/2]));
|
---|
136 | CPPUNIT_ASSERT(testAtom);
|
---|
137 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
|
---|
138 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
139 | getAtom(AtomById(atomIds[ATOM_COUNT-1]));
|
---|
140 | CPPUNIT_ASSERT(testAtom);
|
---|
141 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
|
---|
142 |
|
---|
143 | // find some ID that has not been created
|
---|
144 | atomId_t outsideId=0;
|
---|
145 | bool res = false;
|
---|
146 | for(outsideId=0;!res;++outsideId) {
|
---|
147 | res = true;
|
---|
148 | for(int i = 0; i < ATOM_COUNT; ++i){
|
---|
149 | res &= atomIds[i]!=outsideId;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | // test from outside of set
|
---|
153 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
154 | getAtom(AtomById(outsideId));
|
---|
155 | CPPUNIT_ASSERT(!testAtom);
|
---|
156 | }
|
---|
157 |
|
---|
158 | void AtomDescriptorTest::AtomOfMoleculeTest()
|
---|
159 | {
|
---|
160 | // test Atoms from boundaries and middle of the set
|
---|
161 | atom* testAtom;
|
---|
162 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
---|
163 | CPPUNIT_ASSERT(testAtom);
|
---|
164 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
165 |
|
---|
166 | // create some molecule and associate atom to it
|
---|
167 | testAtom->setType(1);
|
---|
168 | molecule * newmol = World::getInstance().createMolecule();
|
---|
169 | newmol->AddAtom(testAtom);
|
---|
170 | CPPUNIT_ASSERT_EQUAL(newmol->getId(), testAtom->getMolecule()->getId());
|
---|
171 |
|
---|
172 | // get atom by descriptor
|
---|
173 | World::ConstAtomComposite atoms = const_cast<const World &>(World::getInstance()).
|
---|
174 | getAllAtoms(AtomOfMolecule(newmol->getId()));
|
---|
175 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atoms.size() );
|
---|
176 | CPPUNIT_ASSERT_EQUAL( (*atoms.begin())->getId(), testAtom->getId() );
|
---|
177 |
|
---|
178 | // remove molecule again
|
---|
179 | World::getInstance().destroyMolecule(newmol);
|
---|
180 | }
|
---|
181 |
|
---|
182 | void AtomDescriptorTest::AtomOrderTest()
|
---|
183 | {
|
---|
184 | const atom* testAtom;
|
---|
185 |
|
---|
186 | // test in normal order: 1, 2, ...
|
---|
187 | for(int i=1;i<=ATOM_COUNT;++i){
|
---|
188 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
189 | getAtom(AtomByOrder(i));
|
---|
190 | CPPUNIT_ASSERT_EQUAL( atomIds[i-1], testAtom->getId());
|
---|
191 | }
|
---|
192 |
|
---|
193 | // test in reverse order: -1, -2, ...
|
---|
194 | for(int i=1; i<= ATOM_COUNT;++i){
|
---|
195 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
196 | getAtom(AtomByOrder(-i));
|
---|
197 | CPPUNIT_ASSERT_EQUAL( atomIds[(int)ATOM_COUNT-i], testAtom->getId());
|
---|
198 | }
|
---|
199 |
|
---|
200 | // test from outside of set
|
---|
201 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
202 | getAtom(AtomByOrder(ATOM_COUNT+1));
|
---|
203 | CPPUNIT_ASSERT(!testAtom);
|
---|
204 | testAtom = const_cast<const World &>(World::getInstance()).
|
---|
205 | getAtom(AtomByOrder(-ATOM_COUNT-1));
|
---|
206 | CPPUNIT_ASSERT(!testAtom);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | std::set<atomId_t> getDistanceList(const double distance, const Vector &position, atom **list)
|
---|
211 | {
|
---|
212 | const double distanceSquared = distance*distance;
|
---|
213 | std::set<atomId_t> reflist;
|
---|
214 | for (size_t i=0; i<ATOM_COUNT;++i)
|
---|
215 | if (list[i]->getPosition().DistanceSquared(position) < distanceSquared)
|
---|
216 | reflist.insert ( list[i]->getId() );
|
---|
217 | return reflist;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | std::set<atomId_t> getIdList(const World::ConstAtomComposite &list)
|
---|
222 | {
|
---|
223 | std::set<atomId_t> testlist;
|
---|
224 | for (World::ConstAtomComposite::const_iterator iter = list.begin();
|
---|
225 | iter != list.end(); ++iter)
|
---|
226 | testlist.insert( (*iter)->getId() );
|
---|
227 | return testlist;
|
---|
228 | }
|
---|
229 |
|
---|
230 | //void AtomDescriptorTest::AtomsShapeTest()
|
---|
231 | //{
|
---|
232 | // // align atoms along an axis
|
---|
233 | // for(int i=0;i<ATOM_COUNT;++i) {
|
---|
234 | // atoms[i]->setPosition(Vector((double)i, 0., 0.));
|
---|
235 | // //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
|
---|
236 | // }
|
---|
237 | //
|
---|
238 | // // get atom by descriptor ...
|
---|
239 | // // ... from origin up to 2.5
|
---|
240 | // {
|
---|
241 | // const double distance = 1.5;
|
---|
242 | // Vector position(0.,0.,0.);
|
---|
243 | // Shape s = Sphere(position, distance);
|
---|
244 | // World::ConstAtomComposite atomlist = const_cast<const World &>(World::getInstance()).
|
---|
245 | // getAllAtoms(AtomsByShape(s));
|
---|
246 | // CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
|
---|
247 | // std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
---|
248 | // std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
249 | // CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
---|
250 | // }
|
---|
251 | // // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
|
---|
252 | // {
|
---|
253 | // const double distance = 2.9;
|
---|
254 | // Vector position(4.,0.,0.);
|
---|
255 | // Shape s = Sphere(position, distance);
|
---|
256 | // World::ConstAtomComposite atomlist = const_cast<const World &>(World::getInstance()).
|
---|
257 | // getAllAtoms(AtomsByShape(s));
|
---|
258 | // CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
|
---|
259 | // std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
---|
260 | // std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
261 | // CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
---|
262 | // }
|
---|
263 | // // ... from (10,0,0) up to 1.5
|
---|
264 | // {
|
---|
265 | // const double distance = 1.5;
|
---|
266 | // Vector *position = new Vector(10.,0.,0.);
|
---|
267 | // Shape s = Sphere(position, distance);
|
---|
268 | // World::ConstAtomComposite atomlist = const_cast<const World &>(World::getInstance()).
|
---|
269 | // getAllAtoms(AtomsByShape(s));
|
---|
270 | // CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
|
---|
271 | // std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
|
---|
272 | // std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
273 | // CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
---|
274 | // delete position;
|
---|
275 | // }
|
---|
276 | //}
|
---|
277 |
|
---|
278 | void AtomDescriptorTest::AtomsWithinDistanceOfTest()
|
---|
279 | {
|
---|
280 | // align atoms along an axis
|
---|
281 | for(int i=0;i<ATOM_COUNT;++i) {
|
---|
282 | atoms[i]->setPosition(Vector((double)i, 0., 0.));
|
---|
283 | //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
|
---|
284 | }
|
---|
285 |
|
---|
286 | // get atom by descriptor ...
|
---|
287 | // ... from origin up to 2.5
|
---|
288 | {
|
---|
289 | const double distance = 1.5;
|
---|
290 | Vector position(0.,0.,0.);
|
---|
291 | World::ConstAtomComposite atomlist = const_cast<const World &>(World::getInstance()).
|
---|
292 | getAllAtoms(AtomsWithinDistanceOf(distance, position));
|
---|
293 | CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
|
---|
294 | std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
---|
295 | std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
296 | CPPUNIT_ASSERT( reflist == testlist );
|
---|
297 | }
|
---|
298 | // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
|
---|
299 | {
|
---|
300 | const double distance = 2.9;
|
---|
301 | World::ConstAtomComposite atomlist = const_cast<const World &>(World::getInstance()).
|
---|
302 | getAllAtoms(AtomsWithinDistanceOf(distance, Vector(4.,0.,0.)));
|
---|
303 | CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
|
---|
304 | std::set<atomId_t> reflist = getDistanceList(distance, Vector(4.,0.,0.), atoms);
|
---|
305 | std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
306 | CPPUNIT_ASSERT( reflist == testlist );
|
---|
307 | }
|
---|
308 | // ... from (10,0,0) up to 1.5
|
---|
309 | {
|
---|
310 | const double distance = 1.5;
|
---|
311 | Vector *position = new Vector(10.,0.,0.);
|
---|
312 | World::ConstAtomComposite atomlist = const_cast<const World &>(World::getInstance()).
|
---|
313 | getAllAtoms(AtomsWithinDistanceOf(distance, *position));
|
---|
314 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
|
---|
315 | std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
|
---|
316 | std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
317 | CPPUNIT_ASSERT( reflist == testlist );
|
---|
318 | delete position;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | void AtomDescriptorTest::AtomCalcTest()
|
---|
323 | {
|
---|
324 | // test some elementary set operations
|
---|
325 | {
|
---|
326 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
327 | getAllAtoms(AllAtoms()||NoAtoms());
|
---|
328 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
329 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
330 | }
|
---|
331 |
|
---|
332 | {
|
---|
333 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
334 | getAllAtoms(NoAtoms()||AllAtoms());
|
---|
335 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
336 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
337 | }
|
---|
338 |
|
---|
339 | {
|
---|
340 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
341 | getAllAtoms(NoAtoms()&&AllAtoms());
|
---|
342 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
343 | }
|
---|
344 |
|
---|
345 | {
|
---|
346 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
347 | getAllAtoms(AllAtoms()&&NoAtoms());
|
---|
348 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
349 | }
|
---|
350 |
|
---|
351 | {
|
---|
352 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
353 | getAllAtoms(!AllAtoms());
|
---|
354 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
355 | }
|
---|
356 |
|
---|
357 | {
|
---|
358 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
359 | getAllAtoms(!NoAtoms());
|
---|
360 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
361 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
362 | }
|
---|
363 | // exclude and include some atoms
|
---|
364 | {
|
---|
365 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
366 | getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
|
---|
367 | std::set<atomId_t> excluded;
|
---|
368 | excluded.insert(atomIds[ATOM_COUNT/2]);
|
---|
369 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded));
|
---|
370 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
371 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
|
---|
372 | }
|
---|
373 |
|
---|
374 | {
|
---|
375 | std::vector<const atom*> testAtoms = const_cast<const World &>(World::getInstance()).
|
---|
376 | getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
|
---|
377 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
---|
378 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
|
---|
379 | }
|
---|
380 | }
|
---|