Back to index

glibc  2.9
tst-calloc.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <error.h>
00022 #include <limits.h>
00023 #include <malloc.h>
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 
00027 
00028 /* Number of samples per size.  */
00029 #define N 50000
00030 
00031 
00032 static void
00033 fixed_test (int size)
00034 {
00035   char *ptrs[N];
00036   int i;
00037 
00038   for (i = 0; i < N; ++i)
00039     {
00040       int j;
00041 
00042       ptrs[i] = (char *) calloc (1, size);
00043 
00044       if (ptrs[i] == NULL)
00045        break;
00046 
00047       for (j = 0; j < size; ++j)
00048        {
00049          if (ptrs[i][j] != '\0')
00050            error (EXIT_FAILURE, 0,
00051                  "byte not cleared (size %d, element %d, byte %d)",
00052                  size, i, j);
00053          ptrs[i][j] = '\xff';
00054        }
00055     }
00056 
00057   while (i-- > 0)
00058     free (ptrs[i]);
00059 }
00060 
00061 
00062 static void
00063 random_test (void)
00064 {
00065   char *ptrs[N];
00066   int i;
00067 
00068   for (i = 0; i < N; ++i)
00069     {
00070       int j;
00071       int n = 1 + random () % 10;
00072       int elem = 1 + random () % 100;
00073       int size = n * elem;
00074 
00075       ptrs[i] = (char *) calloc (n, elem);
00076 
00077       if (ptrs[i] == NULL)
00078        break;
00079 
00080       for (j = 0; j < size; ++j)
00081        {
00082          if (ptrs[i][j] != '\0')
00083            error (EXIT_FAILURE, 0,
00084                  "byte not cleared (size %d, element %d, byte %d)",
00085                  size, i, j);
00086          ptrs[i][j] = '\xff';
00087        }
00088     }
00089 
00090   while (i-- > 0)
00091     free (ptrs[i]);
00092 }
00093 
00094 
00095 static void
00096 null_test (void)
00097 {
00098   /* If the size is 0 the result is implementation defined.  Just make
00099      sure the program doesn't crash.  */
00100   calloc (0, 0);
00101   calloc (0, UINT_MAX);
00102   calloc (UINT_MAX, 0);
00103   calloc (0, ~((size_t) 0));
00104   calloc (~((size_t) 0), 0);
00105 }
00106 
00107 
00108 int
00109 main (void)
00110 {
00111   /* We are allocating blocks with `calloc' and check whether every
00112      block is completely cleared.  We first try this for some fixed
00113      times and then with random size.  */
00114   fixed_test (15);
00115   fixed_test (5);
00116   fixed_test (17);
00117   fixed_test (6);
00118   fixed_test (31);
00119   fixed_test (96);
00120 
00121   random_test ();
00122 
00123   null_test ();
00124 
00125   return 0;
00126 }