CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testDistCopy.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: testDistCopy.cc,v 1.3 2011/05/31 20:57:01 garren Exp $
3// ----------------------------------------------------------------------
4
5// ======================================================================
6//
7//
8// testDistCopy -- test copied random distributions
9//
10// ======================================================================
11
12#include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
13// ----------------------------------------------------------------------
14// Engines:
15#include "CLHEP/Random/DualRand.h" // CLHEP::DualRand
16#include "CLHEP/Random/MTwistEngine.h" // CLHEP::MTwistEngine
17
18// ----------------------------------------------------------------------
19// Distributions:
20#include "CLHEP/Random/RandBinomial.h" // CLHEP::RandBinomial
21#include "CLHEP/Random/RandBreitWigner.h" // CLHEP::RandBreitWigner
22#include "CLHEP/Random/RandChiSquare.h" // CLHEP::RandChiSquare
23#include "CLHEP/Random/RandExponential.h" // CLHEP::RandExponential
24#include "CLHEP/Random/RandFlat.h" // CLHEP::RandFlat
25#include "CLHEP/Random/RandGamma.h" // CLHEP::RandGamma
26#include "CLHEP/Random/RandGauss.h" // CLHEP::RandGauss
27#include "CLHEP/Random/RandGaussQ.h" // CLHEP::RandGaussQ
28#include "CLHEP/Random/RandGaussT.h" // CLHEP::RandGaussT
29#include "CLHEP/Random/RandGeneral.h" // CLHEP::RandGeneral
30#include "CLHEP/Random/RandLandau.h" // CLHEP::RandLandau
31#include "CLHEP/Random/RandPoissonQ.h" // CLHEP::RandPoissonQ
32#include "CLHEP/Random/RandPoissonT.h" // CLHEP::RandPoissonT
33#include "CLHEP/Random/RandSkewNormal.h" // CLHEP::RandSkewNormal
34#include "CLHEP/Random/RandStudentT.h" // CLHEP::RandStudentT
35
36// ----------------------------------------------------------------------
37// Standard library:
38#include <sstream> // for ostringstream
39#include <string> // for string
40#include <vector>
41
42using namespace CLHEP;
43typedef unsigned int uint;
44
45
46// ----------------------------------------------------------------------
47// copy-construction test
48
49template< typename Dist >
50bool
52{
53 // prime the distribution
54 for( uint i = 0u; i != 17u; ++i )
55 (void) d1.fire();
56
57 // capture its state
58 std::ostringstream os1;
59 d1.put( os1 );
60 HepRandomEngine * e1 = & d1.engine();
61
62 // make a copy and capture the copy's state
63 Dist d2( d1 );
64 std::ostringstream os2;
65 d2.put( os2 );
66 HepRandomEngine * e2 = & d2.engine();
67
68 // do the saved states match and is the underlying engine shared?
69 return os1.str() == os2.str() && e1 == e2;
70} // copy_constructor_is_okay<>()
71
72
73// ----------------------------------------------------------------------
74// copy-construction test
75
76template< typename Dist >
77bool
78 copy_assignment_is_okay( Dist & d1, Dist & d2 )
79{
80 // prime the distributions
81 for( uint i = 0u; i != 17u; ++i )
82 (void) d1.fire();
83 for( uint i = 0u; i != 19u; ++i )
84 (void) d2.fire();
85
86 // capture d1's state
87 std::ostringstream os1;
88 d1.put( os1 );
89 HepRandomEngine * e1 = & d1.engine();
90
91 // make a copy and capture the copy's state
92 d2 = d1;
93 std::ostringstream os2;
94 d2.put( os2 );
95 HepRandomEngine * e2 = & d2.engine();
96
97 // do the saved states match and is the underlying engine shared?
98 return os1.str() == os2.str() && e1 == e2;
99} // copy_assignment_is_okay<>()
100
101
102// ----------------------------------------------------------------------
103// Mask bits to form a word identifying dists that failed their test
104
105//static uint const success = 0u;
106static uint const Binomial_failure = 1u << 1;
107static uint const BreitWigner_failure = 1u << 2;
108static uint const ChiSquare_failure = 1u << 3;
109static uint const Exponential_failure = 1u << 4;
110static uint const Flat_failure = 1u << 5;
111static uint const Gamma_failure = 1u << 6;
112static uint const Gauss_failure = 1u << 7;
113static uint const GaussQ_failure = 1u << 8;
114static uint const GaussT_failure = 1u << 9;
115static uint const General_failure = 1u << 10;
116static uint const Landau_failure = 1u << 11;
117static uint const Poisson_failure = 1u << 12;
118static uint const PoissonQ_failure = 1u << 13;
119static uint const PoissonT_failure = 1u << 14;
120static uint const StudentT_failure = 1u << 15;
121static uint const SkewNormal_failure = 1u << 16;
122
123
124// ----------------------------------------------------------------------
125// RandBinomial
126
128{
129 MTwistEngine r1( 97531L );
130 RandBinomial d1( r1 );
131 if( ! copy_constructor_is_okay(d1) ) return Binomial_failure;
132
133 DualRand r2( 13579L );
134 RandBinomial d2( r2 );
135 if( ! copy_assignment_is_okay(d1,d2) ) return Binomial_failure;
136
137 return 0u;
138}
139
140
141// ----------------------------------------------------------------------
142// RandBreitWigner
143
145{
146 MTwistEngine r1( 97531L );
147 RandBreitWigner d1( r1 );
148 if( ! copy_constructor_is_okay(d1) ) return BreitWigner_failure;
149
150 DualRand r2( 13579L );
151 RandBreitWigner d2( r2 );
152 if( ! copy_assignment_is_okay(d1,d2) ) return BreitWigner_failure;
153
154 return 0u;
155} // testRandBreitWigner
156
157
158// ----------------------------------------------------------------------
159// RandChiSquare
160
162{
163 MTwistEngine r1( 97531L );
164 RandChiSquare d1( r1 );
165 if( ! copy_constructor_is_okay(d1) ) return ChiSquare_failure;
166
167 DualRand r2( 13579L );
168 RandChiSquare d2( r2 );
169 if( ! copy_assignment_is_okay(d1,d2) ) return ChiSquare_failure;
170
171 return 0u;
172} // testRandChiSquare
173
174
175// ----------------------------------------------------------------------
176// RandExponential
177
179{
180 MTwistEngine r1( 97531L );
181 RandExponential d1( r1 );
182 if( ! copy_constructor_is_okay(d1) ) return Exponential_failure;
183
184 DualRand r2( 13579L );
185 RandExponential d2( r2 );
186 if( ! copy_assignment_is_okay(d1,d2) ) return Exponential_failure;
187
188 return 0u;
189} // testRandExponential
190
191
192// ----------------------------------------------------------------------
193// RandFlat
194
196{
197 MTwistEngine r1( 97531L );
198 RandFlat d1( r1 );
199 if( ! copy_constructor_is_okay(d1) ) return Flat_failure;
200
201 DualRand r2( 13579L );
202 RandFlat d2( r2 );
203 if( ! copy_assignment_is_okay(d1,d2) ) return Flat_failure;
204
205 return 0u;
206} // testRandFlat
207
208
209// ----------------------------------------------------------------------
210// RandGamma
211
213{
214 MTwistEngine r1( 97531L );
215 RandGamma d1( r1 );
216 if( ! copy_constructor_is_okay(d1) ) return Gamma_failure;
217
218 DualRand r2( 13579L );
219 RandGamma d2( r2 );
220 if( ! copy_assignment_is_okay(d1,d2) ) return Gamma_failure;
221
222 return 0u;
223} // testRandGamma
224
225
226// ----------------------------------------------------------------------
227// RandGauss
228
230{
231 MTwistEngine r1( 97531L );
232 RandGauss d1( r1 );
233 if( ! copy_constructor_is_okay(d1) ) return Gauss_failure;
234
235 DualRand r2( 13579L );
236 RandGauss d2( r2 );
237 if( ! copy_assignment_is_okay(d1,d2) ) return Gauss_failure;
238
239 return 0u;
240} // testRandGauss
241
242
243// ----------------------------------------------------------------------
244// RandGaussQ
245
247{
248 MTwistEngine r1( 97531L );
249 RandGaussQ d1( r1 );
250 if( ! copy_constructor_is_okay(d1) ) return GaussQ_failure;
251
252 DualRand r2( 13579L );
253 RandGaussQ d2( r2 );
254 if( ! copy_assignment_is_okay(d1,d2) ) return GaussQ_failure;
255
256 return 0u;
257} // testRandGaussQ
258
259
260// ----------------------------------------------------------------------
261// RandGaussT
262
264{
265 MTwistEngine r1( 97531L );
266 RandGaussT d1( r1 );
267 if( ! copy_constructor_is_okay(d1) ) return GaussT_failure;
268
269 DualRand r2( 13579L );
270 RandGaussT d2( r2 );
271 if( ! copy_assignment_is_okay(d1,d2) ) return GaussT_failure;
272
273 return 0u;
274} // testRandGaussT
275
276
277// ----------------------------------------------------------------------
278// RandGeneral
279
281{
282 MTwistEngine r1( 97531L );
283 double pdf1[] = { 1.5, 2.5, 3.0, 4.25, 5.65 };
284 RandGeneral d1( r1, pdf1, sizeof(pdf1)/sizeof(pdf1[0]) );
285 if( ! copy_constructor_is_okay(d1) ) return General_failure;
286
287 DualRand r2( 13579L );
288 double pdf2[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 0.60 };
289 RandGeneral d2( r2, pdf2, sizeof(pdf2)/sizeof(pdf2[0]) );
290 if( ! copy_assignment_is_okay(d1,d2) ) return General_failure;
291
292 return 0u;
293} // testRandGeneral
294
295
296// ----------------------------------------------------------------------
297// RandLandau
298
300{
301 MTwistEngine r1( 97531L );
302 RandLandau d1( r1 );
303 if( ! copy_constructor_is_okay(d1) ) return Landau_failure;
304
305 DualRand r2( 13579L );
306 RandLandau d2( r2 );
307 if( ! copy_assignment_is_okay(d1,d2) ) return Landau_failure;
308
309 return 0u;
310} // testRandLandau
311
312
313// ----------------------------------------------------------------------
314// RandPoisson
315
317{
318 MTwistEngine r1( 97531L );
319 RandPoisson d1( r1 );
320 if( ! copy_constructor_is_okay(d1) ) return Poisson_failure;
321
322 DualRand r2( 13579L );
323 RandPoisson d2( r2 );
324 if( ! copy_assignment_is_okay(d1,d2) ) return Poisson_failure;
325
326 return 0u;
327} // testRandPoisson
328
329
330// ----------------------------------------------------------------------
331// RandPoissonQ
332
334{
335 MTwistEngine r1( 97531L );
336 RandPoissonQ d1( r1 );
337 if( ! copy_constructor_is_okay(d1) ) return PoissonQ_failure;
338
339 DualRand r2( 13579L );
340 RandPoissonQ d2( r2 );
341 if( ! copy_assignment_is_okay(d1,d2) ) return PoissonQ_failure;
342
343 return 0u;
344} // testRandPoissonQ
345
346
347// ----------------------------------------------------------------------
348// RandPoissonT
349
351{
352 MTwistEngine r1( 97531L );
353 RandPoissonT d1( r1 );
354 if( ! copy_constructor_is_okay(d1) ) return PoissonT_failure;
355
356 DualRand r2( 13579L );
357 RandPoissonT d2( r2 );
358 if( ! copy_assignment_is_okay(d1,d2) ) return PoissonT_failure;
359
360 return 0u;
361} // testRandPoissonT
362
363
364// ----------------------------------------------------------------------
365// RandSkewNormal
366
368{
369 MTwistEngine r1( 97531L );
370 RandSkewNormal d1( r1 );
371 if( ! copy_constructor_is_okay(d1) ) return SkewNormal_failure;
372
373 DualRand r2( 13579L );
374 RandSkewNormal d2( r2 );
375 if( ! copy_assignment_is_okay(d1,d2) ) return SkewNormal_failure;
376
377 return 0u;
378} // testRandSkewNormal
379
380
381// ----------------------------------------------------------------------
382// RandStudentT
383
385{
386 MTwistEngine r1( 97531L );
387 RandStudentT d1( r1 );
388 if( ! copy_constructor_is_okay(d1) ) return StudentT_failure;
389
390 DualRand r2( 13579L );
391 RandStudentT d2( r2 );
392 if( ! copy_assignment_is_okay(d1,d2) ) return StudentT_failure;
393
394 return 0u;
395} // testRandStudentT
396
397
398// ----------------------------------------------------------------------
399// main
400
401int main()
402{
403 uint mask = 0u
408 | testRandFlat ()
409 | testRandGamma ()
410 | testRandGauss ()
411 | testRandGaussQ ()
412 | testRandGaussT ()
413 | testRandGeneral ()
414 | testRandLandau ()
415 | testRandPoisson ()
420 ;
421
422 return - int(mask);
423}
424
virtual std::ostream & put(std::ostream &os) const
Definition: RandomEngine.cc:61
uint testRandFlat()
uint testRandPoissonQ()
bool copy_assignment_is_okay(Dist &d1, Dist &d2)
Definition: testDistCopy.cc:78
uint testRandGaussQ()
uint testRandGeneral()
uint testRandPoissonT()
uint testRandLandau()
bool copy_constructor_is_okay(Dist &d1)
Definition: testDistCopy.cc:51
uint testRandBreitWigner()
unsigned int uint
Definition: testDistCopy.cc:43
uint testRandGauss()
uint testRandPoisson()
uint testRandChiSquare()
uint testRandGaussT()
uint testRandBinomial()
uint testRandGamma()
uint testRandSkewNormal()
uint testRandStudentT()
uint testRandExponential()
int main()