CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testExceptions.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 //
00003 // testExceptions.cc - test the DEFECT_NO_EXCEPTIONS version of the Exceptions package
00004 //
00005 // History:
00006 //   19-Dec-1997  WEB  Initial draft; redefining exit() based on an idea
00007 //     by Philippe Canal
00008 //   04-Mar-1998  WEB  Minor grammar update
00009 //   15-Jun-1998  WEB  Added namespace support
00010 //   26-Jun-2001  MF   Tested ctor of ZMexception from ostringstream
00011 //   12-Dec-2001  WEB  Avoid signed/unsigned comparison warnings
00012 //   12-Jun-2002  WEB  Rearrange to ensure correct ZMthrow definition
00013 //
00014 // ----------------------------------------------------------------------
00015 
00016 #include <sstream>
00017 #include <string>
00018   using std::string;
00019 
00020 #include "CLHEP/Exceptions/defs.h"
00021 #include "CLHEP/Cast/itos.h"
00022 #include "CLHEP/Exceptions/ZMthrow.h"
00023 #include "CLHEP/Exceptions/ZMexception.h"
00024 #include "CLHEP/Exceptions/ZMerrno.h"
00025 
00026 
00027 using namespace zmex;
00028 
00029 // ----------
00030 // In case our compilation environment does not support exceptions:
00031 //
00032 //   Since this program tests several cases, including several actual throws,
00033 //   we assume that the DEFECT_NO_EXCEPTIONS version of the ZMthrow macro
00034 //   will use abort() and so override abort()'s behavior in order to keep
00035 //   going for multiple testing herein:  (earlier we used exit(), which we
00036 //   also overide)
00037 // ----------
00038 
00039 #ifdef DEFECT_NO_EXCEPTIONS
00040 
00041 #define exit( x )  \
00042   ZMlogger().emit( "Note:  exception would have been correctly thrown here\n" );
00043 
00044 #define abort()  \
00045   ZMlogger().emit( "Note:  exception would have been correctly thrown here\n" );
00046 
00047 #endif  // DEFECT_NO_EXCEPTIONS
00048 
00049 
00050 // ----------
00051 // Define exception classes and default behaviors:
00052 // ----------
00053 
00054 ZMexStandardDefinition( ZMexception, ZMxTest );
00055 ZMexClassInfo ZMxTest::_classInfo(
00056   "ZMxTest", "Test", ZMexSEVERE );
00057 
00058 ZMexStandardDefinition( ZMxTest, ZMxInfo );
00059 ZMexClassInfo ZMxInfo::_classInfo(
00060   "ZMxInfo", "Test", ZMexINFO );
00061 
00062 ZMexStandardDefinition( ZMxTest, ZMxGoof );
00063 ZMexClassInfo ZMxGoof::_classInfo(
00064   "ZMxGoof", "Test", ZMexERROR );
00065 
00066 ZMexStandardDefinition( ZMxGoof, ZMxOops );
00067 ZMexClassInfo ZMxOops::_classInfo(
00068   "ZMxOops", "Test", ZMexWARNING );
00069 
00070 ZMexStandardDefinition( ZMxGoof, ZMxBooBoo );
00071 ZMexClassInfo ZMxBooBoo::_classInfo(
00072   "ZMxBooBoo", "Test", ZMexFATAL, ZMexIgnoreAlways() );
00073 
00074 
00075 // ----------
00076 // Define output formats, etc
00077 // ----------
00078 
00079 const string QUOTE    = "\"";
00080 const string NEWLINE1 = "\n";
00081 const string NEWLINE2 = "\n\n";
00082 
00083 void display( const ZMexception * ex )  {
00084 
00085   ZMlogger().emit( NEWLINE1
00086                  + ex->name() + ": " + QUOTE + ex->message() + QUOTE + NEWLINE1
00087                  + "  " + (ex->wasThrown() ? "thrown" : "ignored")
00088                  + " by " + ex->handlerUsed() + "()" + NEWLINE1
00089                  );
00090 
00091 }
00092 
00093 
00094 int main()  {
00095   unsigned int k;
00096 
00097   // ----------
00098   // Begin testing, check out basic logger:
00099   // ----------
00100   ZMlogger().emit( NEWLINE1 );
00101   ZMlogger().emit( "----------  Begin testing:  ----------\n" );
00102   ZMlogger().emit( NEWLINE1 );
00103   ZMlogger().emit( "This message checks out basic logger behavior\n" );
00104 
00105 
00106   // ----------
00107   // Test default informational behavior:
00108   // ----------
00109   ZMlogger().emit( NEWLINE2 );
00110   ZMlogger().emit(
00111     "----------  Testing default informational behavior:  ----------\n" );
00112   ZMlogger().emit( NEWLINE1 );
00113   ZMthrow( ZMxInfo( "Testing default informational exception behavior" ) );
00114 
00115   // ----------
00116   // Test default informational behavior using an ostringstream
00117   // ----------
00118 
00119   ZMlogger().emit( NEWLINE2 );
00120   ZMlogger().emit(
00121   "----------  Testing default ostringstream behavior 3 times:  ----------\n" );
00122   std::ostringstream oss;
00123   oss << "Accumulated numbers are ";
00124   for ( k = 0;  k < 3;  ++k )  {
00125     ZMlogger().emit( NEWLINE1 );
00126     if ( k > 0 )
00127       oss << ", ";
00128     oss << k;
00129     ZMthrow( ZMxOops( oss ) );
00130   }
00131 
00132   // ----------
00133   // Test default ignoring behavior 3 times:
00134   // ----------
00135   ZMlogger().emit( NEWLINE2 );
00136   ZMlogger().emit(
00137     "----------  Testing default ignoring behavior 3 times:  ----------\n" );
00138   for ( k = 0;  k < 3;  ++k )  {
00139     ZMlogger().emit( NEWLINE1 );
00140     string testMesg = "Testing default exception ignoring behavior #";
00141     ZMthrow( ZMxOops( testMesg + itos(k+1) ) );
00142   }
00143 
00144 
00145   // ----------
00146   // Test defined ignoring behavior 3 times:
00147   // ----------
00148   ZMlogger().emit( NEWLINE2 );
00149   ZMlogger().emit(
00150     "----------  Testing defined ignoring behavior 3 times:  ----------\n" );
00151   for ( k = 0;  k < 3;  ++k )  {
00152     ZMlogger().emit( NEWLINE1 );
00153     string testMesg = "Testing defined exception ignoring behavior #";
00154     ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
00155   }
00156 
00157   // ----------
00158   // Test default throwing behavior 3 times:
00159   // ----------
00160   ZMlogger().emit( NEWLINE2 );
00161   ZMlogger().emit(
00162     "----------  Testing default throwing behavior 3 times:  ----------\n" );
00163   for ( k = 0;  k < 3;  ++k )  {
00164     ZMlogger().emit( NEWLINE1 );
00165     string testMesg = "Testing default exception throwing behavior #";
00166 #ifndef DEFECT_NO_EXCEPTIONS
00167     try  {
00168 #endif
00169       ZMthrow( ZMxGoof( testMesg + itos(k+1) ) );
00170 #ifndef DEFECT_NO_EXCEPTIONS
00171     }
00172     catch ( ZMexception & e )  {
00173       std::cerr << "Caught: " << e.name() << "\n";
00174     }
00175 #endif
00176   }
00177 
00178 
00179   // ----------
00180   // Test forced throwing behavior 3 times:
00181   // ----------
00182   ZMlogger().emit( NEWLINE2 );
00183   ZMlogger().emit(
00184     "----------  Testing forced throwing behavior 3 times:  ----------\n" );
00185   ZMxBooBoo::setHandler( ZMexThrowAlways() );
00186   for ( k = 0;  k < 3;  ++k )  {
00187     ZMlogger().emit( NEWLINE1 );
00188     string testMesg = "Testing forced exception throwing behavior #";
00189 #ifndef DEFECT_NO_EXCEPTIONS
00190     try  {
00191 #endif
00192       ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
00193 #ifndef DEFECT_NO_EXCEPTIONS
00194     }
00195     catch ( ZMexception & e )  {
00196       std::cerr << "Caught: " << e.name() << "\n";
00197     }
00198 #endif
00199   }
00200 
00201   // ----------
00202   // Test scheduled throwing behavior 3 times:
00203   // ----------
00204   ZMlogger().emit( NEWLINE2 );
00205   ZMlogger().emit(
00206     "----------  Testing scheduled throwing behavior 3 times:  ----------\n" );
00207   ZMxBooBoo::setHandler( ZMexIgnoreNextN( 2 ) );
00208   for ( k = 0;  k < 3;  ++k )  {
00209     ZMlogger().emit( NEWLINE1 );
00210     string testMesg = "Testing scheduled exception throwing behavior #";
00211 #ifndef DEFECT_NO_EXCEPTIONS
00212     try  {
00213 #endif
00214       ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
00215 #ifndef DEFECT_NO_EXCEPTIONS
00216     }
00217     catch ( ZMexception & e )  {
00218       std::cerr << "Caught: " << e.name() << "\n";
00219     }
00220 #endif
00221   }
00222 
00223 
00224   //ZMxColumn::logNMore(  4  );
00225 
00226 
00227   // ----------
00228   // Test exception history:
00229   // ----------
00230   ZMlogger().emit( NEWLINE2 );
00231   ZMlogger().emit( "---------- Test exception history:  ----------\n" );
00232 
00233   ZMlogger().emit( NEWLINE1 );
00234   ZMlogger().emit( string( "The following " )
00235                  + itos( ZMerrno.size() )
00236                  + " exceptions were recorded (most recent first):\n"
00237                  );
00238   for ( k = 0;  k <  ZMerrno.size();  ++k  )
00239     display( ZMerrno.get( k ) );
00240 
00241   const int NEWMAX = 4;
00242   ZMerrno.setMax( NEWMAX );
00243   ZMlogger().emit( NEWLINE1 );
00244   ZMlogger().emit( string("ZMerrno.setMax( ")
00245                  + itos( NEWMAX )
00246                  + " );"
00247                  );
00248 
00249   ZMlogger().emit( string( " we now have record of these " )
00250                  + itos(ZMerrno.size())
00251                  + " exceptions:\n"
00252                  );
00253   for ( k = 0;  k <  ZMerrno.size();  ++k  )
00254     display( ZMerrno.get( k ) );
00255 
00256   // ----------
00257   // Done, go home
00258   // ----------
00259   return 0;
00260 
00261 }  // main()

Generated on Thu Jul 1 22:02:31 2010 for CLHEP by  doxygen 1.4.7