blob: 5e9ef2b6afe5663a03910ad9392b5939311617a4 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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#define ID_1 1
51#define ID_2 2
52
53static int throw_id_1(void)
54{
55 PJ_THROW( ID_1 );
56 return -1;
57}
58
59static int throw_id_2(void)
60{
61 PJ_THROW( ID_2 );
62 return -1;
63}
64
65
66static int test(void)
67{
Benny Prijono5dcb38d2005-11-21 01:55:47 +000068 int rc = 0;
Benny Prijonod0d44f52005-11-21 16:57:02 +000069 PJ_USE_EXCEPTION;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000070
71 /*
72 * No exception situation.
73 */
74 PJ_TRY {
75 rc = rc;
76 }
Benny Prijonod0d44f52005-11-21 16:57:02 +000077 PJ_CATCH_ANY {
Benny Prijono5dcb38d2005-11-21 01:55:47 +000078 rc = -3;
79 }
80 PJ_END;
81
82 if (rc != 0)
83 return rc;
84
85
86 /*
87 * Basic TRY/CATCH
88 */
89 PJ_TRY {
90 rc = throw_id_1();
91
92 // should not reach here.
93 rc = -10;
94 }
Benny Prijonod0d44f52005-11-21 16:57:02 +000095 PJ_CATCH_ANY {
Benny Prijono5dcb38d2005-11-21 01:55:47 +000096 int id = PJ_GET_EXCEPTION();
Benny Prijonod0d44f52005-11-21 16:57:02 +000097 if (id != ID_1) {
98 PJ_LOG(3,("", "...error: got unexpected exception %d (%s)",
99 id, pj_exception_id_name(id)));
100 if (!rc) rc = -20;
101 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000102 }
103 PJ_END;
104
105 if (rc != 0)
106 return rc;
107
108 /*
109 * Multiple exceptions handlers
110 */
111 PJ_TRY {
112 rc = throw_id_2();
113 // should not reach here.
114 rc = -25;
115 }
Benny Prijonod0d44f52005-11-21 16:57:02 +0000116 PJ_CATCH_ANY {
117 switch (PJ_GET_EXCEPTION()) {
118 case ID_1:
119 if (!rc) rc = -30; break;
120 case ID_2:
121 if (!rc) rc = 0; break;
122 default:
123 if (!rc) rc = -40;
124 break;
125 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000126 }
127 PJ_END;
128
129 if (rc != 0)
130 return rc;
131
132 /*
133 * Test default handler.
134 */
135 PJ_TRY {
136 rc = throw_id_1();
137 // should not reach here
138 rc = -50;
139 }
Benny Prijonod0d44f52005-11-21 16:57:02 +0000140 PJ_CATCH_ANY {
141 switch (PJ_GET_EXCEPTION()) {
142 case ID_1:
143 if (!rc) rc = 0;
144 break;
145 default:
146 if (!rc) rc = -60;
147 break;
148 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000149 }
150 PJ_END;
151
152 if (rc != 0)
153 return rc;
154
155 return 0;
156}
157
158int exception_test(void)
159{
160 int i, rc;
161 enum { LOOP = 10 };
162
163 for (i=0; i<LOOP; ++i) {
164 if ((rc=test()) != 0) {
165 PJ_LOG(3,("", "...failed at i=%d (rc=%d)", i, rc));
166 return rc;
167 }
168 }
169 return 0;
170}
171
172#else
173/* To prevent warning about "translation unit is empty"
174 * when this test is disabled.
175 */
176int dummy_exception_test;
177#endif /* INCLUDE_EXCEPTION_TEST */
178
179