blob: fcbe098cf508e3958921fdad591f366f9462824a [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
Benny Prijonodd859a62005-11-01 16:42:51 +00002 */
3#include "test.h"
4
5
6/**
7 * \page page_pjlib_exception_test Test: Exception Handling
8 *
9 * This file provides implementation of \b exception_test(). It tests the
10 * functionality of the exception handling API.
11 *
12 * @note This test use static ID not acquired through proper registration.
13 * This is not recommended, since it may create ID collissions.
14 *
15 * \section exception_test_sec Scope of the Test
16 *
17 * Some scenarios tested:
18 * - no exception situation
19 * - basic TRY/CATCH
20 * - multiple exception handlers
21 * - default handlers
22 *
23 *
24 * This file is <b>pjlib-test/exception.c</b>
25 *
26 * \include pjlib-test/exception.c
27 */
28
29
30#if INCLUDE_EXCEPTION_TEST
31
32#include <pjlib.h>
33
34#define ID_1 1
35#define ID_2 2
36
37static int throw_id_1(void)
38{
39 PJ_THROW( ID_1 );
40 return -1;
41}
42
43static int throw_id_2(void)
44{
45 PJ_THROW( ID_2 );
46 return -1;
47}
48
49
50static int test(void)
51{
52 PJ_USE_EXCEPTION;
53 int rc = 0;
54
55 /*
56 * No exception situation.
57 */
58 PJ_TRY {
59 rc = rc;
60 }
61 PJ_CATCH( ID_1 ) {
62 rc = -2;
63 }
64 PJ_DEFAULT {
65 rc = -3;
66 }
67 PJ_END;
68
69 if (rc != 0)
70 return rc;
71
72
73 /*
74 * Basic TRY/CATCH
75 */
76 PJ_TRY {
77 rc = throw_id_1();
78
79 // should not reach here.
80 rc = -10;
81 }
82 PJ_CATCH( ID_1 ) {
83 if (!rc) rc = 0;
84 }
85 PJ_DEFAULT {
86 if (!rc) rc = -20;
87 }
88 PJ_END;
89
90 if (rc != 0)
91 return rc;
92
93 /*
94 * Multiple exceptions handlers
95 */
96 PJ_TRY {
97 rc = throw_id_2();
98 // should not reach here.
99 rc = -25;
100 }
101 PJ_CATCH( ID_1 ) {
102 if (!rc) rc = -30;
103 }
104 PJ_CATCH( ID_2 ) {
105 if (!rc) rc = 0;
106 }
107 PJ_DEFAULT {
108 if (!rc) rc = -40;
109 }
110 PJ_END;
111
112 if (rc != 0)
113 return rc;
114
115 /*
116 * Test default handler.
117 */
118 PJ_TRY {
119 rc = throw_id_1();
120 // should not reach here
121 rc = -50;
122 }
123 PJ_CATCH( ID_2 ) {
124 if (!rc) rc = -60;
125 }
126 PJ_DEFAULT {
127 if (!rc) rc = 0;
128 }
129 PJ_END;
130
131 if (rc != 0)
132 return rc;
133
134 return 0;
135}
136
137int exception_test(void)
138{
139 int i, rc;
140 enum { LOOP = 10 };
141
142 for (i=0; i<LOOP; ++i) {
Benny Prijono1a73a052005-11-07 21:58:51 +0000143 if ((rc=test()) != 0) {
144 PJ_LOG(3,("", "...failed at i=%d (rc=%d)", i, rc));
Benny Prijonodd859a62005-11-01 16:42:51 +0000145 return rc;
Benny Prijono1a73a052005-11-07 21:58:51 +0000146 }
Benny Prijonodd859a62005-11-01 16:42:51 +0000147 }
148 return 0;
149}
150
151#else
152/* To prevent warning about "translation unit is empty"
153 * when this test is disabled.
154 */
155int dummy_exception_test;
156#endif /* INCLUDE_EXCEPTION_TEST */
157
158