CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testDistCopy.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: testDistCopy.cc,v 1.3 2011/05/31 20:57:01 garren Exp $
00003 // ----------------------------------------------------------------------
00004 
00005 // ======================================================================
00006 //
00007 //
00008 // testDistCopy -- test copied random distributions
00009 //
00010 // ======================================================================
00011 
00012 // ----------------------------------------------------------------------
00013 // Engines:
00014 #include "CLHEP/Random/DualRand.h"            // CLHEP::DualRand
00015 #include "CLHEP/Random/MTwistEngine.h"        // CLHEP::MTwistEngine
00016 
00017 // ----------------------------------------------------------------------
00018 // Distributions:
00019 #include "CLHEP/Random/RandBinomial.h"        // CLHEP::RandBinomial
00020 #include "CLHEP/Random/RandBreitWigner.h"     // CLHEP::RandBreitWigner
00021 #include "CLHEP/Random/RandChiSquare.h"       // CLHEP::RandChiSquare
00022 #include "CLHEP/Random/RandExponential.h"     // CLHEP::RandExponential
00023 #include "CLHEP/Random/RandFlat.h"            // CLHEP::RandFlat
00024 #include "CLHEP/Random/RandGamma.h"           // CLHEP::RandGamma
00025 #include "CLHEP/Random/RandGauss.h"           // CLHEP::RandGauss
00026 #include "CLHEP/Random/RandGaussQ.h"          // CLHEP::RandGaussQ
00027 #include "CLHEP/Random/RandGaussT.h"          // CLHEP::RandGaussT
00028 #include "CLHEP/Random/RandGeneral.h"         // CLHEP::RandGeneral
00029 #include "CLHEP/Random/RandLandau.h"          // CLHEP::RandLandau
00030 #include "CLHEP/Random/RandPoissonQ.h"        // CLHEP::RandPoissonQ
00031 #include "CLHEP/Random/RandPoissonT.h"        // CLHEP::RandPoissonT
00032 #include "CLHEP/Random/RandSkewNormal.h"      // CLHEP::RandSkewNormal
00033 #include "CLHEP/Random/RandStudentT.h"        // CLHEP::RandStudentT
00034 
00035 // ----------------------------------------------------------------------
00036 // Standard library:
00037 #include <sstream>  // for ostringstream
00038 #include <string>   // for string
00039 
00040 
00041 using namespace CLHEP;
00042 typedef  unsigned int  uint;
00043 
00044 
00045 // ----------------------------------------------------------------------
00046 // copy-construction test
00047 
00048 template< typename Dist >
00049 bool
00050   copy_constructor_is_okay( Dist & d1 )
00051 {
00052   // prime the distribution
00053   for( uint i = 0u;  i != 17u;  ++i )
00054     (void) d1.fire();
00055 
00056   // capture its state
00057   std::ostringstream  os1;
00058   d1.put( os1 );
00059   HepRandomEngine *  e1  =  & d1.engine();
00060 
00061   // make a copy and capture the copy's state
00062   Dist  d2( d1 );
00063   std::ostringstream  os2;
00064   d2.put( os2 );
00065   HepRandomEngine *  e2  =  & d2.engine();
00066 
00067   // do the saved states match and is the underlying engine shared?
00068   return os1.str() == os2.str()  &&  e1 == e2;
00069 }  // copy_constructor_is_okay<>()
00070 
00071 
00072 // ----------------------------------------------------------------------
00073 // copy-construction test
00074 
00075 template< typename Dist >
00076 bool
00077   copy_assignment_is_okay( Dist & d1, Dist & d2 )
00078 {
00079   // prime the distributions
00080   for( uint i = 0u;  i != 17u;  ++i )
00081     (void) d1.fire();
00082   for( uint i = 0u;  i != 19u;  ++i )
00083     (void) d2.fire();
00084 
00085   // capture d1's state
00086   std::ostringstream  os1;
00087   d1.put( os1 );
00088   HepRandomEngine *  e1  =  & d1.engine();
00089 
00090   // make a copy and capture the copy's state
00091   d2 = d1;
00092   std::ostringstream  os2;
00093   d2.put( os2 );
00094   HepRandomEngine *  e2  =  & d2.engine();
00095 
00096   // do the saved states match and is the underlying engine shared?
00097   return os1.str() == os2.str()  &&  e1 == e2;
00098 }  // copy_assignment_is_okay<>()
00099 
00100 
00101 // ----------------------------------------------------------------------
00102 // Mask bits to form a word identifying dists that failed their test
00103 
00104 static  uint const  success              =  0u;
00105 static  uint const  Binomial_failure     =  1u <<  1;
00106 static  uint const  BreitWigner_failure  =  1u <<  2;
00107 static  uint const  ChiSquare_failure    =  1u <<  3;
00108 static  uint const  Exponential_failure  =  1u <<  4;
00109 static  uint const  Flat_failure         =  1u <<  5;
00110 static  uint const  Gamma_failure        =  1u <<  6;
00111 static  uint const  Gauss_failure        =  1u <<  7;
00112 static  uint const  GaussQ_failure       =  1u <<  8;
00113 static  uint const  GaussT_failure       =  1u <<  9;
00114 static  uint const  General_failure      =  1u << 10;
00115 static  uint const  Landau_failure       =  1u << 11;
00116 static  uint const  Poisson_failure      =  1u << 12;
00117 static  uint const  PoissonQ_failure     =  1u << 13;
00118 static  uint const  PoissonT_failure     =  1u << 14;
00119 static  uint const  StudentT_failure     =  1u << 15;
00120 static  uint const  SkewNormal_failure   =  1u << 16;
00121 
00122 
00123 // ----------------------------------------------------------------------
00124 // RandBinomial
00125 
00126 uint  testRandBinomial()
00127 {
00128   MTwistEngine  r1( 97531L );
00129   RandBinomial  d1( r1 );
00130   if( ! copy_constructor_is_okay(d1) )  return Binomial_failure;
00131 
00132   DualRand      r2( 13579L );
00133   RandBinomial  d2( r2 );
00134   if( ! copy_assignment_is_okay(d1,d2) )  return Binomial_failure;
00135 
00136   return 0u;
00137 }
00138 
00139 
00140 // ----------------------------------------------------------------------
00141 // RandBreitWigner
00142 
00143 uint  testRandBreitWigner()
00144 {
00145   MTwistEngine     r1( 97531L );
00146   RandBreitWigner  d1( r1 );
00147   if( ! copy_constructor_is_okay(d1) )  return BreitWigner_failure;
00148 
00149   DualRand         r2( 13579L );
00150   RandBreitWigner  d2( r2 );
00151   if( ! copy_assignment_is_okay(d1,d2) )  return BreitWigner_failure;
00152 
00153   return 0u;
00154 }  // testRandBreitWigner
00155 
00156 
00157 // ----------------------------------------------------------------------
00158 // RandChiSquare
00159 
00160 uint  testRandChiSquare()
00161 {
00162   MTwistEngine   r1( 97531L );
00163   RandChiSquare  d1( r1 );
00164   if( ! copy_constructor_is_okay(d1) )  return ChiSquare_failure;
00165 
00166   DualRand       r2( 13579L );
00167   RandChiSquare  d2( r2 );
00168   if( ! copy_assignment_is_okay(d1,d2) )  return ChiSquare_failure;
00169 
00170   return 0u;
00171 }  // testRandChiSquare
00172 
00173 
00174 // ----------------------------------------------------------------------
00175 // RandExponential
00176 
00177 uint  testRandExponential()
00178 {
00179   MTwistEngine     r1( 97531L );
00180   RandExponential  d1( r1 );
00181   if( ! copy_constructor_is_okay(d1) )  return Exponential_failure;
00182 
00183   DualRand         r2( 13579L );
00184   RandExponential  d2( r2 );
00185   if( ! copy_assignment_is_okay(d1,d2) )  return Exponential_failure;
00186 
00187   return 0u;
00188 }  // testRandExponential
00189 
00190 
00191 // ----------------------------------------------------------------------
00192 // RandFlat
00193 
00194 uint  testRandFlat()
00195 {
00196   MTwistEngine  r1( 97531L );
00197   RandFlat      d1( r1 );
00198   if( ! copy_constructor_is_okay(d1) )  return Flat_failure;
00199 
00200   DualRand      r2( 13579L );
00201   RandFlat      d2( r2 );
00202   if( ! copy_assignment_is_okay(d1,d2) )  return Flat_failure;
00203 
00204   return 0u;
00205 }  // testRandFlat
00206 
00207 
00208 // ----------------------------------------------------------------------
00209 // RandGamma
00210 
00211 uint  testRandGamma()
00212 {
00213   MTwistEngine  r1( 97531L );
00214   RandGamma     d1( r1 );
00215   if( ! copy_constructor_is_okay(d1) )  return Gamma_failure;
00216 
00217   DualRand      r2( 13579L );
00218   RandGamma     d2( r2 );
00219   if( ! copy_assignment_is_okay(d1,d2) )  return Gamma_failure;
00220 
00221   return 0u;
00222 }  // testRandGamma
00223 
00224 
00225 // ----------------------------------------------------------------------
00226 // RandGauss
00227 
00228 uint  testRandGauss()
00229 {
00230   MTwistEngine  r1( 97531L );
00231   RandGauss     d1( r1 );
00232   if( ! copy_constructor_is_okay(d1) )  return Gauss_failure;
00233 
00234   DualRand      r2( 13579L );
00235   RandGauss     d2( r2 );
00236   if( ! copy_assignment_is_okay(d1,d2) )  return Gauss_failure;
00237 
00238   return 0u;
00239 }  // testRandGauss
00240 
00241 
00242 // ----------------------------------------------------------------------
00243 // RandGaussQ
00244 
00245 uint  testRandGaussQ()
00246 {
00247   MTwistEngine  r1( 97531L );
00248   RandGaussQ    d1( r1 );
00249   if( ! copy_constructor_is_okay(d1) )  return GaussQ_failure;
00250 
00251   DualRand      r2( 13579L );
00252   RandGaussQ    d2( r2 );
00253   if( ! copy_assignment_is_okay(d1,d2) )  return GaussQ_failure;
00254 
00255   return 0u;
00256 }  // testRandGaussQ
00257 
00258 
00259 // ----------------------------------------------------------------------
00260 // RandGaussT
00261 
00262 uint  testRandGaussT()
00263 {
00264   MTwistEngine  r1( 97531L );
00265   RandGaussT    d1( r1 );
00266   if( ! copy_constructor_is_okay(d1) )  return GaussT_failure;
00267 
00268   DualRand      r2( 13579L );
00269   RandGaussT    d2( r2 );
00270   if( ! copy_assignment_is_okay(d1,d2) )  return GaussT_failure;
00271 
00272   return 0u;
00273 }  // testRandGaussT
00274 
00275 
00276 // ----------------------------------------------------------------------
00277 // RandGeneral
00278 
00279 uint  testRandGeneral()
00280 {
00281   MTwistEngine  r1( 97531L );
00282   double        pdf1[] = { 1.5, 2.5, 3.0, 4.25, 5.65 };
00283   RandGeneral   d1( r1, pdf1, sizeof(pdf1)/sizeof(pdf1[0]) );
00284   if( ! copy_constructor_is_okay(d1) )  return General_failure;
00285 
00286   DualRand      r2( 13579L );
00287   double        pdf2[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 0.60 };
00288   RandGeneral   d2( r2, pdf2, sizeof(pdf2)/sizeof(pdf2[0]) );
00289   if( ! copy_assignment_is_okay(d1,d2) )  return General_failure;
00290 
00291   return 0u;
00292 }  // testRandGeneral
00293 
00294 
00295 // ----------------------------------------------------------------------
00296 // RandLandau
00297 
00298 uint  testRandLandau()
00299 {
00300   MTwistEngine  r1( 97531L );
00301   RandLandau    d1( r1 );
00302   if( ! copy_constructor_is_okay(d1) )  return Landau_failure;
00303 
00304   DualRand      r2( 13579L );
00305   RandLandau    d2( r2 );
00306   if( ! copy_assignment_is_okay(d1,d2) )  return Landau_failure;
00307 
00308   return 0u;
00309 }  // testRandLandau
00310 
00311 
00312 // ----------------------------------------------------------------------
00313 // RandPoisson
00314 
00315 uint  testRandPoisson()
00316 {
00317   MTwistEngine  r1( 97531L );
00318   RandPoisson   d1( r1 );
00319   if( ! copy_constructor_is_okay(d1) )  return Poisson_failure;
00320 
00321   DualRand      r2( 13579L );
00322   RandPoisson   d2( r2 );
00323   if( ! copy_assignment_is_okay(d1,d2) )  return Poisson_failure;
00324 
00325   return 0u;
00326 }  // testRandPoisson
00327 
00328 
00329 // ----------------------------------------------------------------------
00330 // RandPoissonQ
00331 
00332 uint  testRandPoissonQ()
00333 {
00334   MTwistEngine  r1( 97531L );
00335   RandPoissonQ  d1( r1 );
00336   if( ! copy_constructor_is_okay(d1) )  return PoissonQ_failure;
00337 
00338   DualRand      r2( 13579L );
00339   RandPoissonQ  d2( r2 );
00340   if( ! copy_assignment_is_okay(d1,d2) )  return PoissonQ_failure;
00341 
00342   return 0u;
00343 }  // testRandPoissonQ
00344 
00345 
00346 // ----------------------------------------------------------------------
00347 // RandPoissonT
00348 
00349 uint  testRandPoissonT()
00350 {
00351   MTwistEngine  r1( 97531L );
00352   RandPoissonT  d1( r1 );
00353   if( ! copy_constructor_is_okay(d1) )  return PoissonT_failure;
00354 
00355   DualRand      r2( 13579L );
00356   RandPoissonT  d2( r2 );
00357   if( ! copy_assignment_is_okay(d1,d2) )  return PoissonT_failure;
00358 
00359   return 0u;
00360 }  // testRandPoissonT
00361 
00362 
00363 // ----------------------------------------------------------------------
00364 // RandSkewNormal
00365 
00366 uint  testRandSkewNormal()
00367 {
00368   MTwistEngine     r1( 97531L );
00369   RandSkewNormal  d1( r1 );
00370   if( ! copy_constructor_is_okay(d1) )  return SkewNormal_failure;
00371 
00372   DualRand         r2( 13579L );
00373   RandSkewNormal  d2( r2 );
00374   if( ! copy_assignment_is_okay(d1,d2) )  return SkewNormal_failure;
00375 
00376   return 0u;
00377 }  // testRandSkewNormal
00378 
00379 
00380 // ----------------------------------------------------------------------
00381 // RandStudentT
00382 
00383 uint  testRandStudentT()
00384 {
00385   MTwistEngine  r1( 97531L );
00386   RandStudentT  d1( r1 );
00387   if( ! copy_constructor_is_okay(d1) )  return StudentT_failure;
00388 
00389   DualRand      r2( 13579L );
00390   RandStudentT  d2( r2 );
00391   if( ! copy_assignment_is_okay(d1,d2) )  return StudentT_failure;
00392 
00393   return 0u;
00394 }  // testRandStudentT
00395 
00396 
00397 // ----------------------------------------------------------------------
00398 // main
00399 
00400 int  main()
00401 {
00402   uint  mask = 0u
00403              | testRandBinomial   ()
00404              | testRandBreitWigner()
00405              | testRandChiSquare  ()
00406              | testRandExponential()
00407              | testRandFlat       ()
00408              | testRandGamma      ()
00409              | testRandGauss      ()
00410              | testRandGaussQ     ()
00411              | testRandGaussT     ()
00412              | testRandGeneral    ()
00413              | testRandLandau     ()
00414              | testRandPoisson    ()
00415              | testRandPoissonQ   ()
00416              | testRandPoissonT   ()
00417              | testRandSkewNormal ()
00418              | testRandStudentT   ()
00419              ;
00420 
00421   return - int(mask);
00422 }
00423