1 /* Test and measure STRLEN functions.
2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5 Added wcslen support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
23 # define TEST_NAME "strlen"
25 # define TEST_NAME "wcslen"
27 #include "test-string.h"
30 # define STRLEN strlen
32 # define MAX_CHAR CHAR_MAX
35 # define STRLEN wcslen
37 # define MAX_CHAR WCHAR_MAX
40 typedef size_t (*proto_t
) (const CHAR
*);
43 simple_STRLEN (const CHAR
*s
)
53 builtin_strlen (const CHAR
*p
)
55 return __builtin_strlen (p
);
57 IMPL (builtin_strlen
, 0)
60 IMPL (simple_STRLEN
, 0)
65 do_one_test (impl_t
*impl
, const CHAR
*s
, size_t exp_len
)
67 size_t len
= CALL (impl
, s
);
70 error (0, 0, "Wrong result in function %s %zd %zd", impl
->name
,
78 do_test (size_t align
, size_t len
)
83 if (align
+ sizeof (CHAR
) * len
>= page_size
)
86 CHAR
*buf
= (CHAR
*) (buf1
);
88 for (i
= 0; i
< len
; ++i
)
89 buf
[align
+ i
] = 1 + 11111 * i
% MAX_CHAR
;
92 FOR_EACH_IMPL (impl
, 0)
93 do_one_test (impl
, (CHAR
*) (buf
+ align
), len
);
97 do_random_tests (void)
99 size_t i
, j
, n
, align
, len
;
100 CHAR
*p
= (CHAR
*) (buf1
+ page_size
- 512 * sizeof (CHAR
));
102 for (n
= 0; n
< ITERATIONS
; n
++)
104 align
= random () & 15;
105 len
= random () & 511;
106 if (len
+ align
> 510)
107 len
= 511 - align
- (random () & 7);
108 j
= len
+ align
+ 64;
112 for (i
= 0; i
< j
; i
++)
114 if (i
== len
+ align
)
118 p
[i
] = random () & 255;
119 if (i
>= align
&& i
< len
+ align
&& !p
[i
])
120 p
[i
] = (random () & 127) + 1;
124 FOR_EACH_IMPL (impl
, 1)
125 if (CALL (impl
, (CHAR
*) (p
+ align
)) != len
)
127 error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
128 n
, impl
->name
, align
, CALL (impl
, (CHAR
*) (p
+ align
)),
143 FOR_EACH_IMPL (impl
, 0)
144 printf ("\t%s", impl
->name
);
147 /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
149 for (i
= 1; i
< 8; ++i
)
151 do_test (sizeof (CHAR
) * i
, i
);
155 for (i
= 2; i
<= 12; ++i
)
158 do_test (sizeof (CHAR
) * 7, 1 << i
);
159 do_test (sizeof (CHAR
) * i
, 1 << i
);
160 do_test (sizeof (CHAR
) * i
, (size_t)((1 << i
) / 1.5));
167 #include <support/test-driver.c>