source: src/Fragmentation/Summation/SetValues/unittests/SamplingGridUnitTest.cpp@ 6e73f5

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 6e73f5 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: 19.0 KB
RevLine 
[c889b7]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[c889b7]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 * SamplingGridUnitTest.cpp
26 *
27 * Created on: Jul 29, 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
[955051]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
[c889b7]46#include "SamplingGridUnitTest.hpp"
47
48#include "CodePatterns/Assert.hpp"
49
50#include <boost/assign.hpp>
[c6355f]51#include <cmath>
52#include <numeric>
[c889b7]53
54#ifdef HAVE_TESTRUNNER
55#include "UnitTestMain.hpp"
56#endif /*HAVE_TESTRUNNER*/
57
58using namespace boost::assign;
59
60/********************************************** Test classes **************************************/
61
62const double grid_value=1.;
63
[3d9a8d]64#define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
[c6355f]65#define DOMAINVOLUME(l) (size_t)pow(l,3)
[3d9a8d]66
[c889b7]67// Registers the fixture into the 'registry'
68CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
69
70
71void SamplingGridTest::setUp()
72{
73 // failing asserts should be thrown
74 ASSERT_DO(Assert::Throw);
75
76 // create the grid
77 const double begin[3] = { 0., 0., 0. };
[3d9a8d]78 const double end[3] = { 1., 1., 1. };
[c6355f]79 for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
[c889b7]80 values += grid_value;
[3d9a8d]81 grid = new SamplingGrid(begin, end, 2, values);
[c889b7]82 CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
83}
84
85
86void SamplingGridTest::tearDown()
87{
88 delete grid;
89}
90
[1a00bb]91/** UnitTest on compatible combination of props and values
[c889b7]92 */
[1a00bb]93void SamplingGridTest::compatibleGrids_Test()
[c889b7]94{
95 // check illegal grid
96 const double begin[3] = { 0., 0., 0. };
[3d9a8d]97 const double end[3] = { 2., 2., 2. };
[c6355f]98 SamplingGridProperties illegal_props(begin, end, 5);
[1a00bb]99 SamplingGridProperties legal_props(begin, end, 2);
[c889b7]100 CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
[1a00bb]101 CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
[c889b7]102 SamplingGrid::sampledvalues_t illegal_values;
103 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
104 illegal_values += 1.5;
[1a00bb]105 SamplingGrid::sampledvalues_t legal_values;
106 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
[c6355f]107 legal_values += 1.5;
[c889b7]108#ifndef NDEBUG
[1a00bb]109 // throws because props and size of illegal_values don't match
110 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
111 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
[3d9a8d]112 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
[1a00bb]113 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
114 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
115 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure );
[c889b7]116#endif
117 // check that grid is still the same
118 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
119 iter != grid->sampled_grid.end(); ++iter)
120 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
121}
122
[c0e8fb]123/** UnitTest for isCongruent()
124 */
125void SamplingGridTest::isCongruent_Test()
126{
127 const double begin[3] = { 0., 0., 0. };
128 const double end[3] = { 2., 2., 2. };
129 const double otherbegin[3] = { 0.1, 0.1, 0.1 };
130 const double otherend[3] = { 1., 1., 1. };
131 SamplingGridProperties illegal_begin_props(otherbegin, end, 5);
132 SamplingGridProperties illegal_end_props(begin, otherend, 5);
133 SamplingGridProperties illegal_level_props(begin, end, 5);
134 SamplingGridProperties legal_props(begin, end, 2);
135
136 // differing windows
[955051]137// const double begin_window[3] = { 0.5, 0.5, 0.5 };
138// const double end_window[3] = { 1., 1., 1. };
[c0e8fb]139 const double otherbegin_window[3] = { 0.45, 0.45, 0.45 };
140 const double otherend_window[3] = { 1.05, 1.05, 1.05 };
141
142 // check that incompatible grid are also incongruent
143 SamplingGrid default_grid(legal_props);
144 // note that we always construct a temporary SamplingGrid from given props
145 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_begin_props) == default_grid.isCongruent(illegal_begin_props));
146 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_end_props) == default_grid.isCongruent(illegal_end_props));
147 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_level_props) == default_grid.isCongruent(illegal_level_props));
148 CPPUNIT_ASSERT( default_grid.isCompatible(legal_props) == default_grid.isCongruent(legal_props) );
149
150 default_grid.setWindowSize(begin, end);
151 // same window
152 {
153 SamplingGrid illegal_begin_grid(illegal_begin_props);
154 SamplingGrid illegal_end_grid(illegal_end_props);
155 SamplingGrid illegal_level_grid(illegal_level_props);
156 SamplingGrid legal_grid(legal_props);
157 illegal_begin_grid.setWindowSize(begin, end);
158 illegal_end_grid.setWindowSize(begin, end);
159 illegal_level_grid.setWindowSize(begin, end);
160 legal_grid.setWindowSize(begin, end);
161 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(default_grid) );
162 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(default_grid) );
163 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(default_grid) );
164 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
165 }
166
167 // different begin
168 {
169 SamplingGrid illegal_begin_grid(illegal_begin_props);
170 SamplingGrid illegal_end_grid(illegal_end_props);
171 SamplingGrid illegal_level_grid(illegal_level_props);
172 SamplingGrid legal_grid(legal_props);
173 illegal_begin_grid.setWindowSize(otherbegin_window, end);
174 illegal_end_grid.setWindowSize(otherbegin_window, end);
175 illegal_level_grid.setWindowSize(otherbegin_window, end);
176 legal_grid.setWindowSize(begin, end);
177 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
178 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
179 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
180 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
181 }
182
183 // different end
184 {
185 SamplingGrid illegal_begin_grid(illegal_begin_props);
186 SamplingGrid illegal_end_grid(illegal_end_props);
187 SamplingGrid illegal_level_grid(illegal_level_props);
188 SamplingGrid legal_grid(legal_props);
189 illegal_begin_grid.setWindowSize(begin, otherend_window);
190 illegal_end_grid.setWindowSize(begin, otherend_window);
191 illegal_level_grid.setWindowSize(begin, otherend_window);
192 legal_grid.setWindowSize(begin, end);
193 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
194 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
195 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
196 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
197 }
198
199 // different begin and end
200 {
201 SamplingGrid illegal_begin_grid(illegal_begin_props);
202 SamplingGrid illegal_end_grid(illegal_end_props);
203 SamplingGrid illegal_level_grid(illegal_level_props);
204 SamplingGrid legal_grid(legal_props);
205 illegal_begin_grid.setWindowSize(otherbegin_window, otherend_window);
206 illegal_end_grid.setWindowSize(otherbegin_window, otherend_window);
207 illegal_level_grid.setWindowSize(otherbegin_window, otherend_window);
208 legal_grid.setWindowSize(begin, end);
209 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
210 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
211 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
212 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
213 }
214}
215
[c6355f]216/** UnitTest for integral()
217 */
218void SamplingGridTest::integral_Test()
219{
220 double sum = 0.;
221 sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
222 CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
223}
224
225/** UnitTest for getVolume()
[3d9a8d]226 */
227void SamplingGridTest::getVolume_Test()
228{
229 CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
230}
231
[c6355f]232/** UnitTest for getWindowSize()
[1a00bb]233 */
234void SamplingGridTest::getWindowSize_Test()
235{
236 // check size of default grid
[e2404f]237 CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
[1a00bb]238
239 // create another one and check its size, too
240 const double begin[3] = { 0., 0., 0. };
241 const double end[3] = { 1., 1., 1. };
242 for (size_t level = 3; level<=6; ++level) {
243 values.clear();
244 for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i)
245 values += grid_value;
246 delete grid;
[e2404f]247 // use other pointer in case something fails
248 SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
249 grid = tmpgrid;
[1a00bb]250 CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
251 }
252}
253
254/** UnitTest for extendWindow()
255 */
256void SamplingGridTest::extendWindow_Test()
257{
[c6355f]258 // we have a grid with size of one, extend to twice the size and check
[1a00bb]259 const double begin[3] = { 0., 0., 0. };
[de6dfb]260 const double size = 2.;
261 const double end[3] = { size, size, size };
[c6355f]262 double offset[3];
263 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
264 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
265 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
266 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
267 const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
268 SamplingGrid newgrid(begin, end, 2);
269 newgrid.setWindowSize(window_begin, window_end);
270 // resize values by hand to new window size. Otherwise they get zero'd.
271 newgrid.sampled_grid = values;
272 newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
273 newgrid.extendWindow(begin, end);
274
275 // check integral
276 CPPUNIT_ASSERT_EQUAL(
277 grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
278 grid->integral()
279 );
280
281 // check number of points
282 CPPUNIT_ASSERT_EQUAL(
283 (size_t)NUMBEROFSAMPLES(grid->level),
284 grid->getWindowGridPoints()
285 );
286 }
[de6dfb]287}
288
289/** UnitTest for extendWindow() with asymmetric values
290 */
291void SamplingGridTest::extendWindow_asymmetric_Test()
292{
[c6355f]293 std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
[de6dfb]294 const double begin[3] = { 0., 0., 0. };
295 const double end[3] = { 2., 2., 2. };
[c6355f]296 double offset[3];
297 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
298 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
299 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
300 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
301 const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
302 SamplingGrid newgrid(begin, end, 2);
303 CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
304 newgrid.setWindowSize(window_begin, window_end);
305 // window size is only half of domain size
306 const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,3);
307 for (size_t i=0; i< max_samples; ++i)
308 newgrid.sampled_grid += grid_value*i;
309 const size_t sum_weight = (max_samples)*(max_samples-1)/2;
310 const double integral = newgrid.integral();
311 newgrid.extendWindow(begin, end);
312
313 // check that integral has remained the same
314 CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
315 CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
316 }
[1a00bb]317}
318
319/** UnitTest for addOntoWindow()
320 */
321void SamplingGridTest::addOntoWindow_Test()
322{
323 // first window is from (0,0,0) to (1,1,1)
324 CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
325
[c6355f]326 // create values for half-sized window
[1a00bb]327 values.clear();
[c6355f]328 for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
[1a00bb]329 values += grid_value;
[c6355f]330
[1a00bb]331 // check that too large a window throws
332#ifndef NDEBUG
[c6355f]333 const double begin[3] = { .5, .5, .5 };
[1a00bb]334 const double wrongend[3] = { 1.5, 1.5, 1.5 };
335 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
[de6dfb]336 CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
[1a00bb]337#endif
338
[c6355f]339 // create another window from (.5,.5,.5) to (1., 1., 1.)
340 double offset[3];
341 for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
342 for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
343 for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
344 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
345 const double window_end[3] = { .5+offset[0], .5+offset[1], .5+offset[2]};
346
347 SamplingGrid newgrid(*grid);
348 // now perform working operation
349 newgrid.addOntoWindow(window_begin, window_end, values, +1.);
350
351 // check integral to be one and one eighth times the old value
352 CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, newgrid.integral() );
353 }
[1a00bb]354}
355
[de6dfb]356/** UnitTest for addOntoWindow() with asymmetric values
357 */
358void SamplingGridTest::addOntoWindow_asymmetric_Test()
359{
[c6355f]360 const size_t size = grid->end[0]-grid->begin[0];
[de6dfb]361 // check with asymmetric values
362 grid->sampled_grid.clear();
[c6355f]363 grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
[de6dfb]364
365 for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
366 grid->sampled_grid[(i*2+0)*2+0] += .5*grid_value;
367 for (size_t i=0;i<grid->level*(grid->end[1]-grid->begin[1]);++i)
368 grid->sampled_grid[(0*2+i)*2+0] += 1.*grid_value;
369 for (size_t i=0;i<grid->level*(grid->end[2]-grid->begin[2]);++i)
370 grid->sampled_grid[(0*2+0)*2+i] += 1.5*grid_value;
371
372 const double integral = grid->integral();
[c6355f]373
374 // now perform working operation
375 grid->addOntoWindow(grid->begin, grid->end, values, +1.);
376 // values is equal to integral of 1.
377 CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
[de6dfb]378}
379
[c889b7]380/** UnitTest for operator+=()
381 */
382void SamplingGridTest::operatorPlusEqual_Test()
383{
384 // create other grid
385 const double begin[3] = { 0., 0., 0. };
[3d9a8d]386 const double end[3] = { 1., 1., 1. };
[c889b7]387 SamplingGrid::sampledvalues_t othervalues;
388 const double othergrid_value = 1.5;
389 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
390 othervalues += othergrid_value;
[3d9a8d]391 SamplingGrid othergrid(begin, end, 2, othervalues);
[c889b7]392 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
393
394 // perform operation
395 CPPUNIT_ASSERT_NO_THROW( *grid += othergrid );
396
397 // check the contents of the grid
398 const double sum = grid_value+othergrid_value;
399 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
400 iter != grid->sampled_grid.end(); ++iter)
401 CPPUNIT_ASSERT_EQUAL( sum, *iter );
402}
403
404/** UnitTest for operator-=()
405 */
406void SamplingGridTest::operatorMinusEqual_Test()
407{
408 // create other grid
409 const double begin[3] = { 0., 0., 0. };
[3d9a8d]410 const double end[3] = { 1., 1., 1. };
[c889b7]411 SamplingGrid::sampledvalues_t othervalues;
412 const double othergrid_value = 1.5;
413 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
414 othervalues += othergrid_value;
[3d9a8d]415 SamplingGrid othergrid(begin, end, 2, othervalues);
[c889b7]416 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
417
418 // perform operation
419 CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid );
420
421 // check the contents of the grid
422 const double difference = grid_value-othergrid_value;
423 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
424 iter != grid->sampled_grid.end(); ++iter)
425 CPPUNIT_ASSERT_EQUAL( difference, *iter );
426}
427
[955051]428
429/** UnitTest for operator==()
430 */
431void SamplingGridTest::equality_Test()
432{
433 const double begin[3] = { 0., 0., 0. };
434 const double otherbegin[3] = { .5, 0.1, -0.5 };
435 const double end[3] = { 1., 1., 1. };
436 const double otherend[3] = { 2., 2., 2. };
437 const double begin_window[3] = { 0., 0., 0. };
438 const double otherbegin_window[3] = { .75, 0.1, 0. };
439 const double end_window[3] = { 1., 1., 1. };
440 const double otherend_window[3] = { .9, .9, .9 };
441
442 // create other grid
443 SamplingGrid samegrid(begin, end, 2, values);
444 SamplingGrid differentbegin(otherbegin, end, 2, values);
445 SamplingGrid differentend(begin, otherend, 2, values);
446 SamplingGrid::sampledvalues_t morevalues;
447 {
448 const double othergrid_value = 1.5;
449 for (size_t i=0; i< NUMBEROFSAMPLES(4); ++i)
450 morevalues += othergrid_value;
451 }
452 SamplingGrid differentlevel(begin, end, 4, morevalues);
453 SamplingGrid::sampledvalues_t othervalues;
454 {
455 const double othergrid_value = 1.5;
456 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
457 othervalues += othergrid_value;
458 }
459 SamplingGrid differentvalues(begin, end, 2, othervalues);
460
461 // check for same level, begin, and end
462 CPPUNIT_ASSERT( *grid == *grid );
463 CPPUNIT_ASSERT( *grid == samegrid );
464 CPPUNIT_ASSERT( samegrid == *grid);
465 CPPUNIT_ASSERT( *grid != differentbegin);
466 CPPUNIT_ASSERT( differentbegin != *grid);
467 CPPUNIT_ASSERT( *grid != differentend );
468 CPPUNIT_ASSERT( differentend != *grid );
469 CPPUNIT_ASSERT( *grid != differentlevel );
470 CPPUNIT_ASSERT( differentlevel != *grid );
471
472 // check for all but differing values
473 CPPUNIT_ASSERT( *grid != differentvalues );
474 CPPUNIT_ASSERT( differentvalues != *grid );
475
476 // check for different windows
477 SamplingGrid differentwindowbegin(begin, end, 2);
478 differentwindowbegin.setWindow(otherbegin_window, end_window);
479 SamplingGrid differentwindowend(begin, end, 2);
480 differentwindowend.setWindow(begin_window, otherend_window);
481 CPPUNIT_ASSERT( *grid != differentwindowbegin );
482 CPPUNIT_ASSERT( differentwindowbegin != *grid );
483 CPPUNIT_ASSERT( *grid != differentwindowend );
484 CPPUNIT_ASSERT( differentwindowend != *grid );
485
486 // check against ZeroInstance
487 CPPUNIT_ASSERT( *grid != ZeroInstance<SamplingGrid>() );
488 CPPUNIT_ASSERT( samegrid != ZeroInstance<SamplingGrid>() );
489 CPPUNIT_ASSERT( differentbegin != ZeroInstance<SamplingGrid>() );
490 CPPUNIT_ASSERT( differentend != ZeroInstance<SamplingGrid>() );
491 CPPUNIT_ASSERT( differentlevel != ZeroInstance<SamplingGrid>() );
492 CPPUNIT_ASSERT( differentvalues != ZeroInstance<SamplingGrid>() );
493 CPPUNIT_ASSERT( differentwindowbegin != ZeroInstance<SamplingGrid>() );
494 CPPUNIT_ASSERT( differentwindowend != ZeroInstance<SamplingGrid>() );
495}
496
497/** UnitTest for serialization
498 */
499void SamplingGridTest::serializeTest()
500{
501 // serialize
502 std::stringstream outputstream;
503 boost::archive::text_oarchive oa(outputstream);
504 oa << grid;
505
506 // deserialize
507 SamplingGrid *samegrid = NULL;
508 std::stringstream returnstream(outputstream.str());
509 boost::archive::text_iarchive ia(returnstream);
510 ia >> samegrid;
511
512 CPPUNIT_ASSERT( samegrid != NULL );
513 CPPUNIT_ASSERT( *grid == *samegrid );
514
515 delete samegrid;
516}
Note: See TracBrowser for help on using the repository browser.