Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
BAD_ALLOC_ASSIGNMENT.cpp
Go to the documentation of this file.
1// checking for bad_alloc assignment operator
2
3
25// NOTE: this file is #included from the following tests,
26// each of which #defines bad_alloc to the name of
27// the corresponding exception class, and exercises
28// the respective class members
29//
30// BAD_ALLOC_COPY_CTOR.cpp
31// BAD_ALLOC_DEFAULT_CTOR.cpp
32// BAD_ALLOC_DTOR.cpp
33
34// BAD_CAST_ASSIGNMENT.cpp
35// BAD_CAST_COPY_CTOR.cpp
36// BAD_CAST_DEFAULT_CTOR.cpp
37// BAD_CAST_DTOR.cpp
38
39// BAD_EXCEPTION_ASSIGNMENT.cpp
40// BAD_EXCEPTION_COPY_CTOR.cpp
41// BAD_EXCEPTION_DEFAULT_CTOR.cpp
42// BAD_EXCEPTION_DTOR.cpp
43
44// BAD_TYPEID_ASSIGNMENT.cpp
45// BAD_TYPEID_COPY_CTOR.cpp
46// BAD_TYPEID_DEFAULT_CTOR.cpp
47// BAD_TYPEID_DTOR.cpp
48
49// EXCEPTION_ASSIGNMENT.cpp
50// EXCEPTION_COPY_CTOR.cpp
51// EXCEPTION_DEFAULT_CTOR.cpp
52// EXCEPTION_DTOR.cpp
53
54#include "config.h"
55
56
57#if 2 == __GNUG__
58# ifndef _RWSTD_NO_HONOR_STD
59# ifdef _RWSTD_NO_STD_TERMINATE
60# include "terminate.h"
61# endif // _RWSTD_NO_STD_TERMINATE
62# endif // _RWSTD_NO_HONOR_STD
63#endif // gcc 2.x
64
65
66#ifndef _RWSTD_NO_RUNTIME_IN_STD
67# define NAMESPACE(name) namespace name
68#else
69# define NAMESPACE(ignore) extern "C++"
70# define std /* empty */
71#endif // _RWSTD_NO_RUNTIME_IN_STD
72
73
78
79
81
82class bad_alloc
83{
84public:
85 const void *what_;
86
87#if defined (TEST_DEFAULT_CTOR)
88
89 bad_alloc ();
90
91#else // if !defined (TEST_DEFAULT_CTOR)
92
93 // provide a default ctor but with a different signature
94 // than the one above to prevent multiply defined symbol
95 // linker errors in case that one is defined in the
96 // runtime library
97 bad_alloc (int = 0) { }
98
99# if defined (TEST_DTOR)
100
101 virtual ~bad_alloc ();
102
103# elif defined (TEST_COPY_CTOR)
104
105 bad_alloc (const bad_alloc&);
106
107# elif defined (TEST_WHAT)
108
109 virtual const char* what () const;
110
111# else // test assignment
112
113# if !defined (TEST_ASSIGNMENT)
114# define TEST_ASSIGNMENT
115# endif
116
117 bad_alloc& operator= (const bad_alloc&);
118
119 bad_alloc& assign (const bad_alloc &rhs) {
120 ++nassignments;
121 return *this = rhs;
122 }
123
124# endif // TEST_...
125#endif // !TEST_DEFAULT_CTOR
126
127};
128
129
130} // namespace std
131
132
133// use virtual inheritance here to prevent aggressive optimizers
134// (like VisualAge C++) from optimizing the base class dtor away
135struct Base0: virtual std::bad_alloc
136{
138
140
141 Base0 ();
142
143 Base0 (const Base0&);
144
145 virtual ~Base0 ();
146
147 Base0& operator= (const Base0&);
148
149 virtual const char* what () const;
150};
151
152
153struct Base1: virtual std::bad_alloc
154{
156
158
159 Base1 ();
160
161 Base1 (const Base1&);
162
163 virtual ~Base1 ();
164
165 Base1& operator= (const Base1&);
166
167 virtual const char* what () const;
168};
169
170
172
174{
176
177 Derived ();
178
179 Derived (const Derived&);
180
181 virtual ~Derived ();
182
183 Derived& operator= (const Derived&);
184
185 virtual const char* what () const;
186};
187
188
189#if defined (TEST_COPY_CTOR)
190
191int test_member (int argc, void *ptr)
192{
193 // the code below must not be executed
194
195# ifndef _RWSTD_NO_EXCEPTIONS
196
197 try {
198 try {
199 if (1 < argc) {
200 // jump through hoops to try to foil
201 // even the most aggressive optimizers
202 Derived *pd = (Derived*)ptr;
203 Base0 *pb0 = (Derived*)ptr + argc;
204 Base1 *pb1 = (Derived*)ptr + argc + 1;
205 std::bad_alloc *pba = (Derived*)ptr + argc + 2;
206
207 if (argc < 6)
208 throw *pd;
209 if (argc < 7)
210 throw *pb0;
211 if (argc < 8)
212 throw *pb1;
213 if (argc < 9)
214 throw *pba;
215 }
216 }
217 catch (Derived e) {
218 e.index0 = e.index1 = __LINE__;
219 throw e;
220 }
221 catch (Base0 b0) {
222 b0.index0 = __LINE__;
223 throw b0;
224 }
225 catch (Base1 b1) {
226 b1.index1 = __LINE__;
227 throw b1;
228 }
229 catch (std::bad_alloc e) {
230
231 std::bad_alloc copy (e);
232
233 if (argc < 5)
234 throw copy;
235
236 if (argc < 4)
237 throw e;
238
239 if (argc < 3)
240 throw;
241 }
242 }
243 catch (std::bad_alloc e) {
244 return 0 != e.what_;
245 }
246 catch (...) {
247 return 2;
248 }
249
250 return 3;
251
252# else // if defined (_RWSTD_NO_EXCEPTIONS)
253
255 std::bad_alloc *pf = new std::bad_alloc (*pe);
256
257 return !(1 == ndefault_ctors && 1 == ncopy_ctors && pe->what_ == pf->what_);
258
259# endif // _RWSTD_NO_EXCEPTIONS
260
261}
262
263#elif defined (TEST_DEFAULT_CTOR)
264
265int test_member (int, void*)
266{
267 // the code below must not be executed
268
270 std::bad_alloc *pe2 = new Derived;
271
272 return pe1 < pe2;
273}
274
275#elif defined (TEST_DTOR)
276
277int test_member (int argc, void *ptr)
278{
279 // the code below must not be executed
280
281 std::bad_alloc *pe;
282
283 if (1 < argc)
284 pe = (Derived*)ptr;
285 else
286 pe = (std::bad_alloc*)ptr;
287
288 delete pe;
289
290 return !(1 < argc ? 1 == ndtors : 0 == ndtors);
291}
292
293#elif defined (TEST_WHAT)
294
295int test_member (int argc, void *ptr)
296{
297 // the code below must not be executed
298
300 Derived *pd = (Derived*)ptr + 1;
301
302 which_what = argc;
303
304 return pe [0].what () == pd [argc].what ();
305}
306
307#else // test assignment /////////////////////////////////////////
308
309int test_member (int argc, void *ptr)
310{
311 // the code below must not be executed
312
313 std::bad_alloc *pe = (Derived*)ptr;
314
316 argc < 3 ? &std::bad_alloc::operator= : &std::bad_alloc::assign;
317
318 which_what = argc;
319
320 pe [argc] = pe [argc + 1];
321 (pe [argc + 1].*pf)(pe [argc + 2]);
322
323 return pe [0].what_ == pe [1].what_ || 2 < nassignments;
324}
325
326
327#endif // TEST_... ///////////////////////////////////////////////
328
329
330int main (int argc, char *argv[])
331{
332 (void)&argv;
333 (void)&nassignments;
334 (void)&ncopy_ctors;
335 (void)&ndefault_ctors;
336
337 // avoid executing the body of main unless explicitly requested
338 // by specifying at least one command line argument (this foils
339 // aggressive optimizers from eliminating the code)
340 if (argc < 2)
341 return 0;
342
343 return test_member (argc, argv);
344}
345
346
347Base0::Base0 (): Base (), index0 (++ndefault_ctors) { }
348
349
351
352#if defined (TEST_COPY_CTOR)
353 : Base (rhs),
354#else // if defined (TEST_COPY_CTOR)
355 : Base (),
356#endif // TEST_COPY_CTOR
357 index0 (rhs.index0)
358{
359 (void)&rhs; // silence unused argument warnings
360
361 ++ncopy_ctors;
362}
363
364
366{
367 ++ndtors;
368 index0 = 0;
369}
370
371
373{
374 ++nassignments;
375 index0 = rhs.index0;
376
377#if defined (TEST_ASSIGNMENT)
378
379 Base::operator= (rhs);
380
381#endif // TEST_ASSIGNMENT
382
383 return *this;
384}
385
386
387const char* Base0::what () const
388{
389#if defined (TEST_WHAT)
390
391 if (1 < which_what++)
392 return Base::what ();
393
394#endif // TEST_WHAT
395
396 return "Base0";
397
398
399}
400
401
402Base1::Base1 (): Base (), index1 (++ndefault_ctors) { }
403
404
406
407#if defined (TEST_COPY_CTOR)
408 : Base (rhs),
409#else // if defined (TEST_COPY_CTOR)
410 : Base (),
411#endif // TEST_COPY_CTOR
412 index1 (rhs.index1)
413{
414 (void)&rhs; // silence unused argument warnings
415
416 ++ncopy_ctors;
417}
418
419
421{
422 ++ndtors;
423 index1 = 0;
424}
425
426
427const char* Base1::what () const
428{
429#if defined (TEST_WHAT)
430
431 if (2 < which_what++)
432 return Base::what ();
433
434#endif // TEST_WHAT
435
436 return "Base1";
437}
438
439
441{
442 ++nassignments;
443 index1 = rhs.index1;
444
445#if defined (TEST_ASSIGNMENT)
446
447 Base::operator= (rhs);
448
449#endif // TEST_ASSIGNMENT
450
451 return *this;
452}
453
454
456 : Base (), Base0 (), Base1 ()
457{
459}
460
461
463
464#if defined (TEST_COPY_CTOR)
465 : Base (rhs), Base0 (rhs), Base1 (rhs)
466#else // if defined (TEST_COPY_CTOR)
467 : Base (), Base0 (), Base1 ()
468#endif // TEST_COPY_CTOR
469
470{
471 (void)&rhs; // silence unused argument warnings
472
473 ++ncopy_ctors;
474}
475
476
478{
479 ++ndtors;
480}
481
482
484{
485 ++nassignments;
486
489
490 return *this;
491}
492
493
494const char* Derived::what () const
495{
496 if (3 < which_what++)
497 return Base1::what ();
498
499 return Base0::what ();
500}
#define rhs
Definition 17.names.cpp:536
#define e
Definition 17.names.cpp:118
#define b1
Definition 17.names.cpp:226
#define ptr
Definition 17.names.cpp:525
#define pf
Definition 17.names.cpp:516
int test_member(int argc, void *ptr)
int ncopy_ctors
int nassignments
int ndefault_ctors
#define NAMESPACE(name)
#define main
#define bad_alloc
volatile double * pd
Definition json.h:5363
virtual const char * what() const
Base0 & operator=(const Base0 &)
std::bad_alloc Base
virtual const char * what() const
Base1 & operator=(const Base1 &)
std::bad_alloc Base
std::bad_alloc Base
Derived & operator=(const Derived &)
virtual const char * what() const