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