1 |
/* |
2 |
* Unit test functions for ASCEND: utilities/ascPanic.c |
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 <utilities/ascConfig.h> |
26 |
#ifdef __WIN32__ |
27 |
#include <io.h> |
28 |
#endif |
29 |
#include <utilities/ascMalloc.h> |
30 |
#include <utilities/ascPanic.h> |
31 |
/* #include <compiler/redirectFile.h> */ |
32 |
#include "CUnit/CUnit.h" |
33 |
#include "test_ascPanic.h" |
34 |
#include "assertimpl.h" |
35 |
#include "printutil.h" |
36 |
|
37 |
static int f_callback_called = FALSE; |
38 |
static int f_callback_status; |
39 |
|
40 |
/* |
41 |
These tests have been disabled as the stream redirection in ASCEND is |
42 |
starting to be a bit crazy: error.c --> ascPrint.h --> redirectFile.h |
43 |
as well as printutil.c and redirectStdStreams.c. Will set this test to |
44 |
failing and we'll come back and reimplement this later. |
45 |
*/ |
46 |
|
47 |
static void test_ascPanic(void){ |
48 |
CU_FAIL("test_ascPanic needs to be reimplemented"); |
49 |
} |
50 |
|
51 |
#ifdef REIMPLEMENT_STREAMS |
52 |
|
53 |
/* |
54 |
* Callback function for Asc_Panic() during testing. |
55 |
* Sets a flag, then returns non-zero so program continues. |
56 |
*/ |
57 |
static int set_flag_and_return(int status) |
58 |
{ |
59 |
f_callback_called = TRUE; |
60 |
f_callback_status = status; |
61 |
return TRUE; |
62 |
} |
63 |
|
64 |
/* |
65 |
* ascPanic.[ch] is tough to test completely, since |
66 |
* 1. most of its action is text formatting & output |
67 |
* 2. it normally exits the program |
68 |
* |
69 |
* As a first pass, we will disable exiting and just test |
70 |
* whether something is getting written to ASCERR and an |
71 |
* output file. |
72 |
*/ |
73 |
static void test_ascPanic(void) |
74 |
{ |
75 |
FILE *stdoutfile; |
76 |
FILE* outfile; |
77 |
int i_enabled_printing = FALSE; |
78 |
FILE *old_errfile; |
79 |
FILE *old_warnfile; |
80 |
FILE *old_infofile; |
81 |
unsigned long prior_meminuse; |
82 |
|
83 |
prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */ |
84 |
|
85 |
#ifdef NDEBUG |
86 |
CU_FAIL("test_ascPanic() compiled with NDEBUG - some features not tested."); |
87 |
#endif |
88 |
|
89 |
old_errfile = g_ascend_errors; /* save so can be restored later */ |
90 |
old_warnfile = g_ascend_warnings; |
91 |
old_infofile = g_ascend_information; |
92 |
|
93 |
/* this test requires message printing to be enabled */ |
94 |
if (FALSE == test_printing_enabled()) { |
95 |
test_enable_printing(); |
96 |
i_enabled_printing = TRUE; |
97 |
} |
98 |
|
99 |
CU_TEST(NULL == Asc_PanicSetCallback(set_flag_and_return)); |
100 |
Asc_PanicDisplayMessageBox(FALSE); |
101 |
|
102 |
#ifndef NDEBUG |
103 |
enable_assert_longjmp(TRUE); /* prepare to test assertions */ |
104 |
|
105 |
Asc_RedirectCompilerStreams(NULL, NULL, NULL); /* make sure ASCERR is NULL */ |
106 |
|
107 |
f_callback_called = FALSE; |
108 |
g_assert_status = ast_passed; |
109 |
if (0 == setjmp(g_asc_test_env)) |
110 |
Asc_Panic(1, "test_ascPanic", "Error message."); /* error - ASCERR NULL */ |
111 |
CU_TEST(ast_failed == g_assert_status); |
112 |
CU_TEST(FALSE == f_callback_called); |
113 |
|
114 |
enable_assert_longjmp(FALSE); /* done testing assertions */ |
115 |
#endif /* !NDEBUG */ |
116 |
|
117 |
if (NULL != (stdoutfile = fopen("asc_panic_std_tempfile1.txt", "w+"))) { |
118 |
Asc_RedirectCompilerStreams(stdoutfile, stdoutfile, stdoutfile); /* send output to a file */ |
119 |
Asc_Panic(1, "test_ascPanic", "Error message 1."); |
120 |
CU_TEST(1 == f_callback_status); |
121 |
Asc_Panic(2, "test_ascPanic", "Error message 2."); |
122 |
CU_TEST(2 == f_callback_status); |
123 |
rewind(stdoutfile); |
124 |
CU_TEST(EOF != fgetc(stdoutfile)); /* test that file is not empty */ |
125 |
fclose(stdoutfile); |
126 |
remove("asc_panic_std_tempfile1.txt"); |
127 |
Asc_RedirectCompilerDefault(); /* reset reporting streams */ |
128 |
} |
129 |
else { |
130 |
CU_FAIL("Error opening output file 1 in test_ascPanic.c"); |
131 |
} |
132 |
|
133 |
Asc_PanicSetOutfile("asc_panic_extra_tempfile1.txt"); /* turn on output to extra file */ |
134 |
|
135 |
if (NULL != (stdoutfile = fopen("asc_panic_std_tempfile2.txt", "w+"))) { |
136 |
Asc_RedirectCompilerStreams(stdoutfile, stdoutfile, stdoutfile); /* send output to a file */ |
137 |
Asc_Panic(-1, "test_ascPanic", "Error message %d.", -1); |
138 |
CU_TEST(-1 == f_callback_status); |
139 |
Asc_Panic(200, "test_ascPanic", "Error message %d.", 200); |
140 |
CU_TEST(200 == f_callback_status); |
141 |
rewind(stdoutfile); |
142 |
CU_TEST(EOF != fgetc(stdoutfile)); /* test that file is not empty */ |
143 |
fclose(stdoutfile); |
144 |
remove("asc_panic_std_tempfile2.txt"); |
145 |
Asc_RedirectCompilerDefault(); /* reset reporting streams */ |
146 |
if (NULL != (outfile = fopen("asc_panic_extra_tempfile1.txt", "r"))) { |
147 |
CU_TEST(EOF != fgetc(outfile)); /* test that extra file is not empty */ |
148 |
fclose(outfile); |
149 |
remove("asc_panic_extra_tempfile1.txt"); |
150 |
} |
151 |
else { |
152 |
CU_FAIL("Error opening output file 3 in test_ascPanic.c"); |
153 |
} |
154 |
} |
155 |
else { |
156 |
CU_FAIL("Error opening output file 2 in test_ascPanic.c"); |
157 |
} |
158 |
|
159 |
Asc_PanicSetOutfile(NULL); /* turn off output to extra file */ |
160 |
|
161 |
if (NULL != (stdoutfile = fopen("asc_panic_std_tempfile3.txt", "w+"))) { |
162 |
Asc_RedirectCompilerStreams(stdoutfile, stdoutfile, stdoutfile); /* send output to a file */ |
163 |
Asc_Panic(-100, "test_ascPanic", "Error message -100."); |
164 |
CU_TEST(-100 == f_callback_status); |
165 |
Asc_Panic(0, "test_ascPanic", "Error message 0."); |
166 |
CU_TEST(0 == f_callback_status); |
167 |
rewind(stdoutfile); |
168 |
CU_TEST(EOF != fgetc(stdoutfile)); /* test that file is not empty */ |
169 |
fclose(stdoutfile); |
170 |
remove("asc_panic_std_tempfile3.txt"); |
171 |
Asc_RedirectCompilerDefault(); /* reset reporting streams */ |
172 |
} |
173 |
else { |
174 |
CU_FAIL("Error opening output file 4 in test_ascPanic.c"); |
175 |
} |
176 |
|
177 |
CU_TEST(set_flag_and_return == Asc_PanicSetCallback(NULL)); /* reset exit on Asc_Panic() */ |
178 |
Asc_PanicDisplayMessageBox(TRUE); /* reset display of the MessageBox */ |
179 |
|
180 |
if (TRUE == i_enabled_printing) { |
181 |
test_disable_printing(); |
182 |
} |
183 |
|
184 |
Asc_RedirectCompilerStreams(old_errfile, old_warnfile, old_infofile); /* restore streams */ |
185 |
CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */ |
186 |
} |
187 |
#endif /* REIMPLEMENT_STREAMS */ |
188 |
|
189 |
/*===========================================================================*/ |
190 |
/* Registration information */ |
191 |
|
192 |
static CU_TestInfo ascPanic_test_list[] = { |
193 |
{"test_ascPanic", test_ascPanic}, |
194 |
CU_TEST_INFO_NULL |
195 |
}; |
196 |
|
197 |
static CU_SuiteInfo suites[] = { |
198 |
{"test_utilities_ascPanic", NULL, NULL, ascPanic_test_list}, |
199 |
CU_SUITE_INFO_NULL |
200 |
}; |
201 |
|
202 |
/*-------------------------------------------------------------------*/ |
203 |
CU_ErrorCode test_register_utilities_ascPanic(void) |
204 |
{ |
205 |
return CU_register_suites(suites); |
206 |
} |