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