mips: FIx clone3 implementation (BZ 31325)
[glibc.git] / malloc / tst-scratch_buffer.c
blob2e27a18d7729b64f876e30af4da0876a3a45bac8
1 /*
2 Copyright (C) 2015-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <scratch_buffer.h>
21 #include <support/check.h>
22 #include <support/support.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <string.h>
27 static bool
28 unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
30 size_t old_length = buf->length;
31 if (!scratch_buffer_set_array_size (buf, a, b))
33 printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
34 a, b);
35 return false;
37 if (old_length != buf->length)
39 printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
40 a, b);
41 return false;
43 return true;
46 static bool
47 array_size_must_fail (size_t a, size_t b)
49 for (int pass = 0; pass < 2; ++pass)
51 struct scratch_buffer buf;
52 scratch_buffer_init (&buf);
53 if (pass > 0)
54 if (!scratch_buffer_grow (&buf))
56 printf ("scratch_buffer_grow in array_size_must_fail failed\n");
57 return false;
59 if (scratch_buffer_set_array_size (&buf, a, b))
61 printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
62 pass, a, b);
63 return false;
65 if (buf.data != buf.__space.__c)
67 printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
68 pass, a, b);
69 return false;
72 return true;
75 static int
76 do_test (void)
79 struct scratch_buffer buf;
80 scratch_buffer_init (&buf);
81 memset (buf.data, ' ', buf.length);
82 scratch_buffer_free (&buf);
85 struct scratch_buffer buf;
86 scratch_buffer_init (&buf);
87 memset (buf.data, ' ', buf.length);
88 size_t old_length = buf.length;
89 scratch_buffer_grow (&buf);
90 if (buf.length <= old_length)
92 printf ("scratch_buffer_grow did not enlarge buffer\n");
93 return 1;
95 memset (buf.data, ' ', buf.length);
96 scratch_buffer_free (&buf);
99 struct scratch_buffer buf;
100 scratch_buffer_init (&buf);
101 memset (buf.data, '@', buf.length);
102 strcpy (buf.data, "prefix");
103 size_t old_length = buf.length;
104 scratch_buffer_grow_preserve (&buf);
105 if (buf.length <= old_length)
107 printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
108 return 1;
110 if (strcmp (buf.data, "prefix") != 0)
112 printf ("scratch_buffer_grow_preserve did not copy buffer\n");
113 return 1;
115 for (unsigned i = 7; i < old_length; ++i)
116 if (((char *)buf.data)[i] != '@')
118 printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
120 return 1;
122 scratch_buffer_free (&buf);
125 struct scratch_buffer buf;
126 scratch_buffer_init (&buf);
127 for (int pass = 0; pass < 4; ++pass)
129 if (!(unchanged_array_size (&buf, 0, 0)
130 && unchanged_array_size (&buf, 1, 0)
131 && unchanged_array_size (&buf, 0, 1)
132 && unchanged_array_size (&buf, -1, 0)
133 && unchanged_array_size (&buf, 0, -1)
134 && unchanged_array_size (&buf, 1ULL << 16, 0)
135 && unchanged_array_size (&buf, 0, 1ULL << 16)
136 && unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
137 && unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
138 return 1;
139 if (!scratch_buffer_grow (&buf))
141 printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
144 scratch_buffer_free (&buf);
147 if (!(array_size_must_fail (-1, 1)
148 && array_size_must_fail (-1, -1)
149 && array_size_must_fail (1, -1)
150 && array_size_must_fail (((size_t)-1) / 4, 4)
151 && array_size_must_fail (4, ((size_t)-1) / 4)))
152 return 1;
154 return 0;
157 #include <support/test-driver.c>