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