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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1001 by johnpye, Sun Dec 24 01:33:59 2006 UTC revision 1002 by johnpye, Sat Dec 30 14:27:47 2006 UTC
# Line 34  Line 34 
34    
35  static JMP_BUF my_jmp_buf1;  static JMP_BUF my_jmp_buf1;
36    
37    #define MEMUSED(N) CU_TEST(ascmeminuse()==N)
38    
39  static int f_handler1_called;  static int f_handler1_called;
40  static int f_handler1_sigval;  static int f_handler1_sigval;
41  /*  /*
# Line 100  void my_handler2(int sigval) Line 102  void my_handler2(int sigval)
102    LONGJMP(my_jmp_buf2, sigval);    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 test_ascSignal(void)  static void restore_previous_signals(void){
119  {    CU_TEST(old_active==1);
120    SigHandlerFn *old_fpe_handler = NULL;    if (NULL != old_fpe_handler)                /* restore any pre-existing handlers */
121    SigHandlerFn *old_int_handler = NULL;      SIGNAL(SIGFPE, old_fpe_handler);
122    SigHandlerFn *old_seg_handler = NULL;    if (NULL != old_int_handler)
123    SigHandlerFn *old_handler;      SIGNAL(SIGINT, old_int_handler);
124    volatile int signal1_caught;    if (NULL != old_seg_handler)
125    volatile int signal2_caught;      SIGNAL(SIGSEGV, old_seg_handler);
126    volatile int signal3_caught;    old_active = 0;
127    unsigned long prior_meminuse;  }  
128    
129    CONSOLE_DEBUG("Output from console_debug");  /*----------------------------------------*/
   fprintf(stderr,"Output from fprintf\n");  
   CONSOLE_DEBUG("2 Output from console_debug");  
   fprintf(stderr,"2 Output from fprintf\n");  
130    
131    prior_meminuse = ascmeminuse();             /* save meminuse() at start of test function */  static void test_ascsignal_basic(void){
132    
133      SigHandlerFn *old_handler;
134      volatile int signal1_caught;
135    
136  #ifdef NO_SIGNAL_TRAPS  #ifdef NO_SIGNAL_TRAPS
137    /* no point in testing if the functionality is disabled */    /* no point in testing if the functionality is disabled */
138    CU_FAIL("Signal handler manager not enabled.");    CU_FAIL("Signal handler manager not enabled.");
139      return;
140  #else  #else
141      
142    old_fpe_handler = signal(SIGFPE, my_handler1);        /* save any pre-existing handlers */    CONSOLE_DEBUG("SIGFPE = %d",SIGFPE);
143    old_int_handler = signal(SIGINT, my_handler1);    CONSOLE_DEBUG("SIGINT = %d",SIGINT);
144    old_seg_handler = signal(SIGSEGV, my_handler1);    CONSOLE_DEBUG("SIGSEGV= %d",SIGSEGV);
145    
146    signal(SIGFPE, my_handler1);                          /* install some pre-existing handlers */    store_current_signals();
147    signal(SIGINT, SIG_DFL);  
148    signal(SIGSEGV, my_handler2);    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 */    /* Asc_SignalInit(), Asc_SignalDestroy() - not much to test */
153    
154    CU_TEST(0 == Asc_SignalInit());                       /* initialize the signal manager */    CU_TEST(0 == Asc_SignalInit());                       /* initialize the signal manager */
155    
156    old_handler = signal(SIGFPE, SIG_DFL);                /* previously-installed handlers should still be active */    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* previously-installed handlers should still be active */
157    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
158    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
159    CU_TEST(NULL == old_handler);    CU_TEST(NULL == old_handler);
160    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
161    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
162    
163    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
164    
165    old_handler = signal(SIGFPE, SIG_DFL);                /* handlers should have been reinstalled */    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* handlers should have been reinstalled */
166    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
167    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
168    CU_TEST(NULL == old_handler);    CU_TEST(NULL == old_handler);
169    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
170    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
171    
172    Asc_SignalRecover(TRUE);    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() */    /* 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 */    CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));  /* ok - supported signal, ok func */
258    old_handler = signal(SIGFPE, SIG_DFL);    old_handler = SIGNAL(SIGFPE, SIG_DFL);
259    CU_TEST(Asc_SignalTrap == old_handler);    CU_TEST(old_handler == Asc_SignalTrap);
260    
261    CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));     /* ok - supported signal, ok func */    CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));     /* ok - supported signal, ok func */
262    old_handler = signal(SIGINT, SIG_DFL);    CONSOLE_DEBUG("Stack should have my_handler1...");
263    CU_TEST(my_handler1 == old_handler);    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 */    CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, NULL));           /* NULL func - should have no effect */
268    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
269    CU_TEST(my_handler2 == old_handler);                          /* old handler should still be installed */    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 */    CU_TEST(0 != Asc_SignalHandlerPush(SIGILL, Asc_SignalTrap));  /* unsupported signal type */
272    CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2));    /* unsupported signal type */    CU_TEST(0 != Asc_SignalHandlerPush(SIGABRT, my_handler2));    /* unsupported signal type */
   
273    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
274    
275    old_handler = signal(SIGFPE, SIG_DFL);                /* handlers should have been reinstalled */    /* we expect: SIGSEGV = my_handler2, SIGINT = my_handler1, SIGFPE = Asc_SignalTrap */
276    CU_TEST(Asc_SignalTrap == old_handler);    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* handlers should have been reinstalled */
277    old_handler = signal(SIGINT, SIG_DFL);    CU_TEST(old_handler == Asc_SignalTrap);
278    CU_TEST(my_handler1 == old_handler);    old_handler = SIGNAL(SIGINT, SIG_DFL);
279    old_handler = signal(SIGSEGV, SIG_DFL);    CU_TEST(old_handler == my_handler1);
280    CU_TEST(my_handler2 == old_handler);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
281    Asc_SignalRecover(TRUE);    CU_TEST(old_handler == my_handler2);
282    
283      Asc_SignalRecover(TRUE);
284    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
285    old_handler = signal(SIGFPE, SIG_DFL);                /* SIGINT should be reset to no handler */  
286    CU_TEST(Asc_SignalTrap == old_handler);    /* we expect: SIGSEGV = my_handler2, SIGINT = SIG_IGN, SIGFPE = Asc_SignalTrap */
287    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* SIGINT should be reset to no handler */
288    CU_TEST(NULL == old_handler);    CU_TEST(old_handler == SIG_DFL);
289    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
290    CU_TEST(my_handler2 == old_handler);    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);    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 */    CU_TEST(0 != Asc_SignalHandlerPop(SIGFPE, my_handler1));  /* wrong handler indicated */
299    old_handler = signal(SIGFPE, SIG_DFL);                /* so SIGFPE should be not be reset */  
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);    CU_TEST(Asc_SignalTrap == old_handler);
306    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
307    CU_TEST(NULL == old_handler);    CU_TEST(SIG_IGN == old_handler);
308    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
309    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
310    
311    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
312      old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* but Asc_SignalRecover should reset it */
313    
314    old_handler = signal(SIGFPE, SIG_DFL);                /* but Asc_SignalRecover should reset it */    /* we expect: SIGSEGV = my_handler2, SIGINT = SIG_IGN, SIGFPE = SIG_DFL */
315    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
316    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
317    CU_TEST(NULL == old_handler);    CU_TEST(SIG_IGN == old_handler);
318    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
319    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
320    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
321    
322    CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1));     /* unsupported signal type */    CU_TEST(0 != Asc_SignalHandlerPop(SIGILL, my_handler1));     /* unsupported signal type */
323    CU_TEST(0 != Asc_SignalHandlerPop(SIGABRT, Asc_SignalTrap)); /* unsupported signal type */    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 */    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* should be no change in handlers */
326    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
327    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
328    CU_TEST(NULL == old_handler);    CU_TEST(SIG_IGN == old_handler);
329    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
330    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
331    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
332    
333    /* test Asc_SignalTrap() */    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));    CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
353    CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
354    CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap));
355    
356    signal1_caught = FALSE;    signal1_caught = FALSE;
357    if (0 == SETJMP(g_fpe_env)) {                         /* raise & catch a SIGFPE */    CONSOLE_DEBUG("Raise and catch a SIGFPE...");
358      if (0 == SETJMP(g_fpe_env)){
359        CONSOLE_DEBUG("Raising SIGFPE");
360      raise(SIGFPE);      raise(SIGFPE);
361    }      CONSOLE_DEBUG("SHOULDN'T BE HERE");
362    else {      CU_FAIL("Shouldn't be here");
363      }else{
364      signal1_caught = TRUE;      signal1_caught = TRUE;
365      CU_PASS("SIGFPE caught.");      CONSOLE_DEBUG("Caught SIGFPE");
366    }    }
367    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
368    
369    signal1_caught = FALSE;    signal1_caught = FALSE;
370    if (0 == SETJMP(g_int_env)) {                         /* raise & catch a SIGINT */    /* raise & catch a SIGINT */
371      if (0 == SETJMP(g_int_env)){
372      raise(SIGINT);      raise(SIGINT);
373    }    }else{
   else {  
374      signal1_caught = TRUE;      signal1_caught = TRUE;
375      CU_PASS("SIGINT caught.");      CONSOLE_DEBUG("Caught SIGINT");
376    }    }
377    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
378    
379    signal1_caught = FALSE;    signal1_caught = FALSE;
380    if (0 == SETJMP(g_seg_env)) {                         /* raise & catch a SIGSEGV */    /* raise & catch a SIGSEGV */
381      if (0 == SETJMP(g_seg_env)) {
382      raise(SIGSEGV);      raise(SIGSEGV);
383    }    }else{
   else {  
384      signal1_caught = TRUE;      signal1_caught = TRUE;
385      CU_PASS("SIGSEGV caught.");      CONSOLE_DEBUG("Caught SIGSEGV");
386    }    }
387    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
388    
389  Asc_SignalRecover(TRUE);  
390      Asc_SignalRecover(TRUE);
391    
392    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
393    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
394    CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
395    
396    old_handler = signal(SIGFPE, SIG_DFL);                /* handlers should be restored at this point */    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* handlers should be restored at this point */
397    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
398    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
399    CU_TEST(NULL == old_handler);    CU_TEST(NULL == old_handler);
400    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
401    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
402    Asc_SignalRecover(TRUE);    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 */    /* test typical use with nesting of handlers */
420    
421    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */
# Line 278  Asc_SignalRecover(TRUE); Line 426  Asc_SignalRecover(TRUE);
426    signal2_caught = FALSE;    signal2_caught = FALSE;
427    signal3_caught = FALSE;    signal3_caught = FALSE;
428    
429    CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler1));   /* test for SIGFPE */    CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler1));
430    if (0 == SETJMP(my_jmp_buf1)) {    if (0 == SETJMP(my_jmp_buf1)) {
431      CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler2));      CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler2));
432      if (0 == SETJMP(my_jmp_buf2)) {      if (0 == SETJMP(my_jmp_buf2)) {
433        CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));        CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
434        if (0 == SETJMP(g_fpe_env)) {        if(0 == SETJMP(g_fpe_env)) {
435           raise(SIGFPE);           raise(SIGFPE);
436        }           CONSOLE_DEBUG("...");
437        else {           CU_FAIL("Can't be here! No signal was raised!");
438          }else{
439            CONSOLE_DEBUG("...");
440          CU_TEST(f_handler1_called == FALSE);          CU_TEST(f_handler1_called == FALSE);
441          CU_TEST(f_handler1_sigval == 0);          CU_TEST(f_handler1_sigval == 0);
442          CU_TEST(f_handler2_called == FALSE);          CU_TEST(f_handler2_called == FALSE);
443          CU_TEST(f_handler2_sigval == 0);          CU_TEST(f_handler2_sigval == 0);
444          signal3_caught = TRUE;          signal3_caught = TRUE;
445        }        }
446          CONSOLE_DEBUG("...");
447        CU_TEST(FALSE == signal1_caught);        CU_TEST(FALSE == signal1_caught);
448        CU_TEST(FALSE == signal2_caught);        CU_TEST(FALSE == signal2_caught);
449        CU_TEST(TRUE == signal3_caught);        CU_TEST(TRUE == signal3_caught);
# Line 305  Asc_SignalRecover(TRUE); Line 456  Asc_SignalRecover(TRUE);
456        signal2_caught = FALSE;        signal2_caught = FALSE;
457        signal3_caught = FALSE;        signal3_caught = FALSE;
458        raise(SIGFPE);        raise(SIGFPE);
459      }      }else{
460      else {        CONSOLE_DEBUG("...");
461        CU_TEST(f_handler1_called == FALSE);        CU_TEST(f_handler1_called == FALSE);
462        CU_TEST(f_handler1_sigval == 0);        CU_TEST(f_handler1_sigval == 0);
463        CU_TEST(f_handler2_called == TRUE);        CU_TEST(f_handler2_called == TRUE);
# Line 325  Asc_SignalRecover(TRUE); Line 476  Asc_SignalRecover(TRUE);
476      signal2_caught = FALSE;      signal2_caught = FALSE;
477      signal3_caught = FALSE;      signal3_caught = FALSE;
478      raise(SIGFPE);      raise(SIGFPE);
479    }    }else{
480    else {      CONSOLE_DEBUG("...");
481      CU_TEST(f_handler1_called == TRUE);      CU_TEST(f_handler1_called == TRUE);
482      CU_TEST(f_handler1_sigval == SIGFPE);      CU_TEST(f_handler1_sigval == SIGFPE);
483      CU_TEST(f_handler2_called == FALSE);      CU_TEST(f_handler2_called == FALSE);
# Line 338  Asc_SignalRecover(TRUE); Line 489  Asc_SignalRecover(TRUE);
489    CU_TEST(FALSE == signal3_caught);    CU_TEST(FALSE == signal3_caught);
490    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));    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 */    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */
511    f_handler1_sigval = 0;    f_handler1_sigval = 0;
512    f_handler2_called = FALSE;    f_handler2_called = FALSE;
# Line 346  Asc_SignalRecover(TRUE); Line 515  Asc_SignalRecover(TRUE);
515    signal2_caught = FALSE;    signal2_caught = FALSE;
516    signal3_caught = FALSE;    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 */    CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler2));   /* test for SIGINT */
523    if (0 == SETJMP(my_jmp_buf2)) {    if (0 == SETJMP(my_jmp_buf2)) {
524      CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));      CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
525      if (0 == SETJMP(g_int_env)) {      if (0 == SETJMP(g_int_env)) {
526        CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));        CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));
527        if (0 == SETJMP(my_jmp_buf1)) {        if (0 == SETJMP(my_jmp_buf1)) {
528             CONSOLE_DEBUG("Raising to my_handler1");
529           raise(SIGINT);           raise(SIGINT);
530        }           CONSOLE_DEBUG("SHOULDN'T BE HERE!");
531        else {           CU_FAIL("Shouldn't be here!");
532          }else{
533            CONSOLE_DEBUG("Caught from my_handler1");
534          CU_TEST(f_handler1_called == TRUE);          CU_TEST(f_handler1_called == TRUE);
535          CU_TEST(f_handler1_sigval == SIGINT);          CU_TEST(f_handler1_sigval == SIGINT);
536          CU_TEST(f_handler2_called == FALSE);          CU_TEST(f_handler2_called == FALSE);
# Line 365  Asc_SignalRecover(TRUE); Line 541  Asc_SignalRecover(TRUE);
541        CU_TEST(FALSE == signal2_caught);        CU_TEST(FALSE == signal2_caught);
542        CU_TEST(TRUE == signal3_caught);        CU_TEST(TRUE == signal3_caught);
543        CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));        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;        f_handler1_called = FALSE;
552        f_handler1_sigval = 0;        f_handler1_sigval = 0;
553        f_handler2_called = FALSE;        f_handler2_called = FALSE;
# Line 372  Asc_SignalRecover(TRUE); Line 555  Asc_SignalRecover(TRUE);
555        signal1_caught = FALSE;        signal1_caught = FALSE;
556        signal2_caught = FALSE;        signal2_caught = FALSE;
557        signal3_caught = FALSE;        signal3_caught = FALSE;
558          CONSOLE_DEBUG("Raising to Asc_SignalTrap");
559        raise(SIGINT);        raise(SIGINT);
560      }        CONSOLE_DEBUG("SHOULDN'T BE HERE!");
561      else {        CU_FAIL("Shouldn't be here!");
562        }else{
563          CONSOLE_DEBUG("Caught from Asc_SignalTrap");
564        CU_TEST(f_handler1_called == FALSE);        CU_TEST(f_handler1_called == FALSE);
565        CU_TEST(f_handler1_sigval == 0);        CU_TEST(f_handler1_sigval == 0);
566        CU_TEST(f_handler2_called == FALSE);        CU_TEST(f_handler2_called == FALSE);
# Line 392  Asc_SignalRecover(TRUE); Line 578  Asc_SignalRecover(TRUE);
578      signal1_caught = FALSE;      signal1_caught = FALSE;
579      signal2_caught = FALSE;      signal2_caught = FALSE;
580      signal3_caught = FALSE;      signal3_caught = FALSE;
581        CONSOLE_DEBUG("Raising to my_handler2");
582      raise(SIGINT);      raise(SIGINT);
583    }      CONSOLE_DEBUG("SHOULDN'T BE HERE!");
584    else {      CU_FAIL("Shouldn't be here!");
585      }else{
586        CONSOLE_DEBUG("Caught from my_handler2");
587      CU_TEST(f_handler1_called == FALSE);      CU_TEST(f_handler1_called == FALSE);
588      CU_TEST(f_handler1_sigval == 0);      CU_TEST(f_handler1_sigval == 0);
589      CU_TEST(f_handler2_called == TRUE);      CU_TEST(f_handler2_called == TRUE);
# Line 406  Asc_SignalRecover(TRUE); Line 595  Asc_SignalRecover(TRUE);
595    CU_TEST(FALSE == signal3_caught);    CU_TEST(FALSE == signal3_caught);
596    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));    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 */    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */
616    f_handler1_sigval = 0;    f_handler1_sigval = 0;
617    f_handler2_called = FALSE;    f_handler2_called = FALSE;
# Line 474  Asc_SignalRecover(TRUE); Line 680  Asc_SignalRecover(TRUE);
680    CU_TEST(FALSE == signal3_caught);    CU_TEST(FALSE == signal3_caught);
681    CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
682    
683    old_handler = signal(SIGFPE, SIG_DFL);                /* handlers should be restored at this point */    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* handlers should be restored at this point */
684    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
685    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
686    CU_TEST(NULL == old_handler);    CU_TEST(NULL == old_handler);
687    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
688    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
689    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
690    
691    Asc_SignalDestroy();    Asc_SignalDestroy();
692    
693    old_handler = signal(SIGFPE, SIG_DFL);                /* original handlers should still be in place */    old_handler = SIGNAL(SIGFPE, SIG_DFL);                /* original handlers should still be in place */
694    CU_TEST(my_handler1 == old_handler);    CU_TEST(my_handler1 == old_handler);
695    old_handler = signal(SIGINT, SIG_DFL);    old_handler = SIGNAL(SIGINT, SIG_DFL);
696    CU_TEST(NULL == old_handler);    CU_TEST(NULL == old_handler);
697    old_handler = signal(SIGSEGV, SIG_DFL);    old_handler = SIGNAL(SIGSEGV, SIG_DFL);
698    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
699    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
700    
701  #endif  /* NO_SIGNAL_TRAPS */  #endif  /* NO_SIGNAL_TRAPS */
702    
703    if (NULL != old_fpe_handler)                /* restore any pre-existing handlers */    restore_previous_signals();
704      signal(SIGFPE, old_fpe_handler);    Asc_SignalDestroy();
705    if (NULL != old_int_handler)    MEMUSED(0);
     signal(SIGINT, old_int_handler);  
   if (NULL != old_seg_handler)  
     signal(SIGSEGV, old_seg_handler);  
   
   CU_TEST(prior_meminuse == ascmeminuse());   /* make sure we cleaned up after ourselves */  
706  }  }
707    
708  /*===========================================================================*/  /*===========================================================================*/
709  /* Registration information */  /* Registration information */
710    
711    #define T(N) {#N,test_ascsignal_##N}
712  static CU_TestInfo ascSignal_test_list[] = {  static CU_TestInfo ascSignal_test_list[] = {
713    {"test_ascSignal", test_ascSignal},      T(basic)
714    CU_TEST_INFO_NULL      , 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[] = {  static CU_SuiteInfo suites[] = {
725    {"test_utilities_ascSignal", NULL, NULL, ascSignal_test_list},    {"test_utilities_ascSignal", NULL, NULL, ascSignal_test_list},

Legend:
Removed from v.1001  
changed lines
  Added in v.1002

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