2 Copyright (C) 2015-2021 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>
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",
37 if (old_length
!= buf
->length
)
39 printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
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
);
54 if (!scratch_buffer_grow (&buf
))
56 printf ("scratch_buffer_grow in array_size_must_fail failed\n");
59 if (scratch_buffer_set_array_size (&buf
, a
, b
))
61 printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
65 if (buf
.data
!= buf
.__space
.__c
)
67 printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
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");
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");
110 if (strcmp (buf
.data
, "prefix") != 0)
112 printf ("scratch_buffer_grow_preserve did not copy buffer\n");
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",
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))))
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)))
155 struct scratch_buffer buf
;
156 scratch_buffer_init (&buf
);
157 memset (buf
.data
, '@', buf
.length
);
159 size_t sizes
[] = { 16, buf
.length
, buf
.length
+ 16 };
160 for (int i
= 0; i
< array_length (sizes
); i
++)
162 /* The extra size is unitialized through realloc. */
163 size_t l
= sizes
[i
] > buf
.length
? sizes
[i
] : buf
.length
;
164 void *r
= scratch_buffer_dupfree (&buf
, l
);
165 void *c
= xmalloc (l
);
167 TEST_COMPARE_BLOB (r
, l
, buf
.data
, l
);
172 scratch_buffer_free (&buf
);
177 #include <support/test-driver.c>