source: src/Fragmentation/SetValues/unittests/FragmentUnitTest.cpp@ 14de8e1

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 14de8e1 was a576eb, checked in by Frederik Heber <heber@…>, 12 years ago

Added serialization capability to Fragment.

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