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

Contents of /trunk/ascend/utilities/test/test_ascSignal.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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