]> git.ipfire.org Git - thirdparty/glibc.git/blame - malloc/tst-calloc.c
Update to LGPL v2.1.
[thirdparty/glibc.git] / malloc / tst-calloc.c
CommitLineData
aa780bab
UD
1/* Copyright (C) 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
aa780bab
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
aa780bab 14
41bdb6e2
AJ
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. */
aa780bab
UD
19
20#include <errno.h>
21#include <error.h>
22#include <malloc.h>
23#include <stdlib.h>
24#include <stdio.h>
25
26
27/* Number of samples per size. */
28#define N 50000
29
30
31static void
32fixed_test (int size)
33{
34 char *ptrs[N];
35 int i;
36
37 for (i = 0; i < N; ++i)
38 {
39 int j;
40
41 ptrs[i] = (char *) calloc (1, size);
42
43 if (ptrs[i] == NULL)
44 break;
45
46 for (j = 0; j < size; ++j)
47 {
48 if (ptrs[i][j] != '\0')
49 error (EXIT_FAILURE, 0,
50 "byte not cleared (size %d, element %d, byte %d)",
51 size, i, j);
52 ptrs[i][j] = '\xff';
53 }
54 }
55
56 while (i-- > 0)
57 free (ptrs[i]);
58}
59
60
61static void
62random_test (void)
63{
64 char *ptrs[N];
65 int i;
66
67 for (i = 0; i < N; ++i)
68 {
69 int j;
70 int n = 1 + random () % 10;
71 int elem = 1 + random () % 100;
72 int size = n * elem;
73
74 ptrs[i] = (char *) calloc (n, elem);
75
76 if (ptrs[i] == NULL)
77 break;
78
79 for (j = 0; j < size; ++j)
80 {
81 if (ptrs[i][j] != '\0')
82 error (EXIT_FAILURE, 0,
83 "byte not cleared (size %d, element %d, byte %d)",
84 size, i, j);
85 ptrs[i][j] = '\xff';
86 }
87 }
88
89 while (i-- > 0)
90 free (ptrs[i]);
91}
92
93
94int
95main (void)
96{
97 /* We are allocating blocks with `calloc' and check whether every
98 block is completely cleared. We first try this for some fixed
99 times and then with random size. */
100 fixed_test (15);
101 fixed_test (5);
102 fixed_test (17);
103 fixed_test (6);
104 fixed_test (31);
105 fixed_test (96);
106
107 random_test ();
108
109 return 0;
110}