* sysdeps/unix/sysv/linux/m68k/vfork.S: Branch to __fork whenever
[glibc/pb-stable.git] / malloc / tst-calloc.c
blobb3594c937fd7731594d585709d0ae02da167e396
1 /* Copyright (C) 2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <limits.h>
23 #include <malloc.h>
24 #include <stdlib.h>
25 #include <stdio.h>
28 /* Number of samples per size. */
29 #define N 50000
32 static void
33 fixed_test (int size)
35 char *ptrs[N];
36 int i;
38 for (i = 0; i < N; ++i)
40 int j;
42 ptrs[i] = (char *) calloc (1, size);
44 if (ptrs[i] == NULL)
45 break;
47 for (j = 0; j < size; ++j)
49 if (ptrs[i][j] != '\0')
50 error (EXIT_FAILURE, 0,
51 "byte not cleared (size %d, element %d, byte %d)",
52 size, i, j);
53 ptrs[i][j] = '\xff';
57 while (i-- > 0)
58 free (ptrs[i]);
62 static void
63 random_test (void)
65 char *ptrs[N];
66 int i;
68 for (i = 0; i < N; ++i)
70 int j;
71 int n = 1 + random () % 10;
72 int elem = 1 + random () % 100;
73 int size = n * elem;
75 ptrs[i] = (char *) calloc (n, elem);
77 if (ptrs[i] == NULL)
78 break;
80 for (j = 0; j < size; ++j)
82 if (ptrs[i][j] != '\0')
83 error (EXIT_FAILURE, 0,
84 "byte not cleared (size %d, element %d, byte %d)",
85 size, i, j);
86 ptrs[i][j] = '\xff';
90 while (i-- > 0)
91 free (ptrs[i]);
95 static void
96 null_test (void)
98 /* If the size is 0 the result is implementation defined. Just make
99 sure the program doesn't crash. */
100 calloc (0, 0);
101 calloc (0, UINT_MAX);
102 calloc (UINT_MAX, 0);
103 calloc (0, ~((size_t) 0));
104 calloc (~((size_t) 0), 0);
109 main (void)
111 /* We are allocating blocks with `calloc' and check whether every
112 block is completely cleared. We first try this for some fixed
113 times and then with random size. */
114 fixed_test (15);
115 fixed_test (5);
116 fixed_test (17);
117 fixed_test (6);
118 fixed_test (31);
119 fixed_test (96);
121 random_test ();
123 null_test ();
125 return 0;