]> pd.if.org Git - pdclib/blob - functions/stdlib/calloc.c
Initial checkin.
[pdclib] / functions / stdlib / calloc.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* void * calloc( size_t, size_t )
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 #include <string.h>
13
14 #ifndef REGTEST
15
16 void * calloc( size_t nmemb, size_t size )
17 {
18     void * rc = malloc( nmemb * size );
19     if ( rc != NULL )
20     {
21         memset( rc, 0, nmemb * size );
22     }
23     return rc;
24 }
25
26 #endif
27
28 #ifdef TEST
29 #include <_PDCLIB_test.h>
30
31 int main()
32 {
33     char * s;
34     BEGIN_TESTS;
35     TESTCASE( ( s = calloc( 3, 2 ) ) != NULL );
36     TESTCASE( s[0] == '\0' );
37     TESTCASE( s[5] == '\0' );
38     free( s );
39     TESTCASE( ( s = calloc( 6, 1 ) ) != NULL );
40     TESTCASE( s[0] == '\0' );
41     TESTCASE( s[5] == '\0' );
42     free( s );
43     TESTCASE( ( s = calloc( 1, 6 ) ) != NULL );
44     TESTCASE( s[0] == '\0' );
45     TESTCASE( s[5] == '\0' );
46     free( s );
47     return TEST_RESULTS;
48 }
49
50 #endif