]> pd.if.org Git - pdclib/blob - functions/stdlib/atexit.c
Added test driver.
[pdclib] / functions / stdlib / atexit.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* atexit( void (*)( void ) )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <stdlib.h>
12
13 #ifndef REGTEST
14
15 extern struct _PDCLIB_exitfunc_t * regstack;
16
17 int atexit( void (*func)( void ) )
18 {
19     struct _PDCLIB_exitfunc_t * regfunc = (struct _PDCLIB_exitfunc_t *)malloc( sizeof( struct _PDCLIB_exitfunc_t ) );
20     if ( regfunc == NULL )
21     {
22         return -1;
23     }
24     else
25     {
26         regfunc->func = func;
27         regfunc->next = regstack;
28         regstack = regfunc;
29         return 0;
30     }
31 }
32
33 #endif
34
35 #ifdef TEST
36 #include <_PDCLIB_test.h>
37 #include <assert.h>
38
39 static int flags[ 32 ];
40
41 static void counthandler()
42 {
43     static int rc = 0;
44     flags[ rc ] = rc;
45     ++rc;
46 }
47
48 static void checkhandler()
49 {
50     for ( int i = 0; i < 31; ++i )
51     {
52         assert( flags[ i ] == i );
53     }
54 }
55
56 int main()
57 {
58     BEGIN_TESTS;
59     TESTCASE( atexit( &checkhandler ) == 0 );
60     for ( int i = 0; i < 31; ++i )
61     {
62         TESTCASE( atexit( &counthandler ) == 0 );
63     }
64     return TEST_RESULTS;
65 }
66
67 #endif