/[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 59 by jds, Sun Oct 30 01:38:20 2005 UTC revision 61 by jds, Mon Nov 14 02:37:20 2005 UTC
# Line 22  Line 22 
22   */   */
23    
24  #include <stdio.h>  #include <stdio.h>
25    #ifdef __WIN32__
26  #include <io.h>  #include <io.h>
27    #endif
28  #include "utilities/ascConfig.h"  #include "utilities/ascConfig.h"
29  #include "utilities/ascMalloc.h"  #include "utilities/ascMalloc.h"
30  #include "utilities/ascSignal.h"  #include "utilities/ascSignal.h"
# Line 31  Line 33 
33    
34  static jmp_buf my_jmp_buf1;  static jmp_buf my_jmp_buf1;
35    
36  static int handler1_called;  static int f_handler1_called;
37  static int handler1_sigval;  static int f_handler1_sigval;
38  /*  /*
39   *  Signal handler for unit tests.   *  Signal handler for unit tests.
40   *  Resets the signal handlers and sets handler1_called to   *  Resets the signal handlers and sets f_handler1_called to
41   *  TRUE and handler1_sigval to the signal type code (-1 if   *  TRUE and f_handler1_sigval to the signal type code (-1 if
42   *  an unsupported sigval).  Then longjmp's using   *  an unsupported sigval).  Then longjmp's using
43   *  my_jmp_buf1 and the sigval.   *  my_jmp_buf1 and the sigval.
44   */   */
45  void my_handler1(int sigval)  void my_handler1(int sigval)
46  {  {
47    handler1_called = TRUE;    f_handler1_called = TRUE;
48    Asc_SignalRecover(FALSE);    Asc_SignalRecover(FALSE);
49    switch (sigval)    switch (sigval)
50    {    {
51      case SIGFPE:      case SIGFPE:
52        handler1_sigval = SIGFPE;        f_handler1_sigval = SIGFPE;
53        FPRESET;        FPRESET;
54        break;        break;
55      case SIGINT:      case SIGINT:
56        handler1_sigval = SIGINT;        f_handler1_sigval = SIGINT;
57        break;        break;
58      case SIGSEGV:      case SIGSEGV:
59        handler1_sigval = SIGSEGV;        f_handler1_sigval = SIGSEGV;
60        break;        break;
61      default:      default:
62        handler1_sigval = -1;        f_handler1_sigval = -1;
63        break;        break;
64    }    }
65    longjmp(my_jmp_buf1, sigval);    longjmp(my_jmp_buf1, sigval);
# Line 65  void my_handler1(int sigval) Line 67  void my_handler1(int sigval)
67    
68  static jmp_buf my_jmp_buf2;  static jmp_buf my_jmp_buf2;
69    
70  static int handler2_called;  static int f_handler2_called;
71  static int handler2_sigval;  static int f_handler2_sigval;
72  /*  /*
73   *  Signal handler for unit tests.   *  Signal handler for unit tests.
74   *  Resets the signal handlers and sets handler1_called to   *  Resets the signal handlers and sets f_handler1_called to
75   *  TRUE and handler1_sigval to the signal type code (-1 if   *  TRUE and f_handler1_sigval to the signal type code (-1 if
76   *  an unsupported sigval).  Then longjmp's using   *  an unsupported sigval).  Then longjmp's using
77   *  my_jmp_buf1 and the sigval.   *  my_jmp_buf1 and the sigval.
78   */   */
79  void my_handler2(int sigval)  void my_handler2(int sigval)
80  {  {
81    handler2_called = TRUE;    f_handler2_called = TRUE;
82    Asc_SignalRecover(FALSE);    Asc_SignalRecover(FALSE);
83    switch (sigval)    switch (sigval)
84    {    {
85      case SIGFPE:      case SIGFPE:
86        handler2_sigval = SIGFPE;        f_handler2_sigval = SIGFPE;
87        FPRESET;        FPRESET;
88        break;        break;
89      case SIGINT:      case SIGINT:
90        handler2_sigval = SIGINT;        f_handler2_sigval = SIGINT;
91        break;        break;
92      case SIGSEGV:      case SIGSEGV:
93        handler2_sigval = SIGSEGV;        f_handler2_sigval = SIGSEGV;
94        break;        break;
95      default:      default:
96        handler2_sigval = -1;        f_handler2_sigval = -1;
97        break;        break;
98    }    }
99    longjmp(my_jmp_buf2, sigval);    longjmp(my_jmp_buf2, sigval);
# Line 116  static void test_ascSignal(void) Line 118  static void test_ascSignal(void)
118    /* no point in testing if the functionality is disabled */    /* no point in testing if the functionality is disabled */
119    CU_FAIL("Signal handler manager not enabled.");    CU_FAIL("Signal handler manager not enabled.");
120  #else  #else
121      
122    old_fpe_handler = signal(SIGFPE, my_handler1);        /* save any pre-existing handlers */    old_fpe_handler = signal(SIGFPE, my_handler1);        /* save any pre-existing handlers */
123    old_int_handler = signal(SIGINT, my_handler1);    old_int_handler = signal(SIGINT, my_handler1);
124    old_seg_handler = signal(SIGSEGV, my_handler1);    old_seg_handler = signal(SIGSEGV, my_handler1);
125    
126    /* set up pooling & recycling */    /* make sure list system is initialized - needed by Asc_SignalInit() */
127    if (FALSE == gl_pool_initialized()) {    if (FALSE == gl_pool_initialized()) {
128      gl_init();      gl_init();
129      gl_init_pool();      gl_init_pool();
# Line 254  static void test_ascSignal(void) Line 256  static void test_ascSignal(void)
256    }    }
257    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
258    
259    Asc_SignalRecover(TRUE);
260    
261    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
262    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
263    CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));    CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
# Line 268  static void test_ascSignal(void) Line 272  static void test_ascSignal(void)
272    
273    /* test typical use with nesting of handlers */    /* test typical use with nesting of handlers */
274    
275    handler1_called = FALSE;                              /* initialize flags for detecting flow */    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */
276    handler1_sigval = 0;    f_handler1_sigval = 0;
277    handler2_called = FALSE;    f_handler2_called = FALSE;
278    handler2_sigval = 0;    f_handler2_sigval = 0;
279    signal1_caught = FALSE;    signal1_caught = FALSE;
280    signal2_caught = FALSE;    signal2_caught = FALSE;
281    signal3_caught = FALSE;    signal3_caught = FALSE;
# Line 285  static void test_ascSignal(void) Line 289  static void test_ascSignal(void)
289           raise(SIGFPE);           raise(SIGFPE);
290        }        }
291        else {        else {
292          CU_TEST(handler1_called == FALSE);          CU_TEST(f_handler1_called == FALSE);
293          CU_TEST(handler1_sigval == 0);          CU_TEST(f_handler1_sigval == 0);
294          CU_TEST(handler2_called == FALSE);          CU_TEST(f_handler2_called == FALSE);
295          CU_TEST(handler2_sigval == 0);          CU_TEST(f_handler2_sigval == 0);
296          signal3_caught = TRUE;          signal3_caught = TRUE;
297        }        }
298        CU_TEST(FALSE == signal1_caught);        CU_TEST(FALSE == signal1_caught);
299        CU_TEST(FALSE == signal2_caught);        CU_TEST(FALSE == signal2_caught);
300        CU_TEST(TRUE == signal3_caught);        CU_TEST(TRUE == signal3_caught);
301        CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));        CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
302        handler1_called = FALSE;        f_handler1_called = FALSE;
303        handler1_sigval = 0;        f_handler1_sigval = 0;
304        handler2_called = FALSE;        f_handler2_called = FALSE;
305        handler2_sigval = 0;        f_handler2_sigval = 0;
306        signal1_caught = FALSE;        signal1_caught = FALSE;
307        signal2_caught = FALSE;        signal2_caught = FALSE;
308        signal3_caught = FALSE;        signal3_caught = FALSE;
309        raise(SIGFPE);        raise(SIGFPE);
310      }      }
311      else {      else {
312        CU_TEST(handler1_called == FALSE);        CU_TEST(f_handler1_called == FALSE);
313        CU_TEST(handler1_sigval == 0);        CU_TEST(f_handler1_sigval == 0);
314        CU_TEST(handler2_called == TRUE);        CU_TEST(f_handler2_called == TRUE);
315        CU_TEST(handler2_sigval == SIGFPE);        CU_TEST(f_handler2_sigval == SIGFPE);
316        signal2_caught = TRUE;        signal2_caught = TRUE;
317      }      }
318      CU_TEST(FALSE == signal1_caught);      CU_TEST(FALSE == signal1_caught);
319      CU_TEST(TRUE == signal2_caught);      CU_TEST(TRUE == signal2_caught);
320      CU_TEST(FALSE == signal3_caught);      CU_TEST(FALSE == signal3_caught);
321      CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler2));      CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler2));
322      handler1_called = FALSE;      f_handler1_called = FALSE;
323      handler1_sigval = 0;      f_handler1_sigval = 0;
324      handler2_called = FALSE;      f_handler2_called = FALSE;
325      handler2_sigval = 0;      f_handler2_sigval = 0;
326      signal1_caught = FALSE;      signal1_caught = FALSE;
327      signal2_caught = FALSE;      signal2_caught = FALSE;
328      signal3_caught = FALSE;      signal3_caught = FALSE;
329      raise(SIGFPE);      raise(SIGFPE);
330    }    }
331    else {    else {
332      CU_TEST(handler1_called == TRUE);      CU_TEST(f_handler1_called == TRUE);
333      CU_TEST(handler1_sigval == SIGFPE);      CU_TEST(f_handler1_sigval == SIGFPE);
334      CU_TEST(handler2_called == FALSE);      CU_TEST(f_handler2_called == FALSE);
335      CU_TEST(handler2_sigval == 0);      CU_TEST(f_handler2_sigval == 0);
336      signal1_caught = TRUE;      signal1_caught = TRUE;
337    }    }
338    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
# Line 336  static void test_ascSignal(void) Line 340  static void test_ascSignal(void)
340    CU_TEST(FALSE == signal3_caught);    CU_TEST(FALSE == signal3_caught);
341    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));    CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));
342    
343    handler1_called = FALSE;                              /* initialize flags for detecting flow */    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */
344    handler1_sigval = 0;    f_handler1_sigval = 0;
345    handler2_called = FALSE;    f_handler2_called = FALSE;
346    handler2_sigval = 0;    f_handler2_sigval = 0;
347    signal1_caught = FALSE;    signal1_caught = FALSE;
348    signal2_caught = FALSE;    signal2_caught = FALSE;
349    signal3_caught = FALSE;    signal3_caught = FALSE;
# Line 353  static void test_ascSignal(void) Line 357  static void test_ascSignal(void)
357           raise(SIGINT);           raise(SIGINT);
358        }        }
359        else {        else {
360          CU_TEST(handler1_called == TRUE);          CU_TEST(f_handler1_called == TRUE);
361          CU_TEST(handler1_sigval == SIGINT);          CU_TEST(f_handler1_sigval == SIGINT);
362          CU_TEST(handler2_called == FALSE);          CU_TEST(f_handler2_called == FALSE);
363          CU_TEST(handler2_sigval == 0);          CU_TEST(f_handler2_sigval == 0);
364          signal3_caught = TRUE;          signal3_caught = TRUE;
365        }        }
366        CU_TEST(FALSE == signal1_caught);        CU_TEST(FALSE == signal1_caught);
367        CU_TEST(FALSE == signal2_caught);        CU_TEST(FALSE == signal2_caught);
368        CU_TEST(TRUE == signal3_caught);        CU_TEST(TRUE == signal3_caught);
369        CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));        CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
370        handler1_called = FALSE;        f_handler1_called = FALSE;
371        handler1_sigval = 0;        f_handler1_sigval = 0;
372        handler2_called = FALSE;        f_handler2_called = FALSE;
373        handler2_sigval = 0;        f_handler2_sigval = 0;
374        signal1_caught = FALSE;        signal1_caught = FALSE;
375        signal2_caught = FALSE;        signal2_caught = FALSE;
376        signal3_caught = FALSE;        signal3_caught = FALSE;
377        raise(SIGINT);        raise(SIGINT);
378      }      }
379      else {      else {
380        CU_TEST(handler1_called == FALSE);        CU_TEST(f_handler1_called == FALSE);
381        CU_TEST(handler1_sigval == 0);        CU_TEST(f_handler1_sigval == 0);
382        CU_TEST(handler2_called == FALSE);        CU_TEST(f_handler2_called == FALSE);
383        CU_TEST(handler2_sigval == 0);        CU_TEST(f_handler2_sigval == 0);
384        signal2_caught = TRUE;        signal2_caught = TRUE;
385      }      }
386      CU_TEST(FALSE == signal1_caught);      CU_TEST(FALSE == signal1_caught);
387      CU_TEST(TRUE == signal2_caught);      CU_TEST(TRUE == signal2_caught);
388      CU_TEST(FALSE == signal3_caught);      CU_TEST(FALSE == signal3_caught);
389      CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));      CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
390      handler1_called = FALSE;      f_handler1_called = FALSE;
391      handler1_sigval = 0;      f_handler1_sigval = 0;
392      handler2_called = FALSE;      f_handler2_called = FALSE;
393      handler2_sigval = 0;      f_handler2_sigval = 0;
394      signal1_caught = FALSE;      signal1_caught = FALSE;
395      signal2_caught = FALSE;      signal2_caught = FALSE;
396      signal3_caught = FALSE;      signal3_caught = FALSE;
397      raise(SIGINT);      raise(SIGINT);
398    }    }
399    else {    else {
400      CU_TEST(handler1_called == FALSE);      CU_TEST(f_handler1_called == FALSE);
401      CU_TEST(handler1_sigval == 0);      CU_TEST(f_handler1_sigval == 0);
402      CU_TEST(handler2_called == TRUE);      CU_TEST(f_handler2_called == TRUE);
403      CU_TEST(handler2_sigval == SIGINT);      CU_TEST(f_handler2_sigval == SIGINT);
404      signal1_caught = TRUE;      signal1_caught = TRUE;
405    }    }
406    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
# Line 404  static void test_ascSignal(void) Line 408  static void test_ascSignal(void)
408    CU_TEST(FALSE == signal3_caught);    CU_TEST(FALSE == signal3_caught);
409    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));    CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));
410    
411    handler1_called = FALSE;                              /* initialize flags for detecting flow */    f_handler1_called = FALSE;                              /* initialize flags for detecting flow */
412    handler1_sigval = 0;    f_handler1_sigval = 0;
413    handler2_called = FALSE;    f_handler2_called = FALSE;
414    handler2_sigval = 0;    f_handler2_sigval = 0;
415    signal1_caught = FALSE;    signal1_caught = FALSE;
416    signal2_caught = FALSE;    signal2_caught = FALSE;
417    signal3_caught = FALSE;    signal3_caught = FALSE;
# Line 421  static void test_ascSignal(void) Line 425  static void test_ascSignal(void)
425           raise(SIGSEGV);           raise(SIGSEGV);
426        }        }
427        else {        else {
428          CU_TEST(handler1_called == TRUE);          CU_TEST(f_handler1_called == TRUE);
429          CU_TEST(handler1_sigval == SIGSEGV);          CU_TEST(f_handler1_sigval == SIGSEGV);
430          CU_TEST(handler2_called == FALSE);          CU_TEST(f_handler2_called == FALSE);
431          CU_TEST(handler2_sigval == 0);          CU_TEST(f_handler2_sigval == 0);
432          signal3_caught = TRUE;          signal3_caught = TRUE;
433        }        }
434        CU_TEST(FALSE == signal1_caught);        CU_TEST(FALSE == signal1_caught);
435        CU_TEST(FALSE == signal2_caught);        CU_TEST(FALSE == signal2_caught);
436        CU_TEST(TRUE == signal3_caught);        CU_TEST(TRUE == signal3_caught);
437        CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler1));        CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler1));
438        handler1_called = FALSE;        f_handler1_called = FALSE;
439        handler1_sigval = 0;        f_handler1_sigval = 0;
440        handler2_called = FALSE;        f_handler2_called = FALSE;
441        handler2_sigval = 0;        f_handler2_sigval = 0;
442        signal1_caught = FALSE;        signal1_caught = FALSE;
443        signal2_caught = FALSE;        signal2_caught = FALSE;
444        signal3_caught = FALSE;        signal3_caught = FALSE;
445        raise(SIGSEGV);        raise(SIGSEGV);
446      }      }
447      else {      else {
448        CU_TEST(handler1_called == FALSE);        CU_TEST(f_handler1_called == FALSE);
449        CU_TEST(handler1_sigval == 0);        CU_TEST(f_handler1_sigval == 0);
450        CU_TEST(handler2_called == TRUE);        CU_TEST(f_handler2_called == TRUE);
451        CU_TEST(handler2_sigval == SIGSEGV);        CU_TEST(f_handler2_sigval == SIGSEGV);
452        signal2_caught = TRUE;        signal2_caught = TRUE;
453      }      }
454      CU_TEST(FALSE == signal1_caught);      CU_TEST(FALSE == signal1_caught);
455      CU_TEST(TRUE == signal2_caught);      CU_TEST(TRUE == signal2_caught);
456      CU_TEST(FALSE == signal3_caught);      CU_TEST(FALSE == signal3_caught);
457      CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler2));      CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler2));
458      handler1_called = FALSE;      f_handler1_called = FALSE;
459      handler1_sigval = 0;      f_handler1_sigval = 0;
460      handler2_called = FALSE;      f_handler2_called = FALSE;
461      handler2_sigval = 0;      f_handler2_sigval = 0;
462      signal1_caught = FALSE;      signal1_caught = FALSE;
463      signal2_caught = FALSE;      signal2_caught = FALSE;
464      signal3_caught = FALSE;      signal3_caught = FALSE;
465      raise(SIGSEGV);      raise(SIGSEGV);
466    }    }
467    else {    else {
468      CU_TEST(handler1_called == FALSE);      CU_TEST(f_handler1_called == FALSE);
469      CU_TEST(handler1_sigval == 0);      CU_TEST(f_handler1_sigval == 0);
470      CU_TEST(handler2_called == FALSE);      CU_TEST(f_handler2_called == FALSE);
471      CU_TEST(handler2_sigval == 0);      CU_TEST(f_handler2_sigval == 0);
472      signal1_caught = TRUE;      signal1_caught = TRUE;
473    }    }
474    CU_TEST(TRUE == signal1_caught);    CU_TEST(TRUE == signal1_caught);
# Line 490  static void test_ascSignal(void) Line 494  static void test_ascSignal(void)
494    CU_TEST(my_handler2 == old_handler);    CU_TEST(my_handler2 == old_handler);
495    Asc_SignalRecover(TRUE);    Asc_SignalRecover(TRUE);
496    
497      if (TRUE == i_initialized_lists) {
498        gl_destroy_pool();
499      }
500    
501  #endif  /* NO_SIGNAL_TRAPS */  #endif  /* NO_SIGNAL_TRAPS */
502    
503    if (NULL != old_fpe_handler)                /* restore any pre-existing handlers */    if (NULL != old_fpe_handler)                /* restore any pre-existing handlers */
# Line 499  static void test_ascSignal(void) Line 507  static void test_ascSignal(void)
507    if (NULL != old_seg_handler)    if (NULL != old_seg_handler)
508      signal(SIGSEGV, old_seg_handler);      signal(SIGSEGV, old_seg_handler);
509    
   if (TRUE == i_initialized_lists) {          /* clean up list system if necessary */  
     gl_destroy_pool();  
   }  
510    CU_TEST(prior_meminuse == ascmeminuse());   /* make sure we cleaned up after ourselves */    CU_TEST(prior_meminuse == ascmeminuse());   /* make sure we cleaned up after ourselves */
511  }  }
512    

Legend:
Removed from v.59  
changed lines
  Added in v.61

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