/[ascend]/trunk/test/assertimpl.c
ViewVC logotype

Contents of /trunk/test/assertimpl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2649 - (show annotations) (download) (as text)
Wed Dec 12 12:39:25 2012 UTC (11 years, 11 months ago) by jpye
File MIME type: text/x-csrc
File size: 3381 byte(s)
Fixing GPL header, removing postal address (rpmlint incorrect-fsf-address)
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 *//** @file
18 Assert implementation override for ASCEND unit tests
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <setjmp.h>
24 #include <assert.h>
25
26 #include <ascend/general/platform.h>
27 #include <ascend/general/panic.h>
28
29 #include "assertimpl.h"
30
31 enum assert_status_t g_assert_status = ast_passed;
32 jmp_buf g_asc_test_env;
33 static int f_use_longjump = FALSE;
34
35 void enable_assert_longjmp(int TRUE_or_FALSE)
36 {
37 f_use_longjump = TRUE_or_FALSE;
38 }
39
40 /* Override implementation of assert using the signature of the relevant compiler */
41 #ifdef __WIN32__
42 # if defined(__GNUC__) || defined(__MINGW32_VERSION)
43 _CRTIMP void __cdecl my_assert(const char *cond, const char *file, int line)
44 {
45
46 # elif defined(_MSC_VER)
47 # if _MSC_VER < 1400 /* Visual C versions below 14 */
48 _CRTIMP void __cdecl _assert(const char *cond, const char *file, unsigned line)
49 {
50 # else
51 void __cdecl _wassert(__in_z const wchar_t * cond, __in_z const wchar_t *file, __in unsigned line)
52 {
53 # endif /* Visual C version */
54
55 # elif defined(__BORLANDC__)
56 # ifdef __cplusplus
57 namespace std {
58 # endif
59 void _RTLENTRY _EXPFUNC _assert(char *cond, char *file, int line)
60 {
61
62 # else
63 # error Unrecognized Windows compiler.
64
65 # endif
66 #else /* !__WIN32__ */
67 # if defined(__GNUC__)
68 void __assert_fail (const char *cond, const char *file,
69 unsigned int line, const char *__function)
70 /* __THROW __attribute__ ((__noreturn__)) */
71 {
72 UNUSED_PARAMETER(__function);
73
74 # else
75 # error Unrecognized compiler.
76 # endif
77 #endif /* __WIN32__ */
78
79 g_assert_status = ast_failed;
80 if (TRUE == f_use_longjump) {
81 longjmp(g_asc_test_env, -1);
82 }
83 else {
84 fprintf(stderr, "\nAssertion failed: %s, file %s, line %d\n", cond, file, line);
85 abort();
86 }
87 }
88
89 #if defined(__BORLANDC__)
90 # ifdef __cplusplus
91 }
92 # endif
93 #endif
94
95 static int f_asc_assert_failed = FALSE;
96
97 static int assert_callback(int status)
98 {
99 if (ASCERR_ASSERTION_FAILED == status) {
100 f_asc_assert_failed = TRUE;
101 longjmp(g_asc_test_env, -1);
102 }
103 return -1;
104 }
105
106 int asc_assert_failed()
107 {
108 return f_asc_assert_failed;
109 }
110
111 void asc_assert_reset()
112 {
113 f_asc_assert_failed = FALSE;
114 }
115
116 void asc_assert_catch(int TRUE_or_FALSE)
117 {
118 static PanicCallbackFunc old_callback = NULL;
119
120 if (FALSE == TRUE_or_FALSE) {
121 (void)Asc_PanicSetCallback(old_callback);
122 Asc_PanicDisplayMessageBox(TRUE);
123 old_callback = NULL;
124 f_asc_assert_failed = FALSE;
125 }
126 else {
127 old_callback = Asc_PanicSetCallback(assert_callback);
128 Asc_PanicDisplayMessageBox(FALSE);
129 f_asc_assert_failed = FALSE;
130 }
131 }
132

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22