| 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 | * HistogramUnitTest.cpp
|
|---|
| 26 | *
|
|---|
| 27 | * Created on: Jul 26, 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 "Fragmentation/Summation/SetValues/Histogram.hpp"
|
|---|
| 43 |
|
|---|
| 44 | #include "HistogramUnitTest.hpp"
|
|---|
| 45 |
|
|---|
| 46 | #include <cmath>
|
|---|
| 47 | #include <limits>
|
|---|
| 48 |
|
|---|
| 49 | #include <boost/assign.hpp>
|
|---|
| 50 |
|
|---|
| 51 | #include "CodePatterns/Assert.hpp"
|
|---|
| 52 |
|
|---|
| 53 | #ifdef HAVE_TESTRUNNER
|
|---|
| 54 | #include "UnitTestMain.hpp"
|
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 56 |
|
|---|
| 57 | using namespace boost::assign;
|
|---|
| 58 |
|
|---|
| 59 | /********************************************** Test classes **************************************/
|
|---|
| 60 |
|
|---|
| 61 | // Registers the fixture into the 'registry'
|
|---|
| 62 | CPPUNIT_TEST_SUITE_REGISTRATION( HistogramTest );
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | void HistogramTest::setUp()
|
|---|
| 66 | {
|
|---|
| 67 | // failing asserts should be thrown
|
|---|
| 68 | ASSERT_DO(Assert::Throw);
|
|---|
| 69 |
|
|---|
| 70 | histogram = NULL;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | void HistogramTest::tearDown()
|
|---|
| 75 | {
|
|---|
| 76 | delete histogram;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** UnitTest for checking internal state after given samples
|
|---|
| 80 | */
|
|---|
| 81 | void HistogramTest::internalState_Test()
|
|---|
| 82 | {
|
|---|
| 83 | // generate non-empty histogramgram
|
|---|
| 84 | Histogram::samples_t sampled_values;
|
|---|
| 85 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 86 | CPPUNIT_ASSERT_EQUAL( (size_t)4, sampled_values.size() );
|
|---|
| 87 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 88 | CPPUNIT_ASSERT_EQUAL( (size_t)4+1, histogram->bins.size() );
|
|---|
| 89 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->bins.begin() );
|
|---|
| 90 | CPPUNIT_ASSERT_EQUAL( 1., histogram->bins.begin()->second );
|
|---|
| 91 | CPPUNIT_ASSERT_EQUAL( 0., (--histogram->bins.end())->second );
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /** UnitTest for getLowerEndBin()
|
|---|
| 95 | */
|
|---|
| 96 | void HistogramTest::getLowerEndBin_Test()
|
|---|
| 97 | {
|
|---|
| 98 | // generate non-empty histogramgram
|
|---|
| 99 | Histogram::samples_t sampled_values;
|
|---|
| 100 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 101 | CPPUNIT_ASSERT_EQUAL( (size_t)4, sampled_values.size() );
|
|---|
| 102 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 103 |
|
|---|
| 104 | // check values inside
|
|---|
| 105 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(1.) );
|
|---|
| 106 | CPPUNIT_ASSERT_EQUAL( 1., histogram->getLowerEndBin(1.)->first );
|
|---|
| 107 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(2.) );
|
|---|
| 108 | CPPUNIT_ASSERT_EQUAL( 2., histogram->getLowerEndBin(2.)->first );
|
|---|
| 109 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(3.) );
|
|---|
| 110 | CPPUNIT_ASSERT_EQUAL( 3., histogram->getLowerEndBin(3.)->first );
|
|---|
| 111 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(4.) );
|
|---|
| 112 | CPPUNIT_ASSERT_EQUAL( 4., histogram->getLowerEndBin(4.)->first );
|
|---|
| 113 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(1.5) );
|
|---|
| 114 | CPPUNIT_ASSERT_EQUAL( 1., histogram->getLowerEndBin(1.5)->first );
|
|---|
| 115 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(2.5) );
|
|---|
| 116 | CPPUNIT_ASSERT_EQUAL( 2., histogram->getLowerEndBin(2.5)->first );
|
|---|
| 117 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(3.5) );
|
|---|
| 118 | CPPUNIT_ASSERT_EQUAL( 3., histogram->getLowerEndBin(3.5)->first );
|
|---|
| 119 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getLowerEndBin(4.5) );
|
|---|
| 120 | CPPUNIT_ASSERT_EQUAL( 4., histogram->getLowerEndBin(4.5)->first );
|
|---|
| 121 |
|
|---|
| 122 | // check values outside
|
|---|
| 123 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getLowerEndBin(.9) );
|
|---|
| 124 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getLowerEndBin(5.01) );
|
|---|
| 125 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getLowerEndBin(5.) );
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** UnitTest for getHigherEndBin()
|
|---|
| 129 | */
|
|---|
| 130 | void HistogramTest::getHigherEndBin_Test()
|
|---|
| 131 | {
|
|---|
| 132 | // generate non-empty histogramgram
|
|---|
| 133 | Histogram::samples_t sampled_values;
|
|---|
| 134 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 135 | CPPUNIT_ASSERT_EQUAL( (size_t)4, sampled_values.size() );
|
|---|
| 136 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 137 |
|
|---|
| 138 | // check values inside
|
|---|
| 139 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(.5) );
|
|---|
| 140 | CPPUNIT_ASSERT_EQUAL( 1., histogram->getHigherEndBin(.5)->first );
|
|---|
| 141 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(1.) );
|
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( 2., histogram->getHigherEndBin(1.)->first );
|
|---|
| 143 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(2.) );
|
|---|
| 144 | CPPUNIT_ASSERT_EQUAL( 3., histogram->getHigherEndBin(2.)->first );
|
|---|
| 145 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(3.) );
|
|---|
| 146 | CPPUNIT_ASSERT_EQUAL( 4., histogram->getHigherEndBin(3.)->first );
|
|---|
| 147 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(4.) );
|
|---|
| 148 | CPPUNIT_ASSERT_EQUAL( 5., histogram->getHigherEndBin(4.)->first );
|
|---|
| 149 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getHigherEndBin(5.) );
|
|---|
| 150 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(1.5) );
|
|---|
| 151 | CPPUNIT_ASSERT_EQUAL( 2., histogram->getHigherEndBin(1.5)->first );
|
|---|
| 152 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(2.5) );
|
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( 3., histogram->getHigherEndBin(2.5)->first );
|
|---|
| 154 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(3.5) );
|
|---|
| 155 | CPPUNIT_ASSERT_EQUAL( 4., histogram->getHigherEndBin(3.5)->first );
|
|---|
| 156 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(4.5) );
|
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( 5., histogram->getHigherEndBin(4.5)->first );
|
|---|
| 158 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getHigherEndBin(5.5) );
|
|---|
| 159 |
|
|---|
| 160 | // check values outside
|
|---|
| 161 | CPPUNIT_ASSERT( histogram->bins.end() != histogram->getHigherEndBin(-.1) );
|
|---|
| 162 | CPPUNIT_ASSERT_EQUAL( 1., histogram->getHigherEndBin(-.1)->first );
|
|---|
| 163 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getHigherEndBin(5.01) );
|
|---|
| 164 | CPPUNIT_ASSERT( histogram->bins.end() == histogram->getHigherEndBin(5.) );
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | /** UnitTest for getLowerEnd()
|
|---|
| 169 | */
|
|---|
| 170 | void HistogramTest::getLowerEnd_Test()
|
|---|
| 171 | {
|
|---|
| 172 | // create non-empty histogram and test
|
|---|
| 173 | Histogram::samples_t sampled_values;
|
|---|
| 174 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 175 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 176 |
|
|---|
| 177 | // go through each bin and check against getLowerEnd()
|
|---|
| 178 | for (Histogram::Bins_t::const_iterator iter = histogram->bins.begin();
|
|---|
| 179 | iter != histogram->bins.end(); ++iter)
|
|---|
| 180 | CPPUNIT_ASSERT_EQUAL( iter->first, histogram->getLowerEnd(iter->first) );
|
|---|
| 181 |
|
|---|
| 182 | CPPUNIT_ASSERT_EQUAL( 7., histogram->getLowerEnd(7.2) );
|
|---|
| 183 | CPPUNIT_ASSERT_EQUAL( -5., histogram->getLowerEnd(-4.5) );
|
|---|
| 184 | CPPUNIT_ASSERT_EQUAL( 4000., histogram->getLowerEnd(4000.1) );
|
|---|
| 185 | CPPUNIT_ASSERT_EQUAL( -4001., histogram->getLowerEnd(-4000.9) );
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** UnitTest for isEmpty()
|
|---|
| 189 | */
|
|---|
| 190 | void HistogramTest::isEmpty_Test()
|
|---|
| 191 | {
|
|---|
| 192 | histogram = new Histogram(1., 1.);
|
|---|
| 193 | // test on empty histogramgram
|
|---|
| 194 | CPPUNIT_ASSERT( histogram->isEmpty() );
|
|---|
| 195 |
|
|---|
| 196 | // create non-empty histogram and test
|
|---|
| 197 | Histogram::samples_t sampled_values;
|
|---|
| 198 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 199 | delete histogram;
|
|---|
| 200 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 201 | CPPUNIT_ASSERT( !histogram->isEmpty() );
|
|---|
| 202 |
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** UnitTest for area()
|
|---|
| 206 | */
|
|---|
| 207 | void HistogramTest::areaTest()
|
|---|
| 208 | {
|
|---|
| 209 | histogram = new Histogram(1., 1.);
|
|---|
| 210 | // test on empty histogramgram
|
|---|
| 211 | CPPUNIT_ASSERT_EQUAL( 0., histogram->area() );
|
|---|
| 212 |
|
|---|
| 213 | // create non-empty histogram and sum up
|
|---|
| 214 | Histogram::samples_t sampled_values;
|
|---|
| 215 | sampled_values += 1.,2.,3.,4., 4.;
|
|---|
| 216 | delete histogram;
|
|---|
| 217 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 218 | CPPUNIT_ASSERT_EQUAL( 5., histogram->area() );
|
|---|
| 219 | Histogram::samples_t more_values;
|
|---|
| 220 | more_values += 1.75,2.5,3.;
|
|---|
| 221 | Histogram otherhistogram(more_values, 1.75, .625);
|
|---|
| 222 | CPPUNIT_ASSERT_EQUAL( 3., otherhistogram.area() );
|
|---|
| 223 |
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** UnitTest for superposeOtherHistogram()
|
|---|
| 227 | */
|
|---|
| 228 | void HistogramTest::superposeOtherHistogram_Test()
|
|---|
| 229 | {
|
|---|
| 230 | // create two histograms, one is larger
|
|---|
| 231 | Histogram::samples_t sampled_values;
|
|---|
| 232 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 233 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 234 | Histogram::samples_t more_values;
|
|---|
| 235 | more_values += 1.75,2.5,3.;
|
|---|
| 236 | Histogram otherhistogram(more_values, 1.75, .625);
|
|---|
| 237 |
|
|---|
| 238 | // check that size increases
|
|---|
| 239 | CPPUNIT_ASSERT_EQUAL( (size_t)3+1, otherhistogram.bins.size() );
|
|---|
| 240 | otherhistogram += *histogram;
|
|---|
| 241 | const size_t number = (size_t)((
|
|---|
| 242 | otherhistogram.getLowerEnd(4.+histogram->binwidth)
|
|---|
| 243 | + otherhistogram.binwidth
|
|---|
| 244 | - otherhistogram.getLowerEnd(1.))/otherhistogram.binwidth)+1;
|
|---|
| 245 | CPPUNIT_ASSERT_EQUAL( number, otherhistogram.bins.size() );
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** UnitTest for operator+=()
|
|---|
| 249 | */
|
|---|
| 250 | void HistogramTest::operatorPlusEqual_Test()
|
|---|
| 251 | {
|
|---|
| 252 | // create non-empty histograms and sum them
|
|---|
| 253 | Histogram::samples_t sampled_values;
|
|---|
| 254 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 255 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 256 |
|
|---|
| 257 | // add upon larger
|
|---|
| 258 | {
|
|---|
| 259 | Histogram::samples_t more_values;
|
|---|
| 260 | more_values += 0.5, 1.25, 2.5,3., 4.2;
|
|---|
| 261 | Histogram otherhistogram(more_values, 0.5, .625);
|
|---|
| 262 | const double areas = histogram->area() + otherhistogram.area();
|
|---|
| 263 |
|
|---|
| 264 | // check that sum is now sum of both
|
|---|
| 265 | otherhistogram += *histogram;
|
|---|
| 266 | // CPPUNIT_ASSERT_EQUAL( areas, otherhistogram.area() );
|
|---|
| 267 | CPPUNIT_ASSERT( fabs(areas - otherhistogram.area()) < std::numeric_limits<double>::epsilon()*1e+1 );
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | // add upon smaller
|
|---|
| 271 | {
|
|---|
| 272 | Histogram::samples_t more_values;
|
|---|
| 273 | more_values += 1.75,2.5,3.;
|
|---|
| 274 | Histogram otherhistogram(more_values, 1.75, .625);
|
|---|
| 275 | const double areas = histogram->area() + otherhistogram.area();
|
|---|
| 276 |
|
|---|
| 277 | // check that sum is now sum of both
|
|---|
| 278 | otherhistogram += *histogram;
|
|---|
| 279 | // CPPUNIT_ASSERT_EQUAL( areas, otherhistogram.area() );
|
|---|
| 280 | CPPUNIT_ASSERT( fabs(areas - otherhistogram.area()) < std::numeric_limits<double>::epsilon()*1e+1 );
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** UnitTest for operator-=()
|
|---|
| 285 | */
|
|---|
| 286 | void HistogramTest::operatorMinusEqual_Test()
|
|---|
| 287 | {
|
|---|
| 288 | // create non-empty histograms and sum them
|
|---|
| 289 | Histogram::samples_t sampled_values;
|
|---|
| 290 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 291 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 292 |
|
|---|
| 293 | // subtract smaller
|
|---|
| 294 | {
|
|---|
| 295 | Histogram::samples_t more_values;
|
|---|
| 296 | more_values += 0.5, 1.25, 2.5,3., 4.2;
|
|---|
| 297 | Histogram otherhistogram(more_values, 0.5, .625);
|
|---|
| 298 | const double difference = otherhistogram.area() - histogram->area();
|
|---|
| 299 |
|
|---|
| 300 | // check that sum is now difference of both
|
|---|
| 301 | otherhistogram -= *histogram;
|
|---|
| 302 | // CPPUNIT_ASSERT_EQUAL( difference, otherhistogram.area() );
|
|---|
| 303 | CPPUNIT_ASSERT( fabs(difference - otherhistogram.area()) < std::numeric_limits<double>::epsilon()*1e+1 );
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | // subtract larger
|
|---|
| 307 | {
|
|---|
| 308 | Histogram::samples_t more_values;
|
|---|
| 309 | more_values += 1.75,2.5,3.;
|
|---|
| 310 | Histogram otherhistogram(more_values, 1.75, .625);
|
|---|
| 311 | const double difference = otherhistogram.area() - histogram->area();
|
|---|
| 312 |
|
|---|
| 313 | // check that sum is now difference of both
|
|---|
| 314 | otherhistogram -= *histogram;
|
|---|
| 315 | // CPPUNIT_ASSERT_EQUAL( difference, otherhistogram.area() );
|
|---|
| 316 | CPPUNIT_ASSERT( fabs(difference - otherhistogram.area()) < std::numeric_limits<double>::epsilon()*1e+1 );
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /** UnitTest for operator==()
|
|---|
| 321 | */
|
|---|
| 322 | void HistogramTest::equality_Test()
|
|---|
| 323 | {
|
|---|
| 324 | Histogram::samples_t sampled_values;
|
|---|
| 325 | sampled_values += 1.,2.,3.,4.;
|
|---|
| 326 | histogram = new Histogram(sampled_values, 1., 1.);
|
|---|
| 327 |
|
|---|
| 328 | // create different histogram
|
|---|
| 329 | Histogram::samples_t more_values;
|
|---|
| 330 | more_values += 0.5, 1.25, 2.5,3., 4.2;
|
|---|
| 331 | Histogram otherhistogram(more_values, 0.5, .625);
|
|---|
| 332 |
|
|---|
| 333 | CPPUNIT_ASSERT( !(*histogram == otherhistogram) );
|
|---|
| 334 | CPPUNIT_ASSERT( *histogram != otherhistogram );
|
|---|
| 335 |
|
|---|
| 336 | // test against empty histogram
|
|---|
| 337 | Histogram emptyhistogram;
|
|---|
| 338 | CPPUNIT_ASSERT( !(*histogram == emptyhistogram) );
|
|---|
| 339 | CPPUNIT_ASSERT( *histogram != emptyhistogram );
|
|---|
| 340 |
|
|---|
| 341 | // tests against themselves
|
|---|
| 342 | CPPUNIT_ASSERT( *histogram == *histogram );
|
|---|
| 343 | CPPUNIT_ASSERT( otherhistogram == otherhistogram );
|
|---|
| 344 | CPPUNIT_ASSERT( emptyhistogram == emptyhistogram );
|
|---|
| 345 |
|
|---|
| 346 | // check against ZeroInstance
|
|---|
| 347 | CPPUNIT_ASSERT( *histogram != ZeroInstance<Histogram>() );
|
|---|
| 348 | CPPUNIT_ASSERT( otherhistogram != ZeroInstance<Histogram>() );
|
|---|
| 349 | }
|
|---|