2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.abi / cxa_vec.C
blob5370fb8981df03f1cc0387666fb034c043614652
1 // This test fails on VxWorks in kernel mode because it depends on the
2 // library version of "::operator new[]" calling the "::operator new"
3 // defined in this module.  This doesn't work because the library version
4 // of "::operator new[]" is built into the kernel itself; library relocations
5 // are resolved when the kernel is linked.
6 // { dg-do run { xfail { powerpc-ibm-aix* || vxworks_kernel } } }
7 // { dg-options "-flat_namespace" { target *-*-darwin[67]* } }
8 // Test __cxa_vec routines
9 // Copyright (C) 2000, 2005 Free Software Foundation, Inc.
10 // Contributed by Nathan Sidwell 7 Apr 2000 <nathan@nathan@codesourcery.com>
12 #if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100
13 #include <cxxabi.h>
14 #include <stdio.h>
15 #include <new>
16 #include <stdlib.h>
17 #include <setjmp.h>
19 static int ctor_count = 0;
20 static int dtor_count = 0;
21 static bool dtor_repeat = false;
23 // Allocate enough padding to hold an array cookie.
24 #ifdef __ARM_EABI__
25 #define padding 8
26 #else
27 #define padding (sizeof (std::size_t))
28 #endif
30 // our pseudo ctors and dtors
31 static abi::__cxa_cdtor_return_type ctor (void *x)
33   if (!ctor_count)
34     throw 1;
35   ctor_count--;
36 #ifdef __ARM_EABI__
37   return x;
38 #endif
41 static abi::__cxa_cdtor_return_type dtor (void *x)
43   if (!dtor_count)
44     {
45       if (!dtor_repeat)
46         dtor_count--;
47       throw 1;
48     }
49   dtor_count--;
50 #ifdef __ARM_EABI__
51   return x;
52 #endif
55 // track new and delete
56 static int blocks = 0;
57 void *operator new[] (std::size_t size) throw (std::bad_alloc)
59   void *ptr = malloc (size);
60   
61   if (!ptr)
62     throw std::bad_alloc ();
63   blocks++;
64   return ptr;
67 void operator delete[] (void *ptr) throw ()
69   if (ptr)
70     {
71       free (ptr);
72       blocks--;
73     }
75 static jmp_buf jump;
77 // allocate and delete an array with no problems
78 void test0 ()
80   static bool started = false;
81   
82   if (!started)
83     {
84       started = true;
85       std::set_terminate (test0);
86       
87       ctor_count = dtor_count = 5;
88       dtor_repeat = false;
89       blocks = 0;
90       
91       try
92         {
93           void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor);
94           abi::__cxa_vec_delete (ary, 1, padding, dtor);
95           if (ctor_count || dtor_count || blocks)
96             longjmp (jump, 1);
97         }
98       catch (...)
99         {
100           longjmp (jump, 2);
101         }
102     }
103   else
104     {
105       longjmp (jump, 3);
106     }
107   return;
110 // allocate and delete an array with exception on ctor
111 void test1 ()
113   static bool started = false;
114   
115   if (!started)
116     {
117       started = true;
118       std::set_terminate (test1);
119       
120       ctor_count = dtor_count = 5;
121       dtor_repeat = false;
122       blocks = 0;
123       
124       ctor_count = 4;
125       try
126         {
127           void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor);
128           longjmp (jump, 1);
129         }
130       catch (...)
131         {
132           // we expect to get here
133           if (ctor_count || dtor_count != 1 || blocks)
134             longjmp (jump, 2);
135         }
136     }
137   else
138     {
139       longjmp (jump, 3);
140     }
141   return;
144 // allocate and delete an array with exception on dtor
145 void test2 ()
147   static bool started = false;
148   
149   if (!started)
150     {
151       started = true;
152       std::set_terminate (test2);
153       ctor_count = dtor_count = 5;
154       dtor_repeat = false;
155       blocks = 0;
156       
157       dtor_count = 3;
158       try
159         {
160           void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor);
161           abi::__cxa_vec_delete (ary, 1, padding, dtor);
162           longjmp (jump, 1);
163         }
164       catch (...)
165         {
166           // we expect to get here
167           if (ctor_count || dtor_count != -2u || blocks)
168             longjmp (jump, 2);
169         }
170     }
171   else
172     {
173       longjmp (jump, 3);
174     }
175   return;
178 // allocate an array with double exception on dtor
179 void test3 ()
181   static bool started = false;
182   
183   if (!started)
184     {
185       started = true;
186       std::set_terminate (test3);
187       
188       ctor_count = dtor_count = 5;
189       dtor_repeat = false;
190       blocks = 0;
191   
192       dtor_count = 3;
193       dtor_repeat = true;
194       try
195         {
196           void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor);
197           abi::__cxa_vec_delete (ary, 1, padding, dtor);
198           longjmp (jump, 1);
199         }
200       catch (...)
201         {
202           // we do not expect to get here
203           longjmp (jump, 2);
204         }
205     }
206   else
207     {
208       // we expect to get here (via terminate)
209       if (ctor_count || dtor_count || blocks != 1)
210         longjmp (jump, 3);
211       longjmp (jump, -1);
212     }
213   return;
216 // allocate an array with exception on ctor and exception in cleanup
217 void test4 ()
219   static bool started = false;
220   
221   if (!started)
222     {
223       started = true;
224       std::set_terminate (test4);
225       
226       ctor_count = dtor_count = 5;
227       dtor_repeat = false;
228       blocks = 0;
229   
230       ctor_count = 3;
231       dtor_count = 2;
232       try
233         {
234           void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor);
235           longjmp (jump, 1);
236         }
237       catch (...)
238         {
239           // we do not expect to get here
240           longjmp (jump, 2);
241         }
242     }
243   else
244     {
245       // we expect to get here (via terminate)
246       if (ctor_count || dtor_count != -1u || blocks != 1)
247         longjmp (jump, 3);
248       longjmp (jump, -1);
249     }
250   return;
253 static void (*tests[])() =
255   test0,
256   test1,
257   test2,
258   test3,
259   test4,
260   NULL
263 int main ()
265   int ix;
266   int n;
267   int errors = 0;
268   
269   for (ix = 0; tests[ix]; ix++)
270     {
271       if (n = setjmp (jump))
272         {
273           if (n > 0)
274             {
275               printf ("test %d failed %d\n", ix, n);
276               errors++;
277             }
278         }
279       else
280         tests[ix] ();
281     }
282   return errors;
285 #else
286 int main ()
288   return 0;
290 #endif