Add IPV6_FREEBIND from Linux 4.15.
[glibc.git] / malloc / tst-scratch_buffer.c
blob59310dd811d967e6856356ba15621c7afef1e1e7
1 /*
2 Copyright (C) 2015-2018 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 <http://www.gnu.org/licenses/>. */
19 #include <scratch_buffer.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <string.h>
24 static bool
25 unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
27 size_t old_length = buf->length;
28 if (!scratch_buffer_set_array_size (buf, a, b))
30 printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
31 a, b);
32 return false;
34 if (old_length != buf->length)
36 printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
37 a, b);
38 return false;
40 return true;
43 static bool
44 array_size_must_fail (size_t a, size_t b)
46 for (int pass = 0; pass < 2; ++pass)
48 struct scratch_buffer buf;
49 scratch_buffer_init (&buf);
50 if (pass > 0)
51 if (!scratch_buffer_grow (&buf))
53 printf ("scratch_buffer_grow in array_size_must_fail failed\n");
54 return false;
56 if (scratch_buffer_set_array_size (&buf, a, b))
58 printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
59 pass, a, b);
60 return false;
62 if (buf.data != buf.__space.__c)
64 printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
65 pass, a, b);
66 return false;
69 return true;
72 static int
73 do_test (void)
76 struct scratch_buffer buf;
77 scratch_buffer_init (&buf);
78 memset (buf.data, ' ', buf.length);
79 scratch_buffer_free (&buf);
82 struct scratch_buffer buf;
83 scratch_buffer_init (&buf);
84 memset (buf.data, ' ', buf.length);
85 size_t old_length = buf.length;
86 scratch_buffer_grow (&buf);
87 if (buf.length <= old_length)
89 printf ("scratch_buffer_grow did not enlarge buffer\n");
90 return 1;
92 memset (buf.data, ' ', buf.length);
93 scratch_buffer_free (&buf);
96 struct scratch_buffer buf;
97 scratch_buffer_init (&buf);
98 memset (buf.data, '@', buf.length);
99 strcpy (buf.data, "prefix");
100 size_t old_length = buf.length;
101 scratch_buffer_grow_preserve (&buf);
102 if (buf.length <= old_length)
104 printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
105 return 1;
107 if (strcmp (buf.data, "prefix") != 0)
109 printf ("scratch_buffer_grow_preserve did not copy buffer\n");
110 return 1;
112 for (unsigned i = 7; i < old_length; ++i)
113 if (((char *)buf.data)[i] != '@')
115 printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
117 return 1;
119 scratch_buffer_free (&buf);
122 struct scratch_buffer buf;
123 scratch_buffer_init (&buf);
124 for (int pass = 0; pass < 4; ++pass)
126 if (!(unchanged_array_size (&buf, 0, 0)
127 && unchanged_array_size (&buf, 1, 0)
128 && unchanged_array_size (&buf, 0, 1)
129 && unchanged_array_size (&buf, -1, 0)
130 && unchanged_array_size (&buf, 0, -1)
131 && unchanged_array_size (&buf, 1ULL << 16, 0)
132 && unchanged_array_size (&buf, 0, 1ULL << 16)
133 && unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
134 && unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
135 return 1;
136 if (!scratch_buffer_grow (&buf))
138 printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
141 scratch_buffer_free (&buf);
144 if (!(array_size_must_fail (-1, 1)
145 && array_size_must_fail (-1, -1)
146 && array_size_must_fail (1, -1)
147 && array_size_must_fail (((size_t)-1) / 4, 4)
148 && array_size_must_fail (4, ((size_t)-1) / 4)))
149 return 1;
151 return 0;
154 #define TEST_FUNCTION do_test ()
155 #include "../test-skeleton.c"