BZ#14812: Mark fixed in NEWS.
[glibc.git] / string / test-strlen.c
blobbe2a7f930d2e5f8b6a95fb64be31ac04c0dd80ac
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 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 <http://www.gnu.org/licenses/>. */
21 #define TEST_MAIN
22 #ifndef WIDE
23 # define TEST_NAME "strlen"
24 #else
25 # define TEST_NAME "wcslen"
26 #endif
27 #include "test-string.h"
29 #ifndef WIDE
30 # define STRLEN strlen
31 # define CHAR char
32 # define MAX_CHAR CHAR_MAX
33 #else
34 # include <wchar.h>
35 # define STRLEN wcslen
36 # define CHAR wchar_t
37 # define MAX_CHAR WCHAR_MAX
38 #endif
40 typedef size_t (*proto_t) (const CHAR *);
42 size_t
43 simple_STRLEN (const CHAR *s)
45 const CHAR *p;
47 for (p = s; *p; ++p);
48 return p - s;
51 #ifndef WIDE
52 size_t
53 builtin_strlen (const CHAR *p)
55 return __builtin_strlen (p);
57 IMPL (builtin_strlen, 0)
58 #endif
60 IMPL (simple_STRLEN, 0)
61 IMPL (STRLEN, 1)
64 static void
65 do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
67 size_t len = CALL (impl, s);
68 if (len != exp_len)
70 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
71 len, exp_len);
72 ret = 1;
73 return;
76 if (HP_TIMING_AVAIL)
78 hp_timing_t start __attribute ((unused));
79 hp_timing_t stop __attribute ((unused));
80 hp_timing_t best_time = ~ (hp_timing_t) 0;
81 size_t i;
83 for (i = 0; i < 32; ++i)
85 HP_TIMING_NOW (start);
86 CALL (impl, s);
87 HP_TIMING_NOW (stop);
88 HP_TIMING_BEST (best_time, start, stop);
91 printf ("\t%zd", (size_t) best_time);
95 static void
96 do_test (size_t align, size_t len)
98 size_t i;
100 align &= 63;
101 if (align + sizeof(CHAR) * len >= page_size)
102 return;
104 CHAR *buf = (CHAR *) (buf1);
106 for (i = 0; i < len; ++i)
107 buf[align + i] = 1 + 11111 * i % MAX_CHAR;
108 buf[align + len] = 0;
110 if (HP_TIMING_AVAIL)
111 printf ("Length %4zd, alignment %2zd:", len, align);
113 FOR_EACH_IMPL (impl, 0)
114 do_one_test (impl, (CHAR *) (buf + align), len);
116 if (HP_TIMING_AVAIL)
117 putchar ('\n');
120 static void
121 do_random_tests (void)
123 size_t i, j, n, align, len;
124 CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof(CHAR));
126 for (n = 0; n < ITERATIONS; n++)
128 align = random () & 15;
129 len = random () & 511;
130 if (len + align > 510)
131 len = 511 - align - (random () & 7);
132 j = len + align + 64;
133 if (j > 512)
134 j = 512;
136 for (i = 0; i < j; i++)
138 if (i == len + align)
139 p[i] = 0;
140 else
142 p[i] = random () & 255;
143 if (i >= align && i < len + align && !p[i])
144 p[i] = (random () & 127) + 1;
148 FOR_EACH_IMPL (impl, 1)
149 if (CALL (impl, (CHAR *) (p + align)) != len)
151 error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
152 n, impl->name, align, CALL (impl, (CHAR *) (p + align)),
153 len, p);
154 ret = 1;
160 test_main (void)
162 size_t i;
164 test_init ();
166 printf ("%20s", "");
167 FOR_EACH_IMPL (impl, 0)
168 printf ("\t%s", impl->name);
169 putchar ('\n');
171 /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
173 for (i = 1; i < 8; ++i)
175 do_test (sizeof(CHAR) * i, i);
176 do_test (0, i);
179 for (i = 2; i <= 12; ++i)
181 do_test (0, 1 << i);
182 do_test (sizeof(CHAR) * 7, 1 << i);
183 do_test (sizeof(CHAR) * i, 1 << i);
184 do_test (sizeof(CHAR) * i, (size_t)((1 << i) / 1.5));
187 do_random_tests ();
188 return ret;
191 #include "../test-skeleton.c"