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