1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2011 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * AtomDescriptorUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Feb 9, 2010
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "AtomDescriptorUnitTest.hpp"
|
---|
21 |
|
---|
22 | #include <cppunit/CompilerOutputter.h>
|
---|
23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
25 | #include <iostream>
|
---|
26 |
|
---|
27 | #include <Descriptors/AtomDescriptor.hpp>
|
---|
28 | #include <Descriptors/AtomIdDescriptor.hpp>
|
---|
29 | #include <Descriptors/AtomOfMoleculeDescriptor.hpp>
|
---|
30 | #include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp>
|
---|
31 |
|
---|
32 | #include "World.hpp"
|
---|
33 | #include "Atom/atom.hpp"
|
---|
34 | #include "molecule.hpp"
|
---|
35 | #include "LinearAlgebra/Vector.hpp"
|
---|
36 |
|
---|
37 | #ifdef HAVE_TESTRUNNER
|
---|
38 | #include "UnitTestMain.hpp"
|
---|
39 | #endif /*HAVE_TESTRUNNER*/
|
---|
40 |
|
---|
41 | /********************************************** Test classes **************************************/
|
---|
42 | // Registers the fixture into the 'registry'
|
---|
43 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest );
|
---|
44 |
|
---|
45 | // set up and tear down
|
---|
46 | void AtomDescriptorTest::setUp()
|
---|
47 | {
|
---|
48 | World::getInstance();
|
---|
49 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
50 | atoms[i]= World::getInstance().createAtom();
|
---|
51 | atomIds[i]= atoms[i]->getId();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | void AtomDescriptorTest::tearDown()
|
---|
56 | {
|
---|
57 | World::purgeInstance();
|
---|
58 | }
|
---|
59 |
|
---|
60 | // some helper functions
|
---|
61 | static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>())
|
---|
62 | {
|
---|
63 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
64 | atomId_t id = ids[i];
|
---|
65 | if(!excluded.count(id)){
|
---|
66 | std::vector<atom*>::iterator iter;
|
---|
67 | bool res=false;
|
---|
68 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
69 | res |= (*iter)->getId() == id;
|
---|
70 | }
|
---|
71 | if(!res) {
|
---|
72 | cout << "Atom " << id << " missing in returned list" << endl;
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static bool hasNoDuplicateAtoms(std::vector<atom*> atoms)
|
---|
81 | {
|
---|
82 | std::set<atomId_t> found;
|
---|
83 | std::vector<atom*>::iterator iter;
|
---|
84 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
85 | int id = (*iter)->getId();
|
---|
86 | if(found.count(id))
|
---|
87 | return false;
|
---|
88 | found.insert(id);
|
---|
89 | }
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | void AtomDescriptorTest::AtomBaseSetsTest()
|
---|
95 | {
|
---|
96 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
97 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds));
|
---|
98 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms));
|
---|
99 |
|
---|
100 | std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms());
|
---|
101 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
---|
102 | }
|
---|
103 |
|
---|
104 | void AtomDescriptorTest::AtomIdTest()
|
---|
105 | {
|
---|
106 | // test Atoms from boundaries and middle of the set
|
---|
107 | atom* testAtom;
|
---|
108 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
---|
109 | CPPUNIT_ASSERT(testAtom);
|
---|
110 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
111 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2]));
|
---|
112 | CPPUNIT_ASSERT(testAtom);
|
---|
113 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
|
---|
114 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1]));
|
---|
115 | CPPUNIT_ASSERT(testAtom);
|
---|
116 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
|
---|
117 |
|
---|
118 | // find some ID that has not been created
|
---|
119 | atomId_t outsideId=0;
|
---|
120 | bool res = false;
|
---|
121 | for(outsideId=0;!res;++outsideId) {
|
---|
122 | res = true;
|
---|
123 | for(int i = 0; i < ATOM_COUNT; ++i){
|
---|
124 | res &= atomIds[i]!=outsideId;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | // test from outside of set
|
---|
128 | testAtom = World::getInstance().getAtom(AtomById(outsideId));
|
---|
129 | CPPUNIT_ASSERT(!testAtom);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void AtomDescriptorTest::AtomOfMoleculeTest()
|
---|
133 | {
|
---|
134 | // test Atoms from boundaries and middle of the set
|
---|
135 | atom* testAtom;
|
---|
136 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0]));
|
---|
137 | CPPUNIT_ASSERT(testAtom);
|
---|
138 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
139 |
|
---|
140 | // create some molecule and associate atom to it
|
---|
141 | testAtom->setType(1);
|
---|
142 | molecule * newmol = World::getInstance().createMolecule();
|
---|
143 | newmol->AddAtom(testAtom);
|
---|
144 | CPPUNIT_ASSERT_EQUAL(newmol->getId(), testAtom->getMolecule()->getId());
|
---|
145 |
|
---|
146 | // get atom by descriptor
|
---|
147 | World::AtomComposite atoms = World::getInstance().getAllAtoms(AtomOfMolecule(newmol->getId()));
|
---|
148 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atoms.size() );
|
---|
149 | CPPUNIT_ASSERT_EQUAL( (*atoms.begin())->getId(), testAtom->getId() );
|
---|
150 |
|
---|
151 | // remove molecule again
|
---|
152 | World::getInstance().destroyMolecule(newmol);
|
---|
153 | }
|
---|
154 |
|
---|
155 | std::set<atomId_t> getDistanceList(const double distance, const Vector &position, atom **list)
|
---|
156 | {
|
---|
157 | const double distanceSquared = distance*distance;
|
---|
158 | std::set<atomId_t> reflist;
|
---|
159 | for (size_t i=0; i<ATOM_COUNT;++i)
|
---|
160 | if (list[i]->getPosition().DistanceSquared(position) < distanceSquared)
|
---|
161 | reflist.insert ( list[i]->getId() );
|
---|
162 | return reflist;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | std::set<atomId_t> getIdList(const World::AtomComposite &list)
|
---|
167 | {
|
---|
168 | std::set<atomId_t> testlist;
|
---|
169 | for (World::AtomComposite::const_iterator iter = list.begin();
|
---|
170 | iter != list.end(); ++iter)
|
---|
171 | testlist.insert( (*iter)->getId() );
|
---|
172 | return testlist;
|
---|
173 | }
|
---|
174 |
|
---|
175 | void AtomDescriptorTest::AtomsWithinDistanceOfTest()
|
---|
176 | {
|
---|
177 | // align atoms along an axis
|
---|
178 | for(int i=0;i<ATOM_COUNT;++i) {
|
---|
179 | atoms[i]->setPosition(Vector((double)i, 0., 0.));
|
---|
180 | //std::cout << "atoms[" << i << "]: " << atoms[i]->getId() << " at " << atoms[i]->getPosition() << std::endl;
|
---|
181 | }
|
---|
182 |
|
---|
183 | // get atom by descriptor ...
|
---|
184 | // ... from origin up to 2.5
|
---|
185 | {
|
---|
186 | const double distance = 1.5;
|
---|
187 | Vector position(0.,0.,0.);
|
---|
188 | World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, position));
|
---|
189 | CPPUNIT_ASSERT_EQUAL( (size_t)2, atomlist.size() );
|
---|
190 | std::set<atomId_t> reflist = getDistanceList(distance, position, atoms);
|
---|
191 | std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
192 | CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
---|
193 | }
|
---|
194 | // ... from (4,0,0) up to 2.9 (i.e. more shells or different view)
|
---|
195 | {
|
---|
196 | const double distance = 2.9;
|
---|
197 | World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, Vector(4.,0.,0.)));
|
---|
198 | CPPUNIT_ASSERT_EQUAL( (size_t)5, atomlist.size() );
|
---|
199 | std::set<atomId_t> reflist = getDistanceList(distance, Vector(4.,0.,0.), atoms);
|
---|
200 | std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
201 | CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
---|
202 | }
|
---|
203 | // ... from (10,0,0) up to 1.5
|
---|
204 | {
|
---|
205 | const double distance = 1.5;
|
---|
206 | Vector *position = new Vector(10.,0.,0.);
|
---|
207 | World::AtomComposite atomlist = World::getInstance().getAllAtoms(AtomsWithinDistanceOf(distance, *position));
|
---|
208 | CPPUNIT_ASSERT_EQUAL( (size_t)1, atomlist.size() );
|
---|
209 | std::set<atomId_t> reflist = getDistanceList(distance, *position, atoms);
|
---|
210 | std::set<atomId_t> testlist = getIdList(atomlist);
|
---|
211 | CPPUNIT_ASSERT_EQUAL( reflist, testlist );
|
---|
212 | delete position;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | void AtomDescriptorTest::AtomCalcTest()
|
---|
217 | {
|
---|
218 | // test some elementary set operations
|
---|
219 | {
|
---|
220 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms());
|
---|
221 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
222 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
223 | }
|
---|
224 |
|
---|
225 | {
|
---|
226 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms());
|
---|
227 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
228 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
229 | }
|
---|
230 |
|
---|
231 | {
|
---|
232 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms());
|
---|
233 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
234 | }
|
---|
235 |
|
---|
236 | {
|
---|
237 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms());
|
---|
238 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
239 | }
|
---|
240 |
|
---|
241 | {
|
---|
242 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms());
|
---|
243 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
244 | }
|
---|
245 |
|
---|
246 | {
|
---|
247 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms());
|
---|
248 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds));
|
---|
249 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
250 | }
|
---|
251 | // exclude and include some atoms
|
---|
252 | {
|
---|
253 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2])));
|
---|
254 | std::set<atomId_t> excluded;
|
---|
255 | excluded.insert(atomIds[ATOM_COUNT/2]);
|
---|
256 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded));
|
---|
257 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms));
|
---|
258 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
|
---|
259 | }
|
---|
260 |
|
---|
261 | {
|
---|
262 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2])));
|
---|
263 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
---|
264 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
|
---|
265 | }
|
---|
266 | }
|
---|