source: src/Fragmentation/Histogram/unittests/HistogramUnitTest.cpp@ 6bed1f

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

Changed Histogram's cstor to take offset and binwidth instead of CountBins.

  • CountBins was nonsense anyway, could have been taken from samples.size() as well.
  • with these two values, we may also add the samples lateron.
  • Property mode set to 100644
File size: 11.0 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 * 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
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 "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
56using namespace boost::assign;
57
58/********************************************** Test classes **************************************/
59
60// Registers the fixture into the 'registry'
61CPPUNIT_TEST_SUITE_REGISTRATION( HistogramTest );
62
63
64void HistogramTest::setUp()
65{
66 // failing asserts should be thrown
67 ASSERT_DO(Assert::Throw);
68
69 histogram = NULL;
70}
71
72
73void HistogramTest::tearDown()
74{
75 delete histogram;
76}
77
78/** UnitTest for checking internal state after given samples
79 */
80void 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, 1., 1.);
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 */
95void 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, 1., 1.);
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 */
129void 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, 1., 1.);
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 */
169void 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, 1., 1.);
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 */
189void HistogramTest::isEmpty_Test()
190{
191 histogram = new Histogram(1., 1.);
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, 1., 1.);
200 CPPUNIT_ASSERT( !histogram->isEmpty() );
201
202}
203
204/** UnitTest for area()
205 */
206void HistogramTest::areaTest()
207{
208 histogram = new Histogram(1., 1.);
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, 1., 1.);
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, 1.75, .625);
221 CPPUNIT_ASSERT_EQUAL( 3., otherhistogram.area() );
222
223}
224
225/** UnitTest for superposeOtherHistogram()
226 */
227void 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, 1., 1.);
233 Histogram::samples_t more_values;
234 more_values += 1.75,2.5,3.;
235 Histogram otherhistogram(more_values, 1.75, .625);
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 */
249void 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, 1., 1.);
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, 0.5, .625);
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, 1.75, .625);
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 */
285void 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, 1., 1.);
291
292 // subtract smaller
293 {
294 Histogram::samples_t more_values;
295 more_values += 0.5, 1.25, 2.5,3., 4.2;
296 Histogram otherhistogram(more_values, 0.5, .625);
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 += 1.75,2.5,3.;
309 Histogram otherhistogram(more_values, 1.75, .625);
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
Note: See TracBrowser for help on using the repository browser.