File: testExceptions.cc

package info (click to toggle)
clhep 2.1.4.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,012 kB
  • sloc: cpp: 50,094; sh: 6,694; makefile: 2,694; perl: 28
file content (261 lines) | stat: -rwxr-xr-x 7,384 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// ----------------------------------------------------------------------
//
// testExceptions.cc - test the DEFECT_NO_EXCEPTIONS version of the Exceptions package
//
// History:
//   19-Dec-1997  WEB  Initial draft; redefining exit() based on an idea
//     by Philippe Canal
//   04-Mar-1998  WEB  Minor grammar update
//   15-Jun-1998  WEB  Added namespace support
//   26-Jun-2001  MF   Tested ctor of ZMexception from ostringstream
//   12-Dec-2001  WEB  Avoid signed/unsigned comparison warnings
//   12-Jun-2002  WEB  Rearrange to ensure correct ZMthrow definition
//
// ----------------------------------------------------------------------

#include <sstream>
#include <string>
  using std::string;

#include "CLHEP/Exceptions/defs.h"
#include "CLHEP/Cast/itos.h"
#include "CLHEP/Exceptions/ZMthrow.h"
#include "CLHEP/Exceptions/ZMexception.h"
#include "CLHEP/Exceptions/ZMerrno.h"


using namespace zmex;

// ----------
// In case our compilation environment does not support exceptions:
//
//   Since this program tests several cases, including several actual throws,
//   we assume that the DEFECT_NO_EXCEPTIONS version of the ZMthrow macro
//   will use abort() and so override abort()'s behavior in order to keep
//   going for multiple testing herein:  (earlier we used exit(), which we
//   also overide)
// ----------

#ifdef DEFECT_NO_EXCEPTIONS

#define exit( x )  \
  ZMlogger().emit( "Note:  exception would have been correctly thrown here\n" );

#define abort()  \
  ZMlogger().emit( "Note:  exception would have been correctly thrown here\n" );

#endif  // DEFECT_NO_EXCEPTIONS


// ----------
// Define exception classes and default behaviors:
// ----------

ZMexStandardDefinition( ZMexception, ZMxTest );
ZMexClassInfo ZMxTest::_classInfo(
  "ZMxTest", "Test", ZMexSEVERE );

ZMexStandardDefinition( ZMxTest, ZMxInfo );
ZMexClassInfo ZMxInfo::_classInfo(
  "ZMxInfo", "Test", ZMexINFO );

ZMexStandardDefinition( ZMxTest, ZMxGoof );
ZMexClassInfo ZMxGoof::_classInfo(
  "ZMxGoof", "Test", ZMexERROR );

ZMexStandardDefinition( ZMxGoof, ZMxOops );
ZMexClassInfo ZMxOops::_classInfo(
  "ZMxOops", "Test", ZMexWARNING );

ZMexStandardDefinition( ZMxGoof, ZMxBooBoo );
ZMexClassInfo ZMxBooBoo::_classInfo(
  "ZMxBooBoo", "Test", ZMexFATAL, ZMexIgnoreAlways() );


// ----------
// Define output formats, etc
// ----------

const string QUOTE    = "\"";
const string NEWLINE1 = "\n";
const string NEWLINE2 = "\n\n";

void display( const ZMexception * ex )  {

  ZMlogger().emit( NEWLINE1
		 + ex->name() + ": " + QUOTE + ex->message() + QUOTE + NEWLINE1
		 + "  " + (ex->wasThrown() ? "thrown" : "ignored")
		 + " by " + ex->handlerUsed() + "()" + NEWLINE1
		 );

}


int main()  {
  unsigned int k;

  // ----------
  // Begin testing, check out basic logger:
  // ----------
  ZMlogger().emit( NEWLINE1 );
  ZMlogger().emit( "----------  Begin testing:  ----------\n" );
  ZMlogger().emit( NEWLINE1 );
  ZMlogger().emit( "This message checks out basic logger behavior\n" );


  // ----------
  // Test default informational behavior:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
    "----------  Testing default informational behavior:  ----------\n" );
  ZMlogger().emit( NEWLINE1 );
  ZMthrow( ZMxInfo( "Testing default informational exception behavior" ) );

  // ----------
  // Test default informational behavior using an ostringstream
  // ----------

  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
  "----------  Testing default ostringstream behavior 3 times:  ----------\n" );
  std::ostringstream oss;
  oss << "Accumulated numbers are ";
  for ( k = 0;  k < 3;  ++k )  {
    ZMlogger().emit( NEWLINE1 );
    if ( k > 0 )
      oss << ", ";
    oss << k;
    ZMthrow( ZMxOops( oss ) );
  }

  // ----------
  // Test default ignoring behavior 3 times:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
    "----------  Testing default ignoring behavior 3 times:  ----------\n" );
  for ( k = 0;  k < 3;  ++k )  {
    ZMlogger().emit( NEWLINE1 );
    string testMesg = "Testing default exception ignoring behavior #";
    ZMthrow( ZMxOops( testMesg + itos(k+1) ) );
  }


  // ----------
  // Test defined ignoring behavior 3 times:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
    "----------  Testing defined ignoring behavior 3 times:  ----------\n" );
  for ( k = 0;  k < 3;  ++k )  {
    ZMlogger().emit( NEWLINE1 );
    string testMesg = "Testing defined exception ignoring behavior #";
    ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
  }

  // ----------
  // Test default throwing behavior 3 times:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
    "----------  Testing default throwing behavior 3 times:  ----------\n" );
  for ( k = 0;  k < 3;  ++k )  {
    ZMlogger().emit( NEWLINE1 );
    string testMesg = "Testing default exception throwing behavior #";
#ifndef DEFECT_NO_EXCEPTIONS
    try  {
#endif
      ZMthrow( ZMxGoof( testMesg + itos(k+1) ) );
#ifndef DEFECT_NO_EXCEPTIONS
    }
    catch ( ZMexception & e )  {
      std::cerr << "Caught: " << e.name() << "\n";
    }
#endif
  }


  // ----------
  // Test forced throwing behavior 3 times:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
    "----------  Testing forced throwing behavior 3 times:  ----------\n" );
  ZMxBooBoo::setHandler( ZMexThrowAlways() );
  for ( k = 0;  k < 3;  ++k )  {
    ZMlogger().emit( NEWLINE1 );
    string testMesg = "Testing forced exception throwing behavior #";
#ifndef DEFECT_NO_EXCEPTIONS
    try  {
#endif
      ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
#ifndef DEFECT_NO_EXCEPTIONS
    }
    catch ( ZMexception & e )  {
      std::cerr << "Caught: " << e.name() << "\n";
    }
#endif
  }

  // ----------
  // Test scheduled throwing behavior 3 times:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit(
    "----------  Testing scheduled throwing behavior 3 times:  ----------\n" );
  ZMxBooBoo::setHandler( ZMexIgnoreNextN( 2 ) );
  for ( k = 0;  k < 3;  ++k )  {
    ZMlogger().emit( NEWLINE1 );
    string testMesg = "Testing scheduled exception throwing behavior #";
#ifndef DEFECT_NO_EXCEPTIONS
    try  {
#endif
      ZMthrow( ZMxBooBoo( testMesg + itos(k+1) ) );
#ifndef DEFECT_NO_EXCEPTIONS
    }
    catch ( ZMexception & e )  {
      std::cerr << "Caught: " << e.name() << "\n";
    }
#endif
  }


  //ZMxColumn::logNMore(  4  );


  // ----------
  // Test exception history:
  // ----------
  ZMlogger().emit( NEWLINE2 );
  ZMlogger().emit( "---------- Test exception history:  ----------\n" );

  ZMlogger().emit( NEWLINE1 );
  ZMlogger().emit( string( "The following " )
                 + itos( ZMerrno.size() )
                 + " exceptions were recorded (most recent first):\n"
                 );
  for ( k = 0;  k <  ZMerrno.size();  ++k  )
    display( ZMerrno.get( k ) );

  const int NEWMAX = 4;
  ZMerrno.setMax( NEWMAX );
  ZMlogger().emit( NEWLINE1 );
  ZMlogger().emit( string("ZMerrno.setMax( ")
                 + itos( NEWMAX )
                 + " );"
                 );

  ZMlogger().emit( string( " we now have record of these " )
                 + itos(ZMerrno.size())
                 + " exceptions:\n"
                 );
  for ( k = 0;  k <  ZMerrno.size();  ++k  )
    display( ZMerrno.get( k ) );

  // ----------
  // Done, go home
  // ----------
  return 0;

}  // main()