Add PF_MCTP, AF_MCTP from Linux 5.15 to bits/socket.h
[glibc.git] / string / test-strcat.c
blobfe30fa1a995f7a1f9755e601d51b3dd36916e39c
1 /* Test strcat functions.
2 Copyright (C) 1999-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 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strcat"
22 #else
23 # define TEST_NAME "wcscat"
24 #endif /* WIDE */
25 #include "test-string.h"
27 #ifndef WIDE
28 # define STRCAT strcat
29 # define CHAR char
30 # define UCHAR unsigned char
31 # define sfmt "s"
32 # define SIMPLE_STRCAT simple_strcat
33 # define STRLEN strlen
34 # define STRCMP strcmp
35 # define MEMSET memset
36 # define MEMCPY memcpy
37 # define MEMCMP memcmp
38 # define BIG_CHAR CHAR_MAX
39 # define SMALL_CHAR 127
40 #else
41 # include <wchar.h>
42 # define STRCAT wcscat
43 # define CHAR wchar_t
44 # define UCHAR wchar_t
45 # define sfmt "ls"
46 # define SIMPLE_STRCAT simple_wcscat
47 # define STRLEN wcslen
48 # define STRCMP wcscmp
49 # define MEMSET wmemset
50 # define MEMCPY wmemcpy
51 # define MEMCMP wmemcmp
52 # define BIG_CHAR WCHAR_MAX
53 # define SMALL_CHAR 1273
54 #endif /* WIDE */
56 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
57 CHAR *SIMPLE_STRCAT (CHAR *, const CHAR *);
59 IMPL (SIMPLE_STRCAT, 0)
60 IMPL (STRCAT, 1)
62 CHAR *
63 SIMPLE_STRCAT (CHAR *dst, const CHAR *src)
65 CHAR *ret = dst;
66 while (*dst++ != '\0');
67 --dst;
68 while ((*dst++ = *src++) != '\0');
69 return ret;
72 static void
73 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src)
75 size_t k = STRLEN (dst);
76 if (CALL (impl, dst, src) != dst)
78 error (0, 0, "Wrong result in function %s %p %p", impl->name,
79 CALL (impl, dst, src), dst);
80 ret = 1;
81 return;
84 if (STRCMP (dst + k, src) != 0)
86 error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
87 impl->name, dst, src);
88 ret = 1;
89 return;
93 static void
94 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
96 size_t i;
97 CHAR *s1, *s2;
99 align1 &= 7;
100 if ((align1 + len1) * sizeof (CHAR) >= page_size)
101 return;
103 align2 &= 7;
104 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
105 return;
107 s1 = (CHAR *) (buf1) + align1;
108 s2 = (CHAR *) (buf2) + align2;
110 for (i = 0; i < len1; ++i)
111 s1[i] = 32 + 23 * i % (max_char - 32);
112 s1[len1] = '\0';
114 for (i = 0; i < len2; i++)
115 s2[i] = 32 + 23 * i % (max_char - 32);
117 FOR_EACH_IMPL (impl, 0)
119 s2[len2] = '\0';
120 do_one_test (impl, s2, s1);
124 static void
125 do_random_tests (void)
127 size_t i, j, n, align1, align2, len1, len2;
128 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
129 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
130 UCHAR *p3 = (UCHAR *) buf1;
131 UCHAR *res;
133 for (n = 0; n < ITERATIONS; n++)
135 align1 = random () & 31;
136 if (random () & 1)
137 align2 = random () & 31;
138 else
139 align2 = align1 + (random () & 24);
140 len1 = random () & 511;
141 if (len1 + align2 > 512)
142 len2 = random () & 7;
143 else
144 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
145 / (1024 * 1024);
146 j = align1;
147 if (align2 + len2 > j)
148 j = align2 + len2;
149 if (len1 + j >= 511)
150 len1 = 510 - j - (random () & 7);
151 if (len1 >= 512)
152 len1 = 0;
153 if (align1 + len1 < 512 - 8)
155 j = 510 - align1 - len1 - (random () & 31);
156 if (j > 0 && j < 512)
157 align1 += j;
159 j = len1 + align1 + 64;
160 if (j > 512)
161 j = 512;
162 for (i = 0; i < j; i++)
164 if (i == len1 + align1)
165 p1[i] = 0;
166 else
168 p1[i] = random () & BIG_CHAR;
169 if (i >= align1 && i < len1 + align1 && !p1[i])
170 p1[i] = (random () & SMALL_CHAR) + 3;
173 for (i = 0; i < len2; i++)
175 p3[i] = random () & BIG_CHAR;
176 if (!p3[i])
177 p3[i] = (random () & SMALL_CHAR) + 3;
179 p3[len2] = 0;
181 FOR_EACH_IMPL (impl, 1)
183 MEMSET (p2 - 64, '\1', align2 + 64);
184 MEMSET (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
185 MEMCPY (p2 + align2, p3, len2 + 1);
186 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
187 (CHAR *) (p1 + align1));
188 if (res != p2 + align2)
190 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
191 n, impl->name, align1, align2, len1, len2, res,
192 p2 + align2);
193 ret = 1;
195 for (j = 0; j < align2 + 64; ++j)
197 if (p2[j - 64] != '\1')
199 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
200 n, impl->name, align1, align2, len1, len2);
201 ret = 1;
202 break;
205 if (MEMCMP (p2 + align2, p3, len2))
207 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
208 n, impl->name, align1, align2, len1, len2);
209 ret = 1;
211 for (j = align2 + len1 + len2 + 1; j < 512; ++j)
213 if (p2[j] != '\1')
215 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
216 n, impl->name, align1, align2, len1, len2);
217 ret = 1;
218 break;
221 if (MEMCMP (p1 + align1, p2 + align2 + len2, len1 + 1))
223 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
224 n, impl->name, align1, align2, len1, len2);
225 ret = 1;
232 test_main (void)
234 size_t i;
236 test_init ();
238 printf ("%28s", "");
239 FOR_EACH_IMPL (impl, 0)
240 printf ("\t%s", impl->name);
241 putchar ('\n');
243 for (i = 0; i < 16; ++i)
245 do_test (0, 0, i, i, SMALL_CHAR);
246 do_test (0, 0, i, i, BIG_CHAR);
247 do_test (0, i, i, i, SMALL_CHAR);
248 do_test (i, 0, i, i, BIG_CHAR);
251 for (i = 1; i < 8; ++i)
253 do_test (0, 0, 8 << i, 8 << i, SMALL_CHAR);
254 do_test (8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR);
255 do_test (0, 0, 8 << i, 2 << i, SMALL_CHAR);
256 do_test (8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR);
259 for (i = 1; i < 8; ++i)
261 do_test (i, 2 * i, 8 << i, 1, SMALL_CHAR);
262 do_test (2 * i, i, 8 << i, 1, BIG_CHAR);
263 do_test (i, i, 8 << i, 10, SMALL_CHAR);
264 do_test (i, i, 8 << i, 10, BIG_CHAR);
267 do_random_tests ();
268 return ret;
271 #include <support/test-driver.c>