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

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 58952a was 955051, checked in by Frederik Heber <heber@…>, 11 years ago

Added (in)equality operators to all SetValues.

  • Property mode set to 100644
File size: 16.9 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 // check against ZeroInstance
239 CPPUNIT_ASSERT( *fragment != ZeroInstance<Fragment>() );
240 CPPUNIT_ASSERT( otherfragment != ZeroInstance<Fragment>() );
241}
242
243/** UnitTest for operator+=()
244 */
245void FragmentTest::assignment_Test()
246{
247 // create different fragment
248 Fragment::positions_t otherpositions;
249 Fragment::position_t otherpos(3, 2.);
250 otherpositions += otherpos;
251 otherpos[0] = 0.;
252 otherpositions += otherpos;
253 otherpos[1] = 0.;
254 otherpositions += otherpos;
255 Fragment::charges_t othercharges;
256 othercharges += 1., 2., 3.;
257 Fragment otherfragment(otherpositions, othercharges);
258
259 // check for inequality
260 CPPUNIT_ASSERT( otherfragment.nuclei.size() != fragment->nuclei.size() );
261 CPPUNIT_ASSERT( otherfragment != *fragment );
262
263 //assign
264 otherfragment = *fragment;
265
266 // check for equality
267 CPPUNIT_ASSERT( otherfragment.nuclei.size() == fragment->nuclei.size() );
268 CPPUNIT_ASSERT( otherfragment == *fragment );
269}
270
271/** UnitTest for operator+=()
272 */
273void FragmentTest::operatorPlusEqual_NonOverlapping_Test()
274{
275 {
276 // create non-overlapping set
277 Fragment::positions_t otherpositions;
278 Fragment::position_t otherpos(3, 2.);
279 otherpositions += otherpos;
280 otherpos[0] = 0.;
281 otherpositions += otherpos;
282 otherpos[1] = 0.;
283 otherpositions += otherpos;
284 Fragment::charges_t othercharges;
285 othercharges += 1., 2., 3.;
286 Fragment otherfragment(otherpositions, othercharges);
287 const size_t othersize = otherfragment.nuclei.size();
288 const size_t size = fragment->nuclei.size();
289 *fragment += otherfragment;
290 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
291 CPPUNIT_ASSERT_EQUAL( size+othersize, fragment->nuclei.size() );
292 {
293 // tests all for containment
294 Fragment::nuclei_t validnuclei(positions.size()+otherpositions.size());
295 Fragment::nuclei_t::iterator iter =
296 std::transform(positions.begin(), positions.end(),
297 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
298 std::transform(otherpositions.begin(), otherpositions.end(),
299 othercharges.begin(), iter, Fragment::createNucleus);
300 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
301 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
302 }
303 }
304 {
305 // tests positions for no containment in otherfragment
306 Fragment::nuclei_t invalidnuclei(positions.size());
307 std::transform(positions.begin(), positions.end(),
308 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
309 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
310 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
311 }
312 }
313 {
314 // tests otherpositions for containment in otherfragment
315 Fragment::nuclei_t validnuclei(otherpositions.size());
316 std::transform(otherpositions.begin(), otherpositions.end(),
317 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
318 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
319 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
320 }
321 }
322 }
323}
324
325/** UnitTest for operator+=()
326 */
327void FragmentTest::operatorPlusEqual_Test()
328{
329 {
330 // create overlapping set (first overlaps)
331 Fragment::positions_t otherpositions;
332 Fragment::position_t otherpos(3, 1.);
333 otherpositions += otherpos;
334 otherpos[0] = 2.;
335 otherpositions += otherpos;
336 otherpos[1] = 2.;
337 otherpositions += otherpos;
338 Fragment::charges_t othercharges;
339 othercharges += 1., 2., 3.;
340 Fragment otherfragment(otherpositions, othercharges);
341 const size_t othersize = otherfragment.nuclei.size();
342 const size_t size = fragment->nuclei.size();
343 *fragment += otherfragment;
344 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
345 CPPUNIT_ASSERT_EQUAL( size+othersize-1, fragment->nuclei.size() ); // one for already present
346 {
347 // tests all for containment
348 Fragment::nuclei_t validnuclei(positions.size()+otherpositions.size());
349 Fragment::nuclei_t::iterator iter =
350 std::transform(positions.begin(), positions.end(),
351 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
352 std::transform(otherpositions.begin(), otherpositions.end(),
353 othercharges.begin(), iter, Fragment::createNucleus);
354 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
355 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
356 }
357 }
358 {
359 // tests positions for no containment in otherfragment (but last)
360 Fragment::nuclei_t invalidnuclei(positions.size()-1);
361 std::transform(positions.begin(), --positions.end(),
362 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
363 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
364 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
365 }
366 }
367 {
368 // tests otherpositions for containment in otherfragment
369 Fragment::nuclei_t validnuclei(otherpositions.size());
370 std::transform(otherpositions.begin(), otherpositions.end(),
371 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
372 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
373 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
374 }
375 }
376 }
377}
378
379/** UnitTest for operator-=()
380 */
381void FragmentTest::operatorMinusEqual_NonOverlapping_Test()
382{
383 {
384 // create non-overlapping set
385 Fragment::positions_t otherpositions;
386 Fragment::position_t otherpos(3, 2.);
387 otherpositions += otherpos;
388 otherpos[0] = 0.;
389 otherpositions += otherpos;
390 otherpos[1] = 0.;
391 otherpositions += otherpos;
392 Fragment::charges_t othercharges;
393 othercharges += 1., 2., 3.;
394 Fragment otherfragment(otherpositions, othercharges);
395 const size_t othersize = otherfragment.nuclei.size();
396 const size_t size = fragment->nuclei.size();
397 *fragment -= otherfragment;
398 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
399 CPPUNIT_ASSERT_EQUAL( size, fragment->nuclei.size() );
400 {
401 // tests positions for containment
402 Fragment::nuclei_t validnuclei(positions.size());
403 std::transform(positions.begin(), positions.end(),
404 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
405 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
406 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
407 }
408 }
409 {
410 // tests otherpositions for no containment
411 Fragment::nuclei_t invalidnuclei(otherpositions.size());
412 std::transform(otherpositions.begin(), otherpositions.end(),
413 othercharges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
414 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
415 CPPUNIT_ASSERT( !fragment->containsNuclei(nucleus) );
416 }
417 }
418 {
419 // tests positions for no containment in otherfragment
420 Fragment::nuclei_t invalidnuclei(positions.size());
421 std::transform(positions.begin(), positions.end(),
422 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
423 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
424 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
425 }
426 }
427 {
428 // tests otherpositions for containment in otherfragment
429 Fragment::nuclei_t validnuclei(otherpositions.size());
430 std::transform(otherpositions.begin(), otherpositions.end(),
431 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
432 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
433 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
434 }
435 }
436 }
437}
438
439/** UnitTest for operator-=()
440 */
441void FragmentTest::operatorMinusEqual_Test()
442{
443 {
444 // create overlapping set (first overlaps although with different charge)
445 Fragment::positions_t otherpositions;
446 Fragment::position_t otherpos(3, 1.);
447 otherpositions += otherpos;
448 otherpos[0] = 2.;
449 otherpositions += otherpos;
450 otherpos[1] = 2.;
451 otherpositions += otherpos;
452 Fragment::charges_t othercharges;
453 othercharges += 1., 2., 3.;
454 Fragment otherfragment(otherpositions, othercharges);
455 const size_t othersize = otherfragment.nuclei.size();
456 const size_t size = fragment->nuclei.size();
457 CPPUNIT_ASSERT( Fragment::isPositionEqual(otherpositions[0],positions[3]) );
458 *fragment -= otherfragment;
459 CPPUNIT_ASSERT_EQUAL( othersize, otherfragment.nuclei.size() );
460 CPPUNIT_ASSERT_EQUAL( size-1, fragment->nuclei.size() ); // just one overlaps
461 {
462 // tests all but last for containment
463 Fragment::nuclei_t validnuclei(positions.size());
464 std::transform(positions.begin(), positions.end(),
465 charges.begin(), validnuclei.begin(), Fragment::createNucleus);
466 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
467 if (Fragment::isPositionEqual(nucleus.first, otherpositions[0])) // only test position
468 CPPUNIT_ASSERT( !fragment->containsNuclei(nucleus) );
469 else
470 CPPUNIT_ASSERT( fragment->containsNuclei(nucleus) );
471 }
472 }
473 {
474 // tests positions for no containment in otherfragment
475 Fragment::nuclei_t invalidnuclei(positions.size()-1);
476 std::transform(positions.begin(), --positions.end(),
477 charges.begin(), invalidnuclei.begin(), Fragment::createNucleus);
478 BOOST_FOREACH( Fragment::nucleus_t nucleus, invalidnuclei) {
479 CPPUNIT_ASSERT( !otherfragment.containsNuclei(nucleus) );
480 }
481 }
482 {
483 // tests otherpositions for containment in otherfragment
484 Fragment::nuclei_t validnuclei(otherpositions.size());
485 std::transform(otherpositions.begin(), otherpositions.end(),
486 othercharges.begin(), validnuclei.begin(), Fragment::createNucleus);
487 BOOST_FOREACH( Fragment::nucleus_t nucleus, validnuclei) {
488 CPPUNIT_ASSERT( otherfragment.containsNuclei(nucleus) );
489 }
490 }
491 }
492}
493
494
495
496/** UnitTest for serialization
497 */
498void FragmentTest::serializeTest()
499{
500 // serialize
501 std::stringstream outputstream;
502 boost::archive::text_oarchive oa(outputstream);
503 oa << fragment;
504
505 // deserialize
506 Fragment *samefragment = NULL;
507 std::stringstream returnstream(outputstream.str());
508 boost::archive::text_iarchive ia(returnstream);
509 ia >> samefragment;
510
511 CPPUNIT_ASSERT( samefragment != NULL );
512 CPPUNIT_ASSERT( *fragment == *samefragment );
513
514 delete samefragment;
515}
Note: See TracBrowser for help on using the repository browser.