| 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 |  * ClusterUnitTest.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Jan 17, 2012
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 21 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 22 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Atom/CopyAtoms/CopyAtoms_Simple.hpp"
 | 
|---|
| 27 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 28 | #include "Element/periodentafel.hpp"
 | 
|---|
| 29 | #include "Filling/Cluster.hpp"
 | 
|---|
| 30 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 31 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 32 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 33 | #include "Shapes/Shape.hpp"
 | 
|---|
| 34 | #include "World.hpp"
 | 
|---|
| 35 | #include "WorldTime.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "ClusterUnitTest.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 40 | #include "UnitTestMain.hpp"
 | 
|---|
| 41 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /********************************************** Test classes **************************************/
 | 
|---|
| 44 | 
 | 
|---|
| 45 | // Registers the fixture into the 'registry'
 | 
|---|
| 46 | CPPUNIT_TEST_SUITE_REGISTRATION( ClusterTest );
 | 
|---|
| 47 | 
 | 
|---|
| 48 | 
 | 
|---|
| 49 | void ClusterTest::setUp()
 | 
|---|
| 50 | {
 | 
|---|
| 51 |   // failing asserts should be thrown
 | 
|---|
| 52 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   // this must not compile: private default Cstor
 | 
|---|
| 55 |   // cluster = new Cluster();
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   shape  = new Shape(Sphere());
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   // create an atom
 | 
|---|
| 60 |   _atom = World::getInstance().createAtom();
 | 
|---|
| 61 |   const element * hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
| 62 |   CPPUNIT_ASSERT(hydrogen != NULL);
 | 
|---|
| 63 |   _atom->setType(hydrogen);
 | 
|---|
| 64 |   _atom->setPosition(Vector(0.,0.,0.));
 | 
|---|
| 65 |   _atomId = _atom->getId();
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   cluster = new Cluster(*shape);
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | 
 | 
|---|
| 71 | void ClusterTest::tearDown()
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   delete cluster;
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   World::purgeInstance();
 | 
|---|
| 76 |   WorldTime::purgeInstance();
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /** Test whether setting and getting works
 | 
|---|
| 80 |  *
 | 
|---|
| 81 |  */
 | 
|---|
| 82 | void ClusterTest::insert_eraseTest()
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   // check for empty cluster
 | 
|---|
| 85 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, cluster->getAtomIds().size() );
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   // Shape is sphere at (0,0,0) with radius 1
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   {
 | 
|---|
| 90 |     // insert present atom at center of shape
 | 
|---|
| 91 | #ifndef NDEBUG
 | 
|---|
| 92 |     CPPUNIT_ASSERT_NO_THROW( cluster->insert(_atomId) );
 | 
|---|
| 93 | #else
 | 
|---|
| 94 |     cluster->insert(_atomId);
 | 
|---|
| 95 | #endif
 | 
|---|
| 96 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
 | 
|---|
| 97 | #ifndef NDEBUG
 | 
|---|
| 98 |     CPPUNIT_ASSERT_NO_THROW( cluster->erase(_atomId) );
 | 
|---|
| 99 | #else
 | 
|---|
| 100 |     cluster->erase(_atomId);
 | 
|---|
| 101 | #endif
 | 
|---|
| 102 |   }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   {
 | 
|---|
| 105 |     // erase non-existing atom
 | 
|---|
| 106 |     const atomId_t falseId = _atomId+1;
 | 
|---|
| 107 |     CPPUNIT_ASSERT_EQUAL( (atom *)NULL, World::getInstance().getAtom(AtomById(falseId)) );
 | 
|---|
| 108 | #ifndef NDEBUG
 | 
|---|
| 109 |     std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
 | 
|---|
| 110 |     CPPUNIT_ASSERT_THROW( cluster->erase(falseId), Assert::AssertionFailure );
 | 
|---|
| 111 | #else
 | 
|---|
| 112 |     cluster->erase(falseId);
 | 
|---|
| 113 | #endif
 | 
|---|
| 114 |   }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   {
 | 
|---|
| 117 |     // erase non-present atom
 | 
|---|
| 118 | #ifndef NDEBUG
 | 
|---|
| 119 |     std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
 | 
|---|
| 120 |     CPPUNIT_ASSERT_THROW( cluster->erase(_atomId), Assert::AssertionFailure );
 | 
|---|
| 121 | #else
 | 
|---|
| 122 |     cluster->erase(_atomId);
 | 
|---|
| 123 | #endif
 | 
|---|
| 124 |   }
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   {
 | 
|---|
| 127 |     // insert present atom within shape
 | 
|---|
| 128 |     _atom->setPosition(Vector(.5,.5,.5));
 | 
|---|
| 129 | #ifndef NDEBUG
 | 
|---|
| 130 |     CPPUNIT_ASSERT_NO_THROW( cluster->insert(_atomId) );
 | 
|---|
| 131 | #else
 | 
|---|
| 132 |     cluster->insert(_atomId);
 | 
|---|
| 133 | #endif
 | 
|---|
| 134 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
 | 
|---|
| 135 | #ifndef NDEBUG
 | 
|---|
| 136 |     CPPUNIT_ASSERT_NO_THROW( cluster->erase(_atomId) );
 | 
|---|
| 137 | #else
 | 
|---|
| 138 |     cluster->erase(_atomId);
 | 
|---|
| 139 | #endif
 | 
|---|
| 140 |     _atom->setPosition(Vector(0.,0.,0.));
 | 
|---|
| 141 |   }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   {
 | 
|---|
| 144 |     // insert present atom outside shape
 | 
|---|
| 145 |     _atom->setPosition(Vector(2.,0.,0.));
 | 
|---|
| 146 | #ifndef NDEBUG
 | 
|---|
| 147 |     std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
 | 
|---|
| 148 |     CPPUNIT_ASSERT_THROW( cluster->insert(_atomId), Assert::AssertionFailure );
 | 
|---|
| 149 | #else
 | 
|---|
| 150 |     cluster->insert(_atomId);
 | 
|---|
| 151 | #endif
 | 
|---|
| 152 |     CPPUNIT_ASSERT_EQUAL( (size_t)0, cluster->getAtomIds().size() );
 | 
|---|
| 153 |     _atom->setPosition(Vector(0.,0.,0.));
 | 
|---|
| 154 |   }
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   {
 | 
|---|
| 157 |     // insert present atom on boundary shape
 | 
|---|
| 158 |     _atom->setPosition(Vector(1.,0.,0.));
 | 
|---|
| 159 | #ifndef NDEBUG
 | 
|---|
| 160 |     CPPUNIT_ASSERT_NO_THROW( cluster->insert(_atomId) );
 | 
|---|
| 161 | #else
 | 
|---|
| 162 |     cluster->insert(_atomId);
 | 
|---|
| 163 | #endif
 | 
|---|
| 164 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
 | 
|---|
| 165 | #ifndef NDEBUG
 | 
|---|
| 166 |     CPPUNIT_ASSERT_NO_THROW( cluster->erase(_atomId) );
 | 
|---|
| 167 | #else
 | 
|---|
| 168 |     cluster->erase(_atomId);
 | 
|---|
| 169 | #endif
 | 
|---|
| 170 |     _atom->setPosition(Vector(0.,0.,0.));
 | 
|---|
| 171 |   }
 | 
|---|
| 172 | 
 | 
|---|
| 173 |   {
 | 
|---|
| 174 |     // insert non-existing atom
 | 
|---|
| 175 |     const atomId_t falseId = _atomId+1;
 | 
|---|
| 176 |     CPPUNIT_ASSERT_EQUAL( (atom *)NULL, World::getInstance().getAtom(AtomById(falseId)) );
 | 
|---|
| 177 | #ifndef NDEBUG
 | 
|---|
| 178 |     std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
 | 
|---|
| 179 |     CPPUNIT_ASSERT_THROW( cluster->insert(falseId), Assert::AssertionFailure );
 | 
|---|
| 180 | #else
 | 
|---|
| 181 |     cluster->insert(falseId);
 | 
|---|
| 182 | #endif
 | 
|---|
| 183 |     CPPUNIT_ASSERT_EQUAL( (size_t)0, cluster->getAtomIds().size() );
 | 
|---|
| 184 |   }
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|
| 187 | /** Test whether setting and getting works
 | 
|---|
| 188 |  *
 | 
|---|
| 189 |  */
 | 
|---|
| 190 | void ClusterTest::setter_getterTest()
 | 
|---|
| 191 | {
 | 
|---|
| 192 |   CPPUNIT_ASSERT( *shape == cluster->getShape() );
 | 
|---|
| 193 | 
 | 
|---|
| 194 |   CPPUNIT_ASSERT( cluster->atoms == cluster->getAtomIds() );
 | 
|---|
| 195 | }
 | 
|---|
| 196 | 
 | 
|---|
| 197 | /** Test whether translate() works
 | 
|---|
| 198 |  *
 | 
|---|
| 199 |  */
 | 
|---|
| 200 | void ClusterTest::translateTest()
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   // insert
 | 
|---|
| 203 |   cluster->insert(_atomId);
 | 
|---|
| 204 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   // check we are at origin
 | 
|---|
| 207 |   CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getShape().getCenter() );
 | 
|---|
| 208 |   const atom * _atom = NULL;
 | 
|---|
| 209 |   CPPUNIT_ASSERT_NO_THROW( _atom = cluster->getAtomById(_atomId) );
 | 
|---|
| 210 |   CPPUNIT_ASSERT( _atom != NULL );
 | 
|---|
| 211 |   CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), _atom->getPosition() );
 | 
|---|
| 212 | 
 | 
|---|
| 213 |   // move
 | 
|---|
| 214 |   cluster->translate( Vector(1.,0.,0.) );
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0.), cluster->getShape().getCenter() );
 | 
|---|
| 217 |   CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0.), cluster->getAtomById(_atomId)->getPosition() );
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | 
 | 
|---|
| 221 | /** Test whether transform() works
 | 
|---|
| 222 |  *
 | 
|---|
| 223 |  */
 | 
|---|
| 224 | void ClusterTest::transformTest()
 | 
|---|
| 225 | {
 | 
|---|
| 226 |   // insert
 | 
|---|
| 227 |   cluster->insert(_atomId);
 | 
|---|
| 228 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
 | 
|---|
| 229 | 
 | 
|---|
| 230 |   // check we are at origin
 | 
|---|
| 231 |   CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getShape().getCenter() );
 | 
|---|
| 232 |   const atom * _atom = NULL;
 | 
|---|
| 233 |   CPPUNIT_ASSERT_NO_THROW( _atom = cluster->getAtomById(_atomId) );
 | 
|---|
| 234 |   CPPUNIT_ASSERT( _atom != NULL );
 | 
|---|
| 235 |   CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), _atom->getPosition() );
 | 
|---|
| 236 | 
 | 
|---|
| 237 |   // transform
 | 
|---|
| 238 |   RealSpaceMatrix M;
 | 
|---|
| 239 |   M.setIdentity();
 | 
|---|
| 240 |   cluster->transform( M );
 | 
|---|
| 241 | 
 | 
|---|
| 242 |   CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getShape().getCenter() );
 | 
|---|
| 243 |   CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getAtomById(_atomId)->getPosition() );
 | 
|---|
| 244 | }
 | 
|---|
| 245 | 
 | 
|---|
| 246 | /** Test whether IsInShape() works
 | 
|---|
| 247 |  *
 | 
|---|
| 248 |  */
 | 
|---|
| 249 | void ClusterTest::IsInShapeTest()
 | 
|---|
| 250 | {
 | 
|---|
| 251 |   // at origin we are inside
 | 
|---|
| 252 |   CPPUNIT_ASSERT( shape->isInside(_atom->getPosition() ) );
 | 
|---|
| 253 |   CPPUNIT_ASSERT( cluster->IsInShape(_atomId) );
 | 
|---|
| 254 | 
 | 
|---|
| 255 |   // at boundary we are inside
 | 
|---|
| 256 |   _atom->setPosition( Vector(1.,0.,0.) );
 | 
|---|
| 257 |   CPPUNIT_ASSERT( shape->isInside(_atom->getPosition() ) );
 | 
|---|
| 258 |   CPPUNIT_ASSERT( cluster->IsInShape(_atomId) );
 | 
|---|
| 259 | 
 | 
|---|
| 260 |   // now we are outside
 | 
|---|
| 261 |   _atom->setPosition( Vector(2.,0.,0.) );
 | 
|---|
| 262 |   CPPUNIT_ASSERT( !shape->isInside(_atom->getPosition() ) );
 | 
|---|
| 263 |   CPPUNIT_ASSERT( !cluster->IsInShape(_atomId) );
 | 
|---|
| 264 | }
 | 
|---|
| 265 | 
 | 
|---|
| 266 | /** Test whether clone() works
 | 
|---|
| 267 |  *
 | 
|---|
| 268 |  */
 | 
|---|
| 269 | void ClusterTest::cloneTest()
 | 
|---|
| 270 | {
 | 
|---|
| 271 |   // insert atom ...
 | 
|---|
| 272 |   cluster->insert(_atomId);
 | 
|---|
| 273 | 
 | 
|---|
| 274 |   // ... and clone
 | 
|---|
| 275 |   CopyAtoms_Simple copyMethod;
 | 
|---|
| 276 |   ClusterInterface::Cluster_impl clonedCluster = cluster->clone(copyMethod);
 | 
|---|
| 277 | 
 | 
|---|
| 278 |   // check for present atom
 | 
|---|
| 279 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, clonedCluster->getAtomIds().size() );
 | 
|---|
| 280 | 
 | 
|---|
| 281 |   // check for different ids
 | 
|---|
| 282 |   CPPUNIT_ASSERT_EQUAL( (size_t)2, World::getInstance().getAllAtoms().size() );
 | 
|---|
| 283 |   CPPUNIT_ASSERT( *(cluster->getAtomIds().begin()) != *(clonedCluster->getAtomIds().begin()) );
 | 
|---|
| 284 |   // check for same position
 | 
|---|
| 285 |   atomId_t id = *(clonedCluster->getAtomIds().begin());
 | 
|---|
| 286 |   const atom * const _atom = World::getInstance().getAtom(AtomById(id));
 | 
|---|
| 287 |   CPPUNIT_ASSERT( _atom != NULL );
 | 
|---|
| 288 |   CPPUNIT_ASSERT( (*cluster->getAtomRefs().begin())->getPosition() ==_atom->getPosition() );
 | 
|---|
| 289 | 
 | 
|---|
| 290 |   // check that shape is the same
 | 
|---|
| 291 |   CPPUNIT_ASSERT( cluster->getShape() == clonedCluster->getShape() );
 | 
|---|
| 292 |   CPPUNIT_ASSERT( cluster->getShape().getCenter() == clonedCluster->getShape().getCenter() );
 | 
|---|
| 293 |   CPPUNIT_ASSERT( cluster->getShape().getRadius() == clonedCluster->getShape().getRadius() );
 | 
|---|
| 294 | }
 | 
|---|
| 295 | 
 | 
|---|
| 296 | /** Test whether getAtomRefs() works
 | 
|---|
| 297 |  *
 | 
|---|
| 298 |  */
 | 
|---|
| 299 | void ClusterTest::getAtomRefsTest()
 | 
|---|
| 300 | {
 | 
|---|
| 301 |   Cluster::AtomVector Atomvec;
 | 
|---|
| 302 | 
 | 
|---|
| 303 |   // check with empty cluster
 | 
|---|
| 304 |   CPPUNIT_ASSERT_EQUAL( Atomvec, cluster->getAtomRefs() );
 | 
|---|
| 305 | 
 | 
|---|
| 306 |   // insert into both ...
 | 
|---|
| 307 |   Atomvec.push_back(_atom);
 | 
|---|
| 308 |   cluster->insert(_atomId);
 | 
|---|
| 309 | 
 | 
|---|
| 310 |   // ...and check again
 | 
|---|
| 311 |   CPPUNIT_ASSERT_EQUAL( Atomvec, cluster->getAtomRefs() );
 | 
|---|
| 312 | }
 | 
|---|