source: src/Fragmentation/Summation/SetValues/unittests/FragmentUnitTest.cpp@ e0ae58d

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 e0ae58d was 76096d, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Several fixes found through CppCheck.

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