1 // Test pointer chain catching
2 // Copyright (C) 2000 Free Software Foundation, Inc.
3 // Contributed by Nathan Sidwell 9 Apr 2000 <nathan@nathan@codesourcery.com>
4 
5 #include <stdio.h>
6 
fn()7 void fn () {};
fnA8 struct A {void fn () {}};
9 static int var = 1;
10 static const int const_var = 2;
11 
12 struct B;
13 struct C;
14 
test0()15 int test0 ()
16 {
17   try
18     {
19       throw &fn;
20     }
21   catch (void *)
22     {
23       // should not decay to void *
24       return 1;
25     }
26   catch (...)
27     {
28       return 0;
29     }
30   return -1;
31 }
32 
test1()33 int test1 ()
34 {
35   try
36     {
37       throw &A::fn;
38     }
39   catch (void *)
40     {
41       // should not decay to void *
42       return 1;
43     }
44   catch (...)
45     {
46       return 0;
47     }
48   return -1;
49 }
50 
test2()51 int test2 ()
52 {
53   try
54     {
55       throw &var;
56     }
57   catch (void *)
58     {
59       // should decay to void *
60       return 0;
61     }
62   catch (...)
63     {
64       return 1;
65     }
66   return -1;
67 }
68 
test3()69 int test3 ()
70 {
71   try
72     {
73       throw &var;
74     }
75   catch (void const *)
76     {
77       // should decay to const void *
78       return 0;
79     }
80   catch (...)
81     {
82       return 1;
83     }
84   return -1;
85 }
86 
test4()87 int test4 ()
88 {
89   try
90     {
91       throw &const_var;
92     }
93   catch (void *)
94     {
95       // should not decay to void *
96       return 1;
97     }
98   catch (void const *)
99     {
100       // should decay to const void *
101       return 0;
102     }
103   catch (...)
104     {
105       return 2;
106     }
107   return -1;
108 }
109 
test5()110 int test5 ()
111 {
112   try
113     {
114       throw (void ***)0;
115     }
116   catch (void ***)
117     {
118       return 0;
119     }
120   catch (...)
121     {
122       return 1;
123     }
124   return -1;
125 }
126 
test6()127 int test6 ()
128 {
129   try
130     {
131       throw (void const* const* const*)0;
132     }
133   catch (void ***)
134     {
135       return 1;
136     }
137   catch (void * const* const*)
138     {
139       return 2;
140     }
141   catch (void const* * const*)
142     {
143       return 3;
144     }
145   catch (void const* const* *)
146     {
147       return 4;
148     }
149   catch (void const* const* const *)
150     {
151       return 0;
152     }
153   catch (...)
154     {
155       return 1;
156     }
157   return -1;
158 }
159 
test7()160 int test7 ()
161 {
162   try
163     {
164       throw (void ***)0;
165     }
166   catch (void const* const**)
167     {
168       return 1;
169     }
170   catch (void const** const *)
171     {
172       return 2;
173     }
174   catch (void * const* const *)
175     {
176       return 0;
177     }
178   catch (...)
179     {
180       return 3;
181     }
182   return -1;
183 }
184 
test8()185 int test8 ()
186 {
187   try
188     {
189       throw (B **)0;
190     }
191   catch (C **)
192     {
193       return 1;
194     }
195   catch (B **)
196     {
197       return 0;
198     }
199   catch (...)
200     {
201       return 2;
202     }
203   return -1;
204 }
205 
test9()206 int test9 ()
207 {
208   try
209     {
210       throw (B **)0;
211     }
212   catch (C const *const *)
213     {
214       return 1;
215     }
216   catch (B const *const *)
217     {
218       return 0;
219     }
220   catch (...)
221     {
222       return 2;
223     }
224   return -1;
225 }
226 
227 static int (*tests[])() =
228 {
229   test0,
230   test1,
231   test2,
232   test3,
233   test4,
234 
235   test5,
236   test6,
237   test7,
238 
239   test8,
240   test9,
241 
242   NULL
243 };
244 
main()245 int main ()
246 {
247   int ix;
248   int errors = 0;
249 
250   for (ix = 0; tests[ix]; ix++)
251     {
252       int n = tests[ix] ();
253 
254       if (n)
255         {
256           printf ("test %d failed %d\n", ix, n);
257           errors++;
258         }
259     }
260   return errors;
261 }
262