1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
16 *     contributors may be used to endorse or promote products derived from
17 *     this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Portions of this software have been released under the following terms:
31 *
32 * (c) Copyright 1989-1993 OPEN SOFTWARE FOUNDATION, INC.
33 * (c) Copyright 1989-1993 HEWLETT-PACKARD COMPANY
34 * (c) Copyright 1989-1993 DIGITAL EQUIPMENT CORPORATION
35 *
36 * To anyone who acknowledges that this file is provided "AS IS"
37 * without any express or implied warranty:
38 * permission to use, copy, modify, and distribute this file for any
39 * purpose is hereby granted without fee, provided that the above
40 * copyright notices and this notice appears in all source code copies,
41 * and that none of the names of Open Software Foundation, Inc., Hewlett-
42 * Packard Company or Digital Equipment Corporation be used
43 * in advertising or publicity pertaining to distribution of the software
44 * without specific, written prior permission.  Neither Open Software
45 * Foundation, Inc., Hewlett-Packard Company nor Digital
46 * Equipment Corporation makes any representations about the suitability
47 * of this software for any purpose.
48 *
49 * Copyright (c) 2007, Novell, Inc. All rights reserved.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 *
54 * 1.  Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 * 2.  Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in the
58 *     documentation and/or other materials provided with the distribution.
59 * 3.  Neither the name of Novell Inc. nor the names of its contributors
60 *     may be used to endorse or promote products derived from this
61 *     this software without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 *
74 * @APPLE_LICENSE_HEADER_END@
75 */
76
77#include <dce/dcethread.h>
78#include <moonunit/interface.h>
79
80static dcethread_exc dummy_e;
81static dcethread_exc dummy2_e;
82
83MU_FIXTURE_SETUP(exception)
84{
85    DCETHREAD_EXC_INIT(dummy_e);
86    DCETHREAD_EXC_INIT(dummy2_e);
87}
88
89MU_TEST(exception, nothrow)
90{
91    volatile int reached_finally = 0;
92
93    DCETHREAD_TRY
94    {
95	MU_ASSERT(!reached_finally);
96    }
97    DCETHREAD_CATCH(dummy_e)
98    {
99	MU_FAILURE("Reached catch block");
100    }
101    DCETHREAD_FINALLY
102    {
103	MU_ASSERT(!reached_finally);
104	reached_finally = 1;
105    }
106    DCETHREAD_ENDTRY;
107
108    MU_ASSERT(reached_finally);
109}
110
111MU_TEST(exception, throw_catch)
112{
113    volatile int reached_finally = 0;
114    volatile int caught = 0;
115
116    DCETHREAD_TRY
117    {
118	DCETHREAD_RAISE(dummy_e);
119    }
120    DCETHREAD_CATCH(dummy_e)
121    {
122	MU_ASSERT(!reached_finally);
123	caught = 1;
124    }
125    DCETHREAD_FINALLY
126    {
127	reached_finally = 1;
128    }
129    DCETHREAD_ENDTRY;
130
131    MU_ASSERT(caught);
132    MU_ASSERT(reached_finally);
133}
134
135MU_TEST(exception, throw_catch_throw_catch)
136{
137    volatile int caught_inner = 0;
138    volatile int caught_outer = 0;
139    volatile int reached_inner_finally = 0;
140    volatile int reached_outer_finally = 0;
141
142    DCETHREAD_TRY
143    {
144	DCETHREAD_TRY
145	{
146	    MU_ASSERT(!caught_inner);
147	    MU_ASSERT(!reached_inner_finally);
148	    MU_ASSERT(!caught_outer);
149	    MU_ASSERT(!reached_outer_finally);
150	    DCETHREAD_RAISE(dummy2_e);
151	}
152	DCETHREAD_CATCH(dummy2_e)
153	{
154	    MU_ASSERT(!caught_inner);
155	    MU_ASSERT(!reached_inner_finally);
156	    MU_ASSERT(!caught_outer);
157	    MU_ASSERT(!reached_outer_finally);
158	    caught_inner = 1;
159	    DCETHREAD_RAISE(dummy_e);
160	}
161	DCETHREAD_FINALLY
162	{
163	    MU_ASSERT(caught_inner);
164	    MU_ASSERT(!reached_inner_finally);
165	    MU_ASSERT(!caught_outer);
166	    MU_ASSERT(!reached_outer_finally);
167	    reached_inner_finally = 1;
168	}
169	DCETHREAD_ENDTRY;
170    }
171    DCETHREAD_CATCH(dummy_e)
172    {
173	MU_ASSERT(caught_inner);
174	MU_ASSERT(reached_inner_finally);
175	MU_ASSERT(!caught_outer);
176	MU_ASSERT(!reached_outer_finally);
177	caught_outer = 1;
178    }
179    DCETHREAD_FINALLY
180    {
181	MU_ASSERT(caught_inner);
182	MU_ASSERT(reached_inner_finally);
183	MU_ASSERT(caught_outer);
184	MU_ASSERT(!reached_outer_finally);
185	reached_outer_finally = 1;
186    }
187    DCETHREAD_ENDTRY;
188
189    MU_ASSERT(caught_inner);
190    MU_ASSERT(reached_inner_finally);
191    MU_ASSERT(caught_outer);
192    MU_ASSERT(reached_outer_finally);
193}
194
195MU_TEST(exception, throw_catch_finally_throw_catch)
196{
197    volatile int caught_inner = 0;
198    volatile int caught_outer = 0;
199    volatile int reached_inner_finally = 0;
200    volatile int reached_outer_finally = 0;
201
202    DCETHREAD_TRY
203    {
204	DCETHREAD_TRY
205	{
206	    MU_ASSERT(!caught_inner);
207	    MU_ASSERT(!reached_inner_finally);
208	    MU_ASSERT(!caught_outer);
209	    MU_ASSERT(!reached_outer_finally);
210	    DCETHREAD_RAISE(dummy2_e);
211	}
212	DCETHREAD_CATCH(dummy2_e)
213	{
214	    MU_ASSERT(!caught_inner);
215	    MU_ASSERT(!reached_inner_finally);
216	    MU_ASSERT(!caught_outer);
217	    MU_ASSERT(!reached_outer_finally);
218	    caught_inner = 1;
219	}
220	DCETHREAD_FINALLY
221	{
222	    MU_ASSERT(caught_inner);
223	    MU_ASSERT(!reached_inner_finally);
224	    MU_ASSERT(!caught_outer);
225	    MU_ASSERT(!reached_outer_finally);
226	    reached_inner_finally = 1;
227	    DCETHREAD_RAISE(dummy_e);
228	}
229	DCETHREAD_ENDTRY;
230    }
231    DCETHREAD_CATCH(dummy_e)
232    {
233	MU_ASSERT(caught_inner);
234	MU_ASSERT(reached_inner_finally);
235	MU_ASSERT(!caught_outer);
236	MU_ASSERT(!reached_outer_finally);
237	caught_outer = 1;
238    }
239    DCETHREAD_FINALLY
240    {
241	MU_ASSERT(caught_inner);
242	MU_ASSERT(reached_inner_finally);
243	MU_ASSERT(caught_outer);
244	MU_ASSERT(!reached_outer_finally);
245	reached_outer_finally = 1;
246    }
247    DCETHREAD_ENDTRY;
248
249    MU_ASSERT(caught_inner);
250    MU_ASSERT(reached_inner_finally);
251    MU_ASSERT(caught_outer);
252    MU_ASSERT(reached_outer_finally);
253}
254
255MU_TEST(exception, throw_finally_throw_catch)
256{
257    volatile int caught_outer = 0;
258    volatile int reached_inner_finally = 0;
259    volatile int reached_outer_finally = 0;
260
261    DCETHREAD_TRY
262    {
263	DCETHREAD_TRY
264	{
265	    MU_ASSERT(!reached_inner_finally);
266	    MU_ASSERT(!caught_outer);
267	    MU_ASSERT(!reached_outer_finally);
268	    DCETHREAD_RAISE(dummy2_e);
269	}
270	DCETHREAD_FINALLY
271	{
272	    MU_ASSERT(!reached_inner_finally);
273	    MU_ASSERT(!caught_outer);
274	    MU_ASSERT(!reached_outer_finally);
275	    reached_inner_finally = 1;
276	    DCETHREAD_RAISE(dummy_e);
277	}
278	DCETHREAD_ENDTRY;
279    }
280    DCETHREAD_CATCH(dummy_e)
281    {
282	MU_ASSERT(reached_inner_finally);
283	MU_ASSERT(!caught_outer);
284	MU_ASSERT(!reached_outer_finally);
285	caught_outer = 1;
286    }
287    DCETHREAD_FINALLY
288    {
289	MU_ASSERT(reached_inner_finally);
290	MU_ASSERT(caught_outer);
291	MU_ASSERT(!reached_outer_finally);
292	reached_outer_finally = 1;
293    }
294    DCETHREAD_ENDTRY;
295
296    MU_ASSERT(reached_inner_finally);
297    MU_ASSERT(caught_outer);
298    MU_ASSERT(reached_outer_finally);
299}
300
301MU_TEST(exception, uncaught)
302{
303    MU_EXPECT(MU_STATUS_EXCEPTION);
304
305    DCETHREAD_RAISE(dummy_e);
306}
307