1 /* Test and measure strdup functions.
2 Copyright (C) 2023 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 <support/check.h>
25 # define BIG_CHAR WCHAR_MAX
26 # define SMALL_CHAR 1273
27 # define STRCMP wcscmp
28 # define MEMCMP wmemcmp
29 # define MEMSET wmemset
30 # define TCS TEST_COMPARE_STRING_WIDE
34 # define BIG_CHAR CHAR_MAX
35 # define SMALL_CHAR 127
36 # define STRCMP strcmp
37 # define MEMCMP memcmp
38 # define MEMSET memset
39 # define TCS TEST_COMPARE_STRING
43 # define STRDUP_RESULT(dst, len) dst
46 # define TEST_NAME "strdup"
48 # define TEST_NAME "wcsdup"
50 # include "test-string.h"
52 # define STRDUP strdup
54 # define STRDUP wcsdup
58 typedef CHAR
*(*proto_t
) (const CHAR
*);
61 do_zero_len_test (void)
63 CHAR src
[1] = { '\0' };
64 CHAR
*dst
= STRDUP (src
);
71 do_one_test (const CHAR
*src
,
72 size_t len
__attribute__((unused
)))
74 CHAR
*dst
= STRDUP (src
);
76 if (STRCMP (dst
, src
) != 0)
79 "Wrong result in function %s dst \"%" sfmt
"\" src \"%" sfmt
"\"",
89 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
)
93 /* For wcsdup: align1 and align2 here mean alignment not in bytes,
94 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
95 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
97 if ((align1
+ len
) * sizeof (CHAR
) >= page_size
)
101 if ((align2
+ len
) * sizeof (CHAR
) >= page_size
)
104 s1
= (CHAR
*) (buf1
) + align1
;
106 for (i
= 0; i
< len
; i
++)
107 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
110 do_one_test (s1
, len
);
114 do_random_tests (void)
116 size_t i
, j
, n
, align1
, align2
, len
;
117 CHAR
*p1
= (CHAR
*)(buf1
+ page_size
) - 512;
120 for (n
= 0; n
< ITERATIONS
; n
++)
122 /* align1 and align2 are expressed as wchar_t and not in bytes for wide
123 char test, and thus it will be equal to align times wchar_t size.
125 For non wide version we need to check all alignments from 0 to 63
126 since some assembly implementations have separate prolog for alignments
129 align1
= random () & (63 / sizeof (CHAR
));
131 align2
= random () & (63 / sizeof (CHAR
));
133 align2
= align1
+ (random () & 24);
134 len
= random () & 511;
139 len
= 510 - j
- (random () & 7);
140 j
= len
+ align1
+ 64;
143 for (i
= 0; i
< j
; i
++)
145 if (i
== len
+ align1
)
149 p1
[i
] = random () & BIG_CHAR
;
150 if (i
>= align1
&& i
< len
+ align1
&& !p1
[i
])
151 p1
[i
] = (random () & SMALL_CHAR
) + 3;
155 res
= STRDUP(p1
+ align1
);
156 TCS (res
, (p1
+ align1
));
170 printf ("\t%s", TEST_NAME
);
173 for (i
= 0; i
< 16; ++i
)
175 do_test (0, 0, i
, SMALL_CHAR
);
176 do_test (0, 0, i
, BIG_CHAR
);
177 do_test (0, i
, i
, SMALL_CHAR
);
178 do_test (i
, 0, i
, BIG_CHAR
);
181 for (i
= 1; i
< 8; ++i
)
183 do_test (0, 0, 8 << i
, SMALL_CHAR
);
184 do_test (8 - i
, 2 * i
, 8 << i
, SMALL_CHAR
);
187 for (i
= 1; i
< 8; ++i
)
189 do_test (i
, 2 * i
, 8 << i
, SMALL_CHAR
);
190 do_test (2 * i
, i
, 8 << i
, BIG_CHAR
);
191 do_test (i
, i
, 8 << i
, SMALL_CHAR
);
192 do_test (i
, i
, 8 << i
, BIG_CHAR
);
201 #include <support/test-driver.c>