Benny Prijono | dd859a6 | 2005-11-01 16:42:51 +0000 | [diff] [blame^] | 1 | /* $Header: /pjproject-0.3/pjlib/src/pjlib-test/exception.c 2 10/14/05 12:26a Bennylp $ |
| 2 | */ |
| 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 | |
| 37 | static int throw_id_1(void) |
| 38 | { |
| 39 | PJ_THROW( ID_1 ); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | static int throw_id_2(void) |
| 44 | { |
| 45 | PJ_THROW( ID_2 ); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static 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 | |
| 137 | int exception_test(void) |
| 138 | { |
| 139 | int i, rc; |
| 140 | enum { LOOP = 10 }; |
| 141 | |
| 142 | for (i=0; i<LOOP; ++i) { |
| 143 | if ((rc=test()) != 0) |
| 144 | return rc; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | #else |
| 150 | /* To prevent warning about "translation unit is empty" |
| 151 | * when this test is disabled. |
| 152 | */ |
| 153 | int dummy_exception_test; |
| 154 | #endif /* INCLUDE_EXCEPTION_TEST */ |
| 155 | |
| 156 | |