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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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