Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/exception.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20
21
22 /**
23 * \page page_pjlib_exception_test Test: Exception Handling
24 *
25 * This file provides implementation of \b exception_test(). It tests the
26 * functionality of the exception handling API.
27 *
28 * @note This test use static ID not acquired through proper registration.
29 * This is not recommended, since it may create ID collissions.
30 *
31 * \section exception_test_sec Scope of the Test
32 *
33 * Some scenarios tested:
34 * - no exception situation
35 * - basic TRY/CATCH
36 * - multiple exception handlers
37 * - default handlers
38 *
39 *
40 * This file is <b>pjlib-test/exception.c</b>
41 *
42 * \include pjlib-test/exception.c
43 */
44
45
46 #if INCLUDE_EXCEPTION_TEST
47
48 #include <pjlib.h>
49
50 #ifdef _MSC_VER
51 #pragma warning(disable:4702) // warning C4702: unreachable code
52 #endif
53
54 #define ID_1 1
55 #define ID_2 2
56
57 static int throw_id_1(void)
58 {
59 PJ_THROW( ID_1 );
60 PJ_UNREACHED(return -1;)
61 }
62
63 static int throw_id_2(void)
64 {
65 PJ_THROW( ID_2 );
66 PJ_UNREACHED(return -1;)
67 }
68
69 static int try_catch_test(void)
70 {
71 PJ_USE_EXCEPTION;
72 int rc = -200;
73
74 PJ_TRY {
75 PJ_THROW(ID_1);
76 }
77 PJ_CATCH_ANY {
78 rc = 0;
79 }
80 PJ_END;
81 return rc;
82 }
83
84 static int throw_in_handler(void)
85 {
86 PJ_USE_EXCEPTION;
87 int rc = 0;
88
89 PJ_TRY {
90 PJ_THROW(ID_1);
91 }
92 PJ_CATCH_ANY {
93 if (PJ_GET_EXCEPTION() != ID_1)
94 rc = -300;
95 else
96 PJ_THROW(ID_2);
97 }
98 PJ_END;
99 return rc;
100 }
101
102 static int return_in_handler(void)
103 {
104 PJ_USE_EXCEPTION;
105
106 PJ_TRY {
107 PJ_THROW(ID_1);
108 }
109 PJ_CATCH_ANY {
110 return 0;
111 }
112 PJ_END;
113 return -400;
114 }
115
116
117 static int test(void)
118 {
119 int rc = 0;
120 PJ_USE_EXCEPTION;
121
122 /*
123 * No exception situation.
124 */
125 PJ_TRY {
126 PJ_UNUSED_ARG(rc);
127 }
128 PJ_CATCH_ANY {
129 rc = -3;
130 }
131 PJ_END;
132
133 if (rc != 0)
134 return rc;
135
136
137 /*
138 * Basic TRY/CATCH
139 */
140 PJ_TRY {
141 rc = throw_id_1();
142
143 // should not reach here.
144 rc = -10;
145 }
146 PJ_CATCH_ANY {
147 int id = PJ_GET_EXCEPTION();
148 if (id != ID_1) {
149 PJ_LOG(3,("", "...error: got unexpected exception %d (%s)",
150 id, pj_exception_id_name(id)));
151 if (!rc) rc = -20;
152 }
153 }
154 PJ_END;
155
156 if (rc != 0)
157 return rc;
158
159 /*
160 * Multiple exceptions handlers
161 */
162 PJ_TRY {
163 rc = throw_id_2();
164 // should not reach here.
165 rc = -25;
166 }
167 PJ_CATCH_ANY {
168 switch (PJ_GET_EXCEPTION()) {
169 case ID_1:
170 if (!rc) rc = -30;
171 break;
172 case ID_2:
173 if (!rc) rc = 0;
174 break;
175 default:
176 if (!rc) rc = -40;
177 break;
178 }
179 }
180 PJ_END;
181
182 if (rc != 0)
183 return rc;
184
185 /*
186 * Test default handler.
187 */
188 PJ_TRY {
189 rc = throw_id_1();
190 // should not reach here
191 rc = -50;
192 }
193 PJ_CATCH_ANY {
194 switch (PJ_GET_EXCEPTION()) {
195 case ID_1:
196 if (!rc) rc = 0;
197 break;
198 default:
199 if (!rc) rc = -60;
200 break;
201 }
202 }
203 PJ_END;
204
205 if (rc != 0)
206 return rc;
207
208 /*
209 * Nested handlers
210 */
211 PJ_TRY {
212 rc = try_catch_test();
213 }
214 PJ_CATCH_ANY {
215 rc = -70;
216 }
217 PJ_END;
218
219 if (rc != 0)
220 return rc;
221
222 /*
223 * Throwing exception inside handler
224 */
225 rc = -80;
226 PJ_TRY {
227 int rc2;
228 rc2 = throw_in_handler();
229 if (rc2)
230 rc = rc2;
231 }
232 PJ_CATCH_ANY {
233 if (PJ_GET_EXCEPTION() == ID_2) {
234 rc = 0;
235 } else {
236 rc = -90;
237 }
238 }
239 PJ_END;
240
241 if (rc != 0)
242 return rc;
243
244
245 /*
246 * Return from handler. Returning from the function inside a handler
247 * should be okay (though returning from the function inside the
248 * PJ_TRY block IS NOT OKAY!!). We want to test to see if handler
249 * is cleaned up properly, but not sure how to do this.
250 */
251 PJ_TRY {
252 int rc2;
253 rc2 = return_in_handler();
254 if (rc2)
255 rc = rc2;
256 }
257 PJ_CATCH_ANY {
258 rc = -100;
259 }
260 PJ_END;
261
262
263 return 0;
264 }
265
266 int exception_test(void)
267 {
268 int i, rc;
269 enum { LOOP = 10 };
270
271 for (i=0; i<LOOP; ++i) {
272 if ((rc=test()) != 0) {
273 PJ_LOG(3,("", "...failed at i=%d (rc=%d)", i, rc));
274 return rc;
275 }
276 }
277 return 0;
278 }
279
280 #else
281 /* To prevent warning about "translation unit is empty"
282 * when this test is disabled.
283 */
284 int dummy_exception_test;
285 #endif /* INCLUDE_EXCEPTION_TEST */
286
287