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

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