]> pd.if.org Git - pdclib/blob - functions/assert.c
Added test driver.
[pdclib] / functions / assert.c
1 /* _PDCLIB_assert( char * )
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
10 #ifndef _PDCLIB_AUX_H
11 #define _PDCLIB_AUX_H _PDCLIB_AUX_H
12 #include <_PDCLIB_aux.h>
13 #endif
14
15 #if _PDCLIB_C_VERSION == 99
16 void _PDCLIB_assert( char const * const message1, char const * const function, char const * const message2 )
17 {
18     fputs( message1, stderr );
19     fputs( function, stderr );
20     fputs( message2, stderr );
21     abort();
22 }
23 #else
24 void _PDCLIB_assert( char const * const message )
25 {
26     fputs( message, stderr );
27     abort();
28 }
29 #endif
30
31
32 #ifdef TEST
33 #include <_PDCLIB_test.h>
34 #include <signal.h>
35
36 static int rc = 0;
37 static int EXPECTED_ABORT = 0;
38
39 void aborthandler( int signal )
40 {
41     TESTCASE( ! EXPECTED_ABORT );
42     exit( rc );
43 }
44
45 #define NDEBUG
46 #include <assert.h>
47
48 int disabled_test()
49 {
50     int i = 0;
51     assert( i == 0 ); /* NDEBUG set, condition met */
52     assert( i == 1 ); /* NDEBUG set, condition fails */
53     return i;
54 }
55
56 #undef NDEBUG
57 #include <assert.h>
58
59 int main()
60 {
61     int i = 0;
62     BEGIN_TESTS;
63     TESTCASE( signal( SIGABRT, &aborthandler ) != SIG_ERR );
64     assert( i == 0 ); /* NDEBUG not set, condition met */
65     puts( "Expecting failed assert() message here:" );
66     assert( i == 1 ); /* NDEBUG not set, condition fails - should abort */
67     return TEST_RESULTS;
68 }
69
70 #endif