/[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 61 - (show annotations) (download) (as text)
Mon Nov 14 02:37:20 2005 UTC (18 years, 10 months ago) by jds
File MIME type: text/x-csrc
File size: 17292 byte(s)
Minor bug fixes, extend unit tests to solver:

minor doc changes - compiler/func.h, general/list.h, solver/mtx.h, utilities/mem.h
solver/example - upgraded examples so they run under current system
solver/slv_common.[ch] - added unit tests, minor bug fixes, extended vector_data functions
utilities/ascDynaLoad.c - bug fix on *nix so dlopen, dlsym not called with NULL arguments
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 f_handler1_called;
37 static int f_handler1_sigval;
38 /*
39 * Signal handler for unit tests.
40 * Resets the signal handlers and sets f_handler1_called to
41 * TRUE and f_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 f_handler1_called = TRUE;
48 Asc_SignalRecover(FALSE);
49 switch (sigval)
50 {
51 case SIGFPE:
52 f_handler1_sigval = SIGFPE;
53 FPRESET;
54 break;
55 case SIGINT:
56 f_handler1_sigval = SIGINT;
57 break;
58 case SIGSEGV:
59 f_handler1_sigval = SIGSEGV;
60 break;
61 default:
62 f_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 f_handler2_called;
71 static int f_handler2_sigval;
72 /*
73 * Signal handler for unit tests.
74 * Resets the signal handlers and sets f_handler1_called to
75 * TRUE and f_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 f_handler2_called = TRUE;
82 Asc_SignalRecover(FALSE);
83 switch (sigval)
84 {
85 case SIGFPE:
86 f_handler2_sigval = SIGFPE;
87 FPRESET;
88 break;
89 case SIGINT:
90 f_handler2_sigval = SIGINT;
91 break;
92 case SIGSEGV:
93 f_handler2_sigval = SIGSEGV;
94 break;
95 default:
96 f_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 /* make sure list system is initialized - needed by Asc_SignalInit() */
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 Asc_SignalRecover(TRUE);
260
261 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
262 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
263 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
264
265 old_handler = signal(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
266 CU_TEST(my_handler1 == old_handler);
267 old_handler = signal(SIGINT, SIG_DFL);
268 CU_TEST(NULL == old_handler);
269 old_handler = signal(SIGSEGV, SIG_DFL);
270 CU_TEST(my_handler2 == old_handler);
271 Asc_SignalRecover(TRUE);
272
273 /* test typical use with nesting of handlers */
274
275 f_handler1_called = FALSE; /* initialize flags for detecting flow */
276 f_handler1_sigval = 0;
277 f_handler2_called = FALSE;
278 f_handler2_sigval = 0;
279 signal1_caught = FALSE;
280 signal2_caught = FALSE;
281 signal3_caught = FALSE;
282
283 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler1)); /* test for SIGFPE */
284 if (0 == setjmp(my_jmp_buf1)) {
285 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, my_handler2));
286 if (0 == setjmp(my_jmp_buf2)) {
287 CU_TEST(0 == Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap));
288 if (0 == setjmp(g_fpe_env)) {
289 raise(SIGFPE);
290 }
291 else {
292 CU_TEST(f_handler1_called == FALSE);
293 CU_TEST(f_handler1_sigval == 0);
294 CU_TEST(f_handler2_called == FALSE);
295 CU_TEST(f_handler2_sigval == 0);
296 signal3_caught = TRUE;
297 }
298 CU_TEST(FALSE == signal1_caught);
299 CU_TEST(FALSE == signal2_caught);
300 CU_TEST(TRUE == signal3_caught);
301 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, Asc_SignalTrap));
302 f_handler1_called = FALSE;
303 f_handler1_sigval = 0;
304 f_handler2_called = FALSE;
305 f_handler2_sigval = 0;
306 signal1_caught = FALSE;
307 signal2_caught = FALSE;
308 signal3_caught = FALSE;
309 raise(SIGFPE);
310 }
311 else {
312 CU_TEST(f_handler1_called == FALSE);
313 CU_TEST(f_handler1_sigval == 0);
314 CU_TEST(f_handler2_called == TRUE);
315 CU_TEST(f_handler2_sigval == SIGFPE);
316 signal2_caught = TRUE;
317 }
318 CU_TEST(FALSE == signal1_caught);
319 CU_TEST(TRUE == signal2_caught);
320 CU_TEST(FALSE == signal3_caught);
321 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler2));
322 f_handler1_called = FALSE;
323 f_handler1_sigval = 0;
324 f_handler2_called = FALSE;
325 f_handler2_sigval = 0;
326 signal1_caught = FALSE;
327 signal2_caught = FALSE;
328 signal3_caught = FALSE;
329 raise(SIGFPE);
330 }
331 else {
332 CU_TEST(f_handler1_called == TRUE);
333 CU_TEST(f_handler1_sigval == SIGFPE);
334 CU_TEST(f_handler2_called == FALSE);
335 CU_TEST(f_handler2_sigval == 0);
336 signal1_caught = TRUE;
337 }
338 CU_TEST(TRUE == signal1_caught);
339 CU_TEST(FALSE == signal2_caught);
340 CU_TEST(FALSE == signal3_caught);
341 CU_TEST(0 == Asc_SignalHandlerPop(SIGFPE, my_handler1));
342
343 f_handler1_called = FALSE; /* initialize flags for detecting flow */
344 f_handler1_sigval = 0;
345 f_handler2_called = FALSE;
346 f_handler2_sigval = 0;
347 signal1_caught = FALSE;
348 signal2_caught = FALSE;
349 signal3_caught = FALSE;
350
351 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler2)); /* test for SIGINT */
352 if (0 == setjmp(my_jmp_buf2)) {
353 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, Asc_SignalTrap));
354 if (0 == setjmp(g_int_env)) {
355 CU_TEST(0 == Asc_SignalHandlerPush(SIGINT, my_handler1));
356 if (0 == setjmp(my_jmp_buf1)) {
357 raise(SIGINT);
358 }
359 else {
360 CU_TEST(f_handler1_called == TRUE);
361 CU_TEST(f_handler1_sigval == SIGINT);
362 CU_TEST(f_handler2_called == FALSE);
363 CU_TEST(f_handler2_sigval == 0);
364 signal3_caught = TRUE;
365 }
366 CU_TEST(FALSE == signal1_caught);
367 CU_TEST(FALSE == signal2_caught);
368 CU_TEST(TRUE == signal3_caught);
369 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler1));
370 f_handler1_called = FALSE;
371 f_handler1_sigval = 0;
372 f_handler2_called = FALSE;
373 f_handler2_sigval = 0;
374 signal1_caught = FALSE;
375 signal2_caught = FALSE;
376 signal3_caught = FALSE;
377 raise(SIGINT);
378 }
379 else {
380 CU_TEST(f_handler1_called == FALSE);
381 CU_TEST(f_handler1_sigval == 0);
382 CU_TEST(f_handler2_called == FALSE);
383 CU_TEST(f_handler2_sigval == 0);
384 signal2_caught = TRUE;
385 }
386 CU_TEST(FALSE == signal1_caught);
387 CU_TEST(TRUE == signal2_caught);
388 CU_TEST(FALSE == signal3_caught);
389 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, Asc_SignalTrap));
390 f_handler1_called = FALSE;
391 f_handler1_sigval = 0;
392 f_handler2_called = FALSE;
393 f_handler2_sigval = 0;
394 signal1_caught = FALSE;
395 signal2_caught = FALSE;
396 signal3_caught = FALSE;
397 raise(SIGINT);
398 }
399 else {
400 CU_TEST(f_handler1_called == FALSE);
401 CU_TEST(f_handler1_sigval == 0);
402 CU_TEST(f_handler2_called == TRUE);
403 CU_TEST(f_handler2_sigval == SIGINT);
404 signal1_caught = TRUE;
405 }
406 CU_TEST(TRUE == signal1_caught);
407 CU_TEST(FALSE == signal2_caught);
408 CU_TEST(FALSE == signal3_caught);
409 CU_TEST(0 == Asc_SignalHandlerPop(SIGINT, my_handler2));
410
411 f_handler1_called = FALSE; /* initialize flags for detecting flow */
412 f_handler1_sigval = 0;
413 f_handler2_called = FALSE;
414 f_handler2_sigval = 0;
415 signal1_caught = FALSE;
416 signal2_caught = FALSE;
417 signal3_caught = FALSE;
418
419 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, Asc_SignalTrap)); /* test for SIGSEGV */
420 if (0 == setjmp(g_seg_env)) {
421 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler2));
422 if (0 == setjmp(my_jmp_buf2)) {
423 CU_TEST(0 == Asc_SignalHandlerPush(SIGSEGV, my_handler1));
424 if (0 == setjmp(my_jmp_buf1)) {
425 raise(SIGSEGV);
426 }
427 else {
428 CU_TEST(f_handler1_called == TRUE);
429 CU_TEST(f_handler1_sigval == SIGSEGV);
430 CU_TEST(f_handler2_called == FALSE);
431 CU_TEST(f_handler2_sigval == 0);
432 signal3_caught = TRUE;
433 }
434 CU_TEST(FALSE == signal1_caught);
435 CU_TEST(FALSE == signal2_caught);
436 CU_TEST(TRUE == signal3_caught);
437 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler1));
438 f_handler1_called = FALSE;
439 f_handler1_sigval = 0;
440 f_handler2_called = FALSE;
441 f_handler2_sigval = 0;
442 signal1_caught = FALSE;
443 signal2_caught = FALSE;
444 signal3_caught = FALSE;
445 raise(SIGSEGV);
446 }
447 else {
448 CU_TEST(f_handler1_called == FALSE);
449 CU_TEST(f_handler1_sigval == 0);
450 CU_TEST(f_handler2_called == TRUE);
451 CU_TEST(f_handler2_sigval == SIGSEGV);
452 signal2_caught = TRUE;
453 }
454 CU_TEST(FALSE == signal1_caught);
455 CU_TEST(TRUE == signal2_caught);
456 CU_TEST(FALSE == signal3_caught);
457 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, my_handler2));
458 f_handler1_called = FALSE;
459 f_handler1_sigval = 0;
460 f_handler2_called = FALSE;
461 f_handler2_sigval = 0;
462 signal1_caught = FALSE;
463 signal2_caught = FALSE;
464 signal3_caught = FALSE;
465 raise(SIGSEGV);
466 }
467 else {
468 CU_TEST(f_handler1_called == FALSE);
469 CU_TEST(f_handler1_sigval == 0);
470 CU_TEST(f_handler2_called == FALSE);
471 CU_TEST(f_handler2_sigval == 0);
472 signal1_caught = TRUE;
473 }
474 CU_TEST(TRUE == signal1_caught);
475 CU_TEST(FALSE == signal2_caught);
476 CU_TEST(FALSE == signal3_caught);
477 CU_TEST(0 == Asc_SignalHandlerPop(SIGSEGV, Asc_SignalTrap));
478
479 old_handler = signal(SIGFPE, SIG_DFL); /* handlers should be restored at this point */
480 CU_TEST(my_handler1 == old_handler);
481 old_handler = signal(SIGINT, SIG_DFL);
482 CU_TEST(NULL == old_handler);
483 old_handler = signal(SIGSEGV, SIG_DFL);
484 CU_TEST(my_handler2 == old_handler);
485 Asc_SignalRecover(TRUE);
486
487 Asc_SignalDestroy();
488
489 old_handler = signal(SIGFPE, SIG_DFL); /* original handlers should still be in place */
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 if (TRUE == i_initialized_lists) {
498 gl_destroy_pool();
499 }
500
501 #endif /* NO_SIGNAL_TRAPS */
502
503 if (NULL != old_fpe_handler) /* restore any pre-existing handlers */
504 signal(SIGFPE, old_fpe_handler);
505 if (NULL != old_int_handler)
506 signal(SIGINT, old_int_handler);
507 if (NULL != old_seg_handler)
508 signal(SIGSEGV, old_seg_handler);
509
510 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
511 }
512
513 /*===========================================================================*/
514 /* Registration information */
515
516 static CU_TestInfo ascSignal_test_list[] = {
517 {"test_ascSignal", test_ascSignal},
518 CU_TEST_INFO_NULL
519 };
520
521 static CU_SuiteInfo suites[] = {
522 {"test_utilities_ascSignal", NULL, NULL, ascSignal_test_list},
523 CU_SUITE_INFO_NULL
524 };
525
526 /*-------------------------------------------------------------------*/
527 CU_ErrorCode test_register_utilities_ascSignal(void)
528 {
529 return CU_register_suites(suites);
530 }

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