blob: ef4bf4a2629c00df46a804ad2517d1a465bd103c [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{
68 PJ_USE_EXCEPTION;
69 int rc = 0;
70
71 /*
72 * No exception situation.
73 */
74 PJ_TRY {
75 rc = rc;
76 }
77 PJ_CATCH( ID_1 ) {
78 rc = -2;
79 }
80 PJ_DEFAULT {
81 rc = -3;
82 }
83 PJ_END;
84
85 if (rc != 0)
86 return rc;
87
88
89 /*
90 * Basic TRY/CATCH
91 */
92 PJ_TRY {
93 rc = throw_id_1();
94
95 // should not reach here.
96 rc = -10;
97 }
98 PJ_CATCH( ID_1 ) {
99 if (!rc) rc = 0;
100 }
101 PJ_DEFAULT {
102 int id = PJ_GET_EXCEPTION();
103 PJ_LOG(3,("", "...error: got unexpected exception %d (%s)",
104 id, pj_exception_id_name(id)));
105 if (!rc) rc = -20;
106 }
107 PJ_END;
108
109 if (rc != 0)
110 return rc;
111
112 /*
113 * Multiple exceptions handlers
114 */
115 PJ_TRY {
116 rc = throw_id_2();
117 // should not reach here.
118 rc = -25;
119 }
120 PJ_CATCH( ID_1 ) {
121 if (!rc) rc = -30;
122 }
123 PJ_CATCH( ID_2 ) {
124 if (!rc) rc = 0;
125 }
126 PJ_DEFAULT {
127 if (!rc) rc = -40;
128 }
129 PJ_END;
130
131 if (rc != 0)
132 return rc;
133
134 /*
135 * Test default handler.
136 */
137 PJ_TRY {
138 rc = throw_id_1();
139 // should not reach here
140 rc = -50;
141 }
142 PJ_CATCH( ID_2 ) {
143 if (!rc) rc = -60;
144 }
145 PJ_DEFAULT {
146 if (!rc) rc = 0;
147 }
148 PJ_END;
149
150 if (rc != 0)
151 return rc;
152
153 return 0;
154}
155
156int exception_test(void)
157{
158 int i, rc;
159 enum { LOOP = 10 };
160
161 for (i=0; i<LOOP; ++i) {
162 if ((rc=test()) != 0) {
163 PJ_LOG(3,("", "...failed at i=%d (rc=%d)", i, rc));
164 return rc;
165 }
166 }
167 return 0;
168}
169
170#else
171/* To prevent warning about "translation unit is empty"
172 * when this test is disabled.
173 */
174int dummy_exception_test;
175#endif /* INCLUDE_EXCEPTION_TEST */
176
177