1 |
/* |
/* ASCEND modelling environment |
2 |
* Assert implementation override for ASCEND unit tests |
Copyright (C) 2005 Jerry St.Clair |
3 |
* |
Copyright (C) 2006 Carnegie Mellon University |
4 |
* Copyright (C) 2005 Jerry St.Clair |
|
5 |
* |
This program is free software; you can redistribute it and/or modify |
6 |
* This file is part of the Ascend Environment. |
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 |
* The Ascend Environment is free software; you can redistribute it |
any later version. |
9 |
* and/or modify it under the terms of the GNU General Public License as |
|
10 |
* published by the Free Software Foundation; either version 2 of the |
This program is distributed in the hope that it will be useful, |
11 |
* License, or (at your option) any later version. |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* The Ascend Environment is distributed in hope that it will be useful, |
GNU General Public License for more details. |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
You should have received a copy of the GNU General Public License |
16 |
* General Public License for more details. |
along with this program; if not, write to the Free Software |
17 |
* |
Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
* You should have received a copy of the GNU General Public License |
Boston, MA 02111-1307, USA. |
19 |
* along with the program; if not, write to the Free Software Foundation, |
*//** |
20 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
@file |
21 |
* COPYING. |
Assert implementation override for ASCEND unit tests. |
22 |
*/ |
|
23 |
|
These functions override the default assert implementation to |
24 |
/** @file |
allow recovery from failed asserts during testing. When using |
25 |
* Assert implementation override for ASCEND unit tests. |
this implementation, a failed assertion sets a global variable |
26 |
* These functions override the default assert implementation to |
(g_assert_status) and issues a longjmp() out of the function. |
27 |
* allow recovery from failed asserts during testing. When using |
|
28 |
* this implementation, a failed assertion sets a global variable |
For example (in test function): |
29 |
* (g_assert_status) and issues a longjmp() out of the function. |
<pre> |
30 |
* |
g_assert_status = ast_passed; |
31 |
* For example (in test function): |
if (0 == setjmp(g_asc_test_env)) |
32 |
* <pre> |
call_function_containing_assertion(); |
33 |
* g_assert_status = ast_passed; |
|
34 |
* if (0 == setjmp(g_asc_test_env)) |
if (ast_passed == g_assert_status) |
35 |
* call_function_containing_assertion(); |
==> no assertions failed <== |
36 |
* |
else |
37 |
* if (ast_passed == g_assert_status) |
==> assertion failed <== </pre> |
38 |
* ==> no assertions failed <== |
|
39 |
* else |
If you desire that assert() reverts to standard behavior |
40 |
* ==> assertion failed <== </pre> |
(issue a message on stderr and abort the program), call |
41 |
* |
enable_longjump(FALSE). |
42 |
* If you desire that assert() reverts to standard behavior |
|
43 |
* (issue a message on stderr and abort the program), call |
Support is also provided for trapping asc_assert() failures. |
44 |
* enable_longjump(FALSE). |
This mechanism works by using a callback from Asc_Panic to |
45 |
* |
issue a longjmp back to test code for asc_assert() failures. |
46 |
* Support is also provided for trapping asc_assert() failures. |
Test code should enable trapping of asc_assert, then use |
47 |
* This mechanism works by using a callback from Asc_Panic to |
the global jmp_buf g_asc_test_env and setjmp() to wrap the |
48 |
* issue a longjmp back to test code for asc_assert() failures. |
code being tested. Functions are provided to check the whether |
49 |
* Test code should enable trapping of asc_assert, then use |
an asc_assert() failure has occurred and reset the status. |
50 |
* the global jmp_buf g_asc_test_env and setjmp() to wrap the |
|
51 |
* code being tested. Functions are provided to check the whether |
For example (in test function): |
52 |
* an asc_assert() failure has occurred and reset the status. |
<pre> |
53 |
* |
asc_assert_catch(TRUE); (* enable trapping of asc_assert() *) |
54 |
* For example (in test function): |
asc_assert_reset(); (* prepare for a test *) |
55 |
* <pre> |
if (0 == setjmp(g_asc_test_env)) (* setjmp using global jmpbuf & call suspect function *) |
56 |
* asc_assert_catch(TRUE); (* enable trapping of asc_assert() *) |
call_function_containing_assertion(); |
57 |
* asc_assert_reset(); (* prepare for a test *) |
|
58 |
* if (0 == setjmp(g_asc_test_env)) (* setjmp using global jmpbuf & call suspect function *) |
if (TRUE == asc_assert_failed()) (* test whether assertion failed *) |
59 |
* call_function_containing_assertion(); |
==> no assertions failed <== |
60 |
* |
else |
61 |
* if (TRUE == asc_assert_failed()) (* test whether assertion failed *) |
==> assertion failed <== |
62 |
* ==> no assertions failed <== |
asc_assert_catch(FALSE); (* disable trapping of asc_assert() when done *) |
63 |
* else |
</pre> |
|
* ==> assertion failed <== |
|
|
* asc_assert_catch(FALSE); (* disable trapping of asc_assert() when done *) |
|
|
* </pre> |
|
64 |
*/ |
*/ |
65 |
|
|
66 |
#ifndef ASSERTIMPL_H_SEEN |
#ifndef ASSERTIMPL_H_SEEN |
90 |
*/ |
*/ |
91 |
void enable_assert_longjmp(int TRUE_or_FALSE); |
void enable_assert_longjmp(int TRUE_or_FALSE); |
92 |
|
|
93 |
/** |
/** |
94 |
* Returns TRUE if an asc_assert() failed since the last call to |
* Returns TRUE if an asc_assert() failed since the last call to |
95 |
* asc_assert_reset() or asc_assert_catch(TRUE). |
* asc_assert_reset() or asc_assert_catch(TRUE). |
96 |
*/ |
*/ |
97 |
extern int asc_assert_failed(void); |
extern int asc_assert_failed(void); |
98 |
|
|
99 |
/** |
/** |
100 |
* Resets the accounting on asc_assert(). |
* Resets the accounting on asc_assert(). |
101 |
* After calling this function, asc_assert_failed() will return FALSE. |
* After calling this function, asc_assert_failed() will return FALSE. |
102 |
* It should be called after a caught failed assertion. |
* It should be called after a caught failed assertion. |
103 |
*/ |
*/ |
104 |
extern void asc_assert_reset(void); |
extern void asc_assert_reset(void); |
105 |
|
|
106 |
/** |
/** |
107 |
* Enables or disables trapping of asc_assert() failures. |
* Enables or disables trapping of asc_assert() failures. |
108 |
* Pass TRUE to enable catching of failures, or FALSE to disable it. |
* Pass TRUE to enable catching of failures, or FALSE to disable it. |
109 |
* Note that while catching is enabled, any call to Asc_Panic() with a |
* Note that while catching is enabled, any call to Asc_Panic() with a |
118 |
* |
* |
119 |
* If you're playing lots of games with Asc_Panic() callbacks, this |
* If you're playing lots of games with Asc_Panic() callbacks, this |
120 |
* function may mess you up. For example, if you register a new callback |
* function may mess you up. For example, if you register a new callback |
121 |
* after calling this function (1) asc_assert() failures will not be |
* after calling this function (1) asc_assert() failures will not be |
122 |
* trapped and (2) some previous callback can be restored when |
* trapped and (2) some previous callback can be restored when |
123 |
* asc_assert_catch(FALSE) is called. |
* asc_assert_catch(FALSE) is called. |
124 |
* |
* |