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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2273 - (show annotations) (download) (as text)
Mon Aug 9 04:42:16 2010 UTC (13 years, 10 months ago) by jpye
File MIME type: text/x-csrc
File size: 17540 byte(s)
A little bit more debug output in test_ascSignal.c... some tests seem to be failing.
1 /*
2 * Unit test functions for ASCEND: utilities/ascSignal.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/utilities/ascConfig.h>
26 #ifdef __WIN32__
27 #include <io.h>
28 #endif
29 #include <ascend/utilities/ascMalloc.h>
30 #include <ascend/utilities/ascSignal.h>
31 #include "CUnit/CUnit.h"
32 #include "test_ascSignal.h"
33
34 static jmp_buf my_jmp_buf1;
35
36 static int f_handler1_called;
37 static int f_handler1_sigval;
38 /*
39 * Signal handler for unit tests.
40 * Resets the signal handlers and sets f_handler1_called to
41 * TRUE and f_handler1_sigval to the signal type code (-1 if
42 * an unsupported sigval). Then longjmp's using
43 * my_jmp_buf1 and the sigval.
44 */
45 void my_handler1(int sigval)
46 {
47 f_handler1_called = TRUE;
48 Asc_SignalRecover(FALSE);
49 switch (sigval)
50 {
51 case SIGFPE:
52 f_handler1_sigval = SIGFPE;
53 FPRESET;
54 break;
55 case SIGINT:
56 f_handler1_sigval = SIGINT;
57 break;
58 case SIGSEGV:
59 f_handler1_sigval = SIGSEGV;
60 break;
61 default:
62 f_handler1_sigval = -1;
63 break;
64 }
65 longjmp(my_jmp_buf1, sigval);
66 }
67
68 static jmp_buf my_jmp_buf2;
69
70 static int f_handler2_called;
71 static int f_handler2_sigval;
72 /*
73 * Signal handler for unit tests.
74 * Resets the signal handlers and sets f_handler1_called to
75 * TRUE and f_handler1_sigval to the signal type code (-1 if
76 * an unsupported sigval). Then longjmp's using
77 * my_jmp_buf1 and the sigval.
78 */
79 void my_handler2(int sigval)
80 {
81 f_handler2_called = TRUE;
82 Asc_SignalRecover(FALSE);
83 switch (sigval)
84 {
85 case SIGFPE:
86 f_handler2_sigval = SIGFPE;
87 FPRESET;
88 break;
89 case SIGINT:
90 f_handler2_sigval = SIGINT;
91 break;
92 case SIGSEGV:
93 f_handler2_sigval = SIGSEGV;
94 break;
95 default:
96 f_handler2_sigval = -1;
97 break;
98 }
99 longjmp(my_jmp_buf2, sigval);
100 }
101
102
103 static void test_ascSignal(void)
104 {
105 SigHandlerFn* old_fpe_handler = NULL;
106 SigHandlerFn* old_int_handler = NULL;
107 SigHandlerFn* old_seg_handler = NULL;
108 SigHandlerFn* old_handler;
109 volatile int signal1_caught;
110 volatile int signal2_caught;
111 volatile int signal3_caught;
112 unsigned long prior_meminuse;
113
114 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
115
116 #ifdef NO_SIGNAL_TRAPS
117 /* no point in testing if the functionality is disabled */
118 CU_FAIL("Signal handler manager not enabled.");
119 #else
120
121 CONSOLE_DEBUG("Assigning signals");
122
123 old_fpe_handler = signal(SIGFPE, my_handler1); /* save any pre-existing handlers */
124 old_int_handler = signal(SIGINT, my_handler1);
125 old_seg_handler = signal(SIGSEGV, my_handler1);
126
127 signal(SIGFPE, my_handler1); /* install some pre-existing handlers */
128 signal(SIGINT, SIG_DFL);
129 signal(SIGSEGV, my_handler2);
130
131 /* Asc_SignalInit(), Asc_SignalDestroy() - not much to test */
132
133 CONSOLE_DEBUG("Start signal manager");
134
135 CU_TEST(0 == Asc_SignalInit()); /* initialize the signal manager */
136
137 old_handler = signal(SIGFPE, SIG_DFL); /* previously-installed handlers should still be active */
138 CU_TEST(my_handler1 == old_handler);
139 old_handler = signal(SIGINT, SIG_DFL);
140 CU_TEST(NULL == old_handler);
141 old_handler = signal(SIGSEGV, SIG_DFL);
142 CU_TEST(my_handler2 == old_handler);
143
144 CONSOLE_DEBUG("Recover installed signals");
145 Asc_SignalRecover(TRUE);
146
147 old_handler = signal(SIGFPE, SIG_DFL); /* handlers should have been reinstalled */
148 CU_TEST(my_handler1 == old_handler);
149 old_handler = signal(SIGINT, SIG_DFL);
150 CU_TEST(NULL == old_handler);
151 old_handler = signal(SIGSEGV, SIG_DFL);
152 CU_TEST(my_handler2 == old_handler);
153
154 CONSOLE_DEBUG("Recover installed signals again");
155 Asc_SignalRecover(TRUE);
156
157 CONSOLE_DEBUG("Test push/pop of handlers");
158
159 /* test Asc_SignalPush(), Asc_SignalPop() */
160
161 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap)); /* ok - supported signal, ok func */
162 old_handler = signal(SIGFPE, SIG_DFL);
163 CU_TEST(Asc_SignalTrap == old_handler);
164
165 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1)); /* ok - supported signal, ok func */
166 old_handler = signal(SIGINT, SIG_DFL);
167 CU_TEST(my_handler1 == old_handler);
168
169 CU_TEST(-2 == Asc_SignalHandlerPush(SIGSEGV, NULL)); /* NULL func - should have no effect */
170 old_handler = signal(SIGSEGV, SIG_DFL);
171 CU_TEST(my_handler2 == old_handler); /* old handler should still be installed */
172
173 CU_TEST(0 != Asc_SignalHandlerPush(SIGILL, Asc_SignalTrap)); /* unsupported signal type */
174 CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2)); /* unsupported signal type */
175
176 Asc_SignalRecover(TRUE);
177
178 old_handler = signal(SIGFPE, SIG_DFL); /* handlers should have been reinstalled */
179 CU_TEST(Asc_SignalTrap == old_handler);
180 old_handler = signal(SIGINT, SIG_DFL);
181 CU_TEST(my_handler1 == old_handler);
182 old_handler = signal(SIGSEGV, SIG_DFL);
183 CU_TEST(my_handler2 == old_handler);
184 Asc_SignalRecover(TRUE);
185
186 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
187 old_handler = signal(SIGFPE, SIG_DFL); /* SIGINT should be reset to no handler */
188 CU_TEST(Asc_SignalTrap == old_handler);
189 old_handler = signal(SIGINT, SIG_DFL);
190 CU_TEST(NULL == old_handler);
191 old_handler = signal(SIGSEGV, SIG_DFL);
192 CU_TEST(my_handler2 == old_handler);
193 Asc_SignalRecover(TRUE);
194
195 CU_TEST(0 != Asc_SignalHandlerPop(SIGFPE, my_handler1)); /* wrong handler indicated */
196 old_handler = signal(SIGFPE, SIG_DFL); /* so SIGFPE should be not be reset */
197 CU_TEST(Asc_SignalTrap == old_handler);
198 old_handler = signal(SIGINT, SIG_DFL);
199 CU_TEST(NULL == old_handler);
200 old_handler = signal(SIGSEGV, SIG_DFL);
201 CU_TEST(my_handler2 == old_handler);
202
203 CONSOLE_DEBUG("Recover installed signals after a f");
204 Asc_SignalRecover(TRUE);
205 old_handler = signal(SIGFPE, SIG_DFL); /* but Asc_SignalRecover should reset it */
206 CU_TEST(my_handler1 == old_handler);
207 old_handler = signal(SIGINT, SIG_DFL);
208 CU_TEST(NULL == old_handler);
209 old_handler = signal(SIGSEGV, SIG_DFL);
210 CU_TEST(my_handler2 == old_handler);
211 Asc_SignalRecover(TRUE);
212
213 CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1)); /* unsupported signal type */
214 CU_TEST(0 != Asc_SignalHandlerPop(SIGABRT, Asc_SignalTrap)); /* unsupported signal type */
215
216 old_handler = signal(SIGFPE, SIG_DFL); /* should be no change in handlers */
217 CU_TEST(my_handler1 == old_handler);
218 old_handler = signal(SIGINT, SIG_DFL);
219 CU_TEST(NULL == old_handler);
220 old_handler = signal(SIGSEGV, SIG_DFL);
221 CU_TEST(my_handler2 == old_handler);
222 Asc_SignalRecover(TRUE);
223
224 /* test Asc_SignalTrap() */
225
226 CONSOLE_DEBUG("Testing trapping of signals");
227
228 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
229 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
230 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap));
231
232 signal1_caught = FALSE;
233 if (0 == setjmp(g_fpe_env)) { /* raise & catch a SIGFPE */
234 raise(SIGFPE);
235 }
236 else {
237 signal1_caught = TRUE;
238 CU_PASS("SIGFPE caught.");
239 }
240 CU_TEST(TRUE == signal1_caught);
241
242 signal1_caught = FALSE;
243 if (0 == setjmp(g_int_env)) { /* raise & catch a SIGINT */
244 raise(SIGINT);
245 }
246 else {
247 signal1_caught = TRUE;
248 CU_PASS("SIGINT caught.");
249 }
250 CU_TEST(TRUE == signal1_caught);
251
252 signal1_caught = FALSE;
253 if (0 == setjmp(g_seg_env)) { /* raise & catch a SIGSEGV */
254 raise(SIGSEGV);
255 }
256 else {
257 signal1_caught = TRUE;
258 CU_PASS("SIGSEGV caught.");
259 }
260 CU_TEST(TRUE == signal1_caught);
261
262 Asc_SignalRecover(TRUE);
263
264 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
265 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
266 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
267
268 CONSOLE_DEBUG("Check handler settings after pop");
269
270 old_handler = signal(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
271 CU_TEST(my_handler1 == old_handler);
272 old_handler = signal(SIGINT, SIG_DFL);
273 CU_TEST(NULL == old_handler);
274 old_handler = signal(SIGSEGV, SIG_DFL);
275 CU_TEST(my_handler2 == old_handler);
276 Asc_SignalRecover(TRUE);
277
278 /* test typical use with nesting of handlers */
279 CONSOLE_DEBUG("Testing typical use of nested handlers");
280
281 f_handler1_called = FALSE; /* initialize flags for detecting flow */
282 f_handler1_sigval = 0;
283 f_handler2_called = FALSE;
284 f_handler2_sigval = 0;
285 signal1_caught = FALSE;
286 signal2_caught = FALSE;
287 signal3_caught = FALSE;
288
289 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler1)); /* test for SIGFPE */
290 if (0 == setjmp(my_jmp_buf1)) {
291 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler2));
292 if (0 == setjmp(my_jmp_buf2)) {
293 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
294 if (0 == setjmp(g_fpe_env)) {
295 raise(SIGFPE);
296 }
297 else {
298 CU_TEST(f_handler1_called == FALSE);
299 CU_TEST(f_handler1_sigval == 0);
300 CU_TEST(f_handler2_called == FALSE);
301 CU_TEST(f_handler2_sigval == 0);
302 signal3_caught = TRUE;
303 }
304 CU_TEST(FALSE == signal1_caught);
305 CU_TEST(FALSE == signal2_caught);
306 CU_TEST(TRUE == signal3_caught);
307 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
308 f_handler1_called = FALSE;
309 f_handler1_sigval = 0;
310 f_handler2_called = FALSE;
311 f_handler2_sigval = 0;
312 signal1_caught = FALSE;
313 signal2_caught = FALSE;
314 signal3_caught = FALSE;
315 raise(SIGFPE);
316 }
317 else {
318 CU_TEST(f_handler1_called == FALSE);
319 CU_TEST(f_handler1_sigval == 0);
320 CU_TEST(f_handler2_called == TRUE);
321 CU_TEST(f_handler2_sigval == SIGFPE);
322 signal2_caught = TRUE;
323 }
324 CU_TEST(FALSE == signal1_caught);
325 CU_TEST(TRUE == signal2_caught);
326 CU_TEST(FALSE == signal3_caught);
327 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler2));
328 f_handler1_called = FALSE;
329 f_handler1_sigval = 0;
330 f_handler2_called = FALSE;
331 f_handler2_sigval = 0;
332 signal1_caught = FALSE;
333 signal2_caught = FALSE;
334 signal3_caught = FALSE;
335 raise(SIGFPE);
336 }
337 else {
338 CU_TEST(f_handler1_called == TRUE);
339 CU_TEST(f_handler1_sigval == SIGFPE);
340 CU_TEST(f_handler2_called == FALSE);
341 CU_TEST(f_handler2_sigval == 0);
342 signal1_caught = TRUE;
343 }
344 CU_TEST(TRUE == signal1_caught);
345 CU_TEST(FALSE == signal2_caught);
346 CU_TEST(FALSE == signal3_caught);
347 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));
348
349 f_handler1_called = FALSE; /* initialize flags for detecting flow */
350 f_handler1_sigval = 0;
351 f_handler2_called = FALSE;
352 f_handler2_sigval = 0;
353 signal1_caught = FALSE;
354 signal2_caught = FALSE;
355 signal3_caught = FALSE;
356
357 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler2)); /* test for SIGINT */
358 if (0 == setjmp(my_jmp_buf2)) {
359 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
360 if (0 == setjmp(g_int_env)) {
361 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));
362 if (0 == setjmp(my_jmp_buf1)) {
363 raise(SIGINT);
364 }
365 else {
366 CU_TEST(f_handler1_called == TRUE);
367 CU_TEST(f_handler1_sigval == SIGINT);
368 CU_TEST(f_handler2_called == FALSE);
369 CU_TEST(f_handler2_sigval == 0);
370 signal3_caught = TRUE;
371 }
372 CU_TEST(FALSE == signal1_caught);
373 CU_TEST(FALSE == signal2_caught);
374 CU_TEST(TRUE == signal3_caught);
375 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
376 f_handler1_called = FALSE;
377 f_handler1_sigval = 0;
378 f_handler2_called = FALSE;
379 f_handler2_sigval = 0;
380 signal1_caught = FALSE;
381 signal2_caught = FALSE;
382 signal3_caught = FALSE;
383 raise(SIGINT);
384 }
385 else {
386 CU_TEST(f_handler1_called == FALSE);
387 CU_TEST(f_handler1_sigval == 0);
388 CU_TEST(f_handler2_called == FALSE);
389 CU_TEST(f_handler2_sigval == 0);
390 signal2_caught = TRUE;
391 }
392 CU_TEST(FALSE == signal1_caught);
393 CU_TEST(TRUE == signal2_caught);
394 CU_TEST(FALSE == signal3_caught);
395 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
396 f_handler1_called = FALSE;
397 f_handler1_sigval = 0;
398 f_handler2_called = FALSE;
399 f_handler2_sigval = 0;
400 signal1_caught = FALSE;
401 signal2_caught = FALSE;
402 signal3_caught = FALSE;
403 raise(SIGINT);
404 }
405 else {
406 CU_TEST(f_handler1_called == FALSE);
407 CU_TEST(f_handler1_sigval == 0);
408 CU_TEST(f_handler2_called == TRUE);
409 CU_TEST(f_handler2_sigval == SIGINT);
410 signal1_caught = TRUE;
411 }
412 CU_TEST(TRUE == signal1_caught);
413 CU_TEST(FALSE == signal2_caught);
414 CU_TEST(FALSE == signal3_caught);
415 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));
416
417 f_handler1_called = FALSE; /* initialize flags for detecting flow */
418 f_handler1_sigval = 0;
419 f_handler2_called = FALSE;
420 f_handler2_sigval = 0;
421 signal1_caught = FALSE;
422 signal2_caught = FALSE;
423 signal3_caught = FALSE;
424
425 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap)); /* test for SIGSEGV */
426 if (0 == setjmp(g_seg_env)) {
427 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler2));
428 if (0 == setjmp(my_jmp_buf2)) {
429 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler1));
430 if (0 == setjmp(my_jmp_buf1)) {
431 raise(SIGSEGV);
432 }
433 else {
434 CU_TEST(f_handler1_called == TRUE);
435 CU_TEST(f_handler1_sigval == SIGSEGV);
436 CU_TEST(f_handler2_called == FALSE);
437 CU_TEST(f_handler2_sigval == 0);
438 signal3_caught = TRUE;
439 }
440 CU_TEST(FALSE == signal1_caught);
441 CU_TEST(FALSE == signal2_caught);
442 CU_TEST(TRUE == signal3_caught);
443 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler1));
444 f_handler1_called = FALSE;
445 f_handler1_sigval = 0;
446 f_handler2_called = FALSE;
447 f_handler2_sigval = 0;
448 signal1_caught = FALSE;
449 signal2_caught = FALSE;
450 signal3_caught = FALSE;
451 raise(SIGSEGV);
452 }
453 else {
454 CU_TEST(f_handler1_called == FALSE);
455 CU_TEST(f_handler1_sigval == 0);
456 CU_TEST(f_handler2_called == TRUE);
457 CU_TEST(f_handler2_sigval == SIGSEGV);
458 signal2_caught = TRUE;
459 }
460 CU_TEST(FALSE == signal1_caught);
461 CU_TEST(TRUE == signal2_caught);
462 CU_TEST(FALSE == signal3_caught);
463 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler2));
464 f_handler1_called = FALSE;
465 f_handler1_sigval = 0;
466 f_handler2_called = FALSE;
467 f_handler2_sigval = 0;
468 signal1_caught = FALSE;
469 signal2_caught = FALSE;
470 signal3_caught = FALSE;
471 raise(SIGSEGV);
472 }
473 else {
474 CU_TEST(f_handler1_called == FALSE);
475 CU_TEST(f_handler1_sigval == 0);
476 CU_TEST(f_handler2_called == FALSE);
477 CU_TEST(f_handler2_sigval == 0);
478 signal1_caught = TRUE;
479 }
480 CU_TEST(TRUE == signal1_caught);
481 CU_TEST(FALSE == signal2_caught);
482 CU_TEST(FALSE == signal3_caught);
483 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
484
485 CONSOLE_DEBUG("Check recovered signals");
486
487 old_handler = signal(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
488 CU_TEST(my_handler1 == old_handler);
489 old_handler = signal(SIGINT, SIG_DFL);
490 CU_TEST(NULL == old_handler);
491 old_handler = signal(SIGSEGV, SIG_DFL);
492 CU_TEST(my_handler2 == old_handler);
493 Asc_SignalRecover(TRUE);
494
495 Asc_SignalDestroy();
496
497 old_handler = signal(SIGFPE, SIG_DFL); /* original handlers should still be in place */
498 CU_TEST(my_handler1 == old_handler);
499 old_handler = signal(SIGINT, SIG_DFL);
500 CU_TEST(NULL == old_handler);
501 old_handler = signal(SIGSEGV, SIG_DFL);
502 CU_TEST(my_handler2 == old_handler);
503 Asc_SignalRecover(TRUE);
504
505 #endif /* NO_SIGNAL_TRAPS */
506
507 if (NULL != old_fpe_handler) /* restore any pre-existing handlers */
508 signal(SIGFPE, old_fpe_handler);
509 if (NULL != old_int_handler)
510 signal(SIGINT, old_int_handler);
511 if (NULL != old_seg_handler)
512 signal(SIGSEGV, old_seg_handler);
513
514 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
515
516 CONSOLE_DEBUG("Finished");
517 }
518
519 /*===========================================================================*/
520 /* Registration information */
521
522 static CU_TestInfo ascSignal_test_list[] = {
523 {"ascSignal", test_ascSignal},
524 CU_TEST_INFO_NULL
525 };
526
527 static CU_SuiteInfo suites[] = {
528 {"utilities_ascSignal", NULL, NULL, ascSignal_test_list},
529 CU_SUITE_INFO_NULL
530 };
531
532 /*-------------------------------------------------------------------*/
533 CU_ErrorCode test_register_utilities_ascSignal(void)
534 {
535 return CU_register_suites(suites);
536 }

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