| 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 | * 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 |
|
|---|
| 36 | using 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 "SamplingGridUnitTest.hpp"
|
|---|
| 47 |
|
|---|
| 48 | #include "CodePatterns/Assert.hpp"
|
|---|
| 49 |
|
|---|
| 50 | #include <boost/assign.hpp>
|
|---|
| 51 | #include <cmath>
|
|---|
| 52 | #include <numeric>
|
|---|
| 53 |
|
|---|
| 54 | #ifdef HAVE_TESTRUNNER
|
|---|
| 55 | #include "UnitTestMain.hpp"
|
|---|
| 56 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 57 |
|
|---|
| 58 | using namespace boost::assign;
|
|---|
| 59 |
|
|---|
| 60 | /********************************************** Test classes **************************************/
|
|---|
| 61 |
|
|---|
| 62 | const double grid_value=1.;
|
|---|
| 63 |
|
|---|
| 64 | #define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
|
|---|
| 65 | #define DOMAINVOLUME(l) (size_t)pow(l,3)
|
|---|
| 66 |
|
|---|
| 67 | // Registers the fixture into the 'registry'
|
|---|
| 68 | CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | void 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. };
|
|---|
| 78 | const double end[3] = { 1., 1., 1. };
|
|---|
| 79 | for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
|
|---|
| 80 | values += grid_value;
|
|---|
| 81 | grid = new SamplingGrid(begin, end, 2, values);
|
|---|
| 82 | CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | void SamplingGridTest::tearDown()
|
|---|
| 87 | {
|
|---|
| 88 | delete grid;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /** UnitTest on compatible combination of props and values
|
|---|
| 92 | */
|
|---|
| 93 | void SamplingGridTest::compatibleGrids_Test()
|
|---|
| 94 | {
|
|---|
| 95 | // check illegal grid
|
|---|
| 96 | const double begin[3] = { 0., 0., 0. };
|
|---|
| 97 | const double end[3] = { 2., 2., 2. };
|
|---|
| 98 | SamplingGridProperties illegal_props(begin, end, 5);
|
|---|
| 99 | SamplingGridProperties legal_props(begin, end, 2);
|
|---|
| 100 | CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
|
|---|
| 101 | CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
|
|---|
| 102 | SamplingGrid::sampledvalues_t illegal_values;
|
|---|
| 103 | for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
|
|---|
| 104 | illegal_values += 1.5;
|
|---|
| 105 | SamplingGrid::sampledvalues_t legal_values;
|
|---|
| 106 | for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
|
|---|
| 107 | legal_values += 1.5;
|
|---|
| 108 | #ifndef NDEBUG
|
|---|
| 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 );
|
|---|
| 112 | std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
|
|---|
| 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 );
|
|---|
| 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 |
|
|---|
| 123 | /** UnitTest for isCongruent()
|
|---|
| 124 | */
|
|---|
| 125 | void 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
|
|---|
| 137 | // const double begin_window[3] = { 0.5, 0.5, 0.5 };
|
|---|
| 138 | // const double end_window[3] = { 1., 1., 1. };
|
|---|
| 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 |
|
|---|
| 216 | /** UnitTest for integral()
|
|---|
| 217 | */
|
|---|
| 218 | void 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()
|
|---|
| 226 | */
|
|---|
| 227 | void SamplingGridTest::getVolume_Test()
|
|---|
| 228 | {
|
|---|
| 229 | CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /** UnitTest for getWindowSize()
|
|---|
| 233 | */
|
|---|
| 234 | void SamplingGridTest::getWindowSize_Test()
|
|---|
| 235 | {
|
|---|
| 236 | // check size of default grid
|
|---|
| 237 | CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
|
|---|
| 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;
|
|---|
| 247 | // use other pointer in case something fails
|
|---|
| 248 | SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
|
|---|
| 249 | grid = tmpgrid;
|
|---|
| 250 | CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** UnitTest for extendWindow()
|
|---|
| 255 | */
|
|---|
| 256 | void SamplingGridTest::extendWindow_Test()
|
|---|
| 257 | {
|
|---|
| 258 | // we have a grid with size of one, extend to twice the size and check
|
|---|
| 259 | const double begin[3] = { 0., 0., 0. };
|
|---|
| 260 | const double size = 2.;
|
|---|
| 261 | const double end[3] = { size, size, size };
|
|---|
| 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 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /** UnitTest for extendWindow() with asymmetric values
|
|---|
| 290 | */
|
|---|
| 291 | void SamplingGridTest::extendWindow_asymmetric_Test()
|
|---|
| 292 | {
|
|---|
| 293 | std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
|
|---|
| 294 | const double begin[3] = { 0., 0., 0. };
|
|---|
| 295 | const double end[3] = { 2., 2., 2. };
|
|---|
| 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 | }
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /** UnitTest for addOntoWindow()
|
|---|
| 320 | */
|
|---|
| 321 | void 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 |
|
|---|
| 326 | // create values for half-sized window
|
|---|
| 327 | values.clear();
|
|---|
| 328 | for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
|
|---|
| 329 | values += grid_value;
|
|---|
| 330 |
|
|---|
| 331 | // check that too large a window throws
|
|---|
| 332 | #ifndef NDEBUG
|
|---|
| 333 | const double begin[3] = { .5, .5, .5 };
|
|---|
| 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;
|
|---|
| 336 | CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
|
|---|
| 337 | #endif
|
|---|
| 338 |
|
|---|
| 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 | }
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | /** UnitTest for addOntoWindow() with asymmetric values
|
|---|
| 357 | */
|
|---|
| 358 | void SamplingGridTest::addOntoWindow_asymmetric_Test()
|
|---|
| 359 | {
|
|---|
| 360 | const size_t size = grid->end[0]-grid->begin[0];
|
|---|
| 361 | // check with asymmetric values
|
|---|
| 362 | grid->sampled_grid.clear();
|
|---|
| 363 | grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
|
|---|
| 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();
|
|---|
| 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() );
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /** UnitTest for operator+=()
|
|---|
| 381 | */
|
|---|
| 382 | void SamplingGridTest::operatorPlusEqual_Test()
|
|---|
| 383 | {
|
|---|
| 384 | // create other grid
|
|---|
| 385 | const double begin[3] = { 0., 0., 0. };
|
|---|
| 386 | const double end[3] = { 1., 1., 1. };
|
|---|
| 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;
|
|---|
| 391 | SamplingGrid othergrid(begin, end, 2, othervalues);
|
|---|
| 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 | */
|
|---|
| 406 | void SamplingGridTest::operatorMinusEqual_Test()
|
|---|
| 407 | {
|
|---|
| 408 | // create other grid
|
|---|
| 409 | const double begin[3] = { 0., 0., 0. };
|
|---|
| 410 | const double end[3] = { 1., 1., 1. };
|
|---|
| 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;
|
|---|
| 415 | SamplingGrid othergrid(begin, end, 2, othervalues);
|
|---|
| 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 |
|
|---|
| 428 |
|
|---|
| 429 | /** UnitTest for operator==()
|
|---|
| 430 | */
|
|---|
| 431 | void 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 | */
|
|---|
| 499 | void 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 | }
|
|---|