Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | #include "test.h" |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * \page page_pjlib_exception_test Test: Exception Handling |
| 25 | * |
| 26 | * This file provides implementation of \b exception_test(). It tests the |
| 27 | * functionality of the exception handling API. |
| 28 | * |
| 29 | * @note This test use static ID not acquired through proper registration. |
| 30 | * This is not recommended, since it may create ID collissions. |
| 31 | * |
| 32 | * \section exception_test_sec Scope of the Test |
| 33 | * |
| 34 | * Some scenarios tested: |
| 35 | * - no exception situation |
| 36 | * - basic TRY/CATCH |
| 37 | * - multiple exception handlers |
| 38 | * - default handlers |
| 39 | * |
| 40 | * |
| 41 | * This file is <b>pjlib-test/exception.c</b> |
| 42 | * |
| 43 | * \include pjlib-test/exception.c |
| 44 | */ |
| 45 | |
| 46 | |
| 47 | #if INCLUDE_EXCEPTION_TEST |
| 48 | |
| 49 | #include <pjlib.h> |
| 50 | |
| 51 | #define ID_1 1 |
| 52 | #define ID_2 2 |
| 53 | |
| 54 | static int throw_id_1(void) |
| 55 | { |
| 56 | PJ_THROW( ID_1 ); |
Benny Prijono | 95b5340 | 2009-01-01 20:56:36 +0000 | [diff] [blame] | 57 | PJ_UNREACHED(return -1;) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int throw_id_2(void) |
| 61 | { |
| 62 | PJ_THROW( ID_2 ); |
Benny Prijono | 95b5340 | 2009-01-01 20:56:36 +0000 | [diff] [blame] | 63 | PJ_UNREACHED(return -1;) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | static int test(void) |
| 68 | { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 69 | int rc = 0; |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 70 | PJ_USE_EXCEPTION; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * No exception situation. |
| 74 | */ |
| 75 | PJ_TRY { |
| 76 | rc = rc; |
| 77 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 78 | PJ_CATCH_ANY { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 79 | rc = -3; |
| 80 | } |
| 81 | PJ_END; |
| 82 | |
| 83 | if (rc != 0) |
| 84 | return rc; |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * Basic TRY/CATCH |
| 89 | */ |
| 90 | PJ_TRY { |
| 91 | rc = throw_id_1(); |
| 92 | |
| 93 | // should not reach here. |
| 94 | rc = -10; |
| 95 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 96 | PJ_CATCH_ANY { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 97 | int id = PJ_GET_EXCEPTION(); |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 98 | if (id != ID_1) { |
| 99 | PJ_LOG(3,("", "...error: got unexpected exception %d (%s)", |
| 100 | id, pj_exception_id_name(id))); |
| 101 | if (!rc) rc = -20; |
| 102 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 103 | } |
| 104 | PJ_END; |
| 105 | |
| 106 | if (rc != 0) |
| 107 | return rc; |
| 108 | |
| 109 | /* |
| 110 | * Multiple exceptions handlers |
| 111 | */ |
| 112 | PJ_TRY { |
| 113 | rc = throw_id_2(); |
| 114 | // should not reach here. |
| 115 | rc = -25; |
| 116 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 117 | PJ_CATCH_ANY { |
| 118 | switch (PJ_GET_EXCEPTION()) { |
| 119 | case ID_1: |
| 120 | if (!rc) rc = -30; break; |
| 121 | case ID_2: |
| 122 | if (!rc) rc = 0; break; |
| 123 | default: |
| 124 | if (!rc) rc = -40; |
| 125 | break; |
| 126 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 127 | } |
| 128 | PJ_END; |
| 129 | |
| 130 | if (rc != 0) |
| 131 | return rc; |
| 132 | |
| 133 | /* |
| 134 | * Test default handler. |
| 135 | */ |
| 136 | PJ_TRY { |
| 137 | rc = throw_id_1(); |
| 138 | // should not reach here |
| 139 | rc = -50; |
| 140 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 141 | PJ_CATCH_ANY { |
| 142 | switch (PJ_GET_EXCEPTION()) { |
| 143 | case ID_1: |
| 144 | if (!rc) rc = 0; |
| 145 | break; |
| 146 | default: |
| 147 | if (!rc) rc = -60; |
| 148 | break; |
| 149 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 150 | } |
| 151 | PJ_END; |
| 152 | |
| 153 | if (rc != 0) |
| 154 | return rc; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int exception_test(void) |
| 160 | { |
| 161 | int i, rc; |
| 162 | enum { LOOP = 10 }; |
| 163 | |
| 164 | for (i=0; i<LOOP; ++i) { |
| 165 | if ((rc=test()) != 0) { |
| 166 | PJ_LOG(3,("", "...failed at i=%d (rc=%d)", i, rc)); |
| 167 | return rc; |
| 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | #else |
| 174 | /* To prevent warning about "translation unit is empty" |
| 175 | * when this test is disabled. |
| 176 | */ |
| 177 | int dummy_exception_test; |
| 178 | #endif /* INCLUDE_EXCEPTION_TEST */ |
| 179 | |
| 180 | |