benchtests: Add workload name for cosf
[glibc.git] / malloc / tst-realloc.c
blob5eb62a770f2fed32464cc115f5ed44994bbac98d
1 /* Copyright (C) 2013-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <malloc.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <libc-diag.h>
23 #include <support/check.h>
25 static int
26 do_test (void)
28 void *p;
29 unsigned char *c;
30 int save, i, ok;
32 errno = 0;
34 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
35 DIAG_PUSH_NEEDS_COMMENT;
36 #if __GNUC_PREREQ (7, 0)
37 /* GCC 7 warns about too-large allocations; here we want to test
38 that they fail. */
39 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
40 #endif
41 p = realloc (NULL, -1);
42 DIAG_POP_NEEDS_COMMENT;
43 save = errno;
45 if (p != NULL)
46 FAIL_EXIT1 ("realloc (NULL, -1) succeeded.");
48 /* errno should be set to ENOMEM on failure (POSIX). */
49 if (p == NULL && save != ENOMEM)
50 FAIL_EXIT1 ("errno is not set correctly");
52 errno = 0;
54 /* realloc (NULL, ...) behaves similarly to malloc (C89). */
55 p = realloc (NULL, 10);
56 save = errno;
58 if (p == NULL)
59 FAIL_EXIT1 ("realloc (NULL, 10) failed.");
61 free (p);
63 p = calloc (20, 1);
64 if (p == NULL)
65 FAIL_EXIT1 ("calloc (20, 1) failed.");
67 /* Check increasing size preserves contents (C89). */
68 p = realloc (p, 200);
69 if (p == NULL)
70 FAIL_EXIT1 ("realloc (p, 200) failed.");
72 c = p;
73 ok = 1;
75 for (i = 0; i < 20; i++)
77 if (c[i] != 0)
78 ok = 0;
81 if (ok == 0)
82 FAIL_EXIT1 ("first 20 bytes were not cleared");
84 free (p);
86 p = realloc (NULL, 100);
87 if (p == NULL)
88 FAIL_EXIT1 ("realloc (NULL, 100) failed.");
90 memset (p, 0xff, 100);
92 /* Check decreasing size preserves contents (C89). */
93 p = realloc (p, 16);
94 if (p == NULL)
95 FAIL_EXIT1 ("realloc (p, 16) failed.");
97 c = p;
98 ok = 1;
100 for (i = 0; i < 16; i++)
102 if (c[i] != 0xff)
103 ok = 0;
106 if (ok == 0)
107 FAIL_EXIT1 ("first 16 bytes were not correct");
109 /* Check failed realloc leaves original untouched (C89). */
110 DIAG_PUSH_NEEDS_COMMENT;
111 #if __GNUC_PREREQ (7, 0)
112 /* GCC 7 warns about too-large allocations; here we want to test
113 that they fail. */
114 DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
115 #endif
116 c = realloc (p, -1);
117 DIAG_POP_NEEDS_COMMENT;
118 if (c != NULL)
119 FAIL_EXIT1 ("realloc (p, -1) succeeded.");
121 c = p;
122 ok = 1;
124 for (i = 0; i < 16; i++)
126 if (c[i] != 0xff)
127 ok = 0;
130 if (ok == 0)
131 FAIL_EXIT1 ("first 16 bytes were not correct after failed realloc");
133 /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */
134 p = realloc (p, 0);
135 if (p != NULL)
136 FAIL_EXIT1 ("realloc (p, 0) returned non-NULL.");
138 /* realloc (NULL, 0) acts like malloc (0) (glibc). */
139 p = realloc (NULL, 0);
140 if (p == NULL)
141 FAIL_EXIT1 ("realloc (NULL, 0) returned NULL.");
143 free (p);
145 return 0;
148 #define TEST_FUNCTION do_test ()
149 #include "../test-skeleton.c"