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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// $Id$
/**
* @file
*
* This program checks if the compiler / platform supports exceptions,
* in particular, if raising exceptions in constructors work. The
* motivation for this test was a discussion on the development
* mailing list, and the documentation was captured in:
*
* http://bugzilla.dre.vanderbilt.edu/show_bug.cgi?id=3715
*
*/
#include "test_config.h"
// The first part of the test is to compile this line. If the program
// does not compile the platform is just too broken.
#include <memory>
// ... using exceptions with C++ also implies using the standard
// exceptions ...
#include <stdexcept>
// For extra challenge, we use the anonymous namespace
namespace
{
int constructors = 0;
int destructors = 0;
int allocs = 0;
int deallocs = 0;
bool had_failure = false;
void check_constructor_count(int expected,
char const * filename,
int lineno);
void check_destructor_count(int expected,
char const * filename,
int lineno);
void check_alloc_count(int expected,
char const * filename,
int lineno);
void reset_counts();
void never_reached(char const * filename,
int lineno);
int status();
/**
* @class Base
*/
class Base
{
public:
Base()
{
++constructors;
}
Base(Base const & )
{
++constructors;
}
~Base()
{
++destructors;
}
void * operator new(size_t n)
{
++allocs;
return ::operator new(n);
}
void operator delete(void * x)
{
++deallocs;
return ::operator delete(x);
}
};
/**
* @class May_Pop
*
* Create a class that can raise exceptions in its constructor
*
*/
class May_Pop : public Base
{
public:
explicit May_Pop(bool do_raise)
// Even if an exception is raised, the base object is fully
// constructed and must be fully destructed ...
: Base()
{
if (do_raise)
{
throw std::runtime_error("requested exception");
}
// ... if an exception is raised this object is never
// initialized and no constructor / destructor calls should take
// place ...
Base tmp;
}
};
/**
* @class Aggregate
*/
class Aggregate : public Base
{
private:
May_Pop m1;
May_Pop m2;
May_Pop m3;
public:
/**
* Constructor. Element m1 is fully initialized, its constructors
* and destructors should run, m2 is partially initialized, its
* destructor never runs, m3 is never initialized, neither its
* constructor nor destructor runs.
*/
Aggregate()
: Base()
, m1(false)
, m2(true)
, m3(false)
{}
};
}
ACE_RCSID(tests, Compiler_Features_10_Test, "$Id$")
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT("Compiler_Features_10_Test"));
{
// ... start the test fresh ...
reset_counts();
// Let's try to build a simple object and destroy it, without any
// exceptions raised ...
May_Pop m1(false);
// ... two instances of the base class should be created ...
check_constructor_count(2, __FILE__, __LINE__);
// ... but only one instance is destroyed at this point ...
check_destructor_count(1, __FILE__, __LINE__);
}
// ... now both instances are gone ...
check_destructor_count(2, __FILE__, __LINE__);
try
{
// ... start the test fresh ...
reset_counts();
// ... now raise an exception ...
May_Pop m1(true);
never_reached(__FILE__, __LINE__);
}
catch(...)
{
// ... only one instance gets created ...
check_constructor_count(1, __FILE__, __LINE__);
// ... and it is gone ...
check_destructor_count(1, __FILE__, __LINE__);
}
try
{
// ... start the test fresh ...
reset_counts();
// ... now build a complex object with a failure in the middle ...
Aggregate a;
never_reached(__FILE__, __LINE__);
}
catch(...)
{
// ... check the expectations ...
check_constructor_count(4, __FILE__, __LINE__);
check_destructor_count(4, __FILE__, __LINE__);
}
try
{
// ... start the test fresh ...
reset_counts();
std::auto_ptr<Aggregate> b(new Aggregate);
never_reached(__FILE__, __LINE__);
}
catch(...)
{
// ... check the expectations ...
check_constructor_count(4, __FILE__, __LINE__);
check_destructor_count(4, __FILE__, __LINE__);
check_alloc_count(1, __FILE__, __LINE__);
}
ACE_END_TEST;
return status();
}
namespace
{
void
check_constructor_count(int expected,
char const * filename,
int lineno)
{
if (constructors == expected)
{
return;
}
had_failure = true;
ACE_ERROR ((LM_ERROR,
ACE_TEXT("Expected %d constructor calls, had %d -- (%s:%d)\n"),
expected, constructors, filename, lineno));
}
void
check_destructor_count(int expected,
char const * filename,
int lineno)
{
if (destructors == expected)
{
return;
}
had_failure = true;
ACE_ERROR ((LM_ERROR,
ACE_TEXT("Expected %d destructor calls, had %d -- (%s:%d)\n"),
expected, destructors, filename, lineno));
}
void
check_alloc_count(int expected,
char const * filename,
int lineno)
{
if (allocs == expected && deallocs == expected)
{
return;
}
had_failure = true;
if (allocs != expected)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT("Expected %d alloc calls, had %d -- (%s:%d)\n"),
expected, allocs, filename, lineno));
}
if (deallocs != expected)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT("Expected %d dealloc calls, had %d -- (%s:%d)\n"),
expected, deallocs, filename, lineno));
}
}
void
never_reached(char const * filename,
int lineno)
{
had_failure = true;
ACE_ERROR ((LM_ERROR,
ACE_TEXT("Code should not have reached (%s:%d)\n"),
filename, lineno));
}
void
reset_counts()
{
constructors = 0;
destructors = 0;
allocs = 0;
deallocs = 0;
}
int
status()
{
if (had_failure)
{
return 1;
}
return 0;
}
}
|