]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/assert.c
ed56fc60727d61a4639a2f72e1509e16fb98ed2d
[pdclib] / functions / _PDCLIB / assert.c
1 /* $Id$ */
2
3 /* _PDCLIB_assert( char const * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <assert.h>
12
13 #ifndef REGTEST
14
15 #ifndef _PDCLIB_AUX_H
16 #define _PDCLIB_AUX_H _PDCLIB_AUX_H
17 #include <_PDCLIB_aux.h>
18 #endif
19
20 #if _PDCLIB_C_VERSION == 99
21 void _PDCLIB_assert( char const * const message1, char const * const function, char const * const message2 )
22 {
23     fputs( message1, stderr );
24     fputs( function, stderr );
25     fputs( message2, stderr );
26     abort();
27 }
28 #else
29 void _PDCLIB_assert( char const * const message )
30 {
31     fputs( message, stderr );
32     abort();
33 }
34 #endif
35
36 #endif
37
38 #ifdef TEST
39 #include <_PDCLIB_test.h>
40 #include <signal.h>
41
42 static int EXPECTED_ABORT = 0;
43 static int UNEXPECTED_ABORT = 1;
44
45 static void aborthandler( int sig )
46 {
47     TESTCASE( ! EXPECTED_ABORT );
48     exit( (signed int)rc );
49 }
50
51 #define NDEBUG
52 #include <assert.h>
53
54 static int disabled_test( void )
55 {
56     int i = 0;
57     assert( i == 0 ); /* NDEBUG set, condition met */
58     assert( i == 1 ); /* NDEBUG set, condition fails */
59     return i;
60 }
61
62 #undef NDEBUG
63 #include <assert.h>
64
65 int main( void )
66 {
67     TESTCASE( signal( SIGABRT, &aborthandler ) != SIG_ERR );
68     TESTCASE( disabled_test() == 0 );
69     assert( UNEXPECTED_ABORT ); /* NDEBUG not set, condition met */
70     assert( EXPECTED_ABORT ); /* NDEBUG not set, condition fails - should abort */
71     return TEST_RESULTS;
72 }
73
74 #endif