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

Contents of /trunk/base/generic/utilities/test/test_ascSignal.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1002 - (show annotations) (download) (as text)
Sat Dec 30 14:27:47 2006 UTC (13 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 22700 byte(s)
Some work on fixing error with test_ascSignal.
Breaking down into smaller test cases.
Removed some debug output from detection of ASC_RESETNEEDED.
Changed all calls 'signal' to 'SIGNAL' macro that includes optional debug output.
Removed 'libasctest.so' (made part of libasctestsuite.so FWIW)
Fixed big in test.c wrt CUEA_ABORT.
Added 'print_stack' and 'Asc_SignalPrintStack' and 'Asc_SignalStackLength'.

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 <utilities/ascConfig.h>
26 #ifdef __WIN32__
27 #include <io.h>
28 #endif
29 #include <utilities/ascMalloc.h>
30 #include <utilities/ascSignal.h>
31 #include "CUnit/CUnit.h"
32 #include "test_ascSignal.h"
33 #include "printutil.h"
34
35 static JMP_BUF my_jmp_buf1;
36
37 #define MEMUSED(N) CU_TEST(ascmeminuse()==N)
38
39 static int f_handler1_called;
40 static int f_handler1_sigval;
41 /*
42 * Signal handler for unit tests.
43 * Resets the signal handlers and sets f_handler1_called to
44 * TRUE and f_handler1_sigval to the signal type code (-1 if
45 * an unsupported sigval). Then longjmp's using
46 * my_jmp_buf1 and the sigval.
47 */
48 void my_handler1(int sigval)
49 {
50 f_handler1_called = TRUE;
51 Asc_SignalRecover(FALSE);
52 switch (sigval)
53 {
54 case SIGFPE:
55 f_handler1_sigval = SIGFPE;
56 FPRESET;
57 break;
58 case SIGINT:
59 f_handler1_sigval = SIGINT;
60 break;
61 case SIGSEGV:
62 f_handler1_sigval = SIGSEGV;
63 break;
64 default:
65 f_handler1_sigval = -1;
66 break;
67 }
68 LONGJMP(my_jmp_buf1, sigval);
69 }
70
71 static JMP_BUF my_jmp_buf2;
72
73 static int f_handler2_called;
74 static int f_handler2_sigval;
75 /*
76 * Signal handler for unit tests.
77 * Resets the signal handlers and sets f_handler1_called to
78 * TRUE and f_handler1_sigval to the signal type code (-1 if
79 * an unsupported sigval). Then longjmp's using
80 * my_jmp_buf1 and the sigval.
81 */
82 void my_handler2(int sigval)
83 {
84 f_handler2_called = TRUE;
85 Asc_SignalRecover(FALSE);
86 switch (sigval)
87 {
88 case SIGFPE:
89 f_handler2_sigval = SIGFPE;
90 FPRESET;
91 break;
92 case SIGINT:
93 f_handler2_sigval = SIGINT;
94 break;
95 case SIGSEGV:
96 f_handler2_sigval = SIGSEGV;
97 break;
98 default:
99 f_handler2_sigval = -1;
100 break;
101 }
102 LONGJMP(my_jmp_buf2, sigval);
103 }
104
105 static int old_active = 0;
106 static SigHandlerFn *old_fpe_handler = NULL;
107 static SigHandlerFn *old_int_handler = NULL;
108 static SigHandlerFn *old_seg_handler = NULL;
109
110 static void store_current_signals(void){
111 CU_TEST(old_active==0);
112 old_active = 1;
113 old_fpe_handler = SIGNAL(SIGFPE, my_handler1); /* save any pre-existing handlers */
114 old_int_handler = SIGNAL(SIGINT, my_handler1);
115 old_seg_handler = SIGNAL(SIGSEGV, my_handler1);
116 }
117
118 static void restore_previous_signals(void){
119 CU_TEST(old_active==1);
120 if (NULL != old_fpe_handler) /* restore any pre-existing handlers */
121 SIGNAL(SIGFPE, old_fpe_handler);
122 if (NULL != old_int_handler)
123 SIGNAL(SIGINT, old_int_handler);
124 if (NULL != old_seg_handler)
125 SIGNAL(SIGSEGV, old_seg_handler);
126 old_active = 0;
127 }
128
129 /*----------------------------------------*/
130
131 static void test_ascsignal_basic(void){
132
133 SigHandlerFn *old_handler;
134 volatile int signal1_caught;
135
136 #ifdef NO_SIGNAL_TRAPS
137 /* no point in testing if the functionality is disabled */
138 CU_FAIL("Signal handler manager not enabled.");
139 return;
140 #else
141
142 CONSOLE_DEBUG("SIGFPE = %d",SIGFPE);
143 CONSOLE_DEBUG("SIGINT = %d",SIGINT);
144 CONSOLE_DEBUG("SIGSEGV= %d",SIGSEGV);
145
146 store_current_signals();
147
148 SIGNAL(SIGFPE, my_handler1); /* install some pre-existing handlers */
149 SIGNAL(SIGINT, SIG_DFL);
150 SIGNAL(SIGSEGV, my_handler2);
151
152 /* Asc_SignalInit(), Asc_SignalDestroy() - not much to test */
153
154 CU_TEST(0 == Asc_SignalInit()); /* initialize the signal manager */
155
156 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* previously-installed handlers should still be active */
157 CU_TEST(my_handler1 == old_handler);
158 old_handler = SIGNAL(SIGINT, SIG_DFL);
159 CU_TEST(NULL == old_handler);
160 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
161 CU_TEST(my_handler2 == old_handler);
162
163 Asc_SignalRecover(TRUE);
164
165 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* handlers should have been reinstalled */
166 CU_TEST(my_handler1 == old_handler);
167 old_handler = SIGNAL(SIGINT, SIG_DFL);
168 CU_TEST(NULL == old_handler);
169 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
170 CU_TEST(my_handler2 == old_handler);
171
172 Asc_SignalRecover(TRUE);
173
174 restore_previous_signals();
175 Asc_SignalDestroy();
176 MEMUSED(0);
177 }
178
179 /*----------------------------------------*/
180
181 static void test_ascsignal_pushpopint(void){
182 SigHandlerFn *old_handler;
183 volatile int signal1_caught;
184
185 store_current_signals();
186
187 SIGNAL(SIGINT, SIG_IGN);
188 CU_TEST(0==Asc_SignalInit());
189
190 CU_TEST(0==Asc_SignalStackLength(SIGINT));
191
192 CU_TEST(0==Asc_SignalHandlerPush(SIGINT, my_handler1)); /* ok - supported signal, ok func */
193 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
194
195 CONSOLE_DEBUG("Stack should have my_handler1...");
196 Asc_SignalPrintStack(SIGINT);
197
198 old_handler = SIGNAL(SIGINT, SIG_DFL);
199 CU_TEST(old_handler == my_handler1);
200
201 CU_TEST(0 != Asc_SignalHandlerPush(SIGILL, Asc_SignalTrap)); /* unsupported signal type */
202 CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2)); /* unsupported signal type */
203 Asc_SignalRecover(TRUE);
204 CU_TEST(Asc_SignalStackLength(SIGINT)==1);
205
206 old_handler = SIGNAL(SIGINT, SIG_DFL);
207 CU_TEST(old_handler == my_handler1);
208
209 Asc_SignalRecover(TRUE);
210 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
211 CU_TEST(Asc_SignalStackLength(SIGINT)==0);
212
213 old_handler = SIGNAL(SIGINT, SIG_DFL);
214 CU_TEST(old_handler == SIG_IGN);
215
216 Asc_SignalRecover(TRUE);
217 CU_TEST(Asc_SignalStackLength(SIGINT)==0);
218
219 old_handler = SIGNAL(SIGINT, SIG_DFL);
220 CU_TEST(SIG_IGN == old_handler);
221
222 Asc_SignalRecover(TRUE);
223
224 old_handler = SIGNAL(SIGINT, SIG_DFL);
225 CU_TEST(SIG_IGN == old_handler);
226 Asc_SignalRecover(TRUE);
227
228 CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1)); /* unsupported signal type */
229 CU_TEST(0 != Asc_SignalHandlerPop(SIGABRT, Asc_SignalTrap)); /* unsupported signal type */
230
231 old_handler = SIGNAL(SIGINT, SIG_DFL);
232 CU_TEST(SIG_IGN == old_handler);
233
234 restore_previous_signals();
235 Asc_SignalDestroy();
236 MEMUSED(0);
237 }
238
239 /*----------------------------------------*/
240
241 static void test_ascsignal_pushpop(void){
242 SigHandlerFn *old_handler;
243 volatile int signal1_caught;
244
245 store_current_signals();
246
247 SIGNAL(SIGFPE, my_handler1); /* install some pre-existing handlers */
248 SIGNAL(SIGINT, SIG_IGN);
249 SIGNAL(SIGSEGV, my_handler2);
250
251 CU_TEST(0 == Asc_SignalInit());
252
253 /* test Asc_SignalPush(), Asc_SignalPop() */
254 CONSOLE_DEBUG("Initial stack for SIGFPE...");
255 Asc_SignalPrintStack(SIGFPE);
256
257 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap)); /* ok - supported signal, ok func */
258 old_handler = SIGNAL(SIGFPE, SIG_DFL);
259 CU_TEST(old_handler == Asc_SignalTrap);
260
261 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1)); /* ok - supported signal, ok func */
262 CONSOLE_DEBUG("Stack should have my_handler1...");
263 Asc_SignalPrintStack(SIGINT);
264 old_handler = SIGNAL(SIGINT, SIG_DFL);
265 CU_TEST(old_handler == my_handler1);
266
267 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, NULL)); /* NULL func - should have no effect */
268 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
269 CU_TEST(old_handler == my_handler2); /* old handler should still be installed */
270
271 CU_TEST(0 != Asc_SignalHandlerPush(SIGILL, Asc_SignalTrap)); /* unsupported signal type */
272 CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2)); /* unsupported signal type */
273 Asc_SignalRecover(TRUE);
274
275 /* we expect: SIGSEGV = my_handler2, SIGINT = my_handler1, SIGFPE = Asc_SignalTrap */
276 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* handlers should have been reinstalled */
277 CU_TEST(old_handler == Asc_SignalTrap);
278 old_handler = SIGNAL(SIGINT, SIG_DFL);
279 CU_TEST(old_handler == my_handler1);
280 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
281 CU_TEST(old_handler == my_handler2);
282
283 Asc_SignalRecover(TRUE);
284 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
285
286 /* we expect: SIGSEGV = my_handler2, SIGINT = SIG_IGN, SIGFPE = Asc_SignalTrap */
287 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* SIGINT should be reset to no handler */
288 CU_TEST(old_handler == SIG_DFL);
289 old_handler = SIGNAL(SIGINT, SIG_DFL);
290 CU_TEST(old_handler == SIG_IGN);
291 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
292 CU_TEST(old_handler == my_handler2);
293
294 Asc_SignalRecover(TRUE);
295 CONSOLE_DEBUG("Stack for SIGFPE...");
296 Asc_SignalPrintStack(SIGFPE);
297
298 CU_TEST(0 != Asc_SignalHandlerPop(SIGFPE, my_handler1)); /* wrong handler indicated */
299
300 CONSOLE_DEBUG("Stack for SIGFPE...");
301 Asc_SignalPrintStack(SIGFPE);
302
303 /* we expect: SIGSEGV = my_handler2, SIGINT = SIG_IGN, SIGFPE = Asc_SignalTrap */
304 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* so SIGFPE should be not be reset */
305 CU_TEST(Asc_SignalTrap == old_handler);
306 old_handler = SIGNAL(SIGINT, SIG_DFL);
307 CU_TEST(SIG_IGN == old_handler);
308 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
309 CU_TEST(my_handler2 == old_handler);
310
311 Asc_SignalRecover(TRUE);
312 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* but Asc_SignalRecover should reset it */
313
314 /* we expect: SIGSEGV = my_handler2, SIGINT = SIG_IGN, SIGFPE = SIG_DFL */
315 CU_TEST(my_handler1 == old_handler);
316 old_handler = SIGNAL(SIGINT, SIG_DFL);
317 CU_TEST(SIG_IGN == old_handler);
318 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
319 CU_TEST(my_handler2 == old_handler);
320 Asc_SignalRecover(TRUE);
321
322 CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1)); /* unsupported signal type */
323 CU_TEST(0 != Asc_SignalHandlerPop(SIGABRT, Asc_SignalTrap)); /* unsupported signal type */
324
325 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* should be no change in handlers */
326 CU_TEST(my_handler1 == old_handler);
327 old_handler = SIGNAL(SIGINT, SIG_DFL);
328 CU_TEST(SIG_IGN == old_handler);
329 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
330 CU_TEST(my_handler2 == old_handler);
331 Asc_SignalRecover(TRUE);
332
333 restore_previous_signals();
334 Asc_SignalDestroy();
335 MEMUSED(0);
336 }
337
338 /*----------------------------------------*/
339
340 static void test_ascsignal_trap(void){
341 SigHandlerFn *old_handler;
342 volatile int signal1_caught;
343
344 store_current_signals();
345
346 SIGNAL(SIGFPE,my_handler1);
347 SIGNAL(SIGINT,NULL);
348 SIGNAL(SIGSEGV,my_handler2);
349
350 CU_TEST(0 == Asc_SignalInit());
351
352 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
353 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
354 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap));
355
356 signal1_caught = FALSE;
357 CONSOLE_DEBUG("Raise and catch a SIGFPE...");
358 if (0 == SETJMP(g_fpe_env)){
359 CONSOLE_DEBUG("Raising SIGFPE");
360 raise(SIGFPE);
361 CONSOLE_DEBUG("SHOULDN'T BE HERE");
362 CU_FAIL("Shouldn't be here");
363 }else{
364 signal1_caught = TRUE;
365 CONSOLE_DEBUG("Caught SIGFPE");
366 }
367 CU_TEST(TRUE == signal1_caught);
368
369 signal1_caught = FALSE;
370 /* raise & catch a SIGINT */
371 if (0 == SETJMP(g_int_env)){
372 raise(SIGINT);
373 }else{
374 signal1_caught = TRUE;
375 CONSOLE_DEBUG("Caught SIGINT");
376 }
377 CU_TEST(TRUE == signal1_caught);
378
379 signal1_caught = FALSE;
380 /* raise & catch a SIGSEGV */
381 if (0 == SETJMP(g_seg_env)) {
382 raise(SIGSEGV);
383 }else{
384 signal1_caught = TRUE;
385 CONSOLE_DEBUG("Caught SIGSEGV");
386 }
387 CU_TEST(TRUE == signal1_caught);
388
389
390 Asc_SignalRecover(TRUE);
391
392 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
393 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
394 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
395
396 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
397 CU_TEST(my_handler1 == old_handler);
398 old_handler = SIGNAL(SIGINT, SIG_DFL);
399 CU_TEST(NULL == old_handler);
400 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
401 CU_TEST(my_handler2 == old_handler);
402 Asc_SignalRecover(TRUE);
403
404 restore_previous_signals();
405 Asc_SignalDestroy();
406 MEMUSED(0);
407 }
408
409 /*----------------------------------------*/
410
411 static void test_ascsignal_nestingfpe(void){
412 volatile int signal1_caught;
413 volatile int signal2_caught;
414 volatile int signal3_caught;
415
416 store_current_signals();
417 CU_TEST(0 == Asc_SignalInit());
418
419 /* test typical use with nesting of handlers */
420
421 f_handler1_called = FALSE; /* initialize flags for detecting flow */
422 f_handler1_sigval = 0;
423 f_handler2_called = FALSE;
424 f_handler2_sigval = 0;
425 signal1_caught = FALSE;
426 signal2_caught = FALSE;
427 signal3_caught = FALSE;
428
429 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler1));
430 if (0 == SETJMP(my_jmp_buf1)) {
431 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler2));
432 if (0 == SETJMP(my_jmp_buf2)) {
433 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
434 if(0 == SETJMP(g_fpe_env)) {
435 raise(SIGFPE);
436 CONSOLE_DEBUG("...");
437 CU_FAIL("Can't be here! No signal was raised!");
438 }else{
439 CONSOLE_DEBUG("...");
440 CU_TEST(f_handler1_called == FALSE);
441 CU_TEST(f_handler1_sigval == 0);
442 CU_TEST(f_handler2_called == FALSE);
443 CU_TEST(f_handler2_sigval == 0);
444 signal3_caught = TRUE;
445 }
446 CONSOLE_DEBUG("...");
447 CU_TEST(FALSE == signal1_caught);
448 CU_TEST(FALSE == signal2_caught);
449 CU_TEST(TRUE == signal3_caught);
450 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
451 f_handler1_called = FALSE;
452 f_handler1_sigval = 0;
453 f_handler2_called = FALSE;
454 f_handler2_sigval = 0;
455 signal1_caught = FALSE;
456 signal2_caught = FALSE;
457 signal3_caught = FALSE;
458 raise(SIGFPE);
459 }else{
460 CONSOLE_DEBUG("...");
461 CU_TEST(f_handler1_called == FALSE);
462 CU_TEST(f_handler1_sigval == 0);
463 CU_TEST(f_handler2_called == TRUE);
464 CU_TEST(f_handler2_sigval == SIGFPE);
465 signal2_caught = TRUE;
466 }
467 CU_TEST(FALSE == signal1_caught);
468 CU_TEST(TRUE == signal2_caught);
469 CU_TEST(FALSE == signal3_caught);
470 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler2));
471 f_handler1_called = FALSE;
472 f_handler1_sigval = 0;
473 f_handler2_called = FALSE;
474 f_handler2_sigval = 0;
475 signal1_caught = FALSE;
476 signal2_caught = FALSE;
477 signal3_caught = FALSE;
478 raise(SIGFPE);
479 }else{
480 CONSOLE_DEBUG("...");
481 CU_TEST(f_handler1_called == TRUE);
482 CU_TEST(f_handler1_sigval == SIGFPE);
483 CU_TEST(f_handler2_called == FALSE);
484 CU_TEST(f_handler2_sigval == 0);
485 signal1_caught = TRUE;
486 }
487 CU_TEST(TRUE == signal1_caught);
488 CU_TEST(FALSE == signal2_caught);
489 CU_TEST(FALSE == signal3_caught);
490 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));
491
492 CONSOLE_DEBUG("...");
493
494 restore_previous_signals();
495 Asc_SignalDestroy();
496 MEMUSED(0);
497 }
498
499 /*----------------------------------------*/
500
501 static void test_ascsignal_nestingint(void){
502 volatile int signal1_caught;
503 volatile int signal2_caught;
504 volatile int signal3_caught;
505 SigHandlerFn *old_handler;
506
507 store_current_signals();
508 CU_TEST(0 == Asc_SignalInit());
509
510 f_handler1_called = FALSE; /* initialize flags for detecting flow */
511 f_handler1_sigval = 0;
512 f_handler2_called = FALSE;
513 f_handler2_sigval = 0;
514 signal1_caught = FALSE;
515 signal2_caught = FALSE;
516 signal3_caught = FALSE;
517
518 CONSOLE_DEBUG("my_handler1 = %p",my_handler1);
519 CONSOLE_DEBUG("my_handler2 = %p",my_handler2);
520 CONSOLE_DEBUG("Asc_SignalTrap = %p",Asc_SignalTrap);
521
522 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler2)); /* test for SIGINT */
523 if (0 == SETJMP(my_jmp_buf2)) {
524 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
525 if (0 == SETJMP(g_int_env)) {
526 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));
527 if (0 == SETJMP(my_jmp_buf1)) {
528 CONSOLE_DEBUG("Raising to my_handler1");
529 raise(SIGINT);
530 CONSOLE_DEBUG("SHOULDN'T BE HERE!");
531 CU_FAIL("Shouldn't be here!");
532 }else{
533 CONSOLE_DEBUG("Caught from my_handler1");
534 CU_TEST(f_handler1_called == TRUE);
535 CU_TEST(f_handler1_sigval == SIGINT);
536 CU_TEST(f_handler2_called == FALSE);
537 CU_TEST(f_handler2_sigval == 0);
538 signal3_caught = TRUE;
539 }
540 CU_TEST(FALSE == signal1_caught);
541 CU_TEST(FALSE == signal2_caught);
542 CU_TEST(TRUE == signal3_caught);
543 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
544
545 /* check that 'Asc_SignalTrap' is now installed for SIGINT */
546 old_handler = SIGNAL(SIGINT,SIG_DFL);
547 CU_TEST(old_handler == Asc_SignalTrap);
548 old_handler = SIGNAL(SIGINT,Asc_SignalTrap);
549 CU_TEST(old_handler == SIG_DFL);
550
551 f_handler1_called = FALSE;
552 f_handler1_sigval = 0;
553 f_handler2_called = FALSE;
554 f_handler2_sigval = 0;
555 signal1_caught = FALSE;
556 signal2_caught = FALSE;
557 signal3_caught = FALSE;
558 CONSOLE_DEBUG("Raising to Asc_SignalTrap");
559 raise(SIGINT);
560 CONSOLE_DEBUG("SHOULDN'T BE HERE!");
561 CU_FAIL("Shouldn't be here!");
562 }else{
563 CONSOLE_DEBUG("Caught from Asc_SignalTrap");
564 CU_TEST(f_handler1_called == FALSE);
565 CU_TEST(f_handler1_sigval == 0);
566 CU_TEST(f_handler2_called == FALSE);
567 CU_TEST(f_handler2_sigval == 0);
568 signal2_caught = TRUE;
569 }
570 CU_TEST(FALSE == signal1_caught);
571 CU_TEST(TRUE == signal2_caught);
572 CU_TEST(FALSE == signal3_caught);
573 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
574 f_handler1_called = FALSE;
575 f_handler1_sigval = 0;
576 f_handler2_called = FALSE;
577 f_handler2_sigval = 0;
578 signal1_caught = FALSE;
579 signal2_caught = FALSE;
580 signal3_caught = FALSE;
581 CONSOLE_DEBUG("Raising to my_handler2");
582 raise(SIGINT);
583 CONSOLE_DEBUG("SHOULDN'T BE HERE!");
584 CU_FAIL("Shouldn't be here!");
585 }else{
586 CONSOLE_DEBUG("Caught from my_handler2");
587 CU_TEST(f_handler1_called == FALSE);
588 CU_TEST(f_handler1_sigval == 0);
589 CU_TEST(f_handler2_called == TRUE);
590 CU_TEST(f_handler2_sigval == SIGINT);
591 signal1_caught = TRUE;
592 }
593 CU_TEST(TRUE == signal1_caught);
594 CU_TEST(FALSE == signal2_caught);
595 CU_TEST(FALSE == signal3_caught);
596 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));
597
598 restore_previous_signals();
599 Asc_SignalDestroy();
600 MEMUSED(0);
601 }
602
603 /*----------------------------------------*/
604
605 static void test_ascsignal_nestingsegv(void){
606
607 SigHandlerFn *old_handler;
608 volatile int signal1_caught;
609 volatile int signal2_caught;
610 volatile int signal3_caught;
611
612 store_current_signals();
613 CU_TEST(0 == Asc_SignalInit());
614
615 f_handler1_called = FALSE; /* initialize flags for detecting flow */
616 f_handler1_sigval = 0;
617 f_handler2_called = FALSE;
618 f_handler2_sigval = 0;
619 signal1_caught = FALSE;
620 signal2_caught = FALSE;
621 signal3_caught = FALSE;
622
623 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap)); /* test for SIGSEGV */
624 if (0 == SETJMP(g_seg_env)) {
625 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler2));
626 if (0 == SETJMP(my_jmp_buf2)) {
627 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler1));
628 if (0 == SETJMP(my_jmp_buf1)) {
629 raise(SIGSEGV);
630 }
631 else {
632 CU_TEST(f_handler1_called == TRUE);
633 CU_TEST(f_handler1_sigval == SIGSEGV);
634 CU_TEST(f_handler2_called == FALSE);
635 CU_TEST(f_handler2_sigval == 0);
636 signal3_caught = TRUE;
637 }
638 CU_TEST(FALSE == signal1_caught);
639 CU_TEST(FALSE == signal2_caught);
640 CU_TEST(TRUE == signal3_caught);
641 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler1));
642 f_handler1_called = FALSE;
643 f_handler1_sigval = 0;
644 f_handler2_called = FALSE;
645 f_handler2_sigval = 0;
646 signal1_caught = FALSE;
647 signal2_caught = FALSE;
648 signal3_caught = FALSE;
649 raise(SIGSEGV);
650 }
651 else {
652 CU_TEST(f_handler1_called == FALSE);
653 CU_TEST(f_handler1_sigval == 0);
654 CU_TEST(f_handler2_called == TRUE);
655 CU_TEST(f_handler2_sigval == SIGSEGV);
656 signal2_caught = TRUE;
657 }
658 CU_TEST(FALSE == signal1_caught);
659 CU_TEST(TRUE == signal2_caught);
660 CU_TEST(FALSE == signal3_caught);
661 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler2));
662 f_handler1_called = FALSE;
663 f_handler1_sigval = 0;
664 f_handler2_called = FALSE;
665 f_handler2_sigval = 0;
666 signal1_caught = FALSE;
667 signal2_caught = FALSE;
668 signal3_caught = FALSE;
669 raise(SIGSEGV);
670 }
671 else {
672 CU_TEST(f_handler1_called == FALSE);
673 CU_TEST(f_handler1_sigval == 0);
674 CU_TEST(f_handler2_called == FALSE);
675 CU_TEST(f_handler2_sigval == 0);
676 signal1_caught = TRUE;
677 }
678 CU_TEST(TRUE == signal1_caught);
679 CU_TEST(FALSE == signal2_caught);
680 CU_TEST(FALSE == signal3_caught);
681 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
682
683 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
684 CU_TEST(my_handler1 == old_handler);
685 old_handler = SIGNAL(SIGINT, SIG_DFL);
686 CU_TEST(NULL == old_handler);
687 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
688 CU_TEST(my_handler2 == old_handler);
689 Asc_SignalRecover(TRUE);
690
691 Asc_SignalDestroy();
692
693 old_handler = SIGNAL(SIGFPE, SIG_DFL); /* original handlers should still be in place */
694 CU_TEST(my_handler1 == old_handler);
695 old_handler = SIGNAL(SIGINT, SIG_DFL);
696 CU_TEST(NULL == old_handler);
697 old_handler = SIGNAL(SIGSEGV, SIG_DFL);
698 CU_TEST(my_handler2 == old_handler);
699 Asc_SignalRecover(TRUE);
700
701 #endif /* NO_SIGNAL_TRAPS */
702
703 restore_previous_signals();
704 Asc_SignalDestroy();
705 MEMUSED(0);
706 }
707
708 /*===========================================================================*/
709 /* Registration information */
710
711 #define T(N) {#N,test_ascsignal_##N}
712 static CU_TestInfo ascSignal_test_list[] = {
713 T(basic)
714 , T(pushpopint)
715 , T(pushpop)
716 , T(trap)
717 , T(nestingfpe)
718 , T(nestingint)
719 , T(nestingsegv)
720 , CU_TEST_INFO_NULL
721 };
722 #undef T
723
724 static CU_SuiteInfo suites[] = {
725 {"test_utilities_ascSignal", NULL, NULL, ascSignal_test_list},
726 CU_SUITE_INFO_NULL
727 };
728
729 /*-------------------------------------------------------------------*/
730 CU_ErrorCode test_register_utilities_ascSignal(void)
731 {
732 return CU_register_suites(suites);
733 }

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