1 |
jds |
59 |
/* |
2 |
|
|
* Assert implementation override for ASCEND unit tests |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2005 Jerry St.Clair |
5 |
|
|
* |
6 |
|
|
* This file is part of the Ascend Environment. |
7 |
|
|
* |
8 |
|
|
* The Ascend Environment is free software; you can redistribute it |
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 |
11 |
|
|
* License, or (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* The Ascend Environment is distributed in hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
|
|
* General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
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 |
21 |
|
|
* COPYING. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include <stdio.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <setjmp.h> |
27 |
|
|
#include <assert.h> |
28 |
|
|
#include "utilities/ascConfig.h" |
29 |
|
|
#include "utilities/ascPanic.h" |
30 |
|
|
#include "assertimpl.h" |
31 |
|
|
|
32 |
|
|
enum assert_status_t g_assert_status = ast_passed; |
33 |
|
|
jmp_buf g_asc_test_env; |
34 |
|
|
static int f_use_longjump = FALSE; |
35 |
|
|
|
36 |
|
|
void enable_assert_longjmp(int TRUE_or_FALSE) |
37 |
|
|
{ |
38 |
|
|
f_use_longjump = TRUE_or_FALSE; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/* Override implementation of assert using the signature of the relevant compiler */ |
42 |
jds |
60 |
#ifdef __WIN32__ |
43 |
jds |
59 |
#if defined(__GNUC__) || defined(__MINGW32_VERSION) |
44 |
|
|
_CRTIMP void __cdecl _assert(const char *cond, const char *file, int line) |
45 |
|
|
|
46 |
|
|
#elif defined(_MSC_VER) |
47 |
|
|
_CRTIMP void __cdecl _assert(const char *cond, const char *file, unsigned line) |
48 |
|
|
|
49 |
|
|
#elif defined(__BORLANDC__) |
50 |
|
|
#ifdef __cplusplus |
51 |
|
|
namespace std { |
52 |
|
|
#endif |
53 |
|
|
void _RTLENTRY _EXPFUNC _assert(char *cond, char *file, int line) |
54 |
|
|
|
55 |
|
|
#else |
56 |
|
|
#error Unrecognized compiler. |
57 |
|
|
|
58 |
|
|
#endif |
59 |
jds |
60 |
#else /* !__WIN32__ */ |
60 |
|
|
#if defined(__GNUC__) |
61 |
|
|
void __assert_fail (const char *cond, const char *file, |
62 |
|
|
unsigned int line, const char *__function) |
63 |
|
|
/* __THROW __attribute__ ((__noreturn__)) */ |
64 |
|
|
#else |
65 |
|
|
#error Unrecognized compiler. |
66 |
|
|
#endif |
67 |
|
|
#endif /* __WIN32__ */ |
68 |
jds |
59 |
{ |
69 |
|
|
g_assert_status = ast_failed; |
70 |
|
|
if (TRUE == f_use_longjump) { |
71 |
|
|
longjmp(g_asc_test_env, -1); |
72 |
|
|
} |
73 |
|
|
else { |
74 |
|
|
fprintf(stderr, "\nAssertion failed: %s, file %s, line %d\n", cond, file, line); |
75 |
|
|
abort(); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
#if defined(__BORLANDC__) |
80 |
|
|
#ifdef __cplusplus |
81 |
|
|
} |
82 |
|
|
#endif |
83 |
|
|
#endif |
84 |
|
|
|
85 |
|
|
static int f_asc_assert_failed = FALSE; |
86 |
|
|
|
87 |
|
|
static int assert_callback(int status) |
88 |
|
|
{ |
89 |
|
|
if (ASCERR_ASSERTION_FAILED == status) { |
90 |
|
|
f_asc_assert_failed = TRUE; |
91 |
|
|
longjmp(g_asc_test_env, -1); |
92 |
|
|
} |
93 |
|
|
return -1; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
int asc_assert_failed() |
97 |
|
|
{ |
98 |
|
|
return f_asc_assert_failed; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
void asc_assert_reset() |
102 |
|
|
{ |
103 |
|
|
f_asc_assert_failed = FALSE; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
void asc_assert_catch(int TRUE_or_FALSE) |
107 |
|
|
{ |
108 |
|
|
static PanicCallbackFunc old_callback = NULL; |
109 |
|
|
|
110 |
|
|
if (FALSE == TRUE_or_FALSE) { |
111 |
|
|
(void)Asc_PanicSetCallback(old_callback); |
112 |
|
|
Asc_PanicDisplayMessageBox(TRUE); |
113 |
|
|
old_callback = NULL; |
114 |
|
|
f_asc_assert_failed = FALSE; |
115 |
|
|
} |
116 |
|
|
else { |
117 |
|
|
old_callback = Asc_PanicSetCallback(assert_callback); |
118 |
|
|
Asc_PanicDisplayMessageBox(FALSE); |
119 |
|
|
f_asc_assert_failed = FALSE; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|