source: src/Filling/unittests/ClusterUnitTest.cpp@ 019b96

Last change on this file since 019b96 was 94d5ac6, checked in by Frederik Heber <heber@…>, 13 years ago

FIX: As we use GSL internally, we are as of now required to use GPL v2 license.

  • GNU Scientific Library is used at every place in the code, especially the sub-package LinearAlgebra is based on it which in turn is used really everywhere in the remainder of MoleCuilder. Hence, we have to use the GPL license for the whole of MoleCuilder. In effect, GPL's COPYING was present all along and stated the terms of the GPL v2 license.
  • Hence, I added the default GPL v2 disclaimer to every source file and removed the note about a (actually missing) LICENSE file.
  • also, I added a help-redistribute action which again gives the disclaimer of the GPL v2.
  • also, I changed in the disclaimer that is printed at every program start in builder_init.cpp.
  • TEST: Added check on GPL statement present in every module to test CodeChecks project-disclaimer.
  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[8f6e2a]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]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/>.
[8f6e2a]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( (atom *)NULL, World::getInstance().getAtom(AtomById(falseId)) );
123#ifndef NDEBUG
124 std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
125 CPPUNIT_ASSERT_THROW( cluster->erase(falseId), Assert::AssertionFailure );
126#else
127 cluster->erase(falseId);
128#endif
129 }
130
131 {
132 // erase non-present atom
133#ifndef NDEBUG
134 std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
135 CPPUNIT_ASSERT_THROW( cluster->erase(_atomId), Assert::AssertionFailure );
136#else
137 cluster->erase(_atomId);
138#endif
139 }
140
141 {
142 // insert present atom within shape
143 _atom->setPosition(Vector(.5,.5,.5));
144#ifndef NDEBUG
145 CPPUNIT_ASSERT_NO_THROW( cluster->insert(_atomId) );
146#else
147 cluster->insert(_atomId);
148#endif
149 CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
150#ifndef NDEBUG
151 CPPUNIT_ASSERT_NO_THROW( cluster->erase(_atomId) );
152#else
153 cluster->erase(_atomId);
154#endif
155 _atom->setPosition(Vector(0.,0.,0.));
156 }
157
158 {
159 // insert present atom outside shape
160 _atom->setPosition(Vector(2.,0.,0.));
161#ifndef NDEBUG
162 std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
163 CPPUNIT_ASSERT_THROW( cluster->insert(_atomId), Assert::AssertionFailure );
164#else
165 cluster->insert(_atomId);
166#endif
167 CPPUNIT_ASSERT_EQUAL( (size_t)0, cluster->getAtomIds().size() );
168 _atom->setPosition(Vector(0.,0.,0.));
169 }
170
171 {
172 // insert present atom on boundary shape
173 _atom->setPosition(Vector(1.,0.,0.));
174#ifndef NDEBUG
175 CPPUNIT_ASSERT_NO_THROW( cluster->insert(_atomId) );
176#else
177 cluster->insert(_atomId);
178#endif
179 CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
180#ifndef NDEBUG
181 CPPUNIT_ASSERT_NO_THROW( cluster->erase(_atomId) );
182#else
183 cluster->erase(_atomId);
184#endif
185 _atom->setPosition(Vector(0.,0.,0.));
186 }
187
188 {
189 // insert non-existing atom
190 const atomId_t falseId = _atomId+1;
191 CPPUNIT_ASSERT_EQUAL( (atom *)NULL, World::getInstance().getAtom(AtomById(falseId)) );
192#ifndef NDEBUG
193 std::cout << "The following Assertion is intended and does not present a failure of the test." << std::endl;
194 CPPUNIT_ASSERT_THROW( cluster->insert(falseId), Assert::AssertionFailure );
195#else
196 cluster->insert(falseId);
197#endif
198 CPPUNIT_ASSERT_EQUAL( (size_t)0, cluster->getAtomIds().size() );
199 }
200}
201
202/** Test whether setting and getting works
203 *
204 */
205void ClusterTest::setter_getterTest()
206{
207 CPPUNIT_ASSERT( *shape == cluster->getShape() );
208
209 CPPUNIT_ASSERT( cluster->atoms == cluster->getAtomIds() );
210}
211
212/** Test whether translate() works
213 *
214 */
215void ClusterTest::translateTest()
216{
217 // insert
218 cluster->insert(_atomId);
219 CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
220
221 // check we are at origin
222 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getShape().getCenter() );
223 const atom * _atom = NULL;
224 CPPUNIT_ASSERT_NO_THROW( _atom = cluster->getAtomById(_atomId) );
225 CPPUNIT_ASSERT( _atom != NULL );
226 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), _atom->getPosition() );
227
228 // move
229 cluster->translate( Vector(1.,0.,0.) );
230
231 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0.), cluster->getShape().getCenter() );
232 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0.), cluster->getAtomById(_atomId)->getPosition() );
233}
234
235
236/** Test whether transform() works
237 *
238 */
239void ClusterTest::transformTest()
240{
241 // insert
242 cluster->insert(_atomId);
243 CPPUNIT_ASSERT_EQUAL( (size_t)1, cluster->getAtomIds().size() );
244
245 // check we are at origin
246 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getShape().getCenter() );
247 const atom * _atom = NULL;
248 CPPUNIT_ASSERT_NO_THROW( _atom = cluster->getAtomById(_atomId) );
249 CPPUNIT_ASSERT( _atom != NULL );
250 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), _atom->getPosition() );
251
252 // transform
253 RealSpaceMatrix M;
254 M.setIdentity();
255 cluster->transform( M );
256
257 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getShape().getCenter() );
258 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0.), cluster->getAtomById(_atomId)->getPosition() );
259}
260
261/** Test whether IsInShape() works
262 *
263 */
264void ClusterTest::IsInShapeTest()
265{
266 // at origin we are inside
267 CPPUNIT_ASSERT( shape->isInside(_atom->getPosition() ) );
268 CPPUNIT_ASSERT( cluster->IsInShape(_atomId) );
269
270 // at boundary we are inside
271 _atom->setPosition( Vector(1.,0.,0.) );
272 CPPUNIT_ASSERT( shape->isInside(_atom->getPosition() ) );
273 CPPUNIT_ASSERT( cluster->IsInShape(_atomId) );
274
275 // now we are outside
276 _atom->setPosition( Vector(2.,0.,0.) );
277 CPPUNIT_ASSERT( !shape->isInside(_atom->getPosition() ) );
278 CPPUNIT_ASSERT( !cluster->IsInShape(_atomId) );
279}
280
281/** Test whether clone() works
282 *
283 */
284void ClusterTest::cloneTest()
285{
286 // insert atom ...
287 cluster->insert(_atomId);
288
289 // ... and clone
290 CopyAtoms_Simple copyMethod;
291 ClusterInterface::Cluster_impl clonedCluster = cluster->clone(copyMethod);
292
293 // check for present atom
294 CPPUNIT_ASSERT_EQUAL( (size_t)1, clonedCluster->getAtomIds().size() );
295
296 // check for different ids
297 CPPUNIT_ASSERT_EQUAL( (size_t)2, World::getInstance().getAllAtoms().size() );
298 CPPUNIT_ASSERT( *(cluster->getAtomIds().begin()) != *(clonedCluster->getAtomIds().begin()) );
299 // check for same position
300 atomId_t id = *(clonedCluster->getAtomIds().begin());
301 const atom * const _atom = World::getInstance().getAtom(AtomById(id));
302 CPPUNIT_ASSERT( _atom != NULL );
303 CPPUNIT_ASSERT( (*cluster->getAtomRefs().begin())->getPosition() ==_atom->getPosition() );
304
305 // check that shape is the same
306 CPPUNIT_ASSERT( cluster->getShape() == clonedCluster->getShape() );
307 CPPUNIT_ASSERT( cluster->getShape().getCenter() == clonedCluster->getShape().getCenter() );
308 CPPUNIT_ASSERT( cluster->getShape().getRadius() == clonedCluster->getShape().getRadius() );
309}
310
311/** Test whether getAtomRefs() works
312 *
313 */
314void ClusterTest::getAtomRefsTest()
315{
316 Cluster::AtomVector Atomvec;
317
318 // check with empty cluster
319 CPPUNIT_ASSERT_EQUAL( Atomvec, cluster->getAtomRefs() );
320
321 // insert into both ...
322 Atomvec.push_back(_atom);
323 cluster->insert(_atomId);
324
325 // ...and check again
326 CPPUNIT_ASSERT_EQUAL( Atomvec, cluster->getAtomRefs() );
327}
Note: See TracBrowser for help on using the repository browser.