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

Annotation of /trunk/test/assertimpl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2649 - (hide annotations) (download) (as text)
Wed Dec 12 12:39:25 2012 UTC (9 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 johnpye 607 /* 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 jpye 2649 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 johnpye 607 Assert implementation override for ASCEND unit tests
19     */
20    
21 jds 59 #include <stdio.h>
22     #include <stdlib.h>
23     #include <setjmp.h>
24 johnpye 451 #include <assert.h>
25    
26 jpye 2323 #include <ascend/general/platform.h>
27     #include <ascend/general/panic.h>
28 johnpye 451
29 jds 59 #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 jds 60 #ifdef __WIN32__
42 jds 101 # if defined(__GNUC__) || defined(__MINGW32_VERSION)
43 jpye 2523 _CRTIMP void __cdecl my_assert(const char *cond, const char *file, int line)
44 jds 101 {
45 jds 59
46 jds 101 # elif defined(_MSC_VER)
47 jds 216 # if _MSC_VER < 1400 /* Visual C versions below 14 */
48 jds 59 _CRTIMP void __cdecl _assert(const char *cond, const char *file, unsigned line)
49 jds 101 {
50 jds 216 # 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 jds 59
55 jds 101 # elif defined(__BORLANDC__)
56     # ifdef __cplusplus
57 jds 59 namespace std {
58 jds 101 # endif
59 jds 59 void _RTLENTRY _EXPFUNC _assert(char *cond, char *file, int line)
60 jds 101 {
61 jds 59
62 jds 101 # else
63 jds 216 # error Unrecognized Windows compiler.
64 jds 59
65 jds 101 # endif
66 jds 60 #else /* !__WIN32__ */
67 jds 101 # if defined(__GNUC__)
68 jds 60 void __assert_fail (const char *cond, const char *file,
69     unsigned int line, const char *__function)
70 johnpye 607 /* __THROW __attribute__ ((__noreturn__)) */
71 jds 101 {
72     UNUSED_PARAMETER(__function);
73    
74     # else
75     # error Unrecognized compiler.
76     # endif
77 jds 60 #endif /* __WIN32__ */
78 jds 101
79 jds 59 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 jds 101 # ifdef __cplusplus
91 jds 59 }
92 jds 101 # endif
93 jds 59 #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