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