1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2005 Jerry St.Clair |
3 |
Copyright (C) 2006 Carnegie Mellon University |
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, or (at your option) |
8 |
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, see <http://www.gnu.org/licenses/>. |
17 |
*//** |
18 |
@file |
19 |
Assert implementation override for ASCEND unit tests. |
20 |
|
21 |
These functions override the default assert implementation to |
22 |
allow recovery from failed asserts during testing. When using |
23 |
this implementation, a failed assertion sets a global variable |
24 |
(g_assert_status) and issues a longjmp() out of the function. |
25 |
|
26 |
For example (in test function): |
27 |
<pre> |
28 |
g_assert_status = ast_passed; |
29 |
if (0 == setjmp(g_asc_test_env)) |
30 |
call_function_containing_assertion(); |
31 |
|
32 |
if (ast_passed == g_assert_status) |
33 |
==> no assertions failed <== |
34 |
else |
35 |
==> assertion failed <== </pre> |
36 |
|
37 |
If you desire that assert() reverts to standard behavior |
38 |
(issue a message on stderr and abort the program), call |
39 |
enable_longjump(FALSE). |
40 |
|
41 |
Support is also provided for trapping asc_assert() failures. |
42 |
This mechanism works by using a callback from Asc_Panic to |
43 |
issue a longjmp back to test code for asc_assert() failures. |
44 |
Test code should enable trapping of asc_assert, then use |
45 |
the global jmp_buf g_asc_test_env and setjmp() to wrap the |
46 |
code being tested. Functions are provided to check the whether |
47 |
an asc_assert() failure has occurred and reset the status. |
48 |
|
49 |
For example (in test function): |
50 |
<pre> |
51 |
asc_assert_catch(TRUE); (* enable trapping of asc_assert() *) |
52 |
asc_assert_reset(); (* prepare for a test *) |
53 |
if (0 == setjmp(g_asc_test_env)) (* setjmp using global jmpbuf & call suspect function *) |
54 |
call_function_containing_assertion(); |
55 |
|
56 |
if (TRUE == asc_assert_failed()) (* test whether assertion failed *) |
57 |
==> no assertions failed <== |
58 |
else |
59 |
==> assertion failed <== |
60 |
asc_assert_catch(FALSE); (* disable trapping of asc_assert() when done *) |
61 |
</pre> |
62 |
*/ |
63 |
|
64 |
#ifndef ASSERTIMPL_H_SEEN |
65 |
#define ASSERTIMPL_H_SEEN |
66 |
|
67 |
/** assert status results. */ |
68 |
enum assert_status_t { |
69 |
ast_failed, |
70 |
ast_passed |
71 |
}; |
72 |
|
73 |
/** |
74 |
* Global variable indicating status of last assert(). |
75 |
* Will be ast_passed if the assert passed, ast_failed otherwise. |
76 |
*/ |
77 |
extern enum assert_status_t g_assert_status; |
78 |
|
79 |
/** |
80 |
* Global jump buffer variable to use for the call to setjmp(). |
81 |
*/ |
82 |
extern jmp_buf g_asc_test_env; |
83 |
|
84 |
/** |
85 |
* Change the behavior of failed assert()'s. |
86 |
* Pass TRUE to enable alternate behavior using longjmp, FALSE |
87 |
* to use standard behavior. |
88 |
*/ |
89 |
void enable_assert_longjmp(int TRUE_or_FALSE); |
90 |
|
91 |
/** |
92 |
* Returns TRUE if an asc_assert() failed since the last call to |
93 |
* asc_assert_reset() or asc_assert_catch(TRUE). |
94 |
*/ |
95 |
extern int asc_assert_failed(void); |
96 |
|
97 |
/** |
98 |
* Resets the accounting on asc_assert(). |
99 |
* After calling this function, asc_assert_failed() will return FALSE. |
100 |
* It should be called after a caught failed assertion. |
101 |
*/ |
102 |
extern void asc_assert_reset(void); |
103 |
|
104 |
/** |
105 |
* Enables or disables trapping of asc_assert() failures. |
106 |
* Pass TRUE to enable catching of failures, or FALSE to disable it. |
107 |
* Note that while catching is enabled, any call to Asc_Panic() with a |
108 |
* status code of ASCERR_ASSERTION_FAILED will be trapped as a failed |
109 |
* exception. Other status codes will not be trapped. When an |
110 |
* assertion is trapped, <br><br> |
111 |
* |
112 |
* This function registers a callback with Asc_Panic(). If another |
113 |
* callback was already registered, it is replaced until |
114 |
* asc_assert_catch(FALSE) is called. At that time the original |
115 |
* callback will be restored.<br><br> |
116 |
* |
117 |
* If you're playing lots of games with Asc_Panic() callbacks, this |
118 |
* function may mess you up. For example, if you register a new callback |
119 |
* after calling this function (1) asc_assert() failures will not be |
120 |
* trapped and (2) some previous callback can be restored when |
121 |
* asc_assert_catch(FALSE) is called. |
122 |
* |
123 |
* @param TRUE_or_FALSE Pass TRUE to enable trapping of asc_assert |
124 |
* failures, FALSE to disable. |
125 |
*/ |
126 |
extern void asc_assert_catch(int TRUE_or_FALSE); |
127 |
|
128 |
#endif /* ASSERTIMPL_H_SEEN */ |