Merge branch 'elf-move'
[platform/upstream/glibc.git] / string / test-strlen.c
1 /* Test and measure STRLEN functions.
2    Copyright (C) 1999, 2002, 2003, 2005, 2011 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
6
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.
11
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.
16
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    <http://www.gnu.org/licenses/>.  */
20
21 #define TEST_MAIN
22 #include "test-string.h"
23
24 #ifndef WIDE
25 # define STRLEN strlen
26 # define CHAR char
27 # define MAX_CHAR CHAR_MAX
28 #else
29 # include <wchar.h>
30 # define STRLEN wcslen
31 # define CHAR wchar_t
32 # define MAX_CHAR WCHAR_MAX
33 #endif
34
35 typedef size_t (*proto_t) (const CHAR *);
36
37 size_t
38 simple_STRLEN (const CHAR *s)
39 {
40   const CHAR *p;
41
42   for (p = s; *p; ++p);
43   return p - s;
44 }
45
46 #ifndef WIDE
47 size_t
48 builtin_strlen (const CHAR *p)
49 {
50   return __builtin_strlen (p);
51 }
52 IMPL (builtin_strlen, 0)
53 #endif
54
55 IMPL (simple_STRLEN, 0)
56 IMPL (STRLEN, 1)
57
58
59 static void
60 do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
61 {
62   size_t len = CALL (impl, s);
63   if (len != exp_len)
64     {
65       error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
66              len, exp_len);
67       ret = 1;
68       return;
69     }
70
71   if (HP_TIMING_AVAIL)
72     {
73       hp_timing_t start __attribute ((unused));
74       hp_timing_t stop __attribute ((unused));
75       hp_timing_t best_time = ~ (hp_timing_t) 0;
76       size_t i;
77
78       for (i = 0; i < 32; ++i)
79         {
80           HP_TIMING_NOW (start);
81           CALL (impl, s);
82           HP_TIMING_NOW (stop);
83           HP_TIMING_BEST (best_time, start, stop);
84         }
85
86       printf ("\t%zd", (size_t) best_time);
87     }
88 }
89
90 static void
91 do_test (size_t align, size_t len)
92 {
93   size_t i;
94
95   align &= 63;
96   if (align + sizeof(CHAR) * len >= page_size)
97     return;
98
99   CHAR *buf = (CHAR *) (buf1);
100
101   for (i = 0; i < len; ++i)
102     buf[align + i] = 1 + 11111 * i % MAX_CHAR;
103   buf[align + len] = 0;
104
105   if (HP_TIMING_AVAIL)
106     printf ("Length %4zd, alignment %2zd:", len, align);
107
108   FOR_EACH_IMPL (impl, 0)
109     do_one_test (impl, (CHAR *) (buf + align), len);
110
111   if (HP_TIMING_AVAIL)
112     putchar ('\n');
113 }
114
115 static void
116 do_random_tests (void)
117 {
118   size_t i, j, n, align, len;
119   CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof(CHAR));
120
121   for (n = 0; n < ITERATIONS; n++)
122     {
123       align = random () & 15;
124       len = random () & 511;
125       if (len + align > 510)
126         len = 511 - align - (random () & 7);
127       j = len + align + 64;
128       if (j > 512)
129         j = 512;
130
131       for (i = 0; i < j; i++)
132         {
133           if (i == len + align)
134             p[i] = 0;
135           else
136             {
137               p[i] = random () & 255;
138               if (i >= align && i < len + align && !p[i])
139                 p[i] = (random () & 127) + 1;
140             }
141         }
142
143       FOR_EACH_IMPL (impl, 1)
144         if (CALL (impl, (CHAR *) (p + align)) != len)
145           {
146             error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
147                    n, impl->name, align, CALL (impl, (CHAR *) (p + align)),
148                    len, p);
149             ret = 1;
150           }
151     }
152 }
153
154 int
155 test_main (void)
156 {
157   size_t i;
158
159   test_init ();
160
161   printf ("%20s", "");
162   FOR_EACH_IMPL (impl, 0)
163     printf ("\t%s", impl->name);
164   putchar ('\n');
165
166   /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
167
168   for (i = 1; i < 8; ++i)
169   {
170     do_test (sizeof(CHAR) * i, i);
171     do_test (0, i);
172   }
173
174   for (i = 2; i <= 12; ++i)
175     {
176       do_test (0, 1 << i);
177       do_test (sizeof(CHAR) * 7, 1 << i);
178       do_test (sizeof(CHAR) * i, 1 << i);
179       do_test (sizeof(CHAR) * i, (size_t)((1 << i) / 1.5));
180     }
181
182   do_random_tests ();
183   return ret;
184 }
185
186 #include "../test-skeleton.c"