source: src/Filling/unittests/ClusterUnitTest.cpp@ a844d8

Candidate_v1.6.1
Last change on this file since a844d8 was a58c16, checked in by Frederik Heber <heber@…>, 9 years ago

Replaced World::getAllAtoms() by const version where possible.

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