1
/* $Id: exception.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* \page page_pjlib_exception_test Test: Exception Handling
26
* This file provides implementation of \b exception_test(). It tests the
27
* functionality of the exception handling API.
29
* @note This test use static ID not acquired through proper registration.
30
* This is not recommended, since it may create ID collissions.
32
* \section exception_test_sec Scope of the Test
34
* Some scenarios tested:
35
* - no exception situation
37
* - multiple exception handlers
41
* This file is <b>pjlib-test/exception.c</b>
43
* \include pjlib-test/exception.c
47
#if INCLUDE_EXCEPTION_TEST
54
static int throw_id_1(void)
57
PJ_UNREACHED(return -1;)
60
static int throw_id_2(void)
63
PJ_UNREACHED(return -1;)
66
static int try_catch_test(void)
81
static int throw_in_handler(void)
90
if (PJ_GET_EXCEPTION() != ID_1)
99
static int return_in_handler(void)
114
static int test(void)
120
* No exception situation.
140
// should not reach here.
144
int id = PJ_GET_EXCEPTION();
146
PJ_LOG(3,("", "...error: got unexpected exception %d (%s)",
147
id, pj_exception_id_name(id)));
157
* Multiple exceptions handlers
161
// should not reach here.
165
switch (PJ_GET_EXCEPTION()) {
167
if (!rc) rc = -30; break;
169
if (!rc) rc = 0; break;
181
* Test default handler.
185
// should not reach here
189
switch (PJ_GET_EXCEPTION()) {
207
rc = try_catch_test();
218
* Throwing exception inside handler
223
rc2 = throw_in_handler();
228
if (PJ_GET_EXCEPTION() == ID_2) {
241
* Return from handler. Returning from the function inside a handler
242
* should be okay (though returning from the function inside the
243
* PJ_TRY block IS NOT OKAY!!). We want to test to see if handler
244
* is cleaned up properly, but not sure how to do this.
248
rc2 = return_in_handler();
261
int exception_test(void)
266
for (i=0; i<LOOP; ++i) {
267
if ((rc=test()) != 0) {
268
PJ_LOG(3,("", "...failed at i=%d (rc=%d)", i, rc));
276
/* To prevent warning about "translation unit is empty"
277
* when this test is disabled.
279
int dummy_exception_test;
280
#endif /* INCLUDE_EXCEPTION_TEST */