| [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> | 
|---|
| [bbab87] | 30 | #include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp> | 
|---|
| [7a1ce5] | 31 |  | 
|---|
|  | 32 | #include "World.hpp" | 
|---|
| [6f0841] | 33 | #include "Atom/atom.hpp" | 
|---|
| [b49568] | 34 | #include "molecule.hpp" | 
|---|
| [bbab87] | 35 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [7a1ce5] | 36 |  | 
|---|
| [9b6b2f] | 37 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 38 | #include "UnitTestMain.hpp" | 
|---|
|  | 39 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 40 |  | 
|---|
|  | 41 | /********************************************** Test classes **************************************/ | 
|---|
| [7a1ce5] | 42 | // Registers the fixture into the 'registry' | 
|---|
| [57adc7] | 43 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest ); | 
|---|
| [7a1ce5] | 44 |  | 
|---|
|  | 45 | // set up and tear down | 
|---|
| [bbab87] | 46 | void AtomDescriptorTest::setUp() | 
|---|
|  | 47 | { | 
|---|
| [23b547] | 48 | World::getInstance(); | 
|---|
| [7a1ce5] | 49 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| [23b547] | 50 | atoms[i]= World::getInstance().createAtom(); | 
|---|
| [57adc7] | 51 | atomIds[i]= atoms[i]->getId(); | 
|---|
| [7a1ce5] | 52 | } | 
|---|
|  | 53 | } | 
|---|
| [57adc7] | 54 |  | 
|---|
| [bbab87] | 55 | void AtomDescriptorTest::tearDown() | 
|---|
|  | 56 | { | 
|---|
| [23b547] | 57 | World::purgeInstance(); | 
|---|
| [7a1ce5] | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | // some helper functions | 
|---|
| [bbab87] | 61 | static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()) | 
|---|
|  | 62 | { | 
|---|
| [46d958] | 63 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| [57adc7] | 64 | atomId_t id = ids[i]; | 
|---|
| [46d958] | 65 | if(!excluded.count(id)){ | 
|---|
| [7a1ce5] | 66 | std::vector<atom*>::iterator iter; | 
|---|
|  | 67 | bool res=false; | 
|---|
|  | 68 | for(iter=atoms.begin();iter!=atoms.end();++iter){ | 
|---|
| [46d958] | 69 | res |= (*iter)->getId() == id; | 
|---|
| [7a1ce5] | 70 | } | 
|---|
|  | 71 | if(!res) { | 
|---|
| [46d958] | 72 | cout << "Atom " << id << " missing in returned list" << endl; | 
|---|
| [7a1ce5] | 73 | return false; | 
|---|
|  | 74 | } | 
|---|
|  | 75 | } | 
|---|
|  | 76 | } | 
|---|
|  | 77 | return true; | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [bbab87] | 80 | static bool hasNoDuplicateAtoms(std::vector<atom*> atoms) | 
|---|
|  | 81 | { | 
|---|
| [57adc7] | 82 | std::set<atomId_t> found; | 
|---|
| [7a1ce5] | 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 |  | 
|---|
| [bbab87] | 94 | void AtomDescriptorTest::AtomBaseSetsTest() | 
|---|
|  | 95 | { | 
|---|
| [23b547] | 96 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms()); | 
|---|
| [57adc7] | 97 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds)); | 
|---|
|  | 98 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms)); | 
|---|
| [7a1ce5] | 99 |  | 
|---|
| [23b547] | 100 | std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms()); | 
|---|
| [7a1ce5] | 101 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty()); | 
|---|
|  | 102 | } | 
|---|
| [bbab87] | 103 |  | 
|---|
|  | 104 | void AtomDescriptorTest::AtomIdTest() | 
|---|
|  | 105 | { | 
|---|
| [7a1ce5] | 106 | // test Atoms from boundaries and middle of the set | 
|---|
|  | 107 | atom* testAtom; | 
|---|
| [23b547] | 108 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0])); | 
|---|
| [46d958] | 109 | CPPUNIT_ASSERT(testAtom); | 
|---|
|  | 110 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId()); | 
|---|
| [23b547] | 111 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2])); | 
|---|
| [46d958] | 112 | CPPUNIT_ASSERT(testAtom); | 
|---|
|  | 113 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId()); | 
|---|
| [23b547] | 114 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1])); | 
|---|
| [46d958] | 115 | CPPUNIT_ASSERT(testAtom); | 
|---|
|  | 116 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId()); | 
|---|
|  | 117 |  | 
|---|
|  | 118 | // find some ID that has not been created | 
|---|
| [57adc7] | 119 | atomId_t outsideId=0; | 
|---|
| [46d958] | 120 | bool res = false; | 
|---|
| [57adc7] | 121 | for(outsideId=0;!res;++outsideId) { | 
|---|
| [46d958] | 122 | res = true; | 
|---|
|  | 123 | for(int i = 0; i < ATOM_COUNT; ++i){ | 
|---|
|  | 124 | res &= atomIds[i]!=outsideId; | 
|---|
|  | 125 | } | 
|---|
|  | 126 | } | 
|---|
| [7a1ce5] | 127 | // test from outside of set | 
|---|
| [23b547] | 128 | testAtom = World::getInstance().getAtom(AtomById(outsideId)); | 
|---|
| [7a1ce5] | 129 | CPPUNIT_ASSERT(!testAtom); | 
|---|
|  | 130 | } | 
|---|
| [bbab87] | 131 |  | 
|---|
|  | 132 | void AtomDescriptorTest::AtomOfMoleculeTest() | 
|---|
|  | 133 | { | 
|---|
| [b49568] | 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 | } | 
|---|
| [bbab87] | 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 | { | 
|---|
| [7a1ce5] | 218 | // test some elementary set operations | 
|---|
|  | 219 | { | 
|---|
| [23b547] | 220 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms()); | 
|---|
| [57adc7] | 221 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
|  | 222 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 223 | } | 
|---|
|  | 224 |  | 
|---|
|  | 225 | { | 
|---|
| [23b547] | 226 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms()); | 
|---|
| [57adc7] | 227 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
|  | 228 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 229 | } | 
|---|
|  | 230 |  | 
|---|
|  | 231 | { | 
|---|
| [23b547] | 232 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms()); | 
|---|
| [7a1ce5] | 233 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
|  | 234 | } | 
|---|
|  | 235 |  | 
|---|
|  | 236 | { | 
|---|
| [23b547] | 237 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms()); | 
|---|
| [7a1ce5] | 238 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
|  | 241 | { | 
|---|
| [23b547] | 242 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms()); | 
|---|
| [7a1ce5] | 243 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
|  | 244 | } | 
|---|
|  | 245 |  | 
|---|
|  | 246 | { | 
|---|
| [23b547] | 247 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms()); | 
|---|
| [57adc7] | 248 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
|  | 249 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 250 | } | 
|---|
|  | 251 | // exclude and include some atoms | 
|---|
|  | 252 | { | 
|---|
| [23b547] | 253 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2]))); | 
|---|
| [57adc7] | 254 | std::set<atomId_t> excluded; | 
|---|
| [46d958] | 255 | excluded.insert(atomIds[ATOM_COUNT/2]); | 
|---|
| [57adc7] | 256 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded)); | 
|---|
|  | 257 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| [7a1ce5] | 258 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size()); | 
|---|
|  | 259 | } | 
|---|
|  | 260 |  | 
|---|
|  | 261 | { | 
|---|
| [23b547] | 262 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2]))); | 
|---|
| [7a1ce5] | 263 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size()); | 
|---|
| [46d958] | 264 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId()); | 
|---|
| [7a1ce5] | 265 | } | 
|---|
|  | 266 | } | 
|---|