1 /* Test and measure STRLEN functions.
2    Copyright (C) 1999-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strlen"
22 #else
23 # define TEST_NAME "wcslen"
24 #endif
25 #include "test-string.h"
26 
27 #ifndef WIDE
28 # define STRLEN strlen
29 # define CHAR char
30 # define MAX_CHAR CHAR_MAX
31 #else
32 # include <wchar.h>
33 # define STRLEN wcslen
34 # define CHAR wchar_t
35 # define MAX_CHAR WCHAR_MAX
36 #endif
37 
38 typedef size_t (*proto_t) (const CHAR *);
39 
40 /* Naive implementation to verify results.  */
41 size_t
simple_STRLEN(const CHAR * s)42 simple_STRLEN (const CHAR *s)
43 {
44   const CHAR *p;
45 
46   for (p = s; *p; ++p);
47   return p - s;
48 }
49 
50 #ifndef WIDE
51 size_t
builtin_strlen(const CHAR * p)52 builtin_strlen (const CHAR *p)
53 {
54   return __builtin_strlen (p);
55 }
56 IMPL (builtin_strlen, 0)
57 #endif
58 
59 IMPL (STRLEN, 1)
60 
61 
62 static void
do_one_test(impl_t * impl,const CHAR * s,size_t exp_len)63 do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
64 {
65   size_t len = CALL (impl, s);
66   if (len != exp_len)
67     {
68       error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
69 	     len, exp_len);
70       ret = 1;
71       return;
72     }
73 }
74 
75 static void
do_test(size_t align,size_t len)76 do_test (size_t align, size_t len)
77 {
78   size_t i;
79 
80   align &= (getpagesize () / sizeof (CHAR)) - 1;
81   if (align + sizeof (CHAR) * len >= page_size)
82     return;
83 
84   CHAR *buf = (CHAR *) (buf1);
85 
86   for (i = 0; i < len; ++i)
87     buf[align + i] = 1 + 11111 * i % MAX_CHAR;
88   buf[align + len] = 0;
89 
90   FOR_EACH_IMPL (impl, 0)
91     do_one_test (impl, (CHAR *) (buf + align), len);
92 }
93 
94 static void
do_random_tests(void)95 do_random_tests (void)
96 {
97   size_t i, j, n, align, len;
98   CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
99 
100   for (n = 0; n < ITERATIONS; n++)
101     {
102       align = random () & 15;
103       len = random () & 511;
104       if (len + align > 510)
105 	len = 511 - align - (random () & 7);
106       j = len + align + 64;
107       if (j > 512)
108 	j = 512;
109 
110       for (i = 0; i < j; i++)
111 	{
112 	  if (i == len + align)
113 	    p[i] = 0;
114 	  else
115 	    {
116 	      p[i] = random () & 255;
117 	      if (i >= align && i < len + align && !p[i])
118 		p[i] = (random () & 127) + 1;
119 	    }
120 	}
121 
122       FOR_EACH_IMPL (impl, 1)
123 	if (CALL (impl, (CHAR *) (p + align)) != len)
124 	  {
125 	    error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
126 		   n, impl->name, align, CALL (impl, (CHAR *) (p + align)),
127 		   len, p);
128 	    ret = 1;
129 	  }
130     }
131 }
132 
133 int
test_main(void)134 test_main (void)
135 {
136   size_t i;
137 
138   test_init ();
139 
140   printf ("%20s", "");
141   FOR_EACH_IMPL (impl, 0)
142     printf ("\t%s", impl->name);
143   putchar ('\n');
144 
145   /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
146 
147   for (i = 1; i < 8; ++i)
148   {
149     do_test (sizeof (CHAR) * i, i);
150     do_test (0, i);
151   }
152 
153   for (i = 2; i <= 12; ++i)
154     {
155       do_test (0, 1 << i);
156       do_test (sizeof (CHAR) * 7, 1 << i);
157       do_test (sizeof (CHAR) * i, 1 << i);
158       do_test (sizeof (CHAR) * i, (size_t)((1 << i) / 1.5));
159     }
160 
161   /* Test strings near page boundary */
162 
163   size_t maxlength = 64 / sizeof (CHAR) - 1;
164   size_t pagesize = getpagesize () / sizeof (CHAR);
165 
166   for (i = maxlength ; i > 1; --i)
167     {
168       /* String stays on the same page.  */
169       do_test (pagesize - i, i - 1);
170       /* String crosses page boundary.  */
171       do_test (pagesize - i, maxlength);
172     }
173 
174   do_random_tests ();
175   return ret;
176 }
177 
178 #include <support/test-driver.c>
179