/[ascend]/trunk/ascend/utilities/test/test_ascPanic.c
ViewVC logotype

Contents of /trunk/ascend/utilities/test/test_ascPanic.c

Parent Directory Parent Directory | Revision Log Revision Log


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

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22