source: src/Fragmentation/SetValues/unittests/FragmentUnitTest.cpp@ 302345

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 302345 was f3bc5f, checked in by Frederik Heber <heber@…>, 12 years ago

Added extensive unit tests and some more functions to Fragment.

  • Property mode set to 100644
File size: 16.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 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 * FragmentUnitTest.cpp
25 *
26 * Created on: Aug 09, 2012
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35using namespace std;
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
41#include "FragmentUnitTest.hpp"
42
43#include <algorithm>
44#include <limits>
45
46#include <boost/assign.hpp>
47#include <boost/foreach.hpp>
48
49#include "CodePatterns/Assert.hpp"
50
51#ifdef HAVE_TESTRUNNER
52#include "UnitTestMain.hpp"
53#endif /*HAVE_TESTRUNNER*/
54
55using namespace boost::assign;
56
57/********************************************** Test classes **************************************/
58
59// Registers the fixture into the 'registry'
60CPPUNIT_TEST_SUITE_REGISTRATION( FragmentTest );
61
62
63void FragmentTest::setUp()
64{
65 // failing asserts should be thrown
66 ASSERT_DO(Assert::Throw);
67
68 Fragment::position_t pos(3,0.);
69 positions += pos;
70 pos[0] = 1.;
71 positions += pos;
72 pos[1] = 1.;
73 positions += pos;
74 pos[2] = 1.;
75 positions += pos;
76 charges += 1., 2., 3., 4.;
77 CPPUNIT_ASSERT_EQUAL( (size_t)3, pos.size() );
78 CPPUNIT_ASSERT( positions.size() == charges.size() );
79
80 fragment = new Fragment(positions, charges);
81}
82
83
84void FragmentTest::tearDown()
85{
86 delete fragment;
87}
88
89/** UnitTest for isPositionEqual()
90 */
91void FragmentTest::isPositionEqual_Test()
92{
93 // same position
94 for (Fragment::positions_t::const_iterator iter = positions.begin();
95 iter != positions.end(); ++iter)
96 CPPUNIT_ASSERT( Fragment::isPositionEqual(*iter, *iter) );
97
98 // other position
99 Fragment::position_t unequalpos(3,2.);
100 for (Fragment::positions_t::const_iterator iter = positions.begin();
101 iter != positions.end(); ++iter)
102 CPPUNIT_ASSERT( !Fragment::isPositionEqual(*iter, unequalpos) );
103}
104
105
106/** UnitTest for containsNuclei()
107 */
108void FragmentTest::containsNuclei_Test()
109{
110 {
111 // tests present ones for containment
112 Fragment::nuclei_t validnuclei(positions.size());
113 std::transform(positions.begin(), positions.end(),
114 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
115 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
116 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
117 }
118 }
119
120 {
121 // test some others
122 Fragment::nuclei_t invalidnuclei;
123 Fragment::position_t pos(3, -1.);
124 invalidnuclei += Fragment::createNucleus(pos, 1.);
125 pos[0] = 0.;
126 invalidnuclei += Fragment::createNucleus(pos, 1.);
127 pos[1] = 0.;
128 invalidnuclei += Fragment::createNucleus(pos, 1.);
129 pos[2] = -std::numeric_limits<double>::epsilon()*1e+4;
130 invalidnuclei += Fragment::createNucleus(pos, 1.);
131 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
132 CPPUNIT_ASSERT( !fragment->containsNuclei(nucleus) );
133 }
134 }
135}
136
137/** UnitTest for removeNuclei()
138 */
139void FragmentTest::removeNuclei_Test()
140{
141 {
142 // tests present ones for removal
143 size_t size = fragment->nuclei.size();
144 Fragment::nuclei_t validnuclei(positions.size());
145 std::transform(positions.begin(), positions.end(),
146 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
147 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
148 CPPUNIT_ASSERT_NO_THROW( fragment->removeNuclei(nucleus) );
149 CPPUNIT_ASSERT_EQUAL( --size, fragment->nuclei.size() );
150 }
151 }
152 {
153 // test some others
154 Fragment::nuclei_t invalidnuclei;
155 Fragment::position_t pos(3, -1.);
156 invalidnuclei += Fragment::createNucleus(pos, 1.);
157 pos[0] = 0;
158 invalidnuclei += Fragment::createNucleus(pos, 1.);
159 pos[1] = 0;
160 invalidnuclei += Fragment::createNucleus(pos, 1.);
161 pos[2] = -std::numeric_limits<double>::epsilon()*1e+4;
162 invalidnuclei += Fragment::createNucleus(pos, 1.);
163 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
164 CPPUNIT_ASSERT_NO_THROW( fragment->removeNuclei(nucleus) );
165 }
166 }
167}
168
169/** UnitTest for operator==() for Fragment::nucleus_t
170 */
171void FragmentTest::equalityNucleus_Test()
172{
173 Fragment::nuclei_t validnuclei(positions.size());
174 std::transform(positions.begin(), positions.end(),
175 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
176 {
177 // create some nuclei
178 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
179 CPPUNIT_ASSERT( nucleus == nucleus );
180 }
181 }
182
183 {
184 // create nucleus at other position
185 Fragment::position_t pos(3, 2.);
186 Fragment::nucleus_t unequalposnucleus(pos, 1.);
187 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
188 CPPUNIT_ASSERT( nucleus != unequalposnucleus );
189 }
190 }
191
192 {
193 // create nucleus with different charge
194 Fragment::position_t pos(3, 1.);
195 Fragment::nucleus_t unequalchargenucleus(pos, 5.);
196 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
197 CPPUNIT_ASSERT( nucleus != unequalchargenucleus );
198 }
199 }
200}
201
202/** UnitTest for operator==()
203 */
204void FragmentTest::equality_Test()
205{
206 // create different fragment
207 Fragment::positions_t otherpositions;
208 Fragment::position_t otherpos(3, 2.);
209 otherpositions += otherpos;
210 otherpos[0] = 0.;
211 otherpositions += otherpos;
212 otherpos[1] = 0.;
213 otherpositions += otherpos;
214 Fragment::charges_t othercharges;
215 othercharges += 1., 2., 3.;
216 Fragment otherfragment(otherpositions, othercharges);
217
218 CPPUNIT_ASSERT( !(*fragment == otherfragment) );
219 CPPUNIT_ASSERT( *fragment != otherfragment );
220
221 // test against empty fragment
222 Fragment emptyfragment;
223 CPPUNIT_ASSERT( !(*fragment == emptyfragment) );
224 CPPUNIT_ASSERT( *fragment != emptyfragment );
225
226 // tests against themselves
227 CPPUNIT_ASSERT( *fragment == *fragment );
228 CPPUNIT_ASSERT( otherfragment == otherfragment );
229 CPPUNIT_ASSERT( emptyfragment == emptyfragment );
230}
231
232/** UnitTest for operator+=()
233 */
234void FragmentTest::assignment_Test()
235{
236 // create different fragment
237 Fragment::positions_t otherpositions;
238 Fragment::position_t otherpos(3, 2.);
239 otherpositions += otherpos;
240 otherpos[0] = 0.;
241 otherpositions += otherpos;
242 otherpos[1] = 0.;
243 otherpositions += otherpos;
244 Fragment::charges_t othercharges;
245 othercharges += 1., 2., 3.;
246 Fragment otherfragment(otherpositions, othercharges);
247
248 // check for inequality
249 CPPUNIT_ASSERT( otherfragment.nuclei.size() != fragment->nuclei.size() );
250 CPPUNIT_ASSERT( otherfragment != *fragment );
251
252 //assign
253 otherfragment = *fragment;
254
255 // check for equality
256 CPPUNIT_ASSERT( otherfragment.nuclei.size() == fragment->nuclei.size() );
257 CPPUNIT_ASSERT( otherfragment == *fragment );
258}
259
260/** UnitTest for operator+=()
261 */
262void FragmentTest::operatorPlusEqual_NonOverlapping_Test()
263{
264 {
265 // create non-overlapping set
266 Fragment::positions_t otherpositions;
267 Fragment::position_t otherpos(3, 2.);
268 otherpositions += otherpos;
269 otherpos[0] = 0.;
270 otherpositions += otherpos;
271 otherpos[1] = 0.;
272 otherpositions += otherpos;
273 Fragment::charges_t othercharges;
274 othercharges += 1., 2., 3.;
275 Fragment otherfragment(otherpositions, othercharges);
276 const size_t othersize = otherfragment.nuclei.size();
277 const size_t size = fragment->nuclei.size();
278 *fragment += otherfragment;
279 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
280 CPPUNIT_ASSERT_EQUAL( size+othersize, fragment->nuclei.size() );
281 {
282 // tests all for containment
283 Fragment::nuclei_t validnuclei(positions.size()+otherpositions.size());
284 Fragment::nuclei_t::iterator iter =
285 std::transform(positions.begin(), positions.end(),
286 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
287 std::transform(otherpositions.begin(), otherpositions.end(),
288 othercharges.begin(), iter, Fragment::createNucleus);
289 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
290 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
291 }
292 }
293 {
294 // tests positions for no containment in otherfragment
295 Fragment::nuclei_t invalidnuclei(positions.size());
296 std::transform(positions.begin(), positions.end(),
297 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
298 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
299 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
300 }
301 }
302 {
303 // tests otherpositions for containment in otherfragment
304 Fragment::nuclei_t validnuclei(otherpositions.size());
305 std::transform(otherpositions.begin(), otherpositions.end(),
306 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
307 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
308 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
309 }
310 }
311 }
312}
313
314/** UnitTest for operator+=()
315 */
316void FragmentTest::operatorPlusEqual_Test()
317{
318 {
319 // create overlapping set (first overlaps)
320 Fragment::positions_t otherpositions;
321 Fragment::position_t otherpos(3, 1.);
322 otherpositions += otherpos;
323 otherpos[0] = 2.;
324 otherpositions += otherpos;
325 otherpos[1] = 2.;
326 otherpositions += otherpos;
327 Fragment::charges_t othercharges;
328 othercharges += 1., 2., 3.;
329 Fragment otherfragment(otherpositions, othercharges);
330 const size_t othersize = otherfragment.nuclei.size();
331 const size_t size = fragment->nuclei.size();
332 *fragment += otherfragment;
333 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
334 CPPUNIT_ASSERT_EQUAL( size+othersize-1, fragment->nuclei.size() ); // one for already present
335 {
336 // tests all for containment
337 Fragment::nuclei_t validnuclei(positions.size()+otherpositions.size());
338 Fragment::nuclei_t::iterator iter =
339 std::transform(positions.begin(), positions.end(),
340 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
341 std::transform(otherpositions.begin(), otherpositions.end(),
342 othercharges.begin(), iter, Fragment::createNucleus);
343 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
344 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
345 }
346 }
347 {
348 // tests positions for no containment in otherfragment (but last)
349 Fragment::nuclei_t invalidnuclei(positions.size()-1);
350 std::transform(positions.begin(), --positions.end(),
351 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
352 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
353 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
354 }
355 }
356 {
357 // tests otherpositions for containment in otherfragment
358 Fragment::nuclei_t validnuclei(otherpositions.size());
359 std::transform(otherpositions.begin(), otherpositions.end(),
360 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
361 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
362 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
363 }
364 }
365 }
366}
367
368/** UnitTest for operator-=()
369 */
370void FragmentTest::operatorMinusEqual_NonOverlapping_Test()
371{
372 {
373 // create non-overlapping set
374 Fragment::positions_t otherpositions;
375 Fragment::position_t otherpos(3, 2.);
376 otherpositions += otherpos;
377 otherpos[0] = 0.;
378 otherpositions += otherpos;
379 otherpos[1] = 0.;
380 otherpositions += otherpos;
381 Fragment::charges_t othercharges;
382 othercharges += 1., 2., 3.;
383 Fragment otherfragment(otherpositions, othercharges);
384 const size_t othersize = otherfragment.nuclei.size();
385 const size_t size = fragment->nuclei.size();
386 *fragment -= otherfragment;
387 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
388 CPPUNIT_ASSERT_EQUAL( size, fragment->nuclei.size() );
389 {
390 // tests positions for containment
391 Fragment::nuclei_t validnuclei(positions.size());
392 std::transform(positions.begin(), positions.end(),
393 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
394 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
395 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
396 }
397 }
398 {
399 // tests otherpositions for no containment
400 Fragment::nuclei_t invalidnuclei(otherpositions.size());
401 std::transform(otherpositions.begin(), otherpositions.end(),
402 othercharges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
403 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
404 CPPUNIT_ASSERT( !fragment->containsNuclei(nucleus) );
405 }
406 }
407 {
408 // tests positions for no containment in otherfragment
409 Fragment::nuclei_t invalidnuclei(positions.size());
410 std::transform(positions.begin(), positions.end(),
411 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
412 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
413 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
414 }
415 }
416 {
417 // tests otherpositions for containment in otherfragment
418 Fragment::nuclei_t validnuclei(otherpositions.size());
419 std::transform(otherpositions.begin(), otherpositions.end(),
420 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
421 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
422 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
423 }
424 }
425 }
426}
427
428/** UnitTest for operator-=()
429 */
430void FragmentTest::operatorMinusEqual_Test()
431{
432 {
433 // create overlapping set (first overlaps although with different charge)
434 Fragment::positions_t otherpositions;
435 Fragment::position_t otherpos(3, 1.);
436 otherpositions += otherpos;
437 otherpos[0] = 2.;
438 otherpositions += otherpos;
439 otherpos[1] = 2.;
440 otherpositions += otherpos;
441 Fragment::charges_t othercharges;
442 othercharges += 1., 2., 3.;
443 Fragment otherfragment(otherpositions, othercharges);
444 const size_t othersize = otherfragment.nuclei.size();
445 const size_t size = fragment->nuclei.size();
446 CPPUNIT_ASSERT( Fragment::isPositionEqual(otherpositions[0],positions[3]) );
447 *fragment -= otherfragment;
448 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
449 CPPUNIT_ASSERT_EQUAL( size-1, fragment->nuclei.size() ); // just one overlaps
450 {
451 // tests all but last for containment
452 Fragment::nuclei_t validnuclei(positions.size());
453 std::transform(positions.begin(), positions.end(),
454 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
455 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
456 if (Fragment::isPositionEqual(nucleus.first, otherpositions[0])) // only test position
457 CPPUNIT_ASSERT( !fragment->containsNuclei(nucleus) );
458 else
459 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
460 }
461 }
462 {
463 // tests positions for no containment in otherfragment
464 Fragment::nuclei_t invalidnuclei(positions.size()-1);
465 std::transform(positions.begin(), --positions.end(),
466 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
467 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
468 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
469 }
470 }
471 {
472 // tests otherpositions for containment in otherfragment
473 Fragment::nuclei_t validnuclei(otherpositions.size());
474 std::transform(otherpositions.begin(), otherpositions.end(),
475 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
476 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
477 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
478 }
479 }
480 }
481}
Note: See TracBrowser for help on using the repository browser.