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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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