ldbl-128ibm-compat: enforce ibm128 on compat tests
[glibc.git] / string / test-strcat.c
bloba7f1d76092118a1bab73f3185967532e11397117
1 /* Test strcat functions.
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #ifndef WIDE
22 # define TEST_NAME "strcat"
23 #else
24 # define TEST_NAME "wcscat"
25 #endif /* WIDE */
26 #include "test-string.h"
28 #ifndef WIDE
29 # define STRCAT strcat
30 # define CHAR char
31 # define UCHAR unsigned char
32 # define sfmt "s"
33 # define SIMPLE_STRCAT simple_strcat
34 # define STRLEN strlen
35 # define STRCMP strcmp
36 # define MEMSET memset
37 # define MEMCPY memcpy
38 # define MEMCMP memcmp
39 # define BIG_CHAR CHAR_MAX
40 # define SMALL_CHAR 127
41 #else
42 # include <wchar.h>
43 # define STRCAT wcscat
44 # define CHAR wchar_t
45 # define UCHAR wchar_t
46 # define sfmt "ls"
47 # define SIMPLE_STRCAT simple_wcscat
48 # define STRLEN wcslen
49 # define STRCMP wcscmp
50 # define MEMSET wmemset
51 # define MEMCPY wmemcpy
52 # define MEMCMP wmemcmp
53 # define BIG_CHAR WCHAR_MAX
54 # define SMALL_CHAR 1273
55 #endif /* WIDE */
57 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
58 CHAR *SIMPLE_STRCAT (CHAR *, const CHAR *);
60 IMPL (SIMPLE_STRCAT, 0)
61 IMPL (STRCAT, 1)
63 CHAR *
64 SIMPLE_STRCAT (CHAR *dst, const CHAR *src)
66 CHAR *ret = dst;
67 while (*dst++ != '\0');
68 --dst;
69 while ((*dst++ = *src++) != '\0');
70 return ret;
73 static void
74 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src)
76 size_t k = STRLEN (dst);
77 if (CALL (impl, dst, src) != dst)
79 error (0, 0, "Wrong result in function %s %p %p", impl->name,
80 CALL (impl, dst, src), dst);
81 ret = 1;
82 return;
85 if (STRCMP (dst + k, src) != 0)
87 error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
88 impl->name, dst, src);
89 ret = 1;
90 return;
94 static void
95 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
97 size_t i;
98 CHAR *s1, *s2;
100 align1 &= 7;
101 if ((align1 + len1) * sizeof (CHAR) >= page_size)
102 return;
104 align2 &= 7;
105 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
106 return;
108 s1 = (CHAR *) (buf1) + align1;
109 s2 = (CHAR *) (buf2) + align2;
111 for (i = 0; i < len1; ++i)
112 s1[i] = 32 + 23 * i % (max_char - 32);
113 s1[len1] = '\0';
115 for (i = 0; i < len2; i++)
116 s2[i] = 32 + 23 * i % (max_char - 32);
118 FOR_EACH_IMPL (impl, 0)
120 s2[len2] = '\0';
121 do_one_test (impl, s2, s1);
125 static void
126 do_random_tests (void)
128 size_t i, j, n, align1, align2, len1, len2;
129 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
130 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
131 UCHAR *p3 = (UCHAR *) buf1;
132 UCHAR *res;
134 for (n = 0; n < ITERATIONS; n++)
136 align1 = random () & 31;
137 if (random () & 1)
138 align2 = random () & 31;
139 else
140 align2 = align1 + (random () & 24);
141 len1 = random () & 511;
142 if (len1 + align2 > 512)
143 len2 = random () & 7;
144 else
145 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
146 / (1024 * 1024);
147 j = align1;
148 if (align2 + len2 > j)
149 j = align2 + len2;
150 if (len1 + j >= 511)
151 len1 = 510 - j - (random () & 7);
152 if (len1 >= 512)
153 len1 = 0;
154 if (align1 + len1 < 512 - 8)
156 j = 510 - align1 - len1 - (random () & 31);
157 if (j > 0 && j < 512)
158 align1 += j;
160 j = len1 + align1 + 64;
161 if (j > 512)
162 j = 512;
163 for (i = 0; i < j; i++)
165 if (i == len1 + align1)
166 p1[i] = 0;
167 else
169 p1[i] = random () & BIG_CHAR;
170 if (i >= align1 && i < len1 + align1 && !p1[i])
171 p1[i] = (random () & SMALL_CHAR) + 3;
174 for (i = 0; i < len2; i++)
176 p3[i] = random () & BIG_CHAR;
177 if (!p3[i])
178 p3[i] = (random () & SMALL_CHAR) + 3;
180 p3[len2] = 0;
182 FOR_EACH_IMPL (impl, 1)
184 MEMSET (p2 - 64, '\1', align2 + 64);
185 MEMSET (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
186 MEMCPY (p2 + align2, p3, len2 + 1);
187 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
188 (CHAR *) (p1 + align1));
189 if (res != p2 + align2)
191 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
192 n, impl->name, align1, align2, len1, len2, res,
193 p2 + align2);
194 ret = 1;
196 for (j = 0; j < align2 + 64; ++j)
198 if (p2[j - 64] != '\1')
200 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
201 n, impl->name, align1, align2, len1, len2);
202 ret = 1;
203 break;
206 if (MEMCMP (p2 + align2, p3, len2))
208 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
209 n, impl->name, align1, align2, len1, len2);
210 ret = 1;
212 for (j = align2 + len1 + len2 + 1; j < 512; ++j)
214 if (p2[j] != '\1')
216 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
217 n, impl->name, align1, align2, len1, len2);
218 ret = 1;
219 break;
222 if (MEMCMP (p1 + align1, p2 + align2 + len2, len1 + 1))
224 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
225 n, impl->name, align1, align2, len1, len2);
226 ret = 1;
233 test_main (void)
235 size_t i;
237 test_init ();
239 printf ("%28s", "");
240 FOR_EACH_IMPL (impl, 0)
241 printf ("\t%s", impl->name);
242 putchar ('\n');
244 for (i = 0; i < 16; ++i)
246 do_test (0, 0, i, i, SMALL_CHAR);
247 do_test (0, 0, i, i, BIG_CHAR);
248 do_test (0, i, i, i, SMALL_CHAR);
249 do_test (i, 0, i, i, BIG_CHAR);
252 for (i = 1; i < 8; ++i)
254 do_test (0, 0, 8 << i, 8 << i, SMALL_CHAR);
255 do_test (8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR);
256 do_test (0, 0, 8 << i, 2 << i, SMALL_CHAR);
257 do_test (8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR);
260 for (i = 1; i < 8; ++i)
262 do_test (i, 2 * i, 8 << i, 1, SMALL_CHAR);
263 do_test (2 * i, i, 8 << i, 1, BIG_CHAR);
264 do_test (i, i, 8 << i, 10, SMALL_CHAR);
265 do_test (i, i, 8 << i, 10, BIG_CHAR);
268 do_random_tests ();
269 return ret;
272 #include <support/test-driver.c>