| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 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/AtomOrderDescriptor.hpp> | 
|---|
| 31 | #include <Descriptors/AtomsWithinDistanceOfDescriptor.hpp> | 
|---|
| 32 |  | 
|---|
| 33 | #include "World.hpp" | 
|---|
| 34 | #include "Atom/atom.hpp" | 
|---|
| 35 | #include "molecule.hpp" | 
|---|
| 36 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #ifdef HAVE_TESTRUNNER | 
|---|
| 39 | #include "UnitTestMain.hpp" | 
|---|
| 40 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 41 |  | 
|---|
| 42 | /********************************************** Test classes **************************************/ | 
|---|
| 43 | // Registers the fixture into the 'registry' | 
|---|
| 44 | CPPUNIT_TEST_SUITE_REGISTRATION( AtomDescriptorTest ); | 
|---|
| 45 |  | 
|---|
| 46 | // set up and tear down | 
|---|
| 47 | void AtomDescriptorTest::setUp() | 
|---|
| 48 | { | 
|---|
| 49 | World::getInstance(); | 
|---|
| 50 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| 51 | atoms[i]= World::getInstance().createAtom(); | 
|---|
| 52 | atomIds[i]= atoms[i]->getId(); | 
|---|
| 53 | } | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void AtomDescriptorTest::tearDown() | 
|---|
| 57 | { | 
|---|
| 58 | World::purgeInstance(); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | // some helper functions | 
|---|
| 62 | static bool hasAllAtoms(std::vector<atom*> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()) | 
|---|
| 63 | { | 
|---|
| 64 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| 65 | atomId_t id = ids[i]; | 
|---|
| 66 | if(!excluded.count(id)){ | 
|---|
| 67 | std::vector<atom*>::iterator iter; | 
|---|
| 68 | bool res=false; | 
|---|
| 69 | for(iter=atoms.begin();iter!=atoms.end();++iter){ | 
|---|
| 70 | res |= (*iter)->getId() == id; | 
|---|
| 71 | } | 
|---|
| 72 | if(!res) { | 
|---|
| 73 | cout << "Atom " << id << " missing in returned list" << endl; | 
|---|
| 74 | return false; | 
|---|
| 75 | } | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 | return true; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | static bool hasNoDuplicateAtoms(std::vector<atom*> atoms) | 
|---|
| 82 | { | 
|---|
| 83 | std::set<atomId_t> found; | 
|---|
| 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 |  | 
|---|
| 95 | void AtomDescriptorTest::AtomBaseSetsTest() | 
|---|
| 96 | { | 
|---|
| 97 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms()); | 
|---|
| 98 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(allAtoms,atomIds)); | 
|---|
| 99 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(allAtoms)); | 
|---|
| 100 |  | 
|---|
| 101 | std::vector<atom*> noAtoms = World::getInstance().getAllAtoms(NoAtoms()); | 
|---|
| 102 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty()); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void AtomDescriptorTest::AtomIdTest() | 
|---|
| 106 | { | 
|---|
| 107 | // test Atoms from boundaries and middle of the set | 
|---|
| 108 | atom* testAtom; | 
|---|
| 109 | testAtom = World::getInstance().getAtom(AtomById(atomIds[0])); | 
|---|
| 110 | CPPUNIT_ASSERT(testAtom); | 
|---|
| 111 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId()); | 
|---|
| 112 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT/2])); | 
|---|
| 113 | CPPUNIT_ASSERT(testAtom); | 
|---|
| 114 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId()); | 
|---|
| 115 | testAtom = World::getInstance().getAtom(AtomById(atomIds[ATOM_COUNT-1])); | 
|---|
| 116 | CPPUNIT_ASSERT(testAtom); | 
|---|
| 117 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId()); | 
|---|
| 118 |  | 
|---|
| 119 | // find some ID that has not been created | 
|---|
| 120 | atomId_t outsideId=0; | 
|---|
| 121 | bool res = false; | 
|---|
| 122 | for(outsideId=0;!res;++outsideId) { | 
|---|
| 123 | res = true; | 
|---|
| 124 | for(int i = 0; i < ATOM_COUNT; ++i){ | 
|---|
| 125 | res &= atomIds[i]!=outsideId; | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|
| 128 | // test from outside of set | 
|---|
| 129 | testAtom = World::getInstance().getAtom(AtomById(outsideId)); | 
|---|
| 130 | CPPUNIT_ASSERT(!testAtom); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void AtomDescriptorTest::AtomOfMoleculeTest() | 
|---|
| 134 | { | 
|---|
| 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 | } | 
|---|
| 155 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 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 | { | 
|---|
| 288 | // test some elementary set operations | 
|---|
| 289 | { | 
|---|
| 290 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()||NoAtoms()); | 
|---|
| 291 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
| 292 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | { | 
|---|
| 296 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||AllAtoms()); | 
|---|
| 297 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
| 298 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | { | 
|---|
| 302 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()&&AllAtoms()); | 
|---|
| 303 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | { | 
|---|
| 307 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&NoAtoms()); | 
|---|
| 308 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | { | 
|---|
| 312 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!AllAtoms()); | 
|---|
| 313 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty()); | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | { | 
|---|
| 317 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(!NoAtoms()); | 
|---|
| 318 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds)); | 
|---|
| 319 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| 320 | } | 
|---|
| 321 | // exclude and include some atoms | 
|---|
| 322 | { | 
|---|
| 323 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(AllAtoms()&&(!AtomById(atomIds[ATOM_COUNT/2]))); | 
|---|
| 324 | std::set<atomId_t> excluded; | 
|---|
| 325 | excluded.insert(atomIds[ATOM_COUNT/2]); | 
|---|
| 326 | CPPUNIT_ASSERT_EQUAL( true , hasAllAtoms(testAtoms,atomIds,excluded)); | 
|---|
| 327 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateAtoms(testAtoms)); | 
|---|
| 328 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size()); | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | { | 
|---|
| 332 | std::vector<atom*> testAtoms = World::getInstance().getAllAtoms(NoAtoms()||(AtomById(atomIds[ATOM_COUNT/2]))); | 
|---|
| 333 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size()); | 
|---|
| 334 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId()); | 
|---|
| 335 | } | 
|---|
| 336 | } | 
|---|