FSF GCC merge 02/23/03
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.abi / cxa_vec.C
blob05c3fcc6e43d0ad74d83a01a46daac2bde06174b
1 // Test __cxa_vec routines
2 // Copyright (C) 2000 Free Software Foundation, Inc.
3 // Contributed by Nathan Sidwell 7 Apr 2000 <nathan@nathan@codesourcery.com>
5 #if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100
6 #include <cxxabi.h>
7 #include <stdio.h>
8 #include <new>
9 #include <stdlib.h>
10 #include <setjmp.h>
12 static int ctor_count = 0;
13 static int dtor_count = 0;
14 static bool dtor_repeat = false;
16 // our pseudo ctors and dtors
17 static void ctor (void *)
19   if (!ctor_count)
20     throw 1;
21   ctor_count--;
24 static void dtor (void *)
26   if (!dtor_count)
27     {
28       if (!dtor_repeat)
29         dtor_count--;
30       throw 1;
31     }
32   dtor_count--;
35 // track new and delete
36 static int blocks = 0;
37 void *operator new[] (std::size_t size) throw (std::bad_alloc)
39   void *ptr = malloc (size);
40   
41   if (!ptr)
42     throw std::bad_alloc ();
43   blocks++;
44   return ptr;
47 void operator delete[] (void *ptr) throw ()
49   if (ptr)
50     {
51       free (ptr);
52       blocks--;
53     }
55 static jmp_buf jump;
57 // allocate and delete an array with no problems
58 void test0 ()
60   static bool started = false;
61   
62   if (!started)
63     {
64       started = true;
65       std::set_terminate (test0);
66       
67       ctor_count = dtor_count = 5;
68       dtor_repeat = false;
69       blocks = 0;
70       
71       try
72         {
73           void *ary = abi::__cxa_vec_new (5, 1, sizeof (std::size_t), ctor, dtor);
74           abi::__cxa_vec_delete (ary, 1, sizeof (std::size_t), dtor);
75           if (ctor_count || dtor_count || blocks)
76             longjmp (jump, 1);
77         }
78       catch (...)
79         {
80           longjmp (jump, 2);
81         }
82     }
83   else
84     {
85       longjmp (jump, 3);
86     }
87   return;
90 // allocate and delete an array with exception on ctor
91 void test1 ()
93   static bool started = false;
94   
95   if (!started)
96     {
97       started = true;
98       std::set_terminate (test1);
99       
100       ctor_count = dtor_count = 5;
101       dtor_repeat = false;
102       blocks = 0;
103       
104       ctor_count = 4;
105       try
106         {
107           void *ary = abi::__cxa_vec_new (5, 1, sizeof (std::size_t), ctor, dtor);
108           longjmp (jump, 1);
109         }
110       catch (...)
111         {
112           // we expect to get here
113           if (ctor_count || dtor_count != 1 || blocks)
114             longjmp (jump, 2);
115         }
116     }
117   else
118     {
119       longjmp (jump, 3);
120     }
121   return;
124 // allocate and delete an array with exception on dtor
125 void test2 ()
127   static bool started = false;
128   
129   if (!started)
130     {
131       started = true;
132       std::set_terminate (test2);
133       ctor_count = dtor_count = 5;
134       dtor_repeat = false;
135       blocks = 0;
136       
137       dtor_count = 3;
138       try
139         {
140           void *ary = abi::__cxa_vec_new (5, 1, sizeof (std::size_t), ctor, dtor);
141           abi::__cxa_vec_delete (ary, 1, sizeof (std::size_t), dtor);
142           longjmp (jump, 1);
143         }
144       catch (...)
145         {
146           // we expect to get here
147           if (ctor_count || dtor_count != -2u || blocks)
148             longjmp (jump, 2);
149         }
150     }
151   else
152     {
153       longjmp (jump, 3);
154     }
155   return;
158 // allocate an array with double exception on dtor
159 void test3 ()
161   static bool started = false;
162   
163   if (!started)
164     {
165       started = true;
166       std::set_terminate (test3);
167       
168       ctor_count = dtor_count = 5;
169       dtor_repeat = false;
170       blocks = 0;
171   
172       dtor_count = 3;
173       dtor_repeat = true;
174       try
175         {
176           void *ary = abi::__cxa_vec_new (5, 1, sizeof (std::size_t), ctor, dtor);
177           abi::__cxa_vec_delete (ary, 1, sizeof (std::size_t), dtor);
178           longjmp (jump, 1);
179         }
180       catch (...)
181         {
182           // we do not expect to get here
183           longjmp (jump, 2);
184         }
185     }
186   else
187     {
188       // we expect to get here (via terminate)
189       if (ctor_count || dtor_count || blocks != 1)
190         longjmp (jump, 3);
191       longjmp (jump, -1);
192     }
193   return;
196 // allocate an array with exception on ctor and exception in cleanup
197 void test4 ()
199   static bool started = false;
200   
201   if (!started)
202     {
203       started = true;
204       std::set_terminate (test4);
205       
206       ctor_count = dtor_count = 5;
207       dtor_repeat = false;
208       blocks = 0;
209   
210       ctor_count = 3;
211       dtor_count = 2;
212       try
213         {
214           void *ary = abi::__cxa_vec_new (5, 1, sizeof (std::size_t), ctor, dtor);
215           longjmp (jump, 1);
216         }
217       catch (...)
218         {
219           // we do not expect to get here
220           longjmp (jump, 2);
221         }
222     }
223   else
224     {
225       // we expect to get here (via terminate)
226       if (ctor_count || dtor_count != -1u || blocks != 1)
227         longjmp (jump, 3);
228       longjmp (jump, -1);
229     }
230   return;
233 static void (*tests[])() =
235   test0,
236   test1,
237   test2,
238   test3,
239   test4,
240   NULL
243 int main ()
245   int ix;
246   int n;
247   int errors = 0;
248   
249   for (ix = 0; tests[ix]; ix++)
250     {
251       if (n = setjmp (jump))
252         {
253           if (n > 0)
254             {
255               printf ("test %d failed %d\n", ix, n);
256               errors++;
257             }
258         }
259       else
260         tests[ix] ();
261     }
262   return errors;
265 #else
266 int main ()
268   return 0;
270 #endif