Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / string / test-strnlen.c
1 /* Test and measure strlen functions.
2    Copyright (C) 1999-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
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.
10
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.
15
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    <http://www.gnu.org/licenses/>.  */
19
20 #define TEST_MAIN
21 #define TEST_NAME "strnlen"
22 #include "test-string.h"
23
24 typedef size_t (*proto_t) (const char *, size_t);
25 size_t simple_strnlen (const char *, size_t);
26
27 IMPL (simple_strnlen, 0)
28 IMPL (strnlen, 1)
29
30 size_t
31 simple_strnlen (const char *s, size_t maxlen)
32 {
33   size_t i;
34
35   for (i = 0; i < maxlen && s[i]; ++i);
36   return i;
37 }
38
39 static void
40 do_one_test (impl_t *impl, const char *s, size_t maxlen, size_t exp_len)
41 {
42   size_t len = CALL (impl, s, maxlen);
43   if (len != exp_len)
44     {
45       error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
46              len, exp_len);
47       ret = 1;
48       return;
49     }
50
51   if (HP_TIMING_AVAIL)
52     {
53       hp_timing_t start __attribute ((unused));
54       hp_timing_t stop __attribute ((unused));
55       hp_timing_t best_time = ~ (hp_timing_t) 0;
56       size_t i;
57
58       for (i = 0; i < 32; ++i)
59         {
60           HP_TIMING_NOW (start);
61           CALL (impl, s, maxlen);
62           HP_TIMING_NOW (stop);
63           HP_TIMING_BEST (best_time, start, stop);
64         }
65
66       printf ("\t%zd", (size_t) best_time);
67     }
68 }
69
70 static void
71 do_test (size_t align, size_t len, size_t maxlen, int max_char)
72 {
73   size_t i;
74
75   align &= 7;
76   if (align + len >= page_size)
77     return;
78
79   for (i = 0; i < len; ++i)
80     buf1[align + i] = 1 + 7 * i % max_char;
81   buf1[align + len] = 0;
82
83   if (HP_TIMING_AVAIL)
84     printf ("Length %4zd, alignment %2zd:", len, align);
85
86   FOR_EACH_IMPL (impl, 0)
87     do_one_test (impl, (char *) (buf1 + align), maxlen, MIN (len, maxlen));
88
89   if (HP_TIMING_AVAIL)
90     putchar ('\n');
91 }
92
93 static void
94 do_random_tests (void)
95 {
96   size_t i, j, n, align, len;
97   unsigned char *p = buf1 + page_size - 512;
98
99   for (n = 0; n < ITERATIONS; n++)
100     {
101       align = random () & 15;
102       len = random () & 511;
103       if (len + align > 510)
104         len = 511 - align - (random () & 7);
105       j = len + align + 64;
106       if (j > 512)
107         j = 512;
108
109       for (i = 0; i < j; i++)
110         {
111           if (i == len + align)
112             p[i] = 0;
113           else
114             {
115               p[i] = random () & 255;
116               if (i >= align && i < len + align && !p[i])
117                 p[i] = (random () & 127) + 1;
118             }
119         }
120
121       FOR_EACH_IMPL (impl, 1)
122         {
123           if (len > 0
124               && CALL (impl, (char *) (p + align), len - 1) != len - 1)
125             {
126               error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
127                      n, impl->name, align,
128                      CALL (impl, (char *) (p + align), len - 1), len - 1, p);
129               ret = 1;
130             }
131           if (CALL (impl, (char *) (p + align), len) != len)
132             {
133               error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
134                      n, impl->name, align,
135                      CALL (impl, (char *) (p + align), len), len, p);
136               ret = 1;
137             }
138           if (CALL (impl, (char *) (p + align), len + 1) != len)
139             {
140               error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
141                      n, impl->name, align,
142                      CALL (impl, (char *) (p + align), len + 1), len, p);
143               ret = 1;
144             }
145         }
146     }
147 }
148
149 int
150 test_main (void)
151 {
152   size_t i;
153
154   test_init ();
155
156   printf ("%20s", "");
157   FOR_EACH_IMPL (impl, 0)
158     printf ("\t%s", impl->name);
159   putchar ('\n');
160
161   for (i = 1; i < 8; ++i)
162     {
163       do_test (0, i, i - 1, 127);
164       do_test (0, i, i, 127);
165       do_test (0, i, i + 1, 127);
166     }
167
168   for (i = 1; i < 8; ++i)
169     {
170       do_test (i, i, i - 1, 127);
171       do_test (i, i, i, 127);
172       do_test (i, i, i + 1, 127);
173     }
174
175   for (i = 2; i <= 10; ++i)
176     {
177       do_test (0, 1 << i, 5000, 127);
178       do_test (1, 1 << i, 5000, 127);
179     }
180
181   for (i = 1; i < 8; ++i)
182     do_test (0, i, 5000, 255);
183
184   for (i = 1; i < 8; ++i)
185     do_test (i, i, 5000, 255);
186
187   for (i = 2; i <= 10; ++i)
188     {
189       do_test (0, 1 << i, 5000, 255);
190       do_test (1, 1 << i, 5000, 255);
191     }
192
193   do_random_tests ();
194   return ret;
195 }
196
197 #include "../test-skeleton.c"